home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / streambf < prev    next >
Text File  |  1998-06-16  |  6KB  |  211 lines

  1. // streambuf standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _STREAMBUF_
  8. #define _STREAMBUF_
  9. #include <xlocnum>
  10.  
  11. #ifdef  _MSC_VER
  12. #pragma pack(push,8)
  13. #endif  /* _MSC_VER */
  14. _STD_BEGIN
  15.         // TEMPLATE CLASS basic_streambuf
  16. template<class _E, class _Tr = char_traits<_E> >
  17.     class basic_streambuf {
  18. protected:
  19.     basic_streambuf()
  20.         : _Loc() {_Init(); }
  21.     basic_streambuf(_Uninitialized)
  22.         : _Loc(_Noinit) {}
  23. public:
  24.     typedef basic_streambuf<_E, _Tr> _Myt;
  25.     typedef _E char_type;
  26.     typedef _Tr traits_type;
  27.     virtual ~basic_streambuf()
  28.         {}
  29.     typedef _Tr::int_type int_type;
  30.     typedef _Tr::pos_type pos_type;
  31.     typedef _Tr::off_type off_type;
  32.     pos_type pubseekoff(off_type _O, ios_base::seekdir _W,
  33.         ios_base::openmode _M = ios_base::in | ios_base::out)
  34.         {return (seekoff(_O, _W, _M)); }
  35.     pos_type pubseekoff(off_type _O, ios_base::seek_dir _W,
  36.         ios_base::open_mode _M)
  37.         {return (pubseekoff(_O, (ios_base::seekdir)_W,
  38.             (ios_base::openmode)_M)); }
  39.     pos_type pubseekpos(pos_type _P,
  40.         ios_base::openmode _M = ios_base::in | ios_base::out)
  41.         {return (seekpos(_P, _M)); }
  42.     pos_type pubseekpos(pos_type _P, ios_base::open_mode _M)
  43.         {return (seekpos(_P, (ios_base::openmode)_M)); }
  44.     _Myt *pubsetbuf(_E *_S, streamsize _N)
  45.         {return (setbuf(_S, _N)); }
  46.     locale pubimbue(const locale &_Ln)
  47.         {locale _Lo = _Loc;
  48.         imbue(_Ln);
  49.         _Loc = _Ln;
  50.         return (_Lo); }
  51.     locale getloc()
  52.         {return (_Loc); }
  53.     streamsize in_avail()
  54.         {return (gptr() != 0 && gptr() < egptr()
  55.             ? egptr() - gptr() : showmanyc()); }
  56.     int pubsync()
  57.         {return (sync()); }
  58.     int_type sbumpc()
  59.         {return (gptr() != 0 && gptr() < egptr()
  60.             ? _Tr::to_int_type(*_Gninc()) : uflow()); }
  61.     int_type sgetc()
  62.         {return (gptr() != 0 && gptr() < egptr()
  63.             ? _Tr::to_int_type(*gptr()) : underflow()); }
  64.     streamsize sgetn(_E *_S, streamsize _N)
  65.         {return (xsgetn(_S, _N)); }
  66.     int_type snextc()
  67.         {return (_Tr::eq_int_type(_Tr::eof(), sbumpc())
  68.             ? _Tr::eof() : sgetc()); }
  69.     int_type sputbackc(_E _C)
  70.         {return (gptr() != 0 && eback() < gptr()
  71.             && _Tr::eq(_C, gptr()[-1])
  72.             ? _Tr::to_int_type(*_Gndec())
  73.             : pbackfail(_Tr::to_int_type(_C))); }
  74.     void stossc()
  75.         {if (gptr() != 0 && gptr() < egptr())
  76.             _Gninc();
  77.         else
  78.             uflow(); }
  79.     int_type sungetc()
  80.         {return (gptr() != 0 && eback() < gptr()
  81.             ? _Tr::to_int_type(*_Gndec()) : pbackfail()); }
  82.     int_type sputc(_E _C)
  83.         {return (pptr() != 0 && pptr() < epptr()
  84.             ? _Tr::to_int_type(*_Pninc() = _C)
  85.             : overflow(_Tr::to_int_type(_C))); }
  86.     streamsize sputn(const _E *_S, streamsize _N)
  87.         {return (xsputn(_S, _N)); }
  88. protected:
  89.     _E *eback() const
  90.         {return (*_IGbeg); }
  91.     _E *gptr() const
  92.         {return (*_IGnext); }
  93.     _E *pbase() const
  94.         {return (*_IPbeg); }
  95.     _E *pptr() const
  96.         {return (*_IPnext); }
  97.     _E *egptr() const
  98.         {return (*_IGnext + *_IGcnt); }
  99.     void gbump(int _N)
  100.         {*_IGcnt -= _N;
  101.         *_IGnext += _N; }
  102.     void setg(_E *_B, _E *_N, _E *_L)
  103.         {*_IGbeg = _B, *_IGnext = _N, *_IGcnt = _L - _N; }
  104.     _E *epptr() const
  105.         {return (*_IPnext + *_IPcnt); }
  106.     _E *_Gndec()
  107.         {++*_IGcnt;
  108.         return (--*_IGnext); }
  109.     _E *_Gninc()
  110.         {--*_IGcnt;
  111.         return ((*_IGnext)++); }
  112.     void pbump(int _N)
  113.         {*_IPcnt -= _N;
  114.         *_IPnext += _N; }
  115.     void setp(_E *_B, _E *_L)
  116.         {*_IPbeg = _B, *_IPnext = _B, *_IPcnt = _L - _B; }
  117.     void setp(_E *_B, _E *_N, _E *_L)
  118.         {*_IPbeg = _B, *_IPnext = _N, *_IPcnt = _L - _N; }
  119.     _E *_Pninc()
  120.         {--*_IPcnt;
  121.         return ((*_IPnext)++); }
  122.     void _Init()
  123.         {_IGbeg = &_Gbeg, _IPbeg = &_Pbeg;
  124.         _IGnext = &_Gnext, _IPnext = &_Pnext;
  125.         _IGcnt = &_Gcnt, _IPcnt = &_Pcnt;
  126.         setp(0, 0), setg(0, 0, 0); }
  127.     void _Init(_E **_Gb, _E **_Gn, int *_Gc,
  128.         _E **_Pb, _E **_Pn, int *_Pc)
  129.         {_IGbeg = _Gb, _IPbeg = _Pb;
  130.         _IGnext = _Gn, _IPnext = _Pn;
  131.         _IGcnt = _Gc, _IPcnt = _Pc; }
  132.     virtual int_type overflow(int_type = _Tr::eof())
  133.         {return (_Tr::eof()); }
  134.     virtual int_type pbackfail(int_type = _Tr::eof())
  135.         {return (_Tr::eof()); }
  136.     virtual int showmanyc()
  137.         {return (0); }
  138.     virtual int_type underflow()
  139.         {return (_Tr::eof()); }
  140.     virtual int_type uflow()
  141.         {return (_Tr::eq_int_type(_Tr::eof(), underflow())
  142.             ? _Tr::eof() : _Tr::to_int_type(*_Gninc())); }
  143.     virtual streamsize xsgetn(_E * _S, streamsize _N)
  144.         {int_type _C;
  145.         streamsize _M, _Ns;
  146.         for (_Ns = 0; 0 < _N; )
  147.             if (gptr() != 0 && 0 < (_M = egptr() - gptr()))
  148.                 {if (_N < _M)
  149.                     _M = _N;
  150.                 _Tr::copy(_S, gptr(), _M);
  151.                 _S += _M, _Ns += _M, _N -= _M, gbump(_M); }
  152.             else if (_Tr::eq_int_type(_Tr::eof(), _C = uflow()))
  153.                 break;
  154.             else
  155.                 *_S++ = _Tr::to_char_type(_C), ++_Ns, --_N;
  156.         return (_Ns); }
  157.     virtual streamsize xsputn(const _E *_S, streamsize _N)
  158.         {streamsize _M, _Ns;
  159.         for (_Ns = 0; 0 < _N; )
  160.             if (pptr() != 0 && 0 < (_M = epptr() - pptr()))
  161.                 {if (_N < _M)
  162.                     _M = _N;
  163.                 _Tr::copy(pptr(), _S, _M);
  164.                 _S += _M, _Ns += _M, _N -= _M, pbump(_M); }
  165.             else if (_Tr::eq_int_type(_Tr::eof(),
  166.                 overflow(_Tr::to_int_type(*_S))))
  167.                 break;
  168.             else
  169.                 ++_S, ++_Ns, --_N;
  170.         return (_Ns); }
  171.     virtual pos_type seekoff(off_type, ios_base::seekdir,
  172.         ios_base::openmode = ios_base::in | ios_base::out)
  173.         {return (streampos(_BADOFF)); }
  174.     virtual pos_type seekpos(pos_type,
  175.         ios_base::openmode = ios_base::in | ios_base::out)
  176.         {return (streampos(_BADOFF)); }
  177.     virtual _Myt *setbuf(_E *, streamsize)
  178.         {return (this); }
  179.     virtual int sync()
  180.         {return (0); }
  181.     virtual void imbue(const locale& _Loc)
  182.         {}
  183. private:
  184.     _E *_Gbeg, *_Pbeg;
  185.     _E **_IGbeg, **_IPbeg;
  186.     _E *_Gnext, *_Pnext;
  187.     _E **_IGnext, **_IPnext;
  188.     int _Gcnt, _Pcnt;
  189.     int *_IGcnt, *_IPcnt;
  190.     locale _Loc;
  191.     };
  192.  
  193. #ifdef _DLL
  194. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  195. extern template class _CRTIMP basic_streambuf<char, char_traits<char> >;
  196. extern template class _CRTIMP basic_streambuf<wchar_t, char_traits<wchar_t> >;
  197. #pragma warning(default:4231) /* restore previous warning */
  198. #endif        // _DLL
  199.  
  200. _STD_END
  201. #ifdef  _MSC_VER
  202. #pragma pack(pop)
  203. #endif  /* _MSC_VER */
  204.  
  205. #endif /* _STREAMBUF_ */
  206.  
  207. /*
  208.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  209.  * Consult your license regarding permissions and restrictions.
  210.  */
  211.