BOSS 7.0.3
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/facilities-00-00-04/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
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}
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*/
60template <class _Ch, class _Tr = std::char_traits<_Ch> >
61#ifdef _MSC_VER
62class basic_streambuf : public std::basic_streambuf<_Ch, _Tr> {
63#else
64class basic_streambuf : public streambuf {
65#endif
66public:
67 basic_streambuf (std::ostream* o) : _out(o), _in(0), _maxinbuf(0)
68 {
69 setbuf (new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE);
70 }
71 basic_streambuf (std::istream* i) : _out(0), _in(i), _maxinbuf(0)
72 {
73 setbuf (new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE);
74 }
75
76 virtual ~basic_streambuf () {
77 delete _in;
78 delete _out;
79 delete eback();
80 setg (0,0,0);
81 delete pbase();
82 setp (0,0);
83 }
84
85protected:
86
87#ifndef _MSC_VER
88 typedef _Tr::int_type int_type;
89#endif
90
91 // streambuf - derived class overloads
92
93 // overflow - handle an overflow condition by expanding the output buffer
94 // by a standard amount
95 virtual int overflow ( int_type nCh = _Tr::eof() ) {
96 // make a new (larger) buffer area
97 std::streamsize nsz = epptr() - pbase() + STD_ALLOC_SIZE;
98 _Ch* p = new _Ch[ nsz ];
99
100 // error check
101 if (!p) return _Tr::eof ();
102
103 // copy over old data & destroy previous buffer
104 std::streamsize osz = pptr() - pbase ();
105 memcpy (p, pbase(), pptr() - pbase());
106 delete pbase();
107
108 // set up the new buffer area
109 setp (p, p + nsz);
110 pbump (osz);
111
112 // 'consume' the nCh character
113 if (!_Tr::eq_int_type(_Tr::eof(), nCh)) {
114 *pptr() = _Tr::to_char_type(nCh);
115 pbump (sizeof(_Tr::char_type));
116 }
117
118 return nCh;
119 }
120
121 // underflow - need to read in next packet from input
122 virtual int_type underflow () {
123 if ((!_in) || (!_in->good())) return _Tr::eof ();
124
125 // check to see if we already have data
126 if (gptr() != egptr()) return _Tr::to_int_type (*gptr());
127
128 // find out the size of the packet
129 std::streamsize s;
130 _in->read ((_Ch*)(&s), sizeof(std::streamsize));
131 if (!_in->good()) return _Tr::eof ();
132
133 // make sure there is adequate storage...
134 if (s > _maxinbuf) {
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
150 // sync - flush the buffer - default behavior is to just flush the buffer
151 virtual int sync () {
152 if (_out) {
153 *((std::streamsize*)pbase()) = pptr() - pbase() - sizeof(std::streamsize);
154 if (_out->good ()) {
155 _out->write (pbase(), pptr()-pbase());
156 setp (pbase(), epptr()); // reset the buffer
157 pbump (sizeof(std::streamsize));
158 } else return -1;
159 } else {
160 // flush the input
161 setg (eback(), egptr(), egptr());
162 }
163 return 0;
164 }
165
166 // setbuf - set the buffer : if this is going out, account for the size at the
167 // beginning
168 virtual
169#ifdef _MSC_VER
170 std::basic_streambuf<_Ch,_Tr>*
171#else
172 std::streambuf*
173#endif
174 setbuf (_Ch* p, std::streamsize s) {
175 if (_out) {
176 setp (p, p + s);
177 pbump (sizeof(std::streamsize));
178 } else if (_in) {
179 setg (p, p, p);
180 _maxinbuf = s;
181 }
182 return this;
183 }
184
185private:
186 enum { STD_ALLOC_SIZE = 128 }; // standard allocation size
187
188 std::istream* _in; // input stream
189 std::ostream* _out; // output stream
190
191 std::streamsize _maxinbuf; // keep track of the maximum input buffer size...
192};
193
194/* basic_ostream - class definition
195 The basic_ostream class acts as an ostream which assigns a new
196 basic_ofstream object to its buffer, which is a basic_streambuf
197 streambuffer.
198*/
199template <class _Ch, class _Tr = std::char_traits<_Ch> >
200#ifdef _MSC_VER
201class basic_ostream : public std::basic_ostream<_Ch, _Tr> {
202#else
203class basic_ostream : public ostream {
204#endif
205public:
206#ifdef _MSC_VER
207 basic_ostream (std::basic_ostream<_Ch,_Tr>* o)
208 : std::basic_ostream<_Ch,_Tr>
209#else
210 basic_ostream (std::basic_ostream* o)
211 : ostream
212#endif
213 (_buf = new basic_streambuf<_Ch,_Tr>(o))
214 {}
215
216 virtual ~basic_ostream () { delete _buf; }
217
218private:
219 basic_streambuf<_Ch,_Tr>* _buf;
220};
221
222/* basic_istream - class definition
223 The basic_istream class acts as an istream which assigns a new
224 basic_istream object to its buffer, which is a basic_streambuf
225 streambuffer.
226*/
227template <class _Ch, class _Tr = std::char_traits<_Ch> >
228#ifdef _MSC_VER
229class basic_istream : public std::basic_istream<_Ch, _Tr> {
230#else
231class basic_istream : public istream {
232#endif
233public:
234#ifdef _MSC_VER
235 basic_istream (std::basic_istream<_Ch,_Tr>* i)
236 : std::basic_istream<_Ch,_Tr>
237#else
238 basic_istream (std::basic_istream* i)
239 : istream
240#endif
241 (_buf = new basic_streambuf<_Ch,_Tr>(i))
242 {}
243
244 virtual ~basic_istream () { delete _buf; }
245
246private:
247 basic_streambuf<_Ch,_Tr>* _buf;
248};
249
250template <class _Ty, class _Ch, class _Tr>
251inline basic_ostream<_Ch, _Tr>& operator << ( basic_ostream<_Ch,_Tr>& o, const _Ty t ) {
252 int sz = sizeof(_Ty);
253 o.write ((const _Ch*)(&t), sz);
254 return o;
255}
256
257template <class _Ty, class _Ch, class _Tr>
258inline basic_istream<_Ch,_Tr>& operator >> ( basic_istream<_Ch,_Tr>& i, _Ty& t ) {
259 int sz = sizeof(_Ty);
260 i.read ((_Ch*)(&t),sz);
261 return i;
262}
263
264/*
265 Define file stream classes which use binary packets.
266*/
267template <class _Ch, class _Tr = std::char_traits<_Ch> >
268class basic_ofstream : public basic_ostream <_Ch, _Tr> {
269public:
270 basic_ofstream (const char* fname)
271 : basic_ostream<_Ch, _Tr>
272#ifdef _MSC_VER
273 (new std::basic_ofstream<_Ch,_Tr>(fname, std::ios::binary))
274#else
275 (new std::ofstream (fname, std::ios::binary))
276#endif
277 {}
278};
279
280template <class _Ch, class _Tr = std::char_traits<_Ch> >
281class basic_ifstream : public basic_istream <_Ch, _Tr> {
282public:
283 basic_ifstream (const char* fname)
284 : basic_istream<_Ch, _Tr>
285#ifdef _MSC_VER
286 (new std::basic_ifstream<_Ch,_Tr>(fname, std::ios::binary))
287#else
288 (new std::ifstream (fname, std::ios::binary))
289#endif
290 {}
291};
292
293/* The classes binostream, binistream, and binstreambuf define binary stream
294 implementations based upon the char datatype (using byte resolution for allocation).
295*/
296
297typedef basic_ostream<char, std::char_traits<char> > ostream;
298typedef basic_istream<char, std::char_traits<char> > istream;
299typedef basic_ofstream<char, std::char_traits<char> > ofstream;
300typedef basic_ifstream<char, std::char_traits<char> > ifstream;
301typedef basic_streambuf<char, std::char_traits<char> > streambuf;
302
303} // namespace bpkt
304
305#endif // !defined _MSC_VER
306
307#endif // binpacketbuf_h_
308
309
310
311
basic_binistream< _Ch, _Tr > & operator>>(basic_binistream< _Ch, _Tr > &i, _Ty &t)
std::ostream & operator<<(std::ostream &out, const SelectInfo &info)
Definition: Coverage.cxx:39
XmlRpcServer s
Definition: HelloServer.cpp:11
int t()
Definition: t.c:1