BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/include/facilities/bpkt_streams.h
Go to the documentation of this file.
1// $Id: bpkt_streams.h,v 1.1.1.1 2005/10/17 06:11:40 maqm Exp $
2// bpkt_streams.h
3// Author: Sawyer Gillespie
4// UW Department of Physics
5// hgillesp@u.washington.edu
6// Class definitions for packetized binary file i/o
7
8#ifndef binpackeiostream_h_
9#define binpackeiostream_h_
10
11#include <fstream>
12
13// kluge around incompatibilities w. egcs compiler:
14#ifndef _MSC_VER
15
16// DISABLED CODE =======================================
17# if 0
18
19# include <iostream>
20# include <string>
21
22template <class Ch>
23class char_traits : public string_char_traits<Ch> {
24public:
25 typedef int int_type;
26
27 static Ch eof () { return EOF; }
28 static bool not_eof (const int_type& a) { return (a != EOF); }
29 static int_type to_int_type (Ch a) { return static_cast<int_type>(a); }
30 static bool eq_int_type (const int_type& a, const int_type& b) { return a == b; }
31 static Ch to_char_type (const int_type& a) { return static_cast<char>(a); }
32};
33typedef streambuf basic_streambuf;
34typedef istream basic_istream;
35typedef ostream basic_ostream;
36
37# endif
38// DISABLED CODE =======================================
39
40namespace bpkt {
41 typedef std::ostream ostream; // this just maps bpkt::XXX to std::XXX
42 typedef std::ofstream ofstream;
43 typedef std::istream istream;
44 typedef std::ifstream ifstream;
45 typedef std::streambuf streambuf;
46} // namespace bpkt
47
48#else // !defined _MSC_VER
49
50namespace bpkt {
51
52 /* class basic_streambuf:
53 class which packetizes data into binary packets before writing to a given
54 stream. buffers all data until a sync () call is made, at which time the
55 size of the buffer as well as the buffer itself is moved into the output stream.
56 input occurs by first reading in the packet size, then pulling the entire
57 packet from the stream. A flush on the input end will simply scuttle the buffer
58 and re-initialize.
59 */
60 template <class _Ch, class _Tr = std::char_traits<_Ch>>
61# ifdef _MSC_VER
62 class basic_streambuf : public std::basic_streambuf<_Ch, _Tr> {
63# else
64 class basic_streambuf : public streambuf {
65# endif
66 public:
67 basic_streambuf( std::ostream* o ) : _out( o ), _in( 0 ), _maxinbuf( 0 ) {
68 setbuf( new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE );
69 }
70 basic_streambuf( std::istream* i ) : _out( 0 ), _in( i ), _maxinbuf( 0 ) {
71 setbuf( new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE );
72 }
73
74 virtual ~basic_streambuf() {
75 delete _in;
76 delete _out;
77 delete eback();
78 setg( 0, 0, 0 );
79 delete pbase();
80 setp( 0, 0 );
81 }
82
83 protected:
84
85# ifndef _MSC_VER
86 typedef _Tr::int_type int_type;
87# endif
88
89 // streambuf - derived class overloads
90
91 // overflow - handle an overflow condition by expanding the output buffer
92 // by a standard amount
93 virtual int overflow( int_type nCh = _Tr::eof() ) {
94 // make a new (larger) buffer area
95 std::streamsize nsz = epptr() - pbase() + STD_ALLOC_SIZE;
96 _Ch* p = new _Ch[nsz];
97
98 // error check
99 if ( !p ) return _Tr::eof();
100
101 // copy over old data & destroy previous buffer
102 std::streamsize osz = pptr() - pbase();
103 memcpy( p, pbase(), pptr() - pbase() );
104 delete pbase();
105
106 // set up the new buffer area
107 setp( p, p + nsz );
108 pbump( osz );
109
110 // 'consume' the nCh character
111 if ( !_Tr::eq_int_type( _Tr::eof(), nCh ) )
112 {
113 *pptr() = _Tr::to_char_type( nCh );
114 pbump( sizeof( _Tr::char_type ) );
115 }
116
117 return nCh;
118 }
119
120 // underflow - need to read in next packet from input
121 virtual int_type underflow() {
122 if ( ( !_in ) || ( !_in->good() ) ) return _Tr::eof();
123
124 // check to see if we already have data
125 if ( gptr() != egptr() ) return _Tr::to_int_type( *gptr() );
126
127 // find out the size of the packet
128 std::streamsize s;
129 _in->read( (_Ch*)( &s ), sizeof( std::streamsize ) );
130 if ( !_in->good() ) return _Tr::eof();
131
132 // make sure there is adequate storage...
133 if ( s > _maxinbuf )
134 {
135 delete eback();
136 _Ch* p = new _Ch[s];
137 setg( p, p, p );
138 _maxinbuf = s;
139 }
140
141 // read next packet from the input stream
142 _in->read( eback(), s );
143 setg( eback(), eback(), eback() + s );
144
145 if ( gptr() != egptr() ) return _Tr::to_int_type( *gptr() );
146 return _Tr::eof();
147 }
148
149 // sync - flush the buffer - default behavior is to just flush the buffer
150 virtual int sync() {
151 if ( _out )
152 {
153 *( (std::streamsize*)pbase() ) = pptr() - pbase() - sizeof( std::streamsize );
154 if ( _out->good() )
155 {
156 _out->write( pbase(), pptr() - pbase() );
157 setp( pbase(), epptr() ); // reset the buffer
158 pbump( sizeof( std::streamsize ) );
159 }
160 else return -1;
161 }
162 else
163 {
164 // flush the input
165 setg( eback(), egptr(), egptr() );
166 }
167 return 0;
168 }
169
170 // setbuf - set the buffer : if this is going out, account for the size at the
171 // beginning
172 virtual
173# ifdef _MSC_VER
174 std::basic_streambuf<_Ch, _Tr>*
175# else
176 std::streambuf*
177# endif
178 setbuf( _Ch* p, std::streamsize s ) {
179 if ( _out )
180 {
181 setp( p, p + s );
182 pbump( sizeof( std::streamsize ) );
183 }
184 else if ( _in )
185 {
186 setg( p, p, p );
187 _maxinbuf = s;
188 }
189 return this;
190 }
191
192 private:
193 enum { STD_ALLOC_SIZE = 128 }; // standard allocation size
194
195 std::istream* _in; // input stream
196 std::ostream* _out; // output stream
197
198 std::streamsize _maxinbuf; // keep track of the maximum input buffer size...
199 };
200
201 /* basic_ostream - class definition
202 The basic_ostream class acts as an ostream which assigns a new
203 basic_ofstream object to its buffer, which is a basic_streambuf
204 streambuffer.
205 */
206 template <class _Ch, class _Tr = std::char_traits<_Ch>>
207# ifdef _MSC_VER
208 class basic_ostream : public std::basic_ostream<_Ch, _Tr> {
209# else
210 class basic_ostream : public ostream {
211# endif
212 public:
213# ifdef _MSC_VER
214 basic_ostream( std::basic_ostream<_Ch, _Tr>* o )
215 : std::basic_ostream<_Ch, _Tr>
216# else
217 basic_ostream( std::basic_ostream* o )
218 : ostream
219# endif
220 ( _buf = new basic_streambuf<_Ch, _Tr>( o ) ) {
221 }
222
223 virtual ~basic_ostream() { delete _buf; }
224
225 private:
226 basic_streambuf<_Ch, _Tr>* _buf;
227 };
228
229 /* basic_istream - class definition
230 The basic_istream class acts as an istream which assigns a new
231 basic_istream object to its buffer, which is a basic_streambuf
232 streambuffer.
233 */
234 template <class _Ch, class _Tr = std::char_traits<_Ch>>
235# ifdef _MSC_VER
236 class basic_istream : public std::basic_istream<_Ch, _Tr> {
237# else
238 class basic_istream : public istream {
239# endif
240 public:
241# ifdef _MSC_VER
242 basic_istream( std::basic_istream<_Ch, _Tr>* i )
243 : std::basic_istream<_Ch, _Tr>
244# else
245 basic_istream( std::basic_istream* i )
246 : istream
247# endif
248 ( _buf = new basic_streambuf<_Ch, _Tr>( i ) ) {
249 }
250
251 virtual ~basic_istream() { delete _buf; }
252
253 private:
254 basic_streambuf<_Ch, _Tr>* _buf;
255 };
256
257 template <class _Ty, class _Ch, class _Tr>
258 inline basic_ostream<_Ch, _Tr>& operator<<( basic_ostream<_Ch, _Tr>& o, const _Ty t ) {
259 int sz = sizeof( _Ty );
260 o.write( (const _Ch*)( &t ), sz );
261 return o;
262 }
263
264 template <class _Ty, class _Ch, class _Tr>
265 inline basic_istream<_Ch, _Tr>& operator>>( basic_istream<_Ch, _Tr>& i, _Ty& t ) {
266 int sz = sizeof( _Ty );
267 i.read( (_Ch*)( &t ), sz );
268 return i;
269 }
270
271 /*
272 Define file stream classes which use binary packets.
273 */
274 template <class _Ch, class _Tr = std::char_traits<_Ch>>
275 class basic_ofstream : public basic_ostream<_Ch, _Tr> {
276 public:
277 basic_ofstream( const char* fname )
278 : basic_ostream<_Ch, _Tr>
279# ifdef _MSC_VER
280 ( new std::basic_ofstream<_Ch, _Tr>( fname, std::ios::binary ) )
281# else
282 ( new std::ofstream( fname, std::ios::binary ) )
283# endif
284 {
285 }
286 };
287
288 template <class _Ch, class _Tr = std::char_traits<_Ch>>
289 class basic_ifstream : public basic_istream<_Ch, _Tr> {
290 public:
291 basic_ifstream( const char* fname )
292 : basic_istream<_Ch, _Tr>
293# ifdef _MSC_VER
294 ( new std::basic_ifstream<_Ch, _Tr>( fname, std::ios::binary ) )
295# else
296 ( new std::ifstream( fname, std::ios::binary ) )
297# endif
298 {
299 }
300 };
301
302 /* The classes binostream, binistream, and binstreambuf define binary stream
303 implementations based upon the char datatype (using byte resolution for allocation).
304 */
305
306 typedef basic_ostream<char, std::char_traits<char>> ostream;
307 typedef basic_istream<char, std::char_traits<char>> istream;
308 typedef basic_ofstream<char, std::char_traits<char>> ofstream;
309 typedef basic_ifstream<char, std::char_traits<char>> ifstream;
310 typedef basic_streambuf<char, std::char_traits<char>> streambuf;
311
312} // namespace bpkt
313
314#endif // !defined _MSC_VER
315
316#endif // binpacketbuf_h_
std::istream & operator>>(std::istream &is, CosmicEventParser &ev)
ostream & operator<<(ostream &s, const EvtComplex &c)
Definition EvtComplex.cc:27
XmlRpcServer s
int t()
Definition t.c:1