portable_io.h

Go to the documentation of this file.
00001 #ifndef PORTABLE_IO_HEADER
00002 #define PORTABLE_IO_HEADER
00003 
00012 
00013 #include <stdlib.h>
00014 #include <stdio.h>
00015 
00016 #ifdef _MSC_VER
00017 # include <io.h>
00018 # include <direct.h>
00019 # define  snprintf _snprintf
00020 #define popen  _popen
00021 #define pclose _pclose
00022 #else
00023 # include <unistd.h>
00024 # define  O_BINARY 0
00025 #endif
00026 
00027 #include <fcntl.h>
00028 
00029 namespace cxxtls
00030 {
00031 
00032 template<class Iterator>
00033 inline
00034 Iterator
00035 copy_end_of_line(Iterator output)
00041 {
00042   #ifdef _MSC_VER
00043 
00044   //
00045   //  Note:  for this code to work the way you expect, you will need to make sure that
00046   //  the io stream is in the binary mode -- otherwise, you'll get \r's whether you want
00047   //  them or not.
00048   //
00049   //  To ensure that cout is in binary mode, put this in your main
00050   //
00051   //     #include <io.h>
00052   //
00053   //     main()
00054   //     {
00055   //        cout.flush();
00056   //        setmode(1, O_binary);
00057   //        ...
00058   //
00059 
00060   static bool initialized = false;
00061   static bool underBash   = false;
00062 
00063   if(!initialized)
00064   {
00065     initialized = true;
00066 
00067     char*  p = getenv("SHLVL");
00068 
00069     if(p)
00070     {
00071        // SHLVL is a bash, sh, csh, ksh flag to keep track of how
00072        // many subshells deep we are
00073     
00074        underBash = true;
00075     }
00076 
00077 //    else
00078 //    {
00079 //      system("set");
00080 //    }
00081 
00082   } // of initialization
00083 
00084 
00085   if(!underBash)
00086     *(output)++ = '\r';
00087 
00088   #endif
00089 
00090   *(output)++ = '\n';
00091 
00092   return output;
00093 }
00094 
00095 #ifdef _MSC_VER
00096 
00097  extern struct EOL_Sequence_t { char s[2]; }  EOL_Sequence;
00098 
00099 #else
00100 
00101  extern struct EOL_Sequence_t { char s[1]; }  EOL_Sequence;
00102 
00103 #endif
00104 
00105 } // namespace cxxtls
00106 #endif
Generated on Wed Feb 29 22:50:04 2012 for CXXUtilities by  doxygen 1.6.3