/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Calibration/facilities/facilities-00-00-04/facilities/bpkt_streams.h

Go to the documentation of this file.
00001 // $Id: bpkt_streams.h,v 1.1.1.1 2005/10/17 06:11:40 maqm Exp $
00002 //  bpkt_streams.h
00003 //  Author: Sawyer Gillespie
00004 //          UW Department of Physics
00005 //          hgillesp@u.washington.edu
00006 //  Class definitions for packetized binary file i/o
00007 
00008 #ifndef binpackeiostream_h_
00009 #define binpackeiostream_h_
00010 
00011 #include <fstream>
00012 
00013 // kluge around incompatibilities w. egcs compiler:
00014 #ifndef _MSC_VER    
00015 
00016 // DISABLED CODE =======================================
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 // DISABLED CODE =======================================
00039 
00040 namespace bpkt {
00041     typedef std::ostream    ostream;    // this just maps bpkt::XXX to std::XXX
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 /* class basic_streambuf:
00053         class which packetizes data into binary packets before writing to a given
00054         stream. buffers all data until a sync () call is made, at which time the
00055         size of the buffer as well as the buffer itself is moved into the output stream.
00056         input occurs by first reading in the packet size, then pulling the entire
00057         packet from the stream. A flush on the input end will simply scuttle the buffer
00058         and re-initialize.
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     // streambuf - derived class overloads
00092 
00093     // overflow - handle an overflow condition by expanding the output buffer
00094     //            by a standard amount
00095     virtual int overflow    ( int_type nCh = _Tr::eof() ) {
00096         // make a new (larger) buffer area
00097         std::streamsize nsz = epptr() - pbase() + STD_ALLOC_SIZE; 
00098         _Ch*    p = new _Ch[ nsz ];
00099 
00100         // error check
00101         if (!p) return _Tr::eof ();
00102 
00103         // copy over old data & destroy previous buffer
00104         std::streamsize osz = pptr() - pbase ();
00105         memcpy (p, pbase(), pptr() - pbase());   
00106         delete pbase();
00107 
00108         // set up the new buffer area
00109         setp (p, p + nsz);
00110         pbump (osz);    
00111         
00112         // 'consume' the nCh character
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     // underflow - need to read in next packet from input
00122     virtual int_type    underflow () {
00123         if ((!_in) || (!_in->good()))   return _Tr::eof (); 
00124 
00125         // check to see if we already have data
00126         if (gptr() != egptr())  return _Tr::to_int_type (*gptr());
00127 
00128         // find out the size of the packet
00129         std::streamsize s;
00130         _in->read ((_Ch*)(&s), sizeof(std::streamsize));
00131         if (!_in->good())   return _Tr::eof ();
00132 
00133         // make sure there is adequate storage...
00134         if (s > _maxinbuf) {
00135             delete eback();
00136             _Ch*    p = new _Ch[s];
00137             setg (p, p, p);
00138             _maxinbuf = s;
00139         }
00140 
00141         // read next packet from the input stream
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     // sync - flush the buffer - default behavior is to just flush the buffer
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());    // reset the buffer
00157                 pbump (sizeof(std::streamsize));
00158             } else return -1;
00159         } else {
00160             // flush the input
00161             setg (eback(), egptr(), egptr());
00162         }
00163         return 0;
00164     }
00165 
00166     // setbuf - set the buffer : if this is going out, account for the size at the
00167     //          beginning
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 };  // standard allocation size
00187 
00188     std::istream*  _in;    // input stream
00189     std::ostream*  _out;   // output stream
00190 
00191     std::streamsize _maxinbuf;  // keep track of the maximum input buffer size...
00192 };
00193 
00194 /* basic_ostream - class definition
00195     The basic_ostream class acts as an ostream which assigns a new
00196     basic_ofstream object to its buffer, which is a basic_streambuf
00197     streambuffer.
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 /* basic_istream - class definition
00223     The basic_istream class acts as an istream which assigns a new
00224     basic_istream object to its buffer, which is a basic_streambuf
00225     streambuffer.
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     Define file stream classes which use binary packets.
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 /*  The classes binostream, binistream, and binstreambuf define binary stream 
00294     implementations based upon the char datatype (using byte resolution for allocation). 
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 }   // namespace bpkt
00304 
00305 #endif  // !defined _MSC_VER
00306 
00307 #endif  // binpacketbuf_h_
00308 
00309 
00310 
00311 

Generated on Tue Nov 29 22:57:55 2016 for BOSS_7.0.2 by  doxygen 1.4.7