00001
00002
00003
00004
00005
00006
00007
00008 #ifndef binarystreams_h_
00009 #define binarystreams_h_
00010
00011 #include "streamdef.h"
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00023 #ifdef _MSC_VER
00024 class basic_binstreambuf : virtual public std::basic_streambuf<_Ch, _Tr> {
00025 #else
00026 class basic_binstreambuf : virtual public streambuf {
00027 #endif
00028 public:
00029 basic_binstreambuf (std::streamsize sz) : _outbuf(new _Ch[sz + sizeof(std::streamsize)/sizeof(_Ch)]), _inbuf(0) {
00030 memcpy(_outbuf, &sz, sizeof(std::streamsize));
00031 setp (_outbuf + sizeof(std::streamsize), _outbuf + sz*sizeof(_Ch) + sizeof(unsigned int));
00032 }
00033 basic_binstreambuf (const _Ch* p) : _outbuf(0), _inbuf(0) {
00034 std::streamsize sz = *((const std::streamsize*)p);
00035 _inbuf = new _Ch[sz];
00036 memcpy (_inbuf, p + sizeof(std::streamsize), sz);
00037 setg (_inbuf, _inbuf, _inbuf + sz * sizeof(_Ch));
00038 }
00039 virtual ~basic_binstreambuf () { delete _outbuf; delete _inbuf; }
00040
00041
00042 _Ch* givebuf () {
00043 _Ch* p = _outbuf;
00044 _outbuf = 0;
00045 return p;
00046 }
00047
00048
00049
00050 size_t outbufsize () const {
00051 return (_outbuf) ? (sizeof(_Ch) * (*(std::streamsize*)_outbuf) + sizeof(std::streamsize)) : 0;
00052 }
00053
00054
00055
00056 static size_t computesize (size_t s) {
00057 return s + sizeof(std::streamsize);
00058 }
00059
00060 private:
00061 _Ch* _outbuf;
00062 _Ch* _inbuf;
00063 };
00064
00065
00066
00067
00068
00069
00070
00071
00072 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00073 #ifdef _MSC_VER
00074 class basic_binostream : public std::basic_ostream<_Ch, _Tr> {
00075 #else
00076 class basic_binostream : public std::basic_ostream {
00077 #endif
00078 public:
00079 basic_binostream (std::streamsize sz)
00080 #ifdef _MSC_VER
00081 : std::basic_ostream<_Ch,_Tr>(_buf = new basic_binstreambuf<_Ch,_Tr>(sz)) {}
00082 #else
00083 : ostream (_buf = new basic_binstreambuf<_Ch,_Tr>(sz)) {}
00084 #endif
00085 virtual ~basic_binostream () { delete _buf; }
00086
00087
00088
00089 void* popbuf (size_t& sz) {
00090 sz = _buf->outbufsize();
00091 return _buf->givebuf ();
00092 }
00093
00094
00095
00096 static size_t computesize (size_t s) {
00097 return basic_binstreambuf<_Ch,_Tr>::computesize(s);
00098 }
00099
00100 private:
00101 basic_binstreambuf<_Ch,_Tr>* _buf;
00102 };
00103
00104
00105
00106
00107
00108
00109
00110 template <class _Ch, class _Tr = std::char_traits<_Ch> >
00111 #ifdef _MSC_VER
00112 class basic_binistream : public std::basic_istream<_Ch, _Tr> {
00113 #else
00114 class basic_binistream : public istream {
00115 #endif
00116 public:
00117 basic_binistream (const _Ch* ptr)
00118 #ifdef _MSC_VER
00119 : std::basic_istream<_Ch,_Tr>(_buf = new basic_binstreambuf<_Ch,_Tr>(ptr)) {}
00120 #else
00121 : basic_istream (_buf = new basic_binstreambuf<_Ch,_Tr>(ptr)) {}
00122 #endif
00123 virtual ~basic_binistream () { delete _buf; }
00124
00125 private:
00126 basic_binstreambuf<_Ch,_Tr>* _buf;
00127 };
00128
00129 template <class _Ty, class _Ch, class _Tr>
00130 inline basic_binostream<_Ch, _Tr>& operator << ( basic_binostream<_Ch,_Tr>& o, const _Ty t ) {
00131 int sz = sizeof(_Ty);
00132 o.write ((const _Ch*)(&t), sz);
00133 return o;
00134 }
00135
00136 template <class _Ty, class _Ch, class _Tr>
00137 inline basic_binistream<_Ch,_Tr>& operator >> ( basic_binistream<_Ch,_Tr>& i, _Ty& t ) {
00138 int sz = sizeof(_Ty);
00139 i.read ((_Ch*)(&t),sz);
00140 return i;
00141 }
00142
00143
00144
00145
00146 typedef basic_binostream<char> binostream;
00147 typedef basic_binistream<char> binistream;
00148 typedef basic_binstreambuf<char> binstreambuf;
00149
00150 #endif