home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / ostream.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  17.9 KB  |  628 lines

  1. #ifndef __OSTREAM_H
  2. #define __OSTREAM_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. #ifndef __STD_OSTREAM__
  6. #define __STD_OSTREAM__
  7.  
  8. /***************************************************************************
  9.  *
  10.  * ostream - Declarations for the Standard Library ostream classes
  11.  *
  12.  ***************************************************************************
  13.  *
  14.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  15.  *
  16.  * This computer software is owned by Rogue Wave Software, Inc. and is
  17.  * protected by U.S. copyright laws and other laws and by international
  18.  * treaties.  This computer software is furnished by Rogue Wave Software,
  19.  * Inc. pursuant to a written license agreement and may be used, copied,
  20.  * transmitted, and stored only in accordance with the terms of such
  21.  * license and with the inclusion of the above copyright notice.  This
  22.  * computer software or any other copies thereof may not be provided or
  23.  * otherwise made available to any other person.
  24.  *
  25.  * U.S. Government Restricted Rights.  This computer software is provided
  26.  * with Restricted Rights.  Use, duplication, or disclosure by the
  27.  * Government is subject to restrictions as set forth in subparagraph (c)
  28.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  29.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  30.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  31.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  32.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  33.  *
  34.  **************************************************************************/
  35.  
  36. #ifndef __STD_RWCOMPILER_H__ 
  37. #include <stdcomp.h>
  38. #endif
  39.  
  40. #ifdef _RW_STD_IOSTREAM
  41.  
  42. #include <ios>
  43. #include <streambuf>
  44.  
  45. #ifndef _RWSTD_NO_NAMESPACE
  46. namespace std {
  47. #endif
  48.  
  49.   extern ostream _RWSTDExport cout;
  50.   extern ostream _RWSTDExport cerr;
  51.  
  52. #ifndef _RWSTD_NO_WIDE_CHAR
  53.   extern wostream _RWSTDExport wcout;
  54.   extern wostream _RWSTDExport wcerr;
  55. #endif
  56.  
  57. template <class charT, class traits>
  58. inline bool __rw_is_out(basic_ostream<charT,traits>* s);
  59. inline bool __rw_is_out(basic_ostream<char,char_traits<char> >* s);
  60. #ifndef _RWSTD_NO_WIDE_CHAR
  61. inline bool __rw_is_out(basic_ostream<wchar_t,char_traits<wchar_t> >* s);
  62. #endif
  63. template <class charT, class traits>
  64. inline bool __rw_is_err(basic_ostream<charT,traits>* s);
  65. inline bool __rw_is_err(basic_ostream<char,char_traits<char> >* s);
  66. #ifndef _RWSTD_NO_WIDE_CHAR
  67. inline bool __rw_is_err(basic_ostream<wchar_t,char_traits<wchar_t> >* s);
  68. #endif
  69.  
  70.   /*
  71.    *  Class basic_ostream
  72.    */
  73.   
  74.   template<class charT, class traits>
  75.   class _RWSTDExportTemplate basic_ostream : virtual public basic_ios<charT, traits>
  76.   {
  77.  
  78.   public:
  79.     //
  80.     // Types:
  81.     //
  82.     typedef charT                                  char_type;
  83.     typedef _TYPENAME traits::int_type             int_type;
  84.     typedef _TYPENAME traits::pos_type             pos_type;
  85.     typedef _TYPENAME traits::off_type             off_type;
  86.     typedef traits                                 traits_type;
  87.       
  88.     typedef basic_ostream<charT, traits>           ostream_type;
  89.     typedef basic_ios<charT, traits>               ios_type;
  90.  
  91.     _EXPLICIT basic_ostream(basic_streambuf<charT, traits> *sb);
  92. #if defined ( __SUNPRO_CC )
  93.     _EXPLICIT basic_ostream(EmptyCtor) : basic_ios<charT, traits>(1) {}
  94. #endif
  95.     virtual ~basic_ostream();
  96.  
  97.     class sentry {
  98.     public:
  99.       inline _EXPLICIT
  100.       sentry(basic_ostream<charT,traits>& stream)
  101.         : __stream(stream)                  
  102.       {  
  103.                       
  104. #ifdef _RWSTD_MULTI_THREAD
  105.         if ( stream.rdbuf() ) {
  106.           __guard = new _RWSTDGuard(stream.rdbuf()->buffer_mutex_);
  107.         }
  108. #ifndef _RWSTD_NO_EXCEPTIONS
  109.         try {
  110. #endif
  111. #endif // _RWSTD_MULTI_THREAD
  112.           if (stream.tie()) 
  113.             stream.tie()->flush();
  114.  
  115.           if ( stream.is_synch() )
  116.           {
  117.             if ( __rw_is_out(&stream) )
  118.             {
  119.               fflush(stdout);
  120.             }
  121.             if ( __rw_is_err(&stream) )
  122.             {
  123.               fflush(stderr);
  124.             }
  125.           }
  126.  
  127.           if ( stream.rdbuf() )
  128.           {
  129.             if ( stream.rdbuf()->which_open_mode( ) & ios_base::app )
  130.               stream.rdbuf()->pubseekoff(0,ios_base::end,ios_base::out);
  131.           }
  132.  
  133.           ok_ = stream.good(); 
  134.  
  135.           if ( !ok_ ) 
  136.             stream.setstate(ios_base::badbit);
  137.  
  138. #if defined (_RWSTD_MULTI_THREAD) && !defined (_RWSTD_NO_EXCEPTIONS)
  139.         } catch(...) 
  140.         {
  141.           if ( __stream.rdbuf() )
  142.             delete __guard;
  143.           throw;
  144.         }          
  145. #endif // _RWSTD_MULTI_THREAD etc.
  146.  
  147.       }
  148.  
  149.       ~sentry() {
  150.         if( __stream.flags() & ios_base::unitbuf)
  151.         {
  152.           if ( __stream.rdbuf()->pubsync() == -1 )
  153.             __stream.setstate(ios_base::badbit);
  154.         }
  155.  
  156.         if ( __stream.is_synch() )
  157.         {
  158.  
  159.           if ( __rw_is_out(&__stream) ||  __rw_is_err(&__stream) )
  160.           {
  161.             if ( __stream.rdbuf()->pubsync() == -1 )
  162.               __stream.setstate(ios_base::badbit);
  163.           }
  164.         }
  165.  
  166. #ifdef _RWSTD_MULTI_THREAD
  167.         if ( __stream.rdbuf() ) {
  168.           delete __guard;
  169.         }
  170. #endif // _RWSTD_MULTI_THREAD
  171.       }
  172.       operator bool () const { return ok_; }
  173.  
  174.     private:
  175.       sentry(const sentry&); //   not defined
  176.       sentry& operator=(const sentry&); //   not defined
  177.       basic_ostream<charT,traits>& __stream;
  178.       bool ok_;
  179. #ifdef _RWSTD_MULTI_THREAD
  180.       _RWSTDGuard* __guard;
  181. #endif
  182.     };
  183.  
  184.     ostream_type& operator<<(ostream_type& (*pf)(ostream_type&));
  185.     ostream_type& operator<<(ios_base& (*pf)(ios_base&));
  186.     ostream_type& operator<<(ios_type& (*pf)(ios_type&));
  187.  
  188. #ifndef _RWSTD_NO_BOOL
  189.     ostream_type& operator<<(bool n);
  190. #endif
  191.     ostream_type& operator<<(short n);
  192.     ostream_type& operator<<(unsigned short n);
  193.     ostream_type& operator<<(int n);
  194.     ostream_type& operator<<(unsigned int n);
  195.     ostream_type& operator<<(long n);
  196.     ostream_type& operator<<(unsigned long n);
  197.     ostream_type& operator<<(float f);
  198.     ostream_type& operator<<(double f);
  199.     ostream_type& operator<<(long double f); 
  200. #ifdef _RWSTD_LONG_LONG
  201.     ostream_type& operator<<(unsigned _RWSTD_LONG_LONG n); 
  202.     ostream_type& operator<<(_RWSTD_LONG_LONG n); 
  203. #endif
  204.     ostream_type& operator<<(const void *p);
  205.    
  206.     ostream_type& operator<<(basic_streambuf<char_type, traits>& sb)
  207.     {
  208.       ios_base::iostate err = 0;
  209.  
  210. #ifndef _RWSTD_NO_EXCEPTIONS
  211.       try {
  212. #endif
  213.         if ( !(sb.which_open_mode() & ios_base::in) )
  214.           err = ios_base::failbit;
  215.         else
  216.         {
  217.           _TYPENAME basic_ostream<charT, traits>::sentry opfx(*this);
  218.  
  219.           if(opfx)
  220.           {
  221.             if ( traits::eq_int_type(sb.sgetc(),traits::eof()))
  222.               err = ios_base::failbit;
  223.             else {
  224.               int_type    c;
  225.               while( !traits::eq_int_type( (c = sb.sbumpc()),traits::eof()) )
  226.               {
  227.                 if( traits::eq_int_type(this->rdbuf()->sputc(c),traits::eof()) )
  228.                 {
  229.                   err = ios_base::failbit;
  230.                   break;
  231.                 }
  232.               }
  233.             }
  234.           }
  235.         }
  236.  
  237. #ifndef _RWSTD_NO_EXCEPTIONS
  238.       }
  239. #endif
  240.  
  241. #ifndef _RWSTD_NO_EXCEPTIONS
  242.       catch(...)
  243.       {
  244.         bool flag = false;
  245.         try {
  246.           this->setstate(ios_base::badbit);
  247.         }
  248.         catch( ios_base::failure ) { flag= true; }
  249.         if ( flag ) throw;
  250.       }
  251. #endif
  252.       if ( err ) this->setstate(err);
  253.       return *this;
  254.     }
  255.  
  256.     ostream_type& operator<<(basic_streambuf<char_type, traits> *sb)
  257.     {
  258.       ios_base::iostate err = 0; 
  259.  
  260. #ifndef _RWSTD_NO_EXCEPTIONS
  261.       try {
  262. #endif
  263.         if (sb)
  264.         {
  265.           if ( !(sb->which_open_mode() & ios_base::in) )
  266.             err = ios_base::failbit;
  267.           else
  268.           {
  269.             _TYPENAME basic_ostream<charT, traits>::sentry opfx(*this);
  270.  
  271.             if(opfx)
  272.             {
  273.                 if ( traits::eq_int_type(sb->sgetc(),traits::eof()))
  274.                 err = ios_base::failbit;
  275.               else {
  276.                 int_type    c;
  277.                 while( !traits::eq_int_type( (c = sb->sbumpc()),traits::eof()))
  278.                 {
  279.                   if( traits::eq_int_type(this->rdbuf()->sputc(c),traits::eof()))
  280.                   {
  281.                     err = ios_base::failbit;
  282.                     break;
  283.                  }
  284.                 }
  285.               }
  286.             }
  287.           }
  288.         }
  289.         else 
  290.           err = ios_base::badbit;  
  291.  
  292. #ifndef _RWSTD_NO_EXCEPTIONS
  293.       }
  294. #endif
  295.  
  296. #ifndef _RWSTD_NO_EXCEPTIONS
  297.       catch(...)
  298.       {
  299.         bool flag = false;
  300.         try {
  301.           this->setstate(ios_base::badbit);
  302.         }
  303.         catch( ios_base::failure ) { flag= true; }
  304.         if ( flag ) throw;
  305.       }
  306. #endif
  307.       if ( err ) this->setstate(err);
  308.       return *this;
  309.     }
  310.  
  311.     ostream_type& put(char_type c);
  312.     ostream_type& write(const char_type *s, streamsize n);
  313.     ostream_type& flush();
  314.  
  315.     ostream_type& seekp(pos_type pos)
  316.     {
  317. #ifdef _RWSTD_MULTI_THREAD
  318.       if ( this->rdbuf() ) {
  319. #  ifndef __TURBOC__
  320.         _RWSTDGuard guard(this->rdbuf()->buffer_mutex_);
  321. #  else
  322.       STDGUARD(this->rdbuf()->buffer_mutex_);
  323. #  endif // __TURBOC__
  324. #endif // _RWSTD_MULTI_THREAD
  325.  
  326.       if ( this->fail() ) return *this;
  327.  
  328.       this->rdbuf()->pubseekpos(pos, ios_base::out);
  329.  
  330.       return *this;
  331. #ifdef _RWSTD_MULTI_THREAD
  332.       }
  333. #endif // _RWSTD_MULTI_THREAD
  334.     }
  335.  
  336.     ostream_type& seekp(off_type , ios_base::seekdir );
  337.     pos_type tellp();
  338.     
  339. #ifdef _RWSTD_MULTI_THREAD
  340.     _RWSTDGuard *ostream_sentry_guard;
  341. #endif
  342.   protected:
  343.     basic_ostream();
  344. #ifdef _RWSTD_PROTECTED_OSTREAM_COPY_CONSTRUCTOR
  345.     basic_ostream(const basic_ostream&);
  346. #endif
  347.   };
  348.  
  349.   inline bool __rw_is_out(basic_ostream<char,char_traits<char> >* s)
  350.   { return (void*)s == (void*)&cout; }
  351. #ifndef _RWSTD_NO_WIDE_CHAR
  352.   inline bool __rw_is_out(basic_ostream<wchar_t,char_traits<wchar_t> >* s)
  353.   { return (void*)s == (void*)&wcout; }
  354. #endif
  355.  
  356.   inline bool __rw_is_err(basic_ostream<char,char_traits<char> >* s)
  357.   { return (void*)s == (void*)&cerr; }
  358. #ifndef _RWSTD_NO_WIDE_CHAR
  359.   inline bool __rw_is_err(basic_ostream<wchar_t,char_traits<wchar_t> >* s)
  360.   { return (void*)s == (void*)&wcerr; }
  361. #endif
  362.  
  363.   template <class charT, class traits>
  364.   inline bool __rw_is_out(basic_ostream<charT,traits>* s)
  365.   { return false; }
  366.  
  367.   template <class charT, class traits>
  368.   inline bool __rw_is_err(basic_ostream<charT,traits>* s)
  369.   { return false; }
  370.   //
  371.   // Class ostream_iterator
  372.   //
  373.  
  374. #if !defined(_MSC_VER) || defined(__BORLANDC__)
  375.   template <class T, class charT, class traits>
  376. #else
  377.   template <class T, class charT=char, class traits=char_traits<charT> >
  378. #endif
  379.   class _RWSTDExportTemplate ostream_iterator
  380.     : public iterator<output_iterator_tag,T,_TYPENAME traits::off_type,T*,T&>
  381.   {
  382.   protected:
  383.  
  384.     basic_ostream<charT,traits>* __stream;
  385.     const charT*    __str;
  386.  
  387.   public:
  388.     //
  389.     //Types:
  390.     //
  391.     typedef T value_type;
  392.     typedef charT char_type;
  393.     typedef traits traits_type;
  394.     typedef basic_ostream<charT,traits> ostream_type;
  395.  
  396.     ostream_iterator (basic_ostream<charT,traits>& s) 
  397.       : __stream(&s),__str(0) 
  398.     { ; }
  399.     ostream_iterator (basic_ostream<charT,traits>& s,const charT* c) 
  400.       : __stream(&s), __str((charT *)c)  
  401.     { ; }
  402.     ostream_iterator ( const ostream_iterator<T,charT,traits>& x )
  403.       : __stream(x.__stream) , __str(x.__str)
  404.     { ; }
  405.     ostream_iterator<T,charT,traits>& operator= (const T& value)
  406.     { 
  407.       *__stream << (T)value;
  408.       if (__str) *__stream << __str;
  409.       return *this;
  410.     }
  411.     ostream_iterator<T,charT,traits>& operator*  ()    { return *this; }
  412.     ostream_iterator<T,charT,traits>& operator++ ()    { return *this; } 
  413.     ostream_iterator<T,charT,traits>& operator++ (int) { return *this; }
  414.   };
  415. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  416.   typedef basic_ostream<char>                            ostream;
  417. #else
  418.   typedef basic_ostream<char, char_traits<char> >        ostream;
  419. #endif
  420.  
  421. #ifndef _RWSTD_NO_WIDE_CHAR
  422. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  423.   typedef basic_ostream<wchar_t>                         wostream;
  424. #else
  425.   typedef basic_ostream<wchar_t, char_traits<wchar_t> >  wostream;
  426. #endif
  427. #endif
  428.  
  429.   // charT and charT* insertors
  430.  
  431.   template<class charT, class traits>
  432.   basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  433.                                                                   charT );
  434.  
  435. #ifndef _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
  436.  
  437.   template<class charT, class traits>
  438.   basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  439.                                                                   char );
  440.  
  441. #ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
  442.   template <class traits>
  443.   basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  444.                                                                  char );
  445. #else
  446.   ostream& _RWSTDExport operator<< ( ostream&, char );
  447. #endif // _RWSTD_NO_FUNC_PARTIAL_SPEC
  448. #endif // _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
  449.   template<class charT, class traits>
  450.   basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  451.                                                                   const charT* );
  452.  
  453. #ifndef _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
  454.  
  455.   template<class charT, class traits>
  456.   basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  457.                                                                   const char* );
  458.  
  459. #ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
  460.   template <class traits>
  461.   basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  462.                                                                  const char* );
  463. #else
  464.   ostream& _RWSTDExport operator<< ( ostream&, const char* );
  465. #endif // _RWSTD_NO_FUNC_PARTIAL_SPEC
  466. #endif // _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
  467.  
  468.   /*
  469.    * signed and unsigned insertors
  470.    */
  471.   
  472. #ifndef _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
  473.   template <class traits>
  474.   basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  475.                                                                  unsigned char );
  476.  
  477.   template <class traits>
  478.   basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  479.                                                                  signed char );
  480.  
  481.   template <class traits>
  482.   basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&,
  483.                                                                  const unsigned char* );
  484.  
  485.   template <class traits>
  486.   basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&,
  487.                                                                  const signed char* );
  488. #endif // _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
  489.  
  490.   /*
  491.    * String insertor
  492.    */
  493.   template<class charT, class traits, class Allocator>
  494.   inline basic_ostream<charT, traits>&
  495.   operator<< (basic_ostream<charT,traits>& os,
  496.           const basic_string<charT,traits,Allocator>& s)
  497.   {
  498.     ios_base::iostate err = 0;
  499.  
  500. #ifndef _RWSTD_NO_EXCEPTIONS
  501.     try {
  502. #endif
  503.   
  504.       _TYPENAME basic_ostream<charT, traits>::sentry opfx(os);
  505.       if (opfx)
  506.       {
  507.     int   dlen =s.length();
  508.     int   pad = os.width() - dlen;
  509.  
  510.     // place right padding
  511.     if( !(os.flags() & ios_base::adjustfield) ||  os.flags() & ios_base::right )
  512.     {
  513.       while(--pad >= 0)
  514.       {
  515.         if( traits::eq_int_type(os.rdbuf()->sputc(os.fill()),traits::eof())) {
  516.           err = ios_base::badbit;
  517.           break;
  518.         }
  519.       }
  520.     }
  521.          
  522.     // output internal padding
  523.     if(os.good() && (os.flags() & ios_base::internal))
  524.     {
  525.       while(--pad >= 0)
  526.       {
  527.         if( traits::eq_int_type(os.rdbuf()->sputc(os.fill()),traits::eof()))
  528.         {
  529.           err = ios_base::badbit;
  530.           break;
  531.         }
  532.       }
  533.     }
  534.  
  535.     if(os.good() && dlen) {
  536.       if(os.rdbuf() && (os.rdbuf()->sputn(s.c_str(), dlen) != dlen))
  537.         err = ios_base::badbit;
  538.     }
  539.  
  540.     // output left padding. 
  541.     if(os.good() && (os.flags() & ios_base::left))
  542.     {
  543.       while(--pad >= 0)
  544.       {
  545.         if( traits::eq_int_type(os.rdbuf()->sputc(os.fill()),traits::eof()))
  546.         {
  547.           err = ios_base::badbit;
  548.           break;
  549.         }
  550.       }
  551.     }
  552.  
  553.     os.width(0);
  554.       }
  555.  
  556. #ifndef _RWSTD_NO_EXCEPTIONS
  557.     }
  558. #endif
  559.  
  560. #ifndef _RWSTD_NO_EXCEPTIONS
  561.     catch(...)
  562.     {
  563.       bool flag = false;
  564.       try {
  565.         os.setstate(ios_base::badbit);
  566.       }
  567.       catch( ios_base::failure ) { flag= true; }
  568.       if ( flag ) throw;
  569.     }
  570. #endif
  571.  
  572.     if ( err ) os.setstate(err);  
  573.  
  574.     return os;
  575.   }
  576.  
  577.   extern ostream _RWSTDExport cout;
  578.   extern ostream _RWSTDExport cerr;
  579.   extern ostream _RWSTDExport clog;
  580.  
  581.   /*
  582.    * declare a couple of standard manipulators
  583.    * global functions
  584.    */
  585.   
  586.   template<class charT, class traits>
  587.   inline basic_ostream<charT, traits>&
  588.   endl(basic_ostream<charT, traits>& os)
  589.   {
  590.     os.put( charT('\n') );
  591.     os.flush(); 
  592.     return os;
  593.   }
  594.  
  595.   template<class charT, class traits>
  596.   inline basic_ostream<charT, traits>&
  597.   ends(basic_ostream<charT, traits>& os)
  598.   {
  599.     os.put( charT(0) );
  600.     return os;
  601.   }
  602.  
  603.   template<class charT, class traits>
  604.   inline basic_ostream<charT, traits>&
  605.   flush(basic_ostream<charT, traits>& os)
  606.   {
  607.     os.flush();
  608.     return os;
  609.   }
  610.  
  611. #ifndef _RWSTD_NO_NAMESPACE
  612. }
  613. #endif
  614.  
  615. #ifdef _RWSTD_COMPILE_INSTANTIATE
  616. #include <ostream.cc>
  617. #endif
  618.  
  619. #endif //_RW_STD_IOSTREAM
  620.  
  621. #endif //__STD_OSTREAM__ 
  622. #ifndef __USING_STD_NAMES__
  623.   using namespace std;
  624. #endif
  625.  
  626. #pragma option pop
  627. #endif /* __OSTREAM_H */
  628.