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

  1. // ostream standard header
  2.  
  3. #if     _MSC_VER > 1000 /*IFSTRIP=IGN*/
  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. #ifdef __FORCE_INSTANCE
  277. template class _CRTIMP2 basic_ostream<char, char_traits<char> >;
  278. template class _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >;
  279. #else        // __FORCE_INSTANCE
  280. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  281. extern template class _CRTIMP2 basic_ostream<char, char_traits<char> >;
  282. extern template class _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >;
  283. #pragma warning(default:4231) /* restore previous warning */
  284. #endif        // __FORCE_INSTANCE
  285. #endif        // _DLL
  286.  
  287.  
  288.         // INSERTERS
  289. template<class _E, class _Tr> inline
  290.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  291.         basic_ostream<_E, _Tr>& _O, const _E *_X)
  292.     {typedef basic_ostream<_E, _Tr> _Myos;
  293.     ios_base::iostate _St = ios_base::goodbit;
  294.     size_t _N = _Tr::length(_X);
  295.     size_t _M = _O.width() <= 0 || _O.width() <= _N
  296.         ? 0 : _O.width() - _N;
  297.     const _Myos::sentry _Ok(_O);
  298.     if (!_Ok)
  299.         _St |= ios_base::badbit;
  300.     else
  301.         {_TRY_IO_BEGIN
  302.         if ((_O.flags() & ios_base::adjustfield)
  303.             != ios_base::left)
  304.             for (; 0 < _M; --_M)
  305.                 if (_Tr::eq_int_type(_Tr::eof(),
  306.                     _O.rdbuf()->sputc(_O.fill())))
  307.                     {_St |= ios_base::badbit;
  308.                     break; }
  309.         if (_St == ios_base::goodbit
  310.             && _O.rdbuf()->sputn(_X, _N) != _N)
  311.             _St |= ios_base::badbit;
  312.         if (_St == ios_base::goodbit)
  313.             for (; 0 < _M; --_M)
  314.                 if (_Tr::eq_int_type(_Tr::eof(),
  315.                     _O.rdbuf()->sputc(_O.fill())))
  316.                     {_St |= ios_base::badbit;
  317.                     break; }
  318.         _O.width(0);
  319.         _CATCH_IO_(_O) }
  320.     _O.setstate(_St);
  321.     return (_O); }
  322. template<class _E, class _Tr> inline
  323.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  324.         basic_ostream<_E, _Tr>& _O, _E _C)
  325.     {typedef basic_ostream<_E, _Tr> _Myos;
  326.     ios_base::iostate _St = ios_base::goodbit;
  327.     const _Myos::sentry _Ok(_O);
  328.     if (_Ok)
  329.         {size_t _M = _O.width() <= 1 ? 0 : _O.width() - 1;
  330.         _TRY_IO_BEGIN
  331.         if ((_O.flags() & ios_base::adjustfield)
  332.             != ios_base::left)
  333.             for (; _St == ios_base::goodbit && 0 < _M; --_M)
  334.                 if (_Tr::eq_int_type(_Tr::eof(),
  335.                     _O.rdbuf()->sputc(_O.fill())))
  336.                     _St |= ios_base::badbit;
  337.         if (_St == ios_base::goodbit
  338.             && _Tr::eq_int_type(_Tr::eof(),
  339.                 _O.rdbuf()->sputc(_C)))
  340.             _St |= ios_base::badbit;
  341.         for (; _St == ios_base::goodbit && 0 < _M; --_M)
  342.             if (_Tr::eq_int_type(_Tr::eof(),
  343.                 _O.rdbuf()->sputc(_O.fill())))
  344.                 _St |= ios_base::badbit;
  345.         _CATCH_IO_(_O) }
  346.     _O.width(0);
  347.     _O.setstate(_St);
  348.     return (_O); }
  349. template<class _E, class _Tr> inline
  350.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  351.         basic_ostream<_E, _Tr>& _O, const signed char *_X)
  352.     {return (_O << (const char *)_X); }
  353. template<class _E, class _Tr> inline
  354.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  355.         basic_ostream<_E, _Tr>& _O, const signed char _C)
  356.     {return (_O << (char)_C); }
  357. template<class _E, class _Tr> inline
  358.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  359.         basic_ostream<_E, _Tr>& _O, const unsigned char *_X)
  360.     {return (_O << (const char *)_X); }
  361. template<class _E, class _Tr> inline
  362.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  363.         basic_ostream<_E, _Tr>& _O, const unsigned char _C)
  364.     {return (_O << (char)_C); }
  365. template<class _E, class _Tr> inline
  366.     basic_ostream<_E, _Tr>& __cdecl operator<<(
  367.         basic_ostream<_E, _Tr>& _O, const signed short *_X)
  368.     {return (_O << (const wchar_t *)_X); }
  369.         // MANIPULATORS
  370. template<class _E, class _Tr> inline
  371.     basic_ostream<_E, _Tr>&
  372.         __cdecl endl(basic_ostream<_E, _Tr>& _O)
  373.     {_O.put(_O.widen('\n'));
  374.     _O.flush();
  375.     return (_O); }
  376. _CRTIMP2 inline basic_ostream<char, char_traits<char> >&
  377.     __cdecl endl(basic_ostream<char, char_traits<char> >& _O)
  378.     {_O.put('\n');
  379.     _O.flush();
  380.     return (_O); }
  381. _CRTIMP2 inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  382.     __cdecl endl(basic_ostream<wchar_t,
  383.         char_traits<wchar_t> >& _O)
  384.     {_O.put('\n');
  385.     _O.flush();
  386.     return (_O); }
  387. template<class _E, class _Tr> inline
  388.     basic_ostream<_E, _Tr>&
  389.         __cdecl ends(basic_ostream<_E, _Tr>& _O)
  390.     {_O.put(_E('\0'));
  391.     return (_O); }
  392. _CRTIMP2 inline basic_ostream<char, char_traits<char> >&
  393.     __cdecl ends(basic_ostream<char, char_traits<char> >& _O)
  394.     {_O.put('\0');
  395.     return (_O); }
  396. _CRTIMP2 inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  397.     __cdecl ends(basic_ostream<wchar_t,
  398.         char_traits<wchar_t> >& _O)
  399.     {_O.put('\0');
  400.     return (_O); }
  401. template<class _E, class _Tr> inline
  402.     basic_ostream<_E, _Tr>&
  403.         __cdecl flush(basic_ostream<_E, _Tr>& _O)
  404.     {_O.flush();
  405.     return (_O); }
  406. _CRTIMP2 inline basic_ostream<char, char_traits<char> >&
  407.     __cdecl flush(basic_ostream<char, char_traits<char> >& _O)
  408.     {_O.flush();
  409.     return (_O); }
  410. _CRTIMP2 inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  411.     __cdecl flush(basic_ostream<wchar_t,
  412.         char_traits<wchar_t> >& _O)
  413.     {_O.flush();
  414.     return (_O); }
  415.  
  416. #ifdef _DLL
  417. #ifndef CRTDLL2
  418. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  419.  
  420. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  421.        basic_ostream<char, char_traits<char> >&, const char *);
  422. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  423.        basic_ostream<char, char_traits<char> >&, char);
  424. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  425.        basic_ostream<char, char_traits<char> >&, const signed char *);
  426. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  427.        basic_ostream<char, char_traits<char> >&, const signed char);
  428. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  429.        basic_ostream<char, char_traits<char> >&, const unsigned char *);
  430. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  431.        basic_ostream<char, char_traits<char> >&, const unsigned char);
  432. extern template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  433.        basic_ostream<wchar_t, char_traits<wchar_t> >&, const wchar_t *);
  434. extern template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  435.        basic_ostream<wchar_t, char_traits<wchar_t> >&, wchar_t);
  436. extern template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  437.        basic_ostream<wchar_t, char_traits<wchar_t> >&, const signed short *);
  438.  
  439. #pragma warning(default:4231) /* restore previous warning */
  440. #endif        // CRTDLL2
  441. #endif        // _DLL
  442.  
  443. _STD_END
  444. #ifdef  _MSC_VER
  445. #pragma pack(pop)
  446. #endif  /* _MSC_VER */
  447.  
  448. #endif /* _OSTREAM_ */
  449.  
  450. /*
  451.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  452.  * Consult your license regarding permissions and restrictions.
  453.  */
  454.