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

  1. // ostream standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _OSTREAM_
  8. #define _OSTREAM_
  9. #include <ios>
  10.  
  11. #ifdef  _MSC_VER
  12. #pragma pack(push,8)
  13. #endif  /* _MSC_VER */
  14.         // I/O exception macros
  15.  #define _TRY_IO_BEGIN    _TRY_BEGIN
  16.  #define _CATCH_IO_END    _CATCH_ALL \
  17.     setstate(ios_base::badbit, true); _CATCH_END
  18.  #define _CATCH_IO_(x)    _CATCH_ALL \
  19.     (x).setstate(ios_base::badbit, true); _CATCH_END
  20. _STD_BEGIN
  21.         // TEMPLATE CLASS basic_ostream
  22. template<class _E, class _Tr = char_traits<_E> >
  23.     class basic_ostream : virtual public basic_ios<_E, _Tr> {
  24. public:
  25.     typedef basic_ostream<_E, _Tr> _Myt;
  26.     typedef basic_ios<_E, _Tr> _Myios;
  27.     typedef basic_streambuf<_E, _Tr> _Mysb;
  28.     typedef ostreambuf_iterator<_E, _Tr> _Iter;
  29.     typedef num_put<_E, _Iter> _Nput;
  30.     explicit basic_ostream(basic_streambuf<_E, _Tr> *_S,
  31.         bool _Isstd = false, bool _Doinit = true)
  32.         {if (_Doinit)
  33.             init(_S, _Isstd); }
  34.     basic_ostream(_Uninitialized)
  35.         {_Addstd(); }
  36.     virtual ~basic_ostream()
  37.         {}
  38.     class sentry {
  39.     public:
  40.         explicit sentry(_Myt& _Os)
  41.             : _Ok(_Os.opfx()), _Ostr(_Os) {}
  42.         ~sentry()
  43.             {if (!uncaught_exception())
  44.                 _Ostr.osfx(); }
  45.         operator bool() const
  46.             {return (_Ok); }
  47.     private:
  48.         bool _Ok;
  49.         _Myt& _Ostr;
  50.         };
  51.     bool opfx()
  52.         {if (good() && tie() != 0)
  53.             tie()->flush();
  54.         return (good()); }
  55.     void osfx()
  56.         {if (flags() & unitbuf)
  57.             flush(); }
  58.     _Myt& operator<<(_Myt& (__cdecl *_F)(_Myt&))
  59.         {return ((*_F)(*this)); }
  60.     _Myt& operator<<(_Myios& (__cdecl *_F)(_Myios&))
  61.         {(*_F)(*(_Myios *)this);
  62.         return (*this); }
  63.     _Myt& operator<<(ios_base& (__cdecl *_F)(ios_base&))
  64.         {(*_F)(*(ios_base *)this);
  65.         return (*this); }
  66.     _Myt& operator<<(_Bool _X)
  67.         {iostate _St = goodbit;
  68.         const sentry _Ok(*this);
  69.         if (_Ok)
  70.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  71.             _TRY_IO_BEGIN
  72.             if (_Fac.put(_Iter(rdbuf()), *this,
  73.                 fill(), _X).failed())
  74.                 _St |= badbit;
  75.             _CATCH_IO_END }
  76.         setstate(_St);
  77.         return (*this); }
  78.     _Myt& operator<<(short _X)
  79.         {iostate _St = goodbit;
  80.         const sentry _Ok(*this);
  81.         if (_Ok)
  82.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  83.             fmtflags _Bfl = flags() & basefield;
  84.             long _Y = (_Bfl == oct || _Bfl == hex)
  85.                 ? (long)(unsigned short)_X : (long)_X;
  86.             _TRY_IO_BEGIN
  87.             if (_Fac.put(_Iter(rdbuf()), *this,
  88.                 fill(), _Y).failed())
  89.                 _St |= badbit;
  90.             _CATCH_IO_END }
  91.         setstate(_St);
  92.         return (*this); }
  93.     _Myt& operator<<(unsigned short _X)
  94.         {iostate _St = goodbit;
  95.         const sentry _Ok(*this);
  96.         if (_Ok)
  97.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  98.             _TRY_IO_BEGIN
  99.             if (_Fac.put(_Iter(rdbuf()), *this,
  100.                 fill(), (unsigned long)_X).failed())
  101.                 _St |= badbit;
  102.             _CATCH_IO_END }
  103.         setstate(_St);
  104.         return (*this); }
  105.     _Myt& operator<<(int _X)
  106.         {iostate _St = goodbit;
  107.         const sentry _Ok(*this);
  108.         if (_Ok)
  109.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  110.             fmtflags _Bfl = flags() & basefield;
  111.             long _Y = (_Bfl == oct || _Bfl == hex)
  112.                 ? (long)(unsigned int)_X : (long)_X;
  113.             _TRY_IO_BEGIN
  114.             if (_Fac.put(_Iter(rdbuf()), *this,
  115.                 fill(), _Y).failed())
  116.                 _St |= badbit;
  117.             _CATCH_IO_END }
  118.         setstate(_St);
  119.         return (*this); }
  120.     _Myt& operator<<(unsigned int _X)
  121.         {iostate _St = goodbit;
  122.         const sentry _Ok(*this);
  123.         if (_Ok)
  124.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  125.             _TRY_IO_BEGIN
  126.             if (_Fac.put(_Iter(rdbuf()), *this,
  127.                 fill(), (unsigned long)_X).failed())
  128.                 _St |= badbit;
  129.             _CATCH_IO_END }
  130.         setstate(_St);
  131.         return (*this); }
  132.     _Myt& operator<<(long _X)
  133.         {iostate _St = goodbit;
  134.         const sentry _Ok(*this);
  135.         if (_Ok)
  136.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  137.             _TRY_IO_BEGIN
  138.             if (_Fac.put(_Iter(rdbuf()), *this,
  139.                 fill(), _X).failed())
  140.                 _St |= badbit;
  141.             _CATCH_IO_END }
  142.         setstate(_St);
  143.         return (*this); }
  144.     _Myt& operator<<(unsigned long _X)
  145.         {iostate _St = goodbit;
  146.         const sentry _Ok(*this);
  147.         if (_Ok)
  148.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  149.             _TRY_IO_BEGIN
  150.             if (_Fac.put(_Iter(rdbuf()), *this,
  151.                 fill(), _X).failed())
  152.                 _St |= badbit;
  153.             _CATCH_IO_END }
  154.         setstate(_St);
  155.         return (*this); }
  156.     _Myt& operator<<(float _X)
  157.         {iostate _St = goodbit;
  158.         const sentry _Ok(*this);
  159.         if (_Ok)
  160.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  161.             _TRY_IO_BEGIN
  162.             if (_Fac.put(_Iter(rdbuf()), *this,
  163.                 fill(), (double)_X).failed())
  164.                 _St |= badbit;
  165.             _CATCH_IO_END }
  166.         setstate(_St);
  167.         return (*this); }
  168.     _Myt& operator<<(double _X)
  169.         {iostate _St = goodbit;
  170.         const sentry _Ok(*this);
  171.         if (_Ok)
  172.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  173.             _TRY_IO_BEGIN
  174.             if (_Fac.put(_Iter(rdbuf()), *this,
  175.                 fill(), _X).failed())
  176.                 _St |= badbit;
  177.             _CATCH_IO_END }
  178.         setstate(_St);
  179.         return (*this); }
  180.     _Myt& operator<<(long double _X)
  181.         {iostate _St = goodbit;
  182.         const sentry _Ok(*this);
  183.         if (_Ok)
  184.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  185.             _TRY_IO_BEGIN
  186.             if (_Fac.put(_Iter(rdbuf()), *this,
  187.                 fill(), _X).failed())
  188.                 _St |= badbit;
  189.             _CATCH_IO_END }
  190.         setstate(_St);
  191.         return (*this); }
  192.     _Myt& operator<<(const void *_X)
  193.         {iostate _St = goodbit;
  194.         const sentry _Ok(*this);
  195.         if (_Ok)
  196.             {const _Nput& _Fac = _USE(getloc(), _Nput);
  197.             _TRY_IO_BEGIN
  198.             if (_Fac.put(_Iter(rdbuf()), *this,
  199.                 fill(), _X).failed())
  200.                 _St |= badbit;
  201.             _CATCH_IO_END }
  202.         setstate(_St);
  203.         return (*this); }
  204.     _Myt& operator<<(_Mysb *_Pb)
  205.         {iostate _St = goodbit;
  206.         bool _Copied = false;
  207.         const sentry _Ok(*this);
  208.         if (_Ok && _Pb != 0)
  209.             for (int_type _C = _Tr::eof(); ; _Copied = true)
  210.                 {_TRY_BEGIN
  211.                 _C = _Tr::eq_int_type(_Tr::eof(), _C)
  212.                     ? _Pb->sgetc() : _Pb->snextc();
  213.                 _CATCH_ALL
  214.                     setstate(failbit);
  215.                     _RERAISE;
  216.                 _CATCH_END
  217.                 if (_Tr::eq_int_type(_Tr::eof(),_C))
  218.                     break;
  219.                 _TRY_IO_BEGIN
  220.                     if (_Tr::eq_int_type(_Tr::eof(),
  221.                         rdbuf()->sputc(_Tr::to_char_type(_C))))
  222.                         {_St |= badbit;
  223.                         break; }
  224.                 _CATCH_IO_END }
  225.         width(0);
  226.         setstate(!_Copied ? _St | failbit : _St);
  227.         return (*this); }
  228.     _Myt& put(_E _X)
  229.         {iostate _St = goodbit;
  230.         const sentry _Ok(*this);
  231.         if (!_Ok)
  232.             _St |= badbit;
  233.         else
  234.             {_TRY_IO_BEGIN
  235.              if (_Tr::eq_int_type(_Tr::eof(),
  236.                 rdbuf()->sputc(_X)))
  237.                 _St |= badbit;
  238.             _CATCH_IO_END }
  239.         setstate(_St);
  240.         return (*this); }
  241.     _Myt& write(const _E *_S, streamsize _N)
  242.         {iostate _St = goodbit;
  243.         const sentry _Ok(*this);
  244.         if (!_Ok)
  245.             _St |= badbit;
  246.         else
  247.             {_TRY_IO_BEGIN
  248.             if (rdbuf()->sputn(_S, _N) != _N)
  249.                 _St |= badbit;
  250.             _CATCH_IO_END }
  251.         setstate(_St);
  252.         return (*this); }
  253.     _Myt& flush()
  254.         {iostate _St = goodbit;
  255.         if (!fail() && rdbuf()->pubsync() == -1)
  256.             _St |= badbit;
  257.         setstate(_St);
  258.         return (*this); }
  259.     _Myt& seekp(pos_type _P)
  260.         {if (!fail())
  261.             rdbuf()->pubseekpos(_P, out);
  262.         return (*this); }
  263.     _Myt& seekp(off_type _O, ios_base::seekdir _W)
  264.         {if (!fail())
  265.             rdbuf()->pubseekoff(_O, _W, out);
  266.         return (*this); }
  267.     pos_type tellp()
  268.         {if (!fail())
  269.             return (rdbuf()->pubseekoff(0, cur, out));
  270.         else
  271.             return (streampos(_BADOFF)); }
  272.     };
  273.  
  274.  
  275. #ifdef _DLL
  276. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  277. extern template class _CRTIMP basic_ostream<char, char_traits<char> >;
  278. extern template class _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >;
  279. #pragma warning(default:4231) /* restore previous warning */
  280. #endif        // _DLL
  281.  
  282.  
  283.         // INSERTERS
  284. template<class _E, class _Tr> inline
  285.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  286.         basic_ostream<_E, _Tr>& _O, const _E *_X)
  287.     {typedef basic_ostream<_E, _Tr> _Myos;
  288.     ios_base::iostate _St = ios_base::goodbit;
  289.     size_t _N = _Tr::length(_X);
  290.     size_t _M = _O.width() <= 0 || _O.width() <= _N
  291.         ? 0 : _O.width() - _N;
  292.     const _Myos::sentry _Ok(_O);
  293.     if (!_Ok)
  294.         _St |= ios_base::badbit;
  295.     else
  296.         {_TRY_IO_BEGIN
  297.         if ((_O.flags() & ios_base::adjustfield)
  298.             != ios_base::left)
  299.             for (; 0 < _M; --_M)
  300.                 if (_Tr::eq_int_type(_Tr::eof(),
  301.                     _O.rdbuf()->sputc(_O.fill())))
  302.                     {_St |= ios_base::badbit;
  303.                     break; }
  304.         if (_St == ios_base::goodbit
  305.             && _O.rdbuf()->sputn(_X, _N) != _N)
  306.             _St |= ios_base::badbit;
  307.         if (_St == ios_base::goodbit)
  308.             for (; 0 < _M; --_M)
  309.                 if (_Tr::eq_int_type(_Tr::eof(),
  310.                     _O.rdbuf()->sputc(_O.fill())))
  311.                     {_St |= ios_base::badbit;
  312.                     break; }
  313.         _O.width(0);
  314.         _CATCH_IO_(_O) }
  315.     _O.setstate(_St);
  316.     return (_O); }
  317. template<class _E, class _Tr> inline
  318.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  319.         basic_ostream<_E, _Tr>& _O, _E _C)
  320.     {typedef basic_ostream<_E, _Tr> _Myos;
  321.     ios_base::iostate _St = ios_base::goodbit;
  322.     const _Myos::sentry _Ok(_O);
  323.     if (_Ok)
  324.         {size_t _M = _O.width() <= 1 ? 0 : _O.width() - 1;
  325.         _TRY_IO_BEGIN
  326.         if ((_O.flags() & ios_base::adjustfield)
  327.             != ios_base::left)
  328.             for (; _St == ios_base::goodbit && 0 < _M; --_M)
  329.                 if (_Tr::eq_int_type(_Tr::eof(),
  330.                     _O.rdbuf()->sputc(_O.fill())))
  331.                     _St |= ios_base::badbit;
  332.         if (_St == ios_base::goodbit
  333.             && _Tr::eq_int_type(_Tr::eof(),
  334.                 _O.rdbuf()->sputc(_C)))
  335.             _St |= ios_base::badbit;
  336.         for (; _St == ios_base::goodbit && 0 < _M; --_M)
  337.             if (_Tr::eq_int_type(_Tr::eof(),
  338.                 _O.rdbuf()->sputc(_O.fill())))
  339.                 _St |= ios_base::badbit;
  340.         _CATCH_IO_(_O) }
  341.     _O.width(0);
  342.     _O.setstate(_St);
  343.     return (_O); }
  344. template<class _E, class _Tr> inline
  345.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  346.         basic_ostream<_E, _Tr>& _O, const signed char *_X)
  347.     {return (_O << (const char *)_X); }
  348. template<class _E, class _Tr> inline
  349.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  350.         basic_ostream<_E, _Tr>& _O, const signed char _C)
  351.     {return (_O << (char)_C); }
  352. template<class _E, class _Tr> inline
  353.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  354.         basic_ostream<_E, _Tr>& _O, const unsigned char *_X)
  355.     {return (_O << (const char *)_X); }
  356. template<class _E, class _Tr> inline
  357.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  358.         basic_ostream<_E, _Tr>& _O, const unsigned char _C)
  359.     {return (_O << (char)_C); }
  360. template<class _E, class _Tr> inline
  361.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  362.         basic_ostream<_E, _Tr>& _O, const signed short *_X)
  363.     {return (_O << (const wchar_t *)_X); }
  364.         // MANIPULATORS
  365. template<class _E, class _Tr> inline
  366.     basic_ostream<_E, _Tr>&
  367.         __cdecl endl(basic_ostream<_E, _Tr>& _O)
  368.     {_O.put(_O.widen('\n'));
  369.     _O.flush();
  370.     return (_O); }
  371. _CRTIMP inline basic_ostream<char, char_traits<char> >&
  372.     __cdecl endl(basic_ostream<char, char_traits<char> >& _O)
  373.     {_O.put('\n');
  374.     _O.flush();
  375.     return (_O); }
  376. _CRTIMP inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  377.     __cdecl endl(basic_ostream<wchar_t,
  378.         char_traits<wchar_t> >& _O)
  379.     {_O.put('\n');
  380.     _O.flush();
  381.     return (_O); }
  382. template<class _E, class _Tr> inline
  383.     basic_ostream<_E, _Tr>&
  384.         __cdecl ends(basic_ostream<_E, _Tr>& _O)
  385.     {_O.put(_E('\0'));
  386.     return (_O); }
  387. _CRTIMP inline basic_ostream<char, char_traits<char> >&
  388.     __cdecl ends(basic_ostream<char, char_traits<char> >& _O)
  389.     {_O.put('\0');
  390.     return (_O); }
  391. _CRTIMP inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  392.     __cdecl ends(basic_ostream<wchar_t,
  393.         char_traits<wchar_t> >& _O)
  394.     {_O.put('\0');
  395.     return (_O); }
  396. template<class _E, class _Tr> inline
  397.     basic_ostream<_E, _Tr>&
  398.         __cdecl flush(basic_ostream<_E, _Tr>& _O)
  399.     {_O.flush();
  400.     return (_O); }
  401. _CRTIMP inline basic_ostream<char, char_traits<char> >&
  402.     __cdecl flush(basic_ostream<char, char_traits<char> >& _O)
  403.     {_O.flush();
  404.     return (_O); }
  405. _CRTIMP inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  406.     __cdecl flush(basic_ostream<wchar_t,
  407.         char_traits<wchar_t> >& _O)
  408.     {_O.flush();
  409.     return (_O); }
  410.  
  411. #ifdef _DLL
  412. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  413.  
  414. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  415.        basic_ostream<char, char_traits<char> >&, const char *);
  416. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  417.        basic_ostream<char, char_traits<char> >&, char);
  418. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  419.        basic_ostream<char, char_traits<char> >&, const signed char *);
  420. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  421.        basic_ostream<char, char_traits<char> >&, const signed char);
  422. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  423.        basic_ostream<char, char_traits<char> >&, const unsigned char *);
  424. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  425.        basic_ostream<char, char_traits<char> >&, const unsigned char);
  426. extern template _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  427.        basic_ostream<wchar_t, char_traits<wchar_t> >&, const wchar_t *);
  428. extern template _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  429.        basic_ostream<wchar_t, char_traits<wchar_t> >&, wchar_t);
  430. extern template _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  431.        basic_ostream<wchar_t, char_traits<wchar_t> >&, const signed short *);
  432.  
  433. #pragma warning(default:4231) /* restore previous warning */
  434. #endif        // _DLL
  435.  
  436. _STD_END
  437. #ifdef  _MSC_VER
  438. #pragma pack(pop)
  439. #endif  /* _MSC_VER */
  440.  
  441. #endif /* _OSTREAM_ */
  442.  
  443. /*
  444.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  445.  * Consult your license regarding permissions and restrictions.
  446.  */
  447.