00001
00002
00003
00004
00005
00006
00007
00008 #ifndef binpackeiostream_h_
00009 #define binpackeiostream_h_
00010
00011 #include <fstream>
00012
00013
00014 #ifndef _MSC_VER
00015
00016
00017 #if 0
00018
00019 #include <iostream>
00020 #include <string>
00021
00022 template <class Ch>
00023 class char_traits : public string_char_traits<Ch> {
00024 public:
00025 typedef int int_type;
00026
00027 static Ch eof () { return EOF; }
00028 static bool not_eof (const int_type& a) { return (a != EOF); }
00029 static int_type to_int_type (Ch a) { return static_cast<int_type>(a); }
00030 static bool eq_int_type (const int_type& a, const int_type& b) { return a == b; }
00031 static Ch to_char_type (const int_type& a) { return static_cast<char>(a); }
00032 };
00033 typedef streambuf basic_streambuf;
00034 typedef istream basic_istream;
00035 typedef ostream basic_ostream;
00036
00037 #endif
00038
00039
00040 namespace bpkt {
00041 typedef std::ostream ostream;
00042 typedef std::ofstream ofstream;
00043 typedef std::istream istream;
00044 typedef std::ifstream ifstream;
00045 typedef std::streambuf streambuf;
00046 }
00047
00048 #else // !defined _MSC_VER
00049
00050 namespace bpkt {
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00061 #ifdef _MSC_VER
00062 class basic_streambuf : public std::basic_streambuf<_Ch, _Tr> {
00063 #else
00064 class basic_streambuf : public streambuf {
00065 #endif
00066 public:
00067 basic_streambuf (std::ostream* o) : _out(o), _in(0), _maxinbuf(0)
00068 {
00069 setbuf (new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE);
00070 }
00071 basic_streambuf (std::istream* i) : _out(0), _in(i), _maxinbuf(0)
00072 {
00073 setbuf (new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE);
00074 }
00075
00076 virtual ~basic_streambuf () {
00077 delete _in;
00078 delete _out;
00079 delete eback();
00080 setg (0,0,0);
00081 delete pbase();
00082 setp (0,0);
00083 }
00084
00085 protected:
00086
00087 #ifndef _MSC_VER
00088 typedef _Tr::int_type int_type;
00089 #endif
00090
00091
00092
00093
00094
00095 virtual int overflow ( int_type nCh = _Tr::eof() ) {
00096
00097 std::streamsize nsz = epptr() - pbase() + STD_ALLOC_SIZE;
00098 _Ch* p = new _Ch[ nsz ];
00099
00100
00101 if (!p) return _Tr::eof ();
00102
00103
00104 std::streamsize osz = pptr() - pbase ();
00105 memcpy (p, pbase(), pptr() - pbase());
00106 delete pbase();
00107
00108
00109 setp (p, p + nsz);
00110 pbump (osz);
00111
00112
00113 if (!_Tr::eq_int_type(_Tr::eof(), nCh)) {
00114 *pptr() = _Tr::to_char_type(nCh);
00115 pbump (sizeof(_Tr::char_type));
00116 }
00117
00118 return nCh;
00119 }
00120
00121
00122 virtual int_type underflow () {
00123 if ((!_in) || (!_in->good())) return _Tr::eof ();
00124
00125
00126 if (gptr() != egptr()) return _Tr::to_int_type (*gptr());
00127
00128
00129 std::streamsize s;
00130 _in->read ((_Ch*)(&s), sizeof(std::streamsize));
00131 if (!_in->good()) return _Tr::eof ();
00132
00133
00134 if (s > _maxinbuf) {
00135 delete eback();
00136 _Ch* p = new _Ch[s];
00137 setg (p, p, p);
00138 _maxinbuf = s;
00139 }
00140
00141
00142 _in->read (eback(), s);
00143 setg (eback(), eback(), eback() + s);
00144
00145 if (gptr() != egptr()) return _Tr::to_int_type (*gptr());
00146 return _Tr::eof ();
00147 }
00148
00149
00150
00151 virtual int sync () {
00152 if (_out) {
00153 *((std::streamsize*)pbase()) = pptr() - pbase() - sizeof(std::streamsize);
00154 if (_out->good ()) {
00155 _out->write (pbase(), pptr()-pbase());
00156 setp (pbase(), epptr());
00157 pbump (sizeof(std::streamsize));
00158 } else return -1;
00159 } else {
00160
00161 setg (eback(), egptr(), egptr());
00162 }
00163 return 0;
00164 }
00165
00166
00167
00168 virtual
00169 #ifdef _MSC_VER
00170 std::basic_streambuf<_Ch,_Tr>*
00171 #else
00172 std::streambuf*
00173 #endif
00174 setbuf (_Ch* p, std::streamsize s) {
00175 if (_out) {
00176 setp (p, p + s);
00177 pbump (sizeof(std::streamsize));
00178 } else if (_in) {
00179 setg (p, p, p);
00180 _maxinbuf = s;
00181 }
00182 return this;
00183 }
00184
00185 private:
00186 enum { STD_ALLOC_SIZE = 128 };
00187
00188 std::istream* _in;
00189 std::ostream* _out;
00190
00191 std::streamsize _maxinbuf;
00192 };
00193
00194
00195
00196
00197
00198
00199 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00200 #ifdef _MSC_VER
00201 class basic_ostream : public std::basic_ostream<_Ch, _Tr> {
00202 #else
00203 class basic_ostream : public ostream {
00204 #endif
00205 public:
00206 #ifdef _MSC_VER
00207 basic_ostream (std::basic_ostream<_Ch,_Tr>* o)
00208 : std::basic_ostream<_Ch,_Tr>
00209 #else
00210 basic_ostream (std::basic_ostream* o)
00211 : ostream
00212 #endif
00213 (_buf = new basic_streambuf<_Ch,_Tr>(o))
00214 {}
00215
00216 virtual ~basic_ostream () { delete _buf; }
00217
00218 private:
00219 basic_streambuf<_Ch,_Tr>* _buf;
00220 };
00221
00222
00223
00224
00225
00226
00227 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00228 #ifdef _MSC_VER
00229 class basic_istream : public std::basic_istream<_Ch, _Tr> {
00230 #else
00231 class basic_istream : public istream {
00232 #endif
00233 public:
00234 #ifdef _MSC_VER
00235 basic_istream (std::basic_istream<_Ch,_Tr>* i)
00236 : std::basic_istream<_Ch,_Tr>
00237 #else
00238 basic_istream (std::basic_istream* i)
00239 : istream
00240 #endif
00241 (_buf = new basic_streambuf<_Ch,_Tr>(i))
00242 {}
00243
00244 virtual ~basic_istream () { delete _buf; }
00245
00246 private:
00247 basic_streambuf<_Ch,_Tr>* _buf;
00248 };
00249
00250 template <class _Ty, class _Ch, class _Tr>
00251 inline basic_ostream<_Ch, _Tr>& operator << ( basic_ostream<_Ch,_Tr>& o, const _Ty t ) {
00252 int sz = sizeof(_Ty);
00253 o.write ((const _Ch*)(&t), sz);
00254 return o;
00255 }
00256
00257 template <class _Ty, class _Ch, class _Tr>
00258 inline basic_istream<_Ch,_Tr>& operator >> ( basic_istream<_Ch,_Tr>& i, _Ty& t ) {
00259 int sz = sizeof(_Ty);
00260 i.read ((_Ch*)(&t),sz);
00261 return i;
00262 }
00263
00264
00265
00266
00267 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00268 class basic_ofstream : public basic_ostream <_Ch, _Tr> {
00269 public:
00270 basic_ofstream (const char* fname)
00271 : basic_ostream<_Ch, _Tr>
00272 #ifdef _MSC_VER
00273 (new std::basic_ofstream<_Ch,_Tr>(fname, std::ios::binary))
00274 #else
00275 (new std::ofstream (fname, std::ios::binary))
00276 #endif
00277 {}
00278 };
00279
00280 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00281 class basic_ifstream : public basic_istream <_Ch, _Tr> {
00282 public:
00283 basic_ifstream (const char* fname)
00284 : basic_istream<_Ch, _Tr>
00285 #ifdef _MSC_VER
00286 (new std::basic_ifstream<_Ch,_Tr>(fname, std::ios::binary))
00287 #else
00288 (new std::ifstream (fname, std::ios::binary))
00289 #endif
00290 {}
00291 };
00292
00293
00294
00295
00296
00297 typedef basic_ostream<char, std::char_traits<char> > ostream;
00298 typedef basic_istream<char, std::char_traits<char> > istream;
00299 typedef basic_ofstream<char, std::char_traits<char> > ofstream;
00300 typedef basic_ifstream<char, std::char_traits<char> > ifstream;
00301 typedef basic_streambuf<char, std::char_traits<char> > streambuf;
00302
00303 }
00304
00305 #endif // !defined _MSC_VER
00306
00307 #endif // binpacketbuf_h_
00308
00309
00310
00311