00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __UT_FDStream__
00021 #define __UT_FDStream__
00022
00023 #if !defined(UT_REALLY_USE_FD_STREAM)
00024 - You should either have the line
00025 #define UT_REALLY_USE_FD_STREAM
00026 -
00027 - Or, you should be using UT_FPStream
00028 #error
00029 #endif
00030
00031 #include "UT_API.h"
00032
00033 #if (defined(WIN32) && _MSC_VER >= 1300) || defined(GCC3)
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <istream>
00062 #include <ostream>
00063 #include <streambuf>
00064
00065 #include <cstdio>
00066
00067 #include <cstring>
00068
00069
00070 #ifdef _MSC_VER
00071 # include <io.h>
00072 #else
00073 # include <unistd.h>
00074
00075
00076
00077
00078 #endif
00079 #include "UT_Assert.h"
00080
00081 #ifdef GCC3
00082 using namespace std;
00083 #endif
00084
00085
00086
00087
00088
00089
00090
00091 class UT_API UT_FDOutBuf : public std::streambuf {
00092 public:
00093
00094 UT_FDOutBuf (int _fd) : myFD(_fd) { }
00095
00096 void close()
00097 {
00098 UT_ASSERT(myFD >= 0);
00099 ::close(myFD);
00100 myFD = -1;
00101 }
00102
00103 protected:
00104
00105
00106 virtual int_type overflow (int_type c)
00107 {
00108 UT_ASSERT(myFD >= 0);
00109 if (c != EOF) {
00110 char z = c;
00111 if (write (myFD, &z, 1) != 1) {
00112 return EOF;
00113 }
00114 }
00115 return c;
00116 }
00117
00118
00119 virtual pos_type seekoff(off_type _Off,
00120 ios_base::seekdir _Way,
00121 ios_base::openmode = ios::out)
00122 {
00123
00124
00125 UT_ASSERT(myFD >= 0);
00126 UT_ASSERT(_Off == 0);
00127 UT_ASSERT(_Way == ios_base::cur);
00128 #ifdef WIN32
00129 return (pos_type) _tell(myFD);
00130 #else
00131 return (pos_type) lseek(myFD, 0, SEEK_CUR);
00132 #endif
00133 }
00134
00135
00136 virtual
00137 std::streamsize xsputn (const char* s,
00138 std::streamsize num)
00139 {
00140 UT_ASSERT(myFD >= 0);
00141 return write(myFD,s,num);
00142 }
00143
00144 protected:
00145 int myFD;
00146 };
00147
00148 class UT_API UT_OFDStream : public std::ostream {
00149 protected:
00150 UT_FDOutBuf buf;
00151 public:
00152 UT_OFDStream (int fd) : std::ostream(0), buf(fd)
00153 {
00154 rdbuf(&buf);
00155 }
00156
00157 void close()
00158 {
00159 buf.close();
00160 }
00161 };
00162
00163
00164 #else
00165
00166
00167 typedef ofstream UT_OFDStream;
00168
00169 #endif
00170
00171 #endif
00172