8#ifndef binpackeiostream_h_
9#define binpackeiostream_h_
23class char_traits :
public string_char_traits<Ch> {
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); }
33typedef streambuf basic_streambuf;
34typedef istream basic_istream;
35typedef ostream basic_ostream;
60template <
class _Ch,
class _Tr = std::
char_traits<_Ch> >
62class basic_streambuf :
public std::basic_streambuf<_Ch, _Tr> {
64class basic_streambuf :
public streambuf {
67 basic_streambuf (std::ostream* o) : _out(o), _in(0), _maxinbuf(0)
69 setbuf (
new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE);
71 basic_streambuf (std::istream* i) : _out(0), _in(i), _maxinbuf(0)
73 setbuf (
new _Ch[STD_ALLOC_SIZE], STD_ALLOC_SIZE);
76 virtual ~basic_streambuf () {
88 typedef _Tr::int_type int_type;
95 virtual int overflow ( int_type nCh = _Tr::eof() ) {
97 std::streamsize nsz = epptr() - pbase() + STD_ALLOC_SIZE;
98 _Ch* p =
new _Ch[ nsz ];
101 if (!p)
return _Tr::eof ();
104 std::streamsize osz = pptr() - pbase ();
105 memcpy (p, pbase(), pptr() - pbase());
113 if (!_Tr::eq_int_type(_Tr::eof(), nCh)) {
114 *pptr() = _Tr::to_char_type(nCh);
115 pbump (
sizeof(_Tr::char_type));
122 virtual int_type underflow () {
123 if ((!_in) || (!_in->good()))
return _Tr::eof ();
126 if (gptr() != egptr())
return _Tr::to_int_type (*gptr());
130 _in->read ((_Ch*)(&
s),
sizeof(std::streamsize));
131 if (!_in->good())
return _Tr::eof ();
142 _in->read (eback(),
s);
143 setg (eback(), eback(), eback() +
s);
145 if (gptr() != egptr())
return _Tr::to_int_type (*gptr());
151 virtual int sync () {
153 *((std::streamsize*)pbase()) = pptr() - pbase() -
sizeof(std::streamsize);
155 _out->write (pbase(), pptr()-pbase());
156 setp (pbase(), epptr());
157 pbump (
sizeof(std::streamsize));
161 setg (eback(), egptr(), egptr());
170 std::basic_streambuf<_Ch,_Tr>*
174 setbuf (_Ch* p, std::streamsize
s) {
177 pbump (
sizeof(std::streamsize));
186 enum { STD_ALLOC_SIZE = 128 };
191 std::streamsize _maxinbuf;
199template <
class _Ch,
class _Tr = std::
char_traits<_Ch> >
201class basic_ostream :
public std::basic_ostream<_Ch, _Tr> {
203class basic_ostream :
public ostream {
207 basic_ostream (std::basic_ostream<_Ch,_Tr>* o)
208 : std::basic_ostream<_Ch,_Tr>
210 basic_ostream (std::basic_ostream* o)
213 (_buf =
new basic_streambuf<_Ch,_Tr>(o))
216 virtual ~basic_ostream () {
delete _buf; }
219 basic_streambuf<_Ch,_Tr>* _buf;
227template <
class _Ch,
class _Tr = std::
char_traits<_Ch> >
229class basic_istream :
public std::basic_istream<_Ch, _Tr> {
231class basic_istream :
public istream {
235 basic_istream (std::basic_istream<_Ch,_Tr>* i)
236 : std::basic_istream<_Ch,_Tr>
238 basic_istream (std::basic_istream* i)
241 (_buf =
new basic_streambuf<_Ch,_Tr>(i))
244 virtual ~basic_istream () {
delete _buf; }
247 basic_streambuf<_Ch,_Tr>* _buf;
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);
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);
267template <
class _Ch,
class _Tr = std::
char_traits<_Ch> >
268class basic_ofstream :
public basic_ostream <_Ch, _Tr> {
270 basic_ofstream (
const char* fname)
271 : basic_ostream<_Ch, _Tr>
273 (
new std::basic_ofstream<_Ch,_Tr>(fname, std::ios::binary))
275 (
new std::ofstream (fname, std::ios::binary))
280template <
class _Ch,
class _Tr = std::
char_traits<_Ch> >
281class basic_ifstream :
public basic_istream <_Ch, _Tr> {
283 basic_ifstream (
const char* fname)
284 : basic_istream<_Ch, _Tr>
286 (
new std::basic_ifstream<_Ch,_Tr>(fname, std::ios::binary))
288 (
new std::ifstream (fname, std::ios::binary))
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;
std::ostream & operator<<(std::ostream &out, const SelectInfo &info)
basic_binistream< _Ch, _Tr > & operator>>(basic_binistream< _Ch, _Tr > &i, _Ty &t)