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

  1. #ifndef __ISTREAM_H
  2. #define __ISTREAM_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. #ifndef __STD_ISTREAM__
  6. #define __STD_ISTREAM__
  7.  
  8. /***************************************************************************
  9.  *
  10.  * istream - Declarations for the Standard Library istreams
  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. #include <stdcomp.h>
  37.  
  38. #include <ios>
  39. #include <ostream>
  40.  
  41. #ifndef _RWSTD_NO_NAMESPACE
  42. namespace std {
  43. #endif
  44.   
  45.   extern istream _RWSTDExport cin;
  46.  
  47. #ifndef _RWSTD_NO_WIDE_CHAR
  48.   extern wistream _RWSTDExport wcin;
  49. #endif
  50.  
  51.   template<class charT, class traits>
  52.   class _RWSTDExportTemplate basic_istream : virtual public basic_ios<charT, traits>
  53.   {
  54.       
  55.   public:
  56.     // 
  57.     // Types:
  58.     //
  59.     typedef charT                                    char_type;
  60.     typedef _TYPENAME traits::int_type               int_type;
  61.     typedef _TYPENAME traits::pos_type               pos_type;
  62.     typedef _TYPENAME traits::off_type               off_type;
  63.     typedef traits                                   traits_type;
  64.       
  65.     typedef basic_istream<charT, traits>             istream_type;
  66.     typedef basic_ios<charT, traits>                 ios_type;
  67.     typedef basic_streambuf<charT, traits>           streambuf_type;
  68.  
  69.     _EXPLICIT basic_istream(basic_streambuf<charT, traits> *sb);
  70. #if defined ( __SUNPRO_CC )
  71.     _EXPLICIT basic_istream(EmptyCtor) : basic_ios<charT, traits>(1) {}
  72. #endif
  73.     virtual ~basic_istream();
  74.  
  75.     class sentry 
  76.     {
  77.     public:
  78.  
  79.       inline _EXPLICIT sentry(basic_istream<charT,traits>& stream,
  80.                               bool noskipws = 0)
  81.         : __stream(stream)
  82.       {
  83. #ifdef _RWSTD_MULTI_THREAD
  84.         if ( stream.rdbuf() )
  85.           __guard = new _RWSTDGuard(stream.rdbuf()->buffer_mutex_);
  86. #ifndef _RWSTD_NO_EXCEPTIONS
  87.         try {
  88. #endif
  89. #endif // _RWSTD_MULTI_THREAD
  90.  
  91.           if(!(stream.good())) 
  92.           {   
  93.             stream.setstate(ios_base::failbit);
  94.             __ok = false;
  95.           }
  96.           else
  97.           {
  98.             if(stream.tie())  
  99.               stream.tie()->flush();
  100.  
  101.             if(!noskipws && (stream.flags() & ios_base::skipws))  
  102.             {
  103.               int_type        c;
  104.  
  105. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  106.               const ctype<charT>& ct = use_facet< ctype<charT> >(stream.getloc());
  107. #else
  108.               const ctype<charT>& ct = use_facet(stream.getloc(),(ctype<charT>*)0);
  109. #endif // _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  110.          
  111.               while((c = stream.rdbuf()->sgetc()),
  112.                     ( !traits::eq_int_type(c,traits::eof()) && ct.is(ct.space,c) )) 
  113.               {
  114.                 stream.rdbuf()->snextc();
  115.               }
  116.  
  117.               if( traits::eq_int_type(c,traits::eof()) )
  118.               {
  119.                 stream.setstate(ios_base::eofbit);
  120.               }
  121.             }
  122.  
  123.             if(!(stream.good())) 
  124.             { 
  125.               stream.setstate(ios_base::failbit);
  126.               __ok = false;
  127.             }
  128.             else 
  129.               __ok =true;
  130.           }
  131. #if defined (_RWSTD_MULTI_THREAD) && !defined (_RWSTD_NO_EXCEPTIONS)
  132.         } catch(...) 
  133.         {
  134.           if ( __stream.rdbuf() )
  135.             delete __guard;
  136.           throw;
  137.         }          
  138. #endif // _RWSTD_MULTI_THREAD etc.
  139.       }
  140.  
  141.       ~sentry() { 
  142. #ifdef _RWSTD_MULTI_THREAD
  143.         if ( __stream.rdbuf() )
  144.           delete __guard;
  145. #endif // _RWSTD_MULTI_THREAD
  146.       }
  147.       operator bool () { return __ok; }
  148.  
  149.     private:
  150.       basic_istream<charT,traits>& __stream;
  151.       bool __ok;
  152. #ifdef _RWSTD_MULTI_THREAD
  153.       _RWSTDGuard* __guard;
  154. #endif
  155.  
  156.     };
  157.  
  158.     istream_type& operator>>(istream_type& (*pf)(istream_type&));
  159.     istream_type& operator>>(ios_base& (*pf)(ios_base&));
  160.     istream_type& operator>>(ios_type& (*pf)(ios_type&));
  161.  
  162. #ifndef _RWSTD_NO_BOOL
  163.     istream_type& operator>>(bool& n);
  164. #endif
  165.     istream_type& operator>>(short& n);
  166.     istream_type& operator>>(unsigned short& n);
  167.     istream_type& operator>>(int& n);
  168.     istream_type& operator>>(unsigned int& n);
  169.     istream_type& operator>>(long& n);
  170.     istream_type& operator>>(unsigned long& n);
  171.     istream_type& operator>>(float& f);
  172.     istream_type& operator>>(double& f);
  173.     istream_type& operator>>(long double& f);
  174. #ifdef _RWSTD_LONG_LONG
  175.     istream_type& operator>>(_RWSTD_LONG_LONG& n);
  176.     istream_type& operator>>(unsigned _RWSTD_LONG_LONG& n);
  177. #endif
  178.     istream_type& operator>>(void*& p);
  179.     istream_type& operator>>(streambuf_type& sb);
  180.     istream_type& operator>>(streambuf_type *sb);
  181.  
  182.     int_type get()
  183.     {
  184.       ios_base::iostate err = 0;
  185.  
  186. #ifndef _RWSTD_NO_EXCEPTIONS
  187.       try {
  188. #endif 
  189.         _TYPENAME basic_istream<charT, traits>::sentry ipfx(*this,1);
  190.         __chcount = 0;
  191.  
  192.         if(ipfx)
  193.         { 
  194.           int_type      c = this->rdbuf()->sbumpc();
  195.  
  196.           if( traits::eq_int_type(c,traits::eof()) )  
  197.           {
  198.             err = ios_base::eofbit | ios_base::failbit;
  199.           }
  200.           else
  201.           {
  202.             __chcount = 1;
  203.             return c;
  204.           }
  205.         }
  206.  
  207. #ifndef _RWSTD_NO_EXCEPTIONS
  208.       } // end of try block
  209.       catch(...)
  210.       {
  211.         bool flag = false;
  212.         try {
  213.           this->setstate(ios_base::badbit);
  214.         }
  215.         catch( ios_base::failure ) { flag= true; }
  216.         if ( flag ) throw;
  217.       }
  218. #endif // _RWSTD_NO_EXCEPTIONS
  219.  
  220.       if ( err ) this->setstate(err);
  221.  
  222.       return traits::eof();
  223.     }
  224.     istream_type& get(char_type *s, streamsize n, char_type delim);
  225.     istream_type& get(char_type *s, streamsize n)
  226.     { return get(s,n,this->widen('\n')); }
  227.  
  228.     istream_type& get(char_type& c);
  229.  
  230.     istream_type& get(streambuf_type& sb, char_type delim);
  231.     istream_type& get(streambuf_type& sb)
  232.     { return get(sb,this->widen('\n')); }
  233.  
  234.     istream_type& getline(char_type *s, streamsize n, char_type delim);
  235.     istream_type& getline(char_type *s, streamsize n)
  236.     { return getline(s,n,this->widen('\n')); }
  237.  
  238.     istream_type& ignore(streamsize n = 1, int_type delim = traits::eof());
  239.  
  240.     istream_type& read(char_type *s, streamsize n);
  241.  
  242.     streamsize readsome(char_type *s, streamsize n);
  243.  
  244.     int_type peek();
  245.       
  246.     pos_type tellg();
  247.  
  248.     //
  249.     // The seekg functions are inlined for portability
  250.     //
  251.     istream_type& seekg(pos_type pos)
  252.     {
  253.       ios_base::iostate err = 0;
  254.  
  255. #ifndef _RWSTD_NO_EXCEPTIONS
  256.       try {
  257. #endif 
  258.         if ( this->rdstate() & ios_base::eofbit )
  259.           clear( this->rdstate() & ~ios_base::eofbit );
  260.  
  261. #ifdef _RWSTD_MULTI_THREAD
  262.         if ( this->rdbuf() ) {
  263.           _RWSTDGuard guard(this->rdbuf()->buffer_mutex_);
  264. #endif // _RWSTD_MULTI_THREAD
  265.  
  266.           // using fail will disable seek when at end of char buffer, bad() would work better
  267.           if ( !this->fail() )
  268.               this->rdbuf()->pubseekpos(pos, ios_base::in);
  269.  
  270. #ifdef _RWSTD_MULTI_THREAD
  271.         }
  272. #endif // _RWSTD_MULTI_THREAD
  273.  
  274. #ifndef _RWSTD_NO_EXCEPTIONS
  275.       } // end of try block
  276. #endif
  277.  
  278. #ifndef _RWSTD_NO_EXCEPTIONS
  279.       catch(...)
  280.       {
  281.         bool flag = false;
  282.         try {
  283.           this->setstate(ios_base::badbit);
  284.         }
  285.         catch( ios_base::failure ) { flag= true; }
  286.         if ( flag ) throw;
  287.       }
  288. #endif // _RWSTD_NO_EXCEPTIONS
  289.  
  290.       return *this;
  291.     }
  292.  
  293.     istream_type& seekg(off_type off, ios_base::seekdir dir)
  294.     {
  295.       ios_base::iostate err = 0;
  296.  
  297. #ifndef _RWSTD_NO_EXCEPTIONS
  298.       try {
  299. #endif 
  300.  
  301. #ifdef _RWSTD_MULTI_THREAD
  302.           if ( this->rdbuf() ) {
  303.               _RWSTDGuard guard(this->rdbuf()->buffer_mutex_);
  304. #endif 
  305.               if ( !this->fail() )
  306.               {
  307.                   this->rdbuf()->pubseekoff(off, dir, ios_base::in);
  308.               }
  309.  
  310. #ifdef _RWSTD_MULTI_THREAD
  311.           }
  312. #endif 
  313.  
  314. #ifndef _RWSTD_NO_EXCEPTIONS
  315.       }
  316. #endif
  317.  
  318. #ifndef _RWSTD_NO_EXCEPTIONS
  319.       catch(...)
  320.       {
  321.         bool flag = false;
  322.         try {
  323.           this->setstate(ios_base::badbit);
  324.         }
  325.         catch( ios_base::failure ) { flag= true; }
  326.         if ( flag ) throw;
  327.       }
  328. #endif // _RWSTD_NO_EXCEPTIONS
  329.  
  330.       return *this;
  331.     }
  332.  
  333.     int sync();
  334.  
  335.     istream_type& putback(char_type c);
  336.  
  337.     istream_type& unget();
  338.  
  339.     streamsize gcount() const;
  340.  
  341. #ifdef _RWSTD_MULTI_THREAD
  342.     _RWSTDGuard *istream_sentry_guard;
  343. #endif
  344.     
  345.   protected:
  346.     basic_istream( );
  347.  
  348.   private:
  349.     streamsize       __chcount;
  350.   };
  351.   /*
  352.    *  Class basic_iostream
  353.    */
  354.  
  355.   template<class charT, class traits>
  356.   class _RWSTDExportTemplate basic_iostream : public basic_istream<charT, traits>,
  357.                                               public basic_ostream<charT, traits> 
  358.   {
  359.   public:
  360.     _EXPLICIT basic_iostream(basic_streambuf<charT, traits> *sb);
  361.     virtual ~basic_iostream();
  362.       
  363.   protected:
  364.     _EXPLICIT basic_iostream();
  365.   };
  366.  
  367.   //
  368.   // istream iterator.
  369.   //
  370.  
  371. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  372.  
  373.   template <class T, class charT, class traits, class Distance>
  374.   class _RWSTDExportTemplate istream_iterator;
  375.  
  376.   template <class T, class charT, class traits, class Distance>
  377.   bool operator== 
  378.   (const istream_iterator<T,charT,traits,Distance>& x,
  379.    const istream_iterator<T,charT,traits,Distance>& y);
  380.  
  381. #endif
  382.  
  383. #if !defined(_MSC_VER) || defined(__BORLANDC__)
  384.   template <class T, class charT, class traits, class Distance>
  385. #else
  386.   template <class T, class charT=char, class traits=char_traits<charT>, class Distance=ptrdiff_t>
  387. #endif
  388.   class _RWSTDExportTemplate istream_iterator
  389.     : public iterator<input_iterator_tag,T,Distance,const T*, const T&>
  390.   {
  391. #ifdef __TURBOC__
  392.     friend inline bool (std::operator==) (
  393.         const istream_iterator<T,charT,traits,Distance>& x,
  394.         const istream_iterator<T,charT,traits,Distance>& y);
  395. #else
  396.     friend inline bool operator== (
  397.         const istream_iterator<T,charT,traits,Distance>& x,
  398.         const istream_iterator<T,charT,traits,Distance>& y);
  399. #endif
  400.   public: 
  401.     typedef charT char_type;
  402.     typedef traits traits_type;
  403.     typedef basic_istream<charT,traits> istream_type;
  404.     typedef T value_type;
  405.     
  406.     istream_iterator () : stream(&cin), end_marker(false) {}
  407.     istream_iterator (basic_istream<charT,traits>& s) : stream(&s) { read(); }
  408.     istream_iterator ( const istream_iterator<T,charT,traits,Distance>& x )
  409.       : stream(x.stream) , value(x.value) , end_marker(x.end_marker)
  410.     { ; }
  411.     const T& operator* () const { return value; }
  412. #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  413.     const T* operator->() const { return &value; }
  414. #endif // _RWSTD_NO_NONCLASS_ARROW_RETURN
  415.     istream_iterator<T,charT,traits,Distance>& operator++ ()
  416.     { 
  417.       read(); 
  418.       return *this;
  419.     }
  420.     istream_iterator<T,charT,traits,Distance> operator++ (int)
  421.     {
  422.       istream_iterator<T,charT,traits,Distance> tmp = *this;
  423.       read(); 
  424.       return tmp;
  425.     }
  426.       
  427.   protected:
  428.     basic_istream<charT,traits>* stream;
  429.     T        value;
  430.     bool     end_marker;
  431.  
  432.     void read ()
  433.     {
  434.       end_marker = (*stream) ? true : false;
  435.       if (end_marker) *stream >> value;
  436.       end_marker = (*stream) ? true : false;
  437.     }
  438.  
  439.   };
  440.  
  441.   template <class T, class charT, class traits, class Distance>
  442.   inline bool operator== 
  443.   (const istream_iterator<T,charT,traits,Distance>& x,
  444.    const istream_iterator<T,charT,traits,Distance>& y)
  445.   {
  446.     return x.stream == y.stream && x.end_marker == y.end_marker ||
  447.     x.end_marker == false && y.end_marker == false;
  448.   }
  449.  
  450. #ifndef _RWSTD_NO_NAMESPACE
  451.   template <class T, class charT, class traits, class Distance>
  452.   inline bool operator!= 
  453.   (const istream_iterator<T,charT,traits,Distance>& x,
  454.    const istream_iterator<T,charT,traits,Distance>& y)
  455.   {
  456.     return !(x == y);
  457.   }
  458. #endif
  459.  
  460.   template<class charT, class traits>
  461.   basic_istream<charT, traits>& _RWSTDExportTemplate
  462.   ws(basic_istream<charT, traits>& is);
  463.  
  464. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  465.   typedef basic_istream<char>                               istream;
  466. #else
  467.   typedef basic_istream<char, char_traits<char> >           istream;
  468. #endif
  469.  
  470. #ifndef _RWSTD_NO_WIDE_CHAR
  471. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  472.   typedef basic_istream<wchar_t>                            wistream;
  473. #else
  474.   typedef basic_istream<wchar_t, char_traits<wchar_t> >     wistream;
  475. #endif
  476. #endif
  477.  
  478. #ifndef _RWSTD_NO_DEFAULT_TEMPLATES
  479.   typedef basic_iostream<char>                              iostream;
  480. #else
  481.   typedef basic_iostream<char, char_traits<char> >          iostream;
  482. #endif
  483.  
  484. #ifndef _RWSTD_NO_WIDE_CHAR
  485. #ifndef _RWSTD_NO_DEFAULT_TEMPLATES
  486.   typedef basic_iostream<wchar_t>                           wiostream;
  487. #else
  488.   typedef basic_iostream<wchar_t, char_traits<wchar_t> >    wiostream;
  489. #endif
  490. #endif
  491.   // charT and charT* extractors
  492.  
  493.   template<class charT, class traits>
  494.   basic_istream<charT, traits>& _RWSTDExportTemplate 
  495.   operator>> ( basic_istream<charT, traits>&, charT& );
  496.  
  497.   template<class charT, class traits>
  498.   basic_istream<charT, traits>& _RWSTDExportTemplate 
  499.   operator>> ( basic_istream<charT, traits>&, charT* );
  500.   // signed and unsigned extractors
  501.  
  502. #ifndef _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
  503.   template <class traits>
  504.   basic_istream<char, traits>& _RWSTDExportTemplate 
  505.   operator>> ( basic_istream<char, traits>&, unsigned char& );
  506.  
  507.   template <class traits>
  508.   basic_istream<char, traits>& _RWSTDExportTemplate 
  509.   operator>> ( basic_istream<char, traits>&, signed char& );
  510.  
  511.   template <class traits>
  512.   basic_istream<char, traits>& _RWSTDExportTemplate 
  513.   operator>> ( basic_istream<char, traits>&, unsigned char* );
  514.  
  515.   template <class traits>
  516.   basic_istream<char, traits>& _RWSTDExportTemplate 
  517.   operator>> ( basic_istream<char, traits>&, signed char* );
  518. #endif
  519.  
  520.   // String extractor and getline
  521.  
  522. #ifndef _RWSTD_NO_NAMESPACE
  523. }
  524. namespace __rwstd {
  525. #endif
  526.  
  527.   template <class streamT, class stringT, class traits>
  528.   streamT& rw_extract_string(streamT& is, stringT& s, traits);
  529.  
  530. #ifndef _RWSTD_NO_NAMESPACE
  531. }
  532. namespace std {
  533. #endif
  534.  
  535.   template<class charT, class traits, class Allocator>
  536.   inline basic_istream<charT, traits>&
  537.   operator>> (basic_istream<charT,traits>& is,
  538.                                    basic_string<charT,traits,Allocator>& s)
  539.   {
  540.     return __RWSTD::rw_extract_string(is,s,traits());
  541.   }
  542.  
  543.   template<class charT, class traits, class Allocator>
  544.   basic_istream<charT, traits>& _RWSTDExportTemplate
  545.   getline( basic_istream<charT, traits>&,
  546.            basic_string<charT, traits, Allocator>&,
  547.            charT delim );
  548.  
  549.   template<class charT, class traits, class Allocator>
  550.   inline basic_istream<charT, traits>& 
  551.   getline( basic_istream<charT, traits>& is,
  552.            basic_string<charT, traits, Allocator>& str )
  553.   { return getline(is,str,is.widen('\n')); }  
  554. #ifndef _RWSTD_NO_NAMESPACE
  555. }
  556. #endif
  557. #ifdef _RWSTD_COMPILE_INSTANTIATE
  558. #include <istream.cc>
  559. #endif
  560. #endif  // __STD__ISTREAM__ 
  561. #ifndef __USING_STD_NAMES__
  562.   using namespace std;
  563. #endif
  564.  
  565. #pragma option pop
  566. #endif /* __ISTREAM_H */
  567.