00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef BOOST_FDSTREAM_HPP
00028 #define BOOST_FDSTREAM_HPP
00029 
00030 #include <istream>
00031 #include <ostream>
00032 #include <streambuf>
00033 
00034 #include <cstdio>
00035 
00036 #include <cstring>
00037 
00038 
00039 
00040 #ifdef _MSC_VER
00041 # include <io.h>
00042 #else
00043 # include <unistd.h>
00044 
00045 
00046 
00047 
00048 #endif
00049 
00050 
00051 
00052 namespace std {
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 class fdoutbuf : public std::streambuf {
00062   protected:
00063     int fd;    
00064   public:
00065     
00066     fdoutbuf (int _fd) : fd(_fd) {
00067     }
00068   protected:
00069     
00070     virtual int_type overflow (int_type c) {
00071         if (c != EOF) {
00072             char z = c;
00073             if (write (fd, &z, 1) != 1) {
00074                 return EOF;
00075             }
00076         }
00077         return c;
00078     }
00079     
00080     virtual
00081     std::streamsize xsputn (const char* s,
00082                             std::streamsize num) {
00083         return write(fd,s,num);
00084     }
00085 };
00086 
00087 class fdostream : public std::ostream {
00088   protected:
00089     fdoutbuf buf;
00090   public:
00091     fdostream (int fd) : std::ostream(0), buf(fd) {
00092         rdbuf(&buf);
00093     }
00094 };
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 class fdinbuf : public std::streambuf {
00103   protected:
00104     int fd;    
00105   protected:
00106     
00107 
00108 
00109 
00110     static const int pbSize = 4;        
00111     static const int bufSize = 1024;    
00112     char buffer[bufSize+pbSize];        
00113 
00114   public:
00115     
00116 
00117 
00118 
00119 
00120 
00121     fdinbuf (int _fd) : fd(_fd) {
00122         setg (buffer+pbSize,     
00123               buffer+pbSize,     
00124               buffer+pbSize);    
00125     }
00126 
00127   protected:
00128     
00129     virtual int_type underflow () {
00130 #ifndef _MSC_VER
00131         using std::memmove;
00132 #endif
00133 
00134         
00135         if (gptr() < egptr()) {
00136             return traits_type::to_int_type(*gptr());
00137         }
00138 
00139         
00140 
00141 
00142 
00143         int numPutback;
00144         numPutback = gptr() - eback();
00145         if (numPutback > pbSize) {
00146             numPutback = pbSize;
00147         }
00148 
00149         
00150 
00151 
00152         memmove (buffer+(pbSize-numPutback), gptr()-numPutback,
00153                 numPutback);
00154 
00155         
00156         int num;
00157         num = read (fd, buffer+pbSize, bufSize);
00158         if (num <= 0) {
00159             
00160             return EOF;
00161         }
00162 
00163         
00164         setg (buffer+(pbSize-numPutback),   
00165               buffer+pbSize,                
00166               buffer+pbSize+num);           
00167 
00168         
00169         return traits_type::to_int_type(*gptr());
00170     }
00171 };
00172 
00173 class fdistream : public std::istream {
00174   protected:
00175     fdinbuf buf;
00176   public:
00177     fdistream (int fd) : std::istream(0), buf(fd) {
00178         rdbuf(&buf);
00179     }
00180 };
00181 
00182 
00183 } 
00184 
00185 #endif