istream.tcc

00001 // istream classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 //
00032 // ISO C++ 14882: 27.6.2  Output streams
00033 //
00034 
00035 #pragma GCC system_header
00036 
00037 #include <locale>
00038 #include <ostream> // For flush()
00039 
00040 namespace std 
00041 {
00042   template<typename _CharT, typename _Traits>
00043     basic_istream<_CharT, _Traits>::sentry::
00044     sentry(basic_istream<_CharT, _Traits>& __in, bool __noskipws)
00045     {
00046       if (__in.good()) 
00047     {
00048       if (__in.tie())
00049         __in.tie()->flush();
00050       if (!__noskipws && (__in.flags() & ios_base::skipws))
00051         {     
00052           const __int_type __eof = traits_type::eof();
00053           __streambuf_type* __sb = __in.rdbuf();
00054           __int_type __c = __sb->sgetc();
00055 
00056           if (__in._M_check_facet(__in._M_fctype))
00057         while (!traits_type::eq_int_type(__c, __eof)
00058                && __in._M_fctype->is(ctype_base::space, 
00059                          traits_type::to_char_type(__c)))
00060           __c = __sb->snextc();
00061 
00062 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00063 //195.  Should basic_istream::sentry's constructor ever set eofbit? 
00064           if (traits_type::eq_int_type(__c, __eof))
00065         __in.setstate(ios_base::eofbit);
00066 #endif
00067         }
00068     }
00069 
00070       if (__in.good())
00071     _M_ok = true;
00072       else
00073     {
00074       _M_ok = false;
00075       __in.setstate(ios_base::failbit);
00076     }
00077     }
00078 
00079   template<typename _CharT, typename _Traits>
00080     basic_istream<_CharT, _Traits>& 
00081     basic_istream<_CharT, _Traits>::
00082     operator>>(__istream_type& (*__pf)(__istream_type&))
00083     {
00084       __pf(*this);
00085       return *this;
00086     }
00087 
00088   template<typename _CharT, typename _Traits>
00089     basic_istream<_CharT, _Traits>& 
00090     basic_istream<_CharT, _Traits>::
00091     operator>>(__ios_type& (*__pf)(__ios_type&))
00092     {
00093       __pf(*this);
00094       return *this;
00095     }
00096   
00097   template<typename _CharT, typename _Traits>
00098     basic_istream<_CharT, _Traits>& 
00099     basic_istream<_CharT, _Traits>::
00100     operator>>(ios_base& (*__pf)(ios_base&))
00101     {
00102       __pf(*this);
00103       return *this;
00104     }
00105   
00106   template<typename _CharT, typename _Traits>
00107     basic_istream<_CharT, _Traits>& 
00108     basic_istream<_CharT, _Traits>::
00109     operator>>(bool& __n)
00110     {
00111       sentry __cerb(*this, false);
00112       if (__cerb) 
00113     {
00114       try 
00115         {
00116           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00117           if (_M_check_facet(_M_fnumget))
00118         _M_fnumget->get(*this, 0, *this, __err, __n);
00119           this->setstate(__err);
00120         }
00121       catch(exception& __fail)
00122         {
00123           // 27.6.1.2.1 Common requirements.
00124           // Turn this on without causing an ios::failure to be thrown.
00125           this->setstate(ios_base::badbit);
00126           if ((this->exceptions() & ios_base::badbit) != 0)
00127         __throw_exception_again;
00128         }
00129     }
00130       return *this;
00131     }
00132 
00133   template<typename _CharT, typename _Traits>
00134     basic_istream<_CharT, _Traits>& 
00135     basic_istream<_CharT, _Traits>::
00136     operator>>(short& __n)
00137     {
00138       sentry __cerb(*this, false);
00139       if (__cerb) 
00140     {
00141       try 
00142         {
00143           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00144           long __l;
00145           if (_M_check_facet(_M_fnumget))
00146         _M_fnumget->get(*this, 0, *this, __err, __l);
00147 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00148           // 118. basic_istream uses nonexistent num_get member functions.
00149           if (!(__err & ios_base::failbit)
00150           && (numeric_limits<short>::min() <= __l 
00151               && __l <= numeric_limits<short>::max()))
00152         __n = __l;
00153           else
00154                 __err |= ios_base::failbit;
00155 #endif
00156           this->setstate(__err);
00157         }
00158       catch(exception& __fail)
00159         {
00160           // 27.6.1.2.1 Common requirements.
00161           // Turn this on without causing an ios::failure to be thrown.
00162           this->setstate(ios_base::badbit);
00163           if ((this->exceptions() & ios_base::badbit) != 0)
00164         __throw_exception_again;
00165         }
00166     }
00167       return *this;
00168     }
00169 
00170   template<typename _CharT, typename _Traits>
00171     basic_istream<_CharT, _Traits>& 
00172     basic_istream<_CharT, _Traits>::
00173     operator>>(unsigned short& __n)
00174     {
00175       sentry __cerb(*this, false);
00176       if (__cerb) 
00177     {
00178       try 
00179         {
00180           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00181           if (_M_check_facet(_M_fnumget))
00182         _M_fnumget->get(*this, 0, *this, __err, __n);
00183           this->setstate(__err);
00184         }
00185       catch(exception& __fail)
00186         {
00187           // 27.6.1.2.1 Common requirements.
00188           // Turn this on without causing an ios::failure to be thrown.
00189           this->setstate(ios_base::badbit);
00190           if ((this->exceptions() & ios_base::badbit) != 0)
00191         __throw_exception_again;
00192         }
00193     }
00194       return *this;
00195     }
00196 
00197   template<typename _CharT, typename _Traits>
00198     basic_istream<_CharT, _Traits>& 
00199     basic_istream<_CharT, _Traits>::
00200     operator>>(int& __n)
00201     {
00202       sentry __cerb(*this, false);
00203       if (__cerb) 
00204     {
00205       try 
00206         {
00207           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00208           long __l;
00209           if (_M_check_facet(_M_fnumget))
00210         _M_fnumget->get(*this, 0, *this, __err, __l);
00211 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00212           // 118. basic_istream uses nonexistent num_get member functions.
00213           if (!(__err & ios_base::failbit)
00214           && (numeric_limits<int>::min() <= __l 
00215               && __l <= numeric_limits<int>::max()))
00216         __n = __l;
00217           else
00218                 __err |= ios_base::failbit;
00219 #endif
00220           this->setstate(__err);
00221         }
00222       catch(exception& __fail)
00223         {
00224           // 27.6.1.2.1 Common requirements.
00225           // Turn this on without causing an ios::failure to be thrown.
00226           this->setstate(ios_base::badbit);
00227           if ((this->exceptions() & ios_base::badbit) != 0)
00228         __throw_exception_again;
00229         }
00230     }
00231       return *this;
00232     }
00233 
00234   template<typename _CharT, typename _Traits>
00235     basic_istream<_CharT, _Traits>& 
00236     basic_istream<_CharT, _Traits>::
00237     operator>>(unsigned int& __n)
00238     {
00239       sentry __cerb(*this, false);
00240       if (__cerb) 
00241     {
00242       try 
00243         {
00244           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00245           if (_M_check_facet(_M_fnumget))
00246         _M_fnumget->get(*this, 0, *this, __err, __n);
00247           this->setstate(__err);
00248         }
00249       catch(exception& __fail)
00250         {
00251           // 27.6.1.2.1 Common requirements.
00252           // Turn this on without causing an ios::failure to be thrown.
00253           this->setstate(ios_base::badbit);
00254           if ((this->exceptions() & ios_base::badbit) != 0)
00255         __throw_exception_again;
00256         }
00257     }
00258       return *this;
00259     }
00260 
00261   template<typename _CharT, typename _Traits>
00262     basic_istream<_CharT, _Traits>& 
00263     basic_istream<_CharT, _Traits>::
00264     operator>>(long& __n)
00265     {
00266       sentry __cerb(*this, false);
00267       if (__cerb) 
00268     {
00269       try 
00270         {
00271           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00272           if (_M_check_facet(_M_fnumget))
00273         _M_fnumget->get(*this, 0, *this, __err, __n);
00274           this->setstate(__err);
00275         }
00276       catch(exception& __fail)
00277         {
00278           // 27.6.1.2.1 Common requirements.
00279           // Turn this on without causing an ios::failure to be thrown.
00280           this->setstate(ios_base::badbit);
00281           if ((this->exceptions() & ios_base::badbit) != 0)
00282         __throw_exception_again;
00283         }
00284     }
00285       return *this;
00286     }
00287 
00288   template<typename _CharT, typename _Traits>
00289     basic_istream<_CharT, _Traits>& 
00290     basic_istream<_CharT, _Traits>::
00291     operator>>(unsigned long& __n)
00292     {
00293       sentry __cerb(*this, false);
00294       if (__cerb) 
00295     {
00296       try 
00297         {
00298           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00299           if (_M_check_facet(_M_fnumget))
00300         _M_fnumget->get(*this, 0, *this, __err, __n);
00301           this->setstate(__err);
00302         }
00303       catch(exception& __fail)
00304         {
00305           // 27.6.1.2.1 Common requirements.
00306           // Turn this on without causing an ios::failure to be thrown.
00307           this->setstate(ios_base::badbit);
00308           if ((this->exceptions() & ios_base::badbit) != 0)
00309         __throw_exception_again;
00310         }
00311     }
00312       return *this;
00313     }
00314 
00315 #ifdef _GLIBCPP_USE_LONG_LONG
00316   template<typename _CharT, typename _Traits>
00317     basic_istream<_CharT, _Traits>& 
00318     basic_istream<_CharT, _Traits>::
00319     operator>>(long long& __n)
00320     {
00321       sentry __cerb(*this, false);
00322       if (__cerb) 
00323     {
00324       try 
00325         {
00326           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00327           if (_M_check_facet(_M_fnumget))
00328         _M_fnumget->get(*this, 0, *this, __err, __n);
00329           this->setstate(__err);
00330         }
00331       catch(exception& __fail)
00332         {
00333           // 27.6.1.2.1 Common requirements.
00334           // Turn this on without causing an ios::failure to be thrown.
00335           this->setstate(ios_base::badbit);
00336           if ((this->exceptions() & ios_base::badbit) != 0)
00337           __throw_exception_again;
00338         }
00339     }
00340       return *this;
00341     }
00342 
00343   template<typename _CharT, typename _Traits>
00344     basic_istream<_CharT, _Traits>& 
00345     basic_istream<_CharT, _Traits>::
00346     operator>>(unsigned long long& __n)
00347     {
00348       sentry __cerb(*this, false);
00349       if (__cerb) 
00350     {
00351       try 
00352         {
00353           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00354           if (_M_check_facet(_M_fnumget))
00355         _M_fnumget->get(*this, 0, *this, __err, __n);
00356           this->setstate(__err);
00357         }
00358       catch(exception& __fail)
00359         {
00360           // 27.6.1.2.1 Common requirements.
00361           // Turn this on without causing an ios::failure to be thrown.
00362           this->setstate(ios_base::badbit);
00363           if ((this->exceptions() & ios_base::badbit) != 0)
00364         __throw_exception_again;
00365         }
00366     }
00367       return *this;
00368     }
00369 #endif
00370 
00371   template<typename _CharT, typename _Traits>
00372     basic_istream<_CharT, _Traits>& 
00373     basic_istream<_CharT, _Traits>::
00374     operator>>(float& __n)
00375     {
00376       sentry __cerb(*this, false);
00377       if (__cerb) 
00378     {
00379       try 
00380         {
00381           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00382           if (_M_check_facet(_M_fnumget))
00383         _M_fnumget->get(*this, 0, *this, __err, __n);
00384           this->setstate(__err);
00385         }
00386       catch(exception& __fail)
00387         {
00388           // 27.6.1.2.1 Common requirements.
00389           // Turn this on without causing an ios::failure to be thrown.
00390           this->setstate(ios_base::badbit);
00391           if ((this->exceptions() & ios_base::badbit) != 0)
00392         __throw_exception_again;
00393         }
00394     }
00395       return *this;
00396     }
00397 
00398   template<typename _CharT, typename _Traits>
00399     basic_istream<_CharT, _Traits>& 
00400     basic_istream<_CharT, _Traits>::
00401     operator>>(double& __n)
00402     {
00403       sentry __cerb(*this, false);
00404       if (__cerb) 
00405     {
00406       try 
00407         {
00408           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00409           if (_M_check_facet(_M_fnumget))
00410         _M_fnumget->get(*this, 0, *this, __err, __n);
00411           this->setstate(__err);
00412         }
00413       catch(exception& __fail)
00414         {
00415           // 27.6.1.2.1 Common requirements.
00416           // Turn this on without causing an ios::failure to be thrown.
00417           this->setstate(ios_base::badbit);
00418           if ((this->exceptions() & ios_base::badbit) != 0)
00419         __throw_exception_again;
00420         }
00421     }
00422       return *this;
00423     }
00424 
00425   template<typename _CharT, typename _Traits>
00426     basic_istream<_CharT, _Traits>& 
00427     basic_istream<_CharT, _Traits>::
00428     operator>>(long double& __n)
00429     {
00430       sentry __cerb(*this, false);
00431       if (__cerb) 
00432     {
00433       try 
00434         {
00435           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00436           if (_M_check_facet(_M_fnumget))
00437         _M_fnumget->get(*this, 0, *this, __err, __n);
00438           this->setstate(__err);
00439         }
00440       catch(exception& __fail)
00441         {
00442           // 27.6.1.2.1 Common requirements.
00443           // Turn this on without causing an ios::failure to be thrown.
00444           this->setstate(ios_base::badbit);
00445           if ((this->exceptions() & ios_base::badbit) != 0)
00446         __throw_exception_again;
00447         }
00448     }
00449       return *this;
00450     }
00451 
00452   template<typename _CharT, typename _Traits>
00453     basic_istream<_CharT, _Traits>& 
00454     basic_istream<_CharT, _Traits>::
00455     operator>>(void*& __n)
00456     {
00457       sentry __cerb(*this, false);
00458       if (__cerb) 
00459     {
00460       try 
00461         {
00462           ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
00463           if (_M_check_facet(_M_fnumget))
00464         _M_fnumget->get(*this, 0, *this, __err, __n);
00465           this->setstate(__err);
00466         }
00467       catch(exception& __fail)
00468         {
00469           // 27.6.1.2.1 Common requirements.
00470           // Turn this on without causing an ios::failure to be thrown.
00471           this->setstate(ios_base::badbit);
00472           if ((this->exceptions() & ios_base::badbit) != 0)
00473         __throw_exception_again;
00474         }
00475     }
00476       return *this;
00477     }
00478 
00479   template<typename _CharT, typename _Traits>
00480     basic_istream<_CharT, _Traits>& 
00481     basic_istream<_CharT, _Traits>::
00482     operator>>(__streambuf_type* __sbout)
00483     {
00484        sentry __cerb(*this, false);
00485        if (__cerb)
00486      {
00487        try
00488          {
00489            streamsize __xtrct = 0;
00490            if (__sbout)
00491          {
00492            __streambuf_type* __sbin = this->rdbuf();
00493            __xtrct = __copy_streambufs(*this, __sbin, __sbout);
00494          }
00495            if (!__sbout || !__xtrct)
00496          this->setstate(ios_base::failbit);
00497          }
00498        catch(exception& __fail)
00499          {
00500            // 27.6.2.5.1 Common requirements.
00501            // Turn this on without causing an ios::failure to be thrown.
00502            this->setstate(ios_base::badbit);
00503            if ((this->exceptions() & ios_base::badbit) != 0)
00504          __throw_exception_again;
00505          }
00506      }
00507        return *this;
00508     }
00509 
00510   template<typename _CharT, typename _Traits>
00511     typename basic_istream<_CharT, _Traits>::int_type
00512     basic_istream<_CharT, _Traits>::
00513     get(void)
00514     {
00515       const int_type __eof = traits_type::eof();
00516       int_type __c = __eof;
00517       _M_gcount = 0;
00518       sentry __cerb(*this, true);
00519       if (__cerb) 
00520     {
00521       try 
00522         {
00523           __c = this->rdbuf()->sbumpc();
00524           // 27.6.1.1 paragraph 3
00525           if (!traits_type::eq_int_type(__c, __eof))
00526         _M_gcount = 1;
00527           else
00528         this->setstate(ios_base::eofbit | ios_base::failbit);
00529         }
00530       catch(exception& __fail)
00531         {
00532           // 27.6.1.3 paragraph 1
00533           // Turn this on without causing an ios::failure to be thrown.
00534           this->setstate(ios_base::badbit);
00535           if ((this->exceptions() & ios_base::badbit) != 0)
00536         __throw_exception_again;
00537         }
00538     }
00539       return __c;
00540     }
00541 
00542   template<typename _CharT, typename _Traits>
00543     basic_istream<_CharT, _Traits>&
00544     basic_istream<_CharT, _Traits>::
00545     get(char_type& __c)
00546     {
00547       _M_gcount = 0;
00548       sentry __cerb(*this, true);
00549       if (__cerb) 
00550     {
00551       try 
00552         {
00553           const int_type __eof = traits_type::eof();
00554           int_type __bufval = this->rdbuf()->sbumpc();
00555           // 27.6.1.1 paragraph 3
00556           if (!traits_type::eq_int_type(__bufval, __eof))
00557         {
00558           _M_gcount = 1;
00559           __c = traits_type::to_char_type(__bufval);
00560         }
00561           else
00562         this->setstate(ios_base::eofbit | ios_base::failbit);
00563         }
00564       catch(exception& __fail)
00565         {
00566           // 27.6.1.3 paragraph 1
00567           // Turn this on without causing an ios::failure to be thrown.
00568           this->setstate(ios_base::badbit);
00569           if ((this->exceptions() & ios_base::badbit) != 0)
00570         __throw_exception_again;
00571         }
00572     }
00573       return *this;
00574     }
00575 
00576   template<typename _CharT, typename _Traits>
00577     basic_istream<_CharT, _Traits>&
00578     basic_istream<_CharT, _Traits>::
00579     get(char_type* __s, streamsize __n, char_type __delim)
00580     {
00581       _M_gcount = 0;
00582       sentry __cerb(*this, true);
00583       if (__cerb) 
00584     {
00585       try 
00586         {
00587           const int_type __idelim = traits_type::to_int_type(__delim);
00588           const int_type __eof = traits_type::eof();
00589           __streambuf_type* __sb = this->rdbuf();
00590           int_type __c = __sb->sgetc(); 
00591           
00592           while (_M_gcount + 1 < __n 
00593              && !traits_type::eq_int_type(__c, __eof)
00594              && !traits_type::eq_int_type(__c, __idelim))
00595         {
00596           *__s++ = traits_type::to_char_type(__c);
00597           __c = __sb->snextc();
00598           ++_M_gcount;
00599         }
00600           if (traits_type::eq_int_type(__c, __eof))
00601         this->setstate(ios_base::eofbit);
00602         }
00603       catch(exception& __fail)
00604         {
00605           // 27.6.1.3 paragraph 1
00606           // Turn this on without causing an ios::failure to be thrown.
00607           this->setstate(ios_base::badbit);
00608           if ((this->exceptions() & ios_base::badbit) != 0)
00609         __throw_exception_again;
00610         }
00611     }
00612       *__s = char_type();
00613       if (!_M_gcount)
00614     this->setstate(ios_base::failbit);
00615       return *this;
00616     }
00617 
00618   template<typename _CharT, typename _Traits>
00619     basic_istream<_CharT, _Traits>&
00620     basic_istream<_CharT, _Traits>::
00621     get(__streambuf_type& __sb, char_type __delim)
00622     {
00623       _M_gcount = 0;
00624       sentry __cerb(*this, true);
00625       if (__cerb) 
00626     {
00627       try 
00628         {
00629           const int_type __idelim = traits_type::to_int_type(__delim);
00630           const int_type __eof = traits_type::eof();          
00631           __streambuf_type* __this_sb = this->rdbuf();
00632           int_type __c = __this_sb->sgetc();
00633           char_type __c2 = traits_type::to_char_type(__c);
00634           
00635           while (!traits_type::eq_int_type(__c, __eof) 
00636              && !traits_type::eq_int_type(__c, __idelim) 
00637              && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
00638         {
00639           ++_M_gcount;
00640           __c = __this_sb->snextc();
00641           __c2 = traits_type::to_char_type(__c);
00642         }
00643           if (traits_type::eq_int_type(__c, __eof))
00644         this->setstate(ios_base::eofbit);
00645         }
00646       catch(exception& __fail)
00647         {
00648           // 27.6.1.3 paragraph 1
00649           // Turn this on without causing an ios::failure to be thrown.
00650           this->setstate(ios_base::badbit);
00651           if ((this->exceptions() & ios_base::badbit) != 0)
00652         __throw_exception_again;
00653         }
00654     }
00655       if (!_M_gcount)
00656     this->setstate(ios_base::failbit);
00657       return *this;
00658     }
00659 
00660   template<typename _CharT, typename _Traits>
00661     basic_istream<_CharT, _Traits>&
00662     basic_istream<_CharT, _Traits>::
00663     getline(char_type* __s, streamsize __n, char_type __delim)
00664     {
00665       _M_gcount = 0;
00666       sentry __cerb(*this, true);
00667       if (__cerb) 
00668     {
00669           try 
00670         {
00671           const int_type __idelim = traits_type::to_int_type(__delim);
00672           const int_type __eof = traits_type::eof();
00673           __streambuf_type* __sb = this->rdbuf();
00674           int_type __c = __sb->sgetc();
00675         
00676           while (_M_gcount + 1 < __n 
00677              && !traits_type::eq_int_type(__c, __eof)
00678              && !traits_type::eq_int_type(__c, __idelim))
00679         {
00680           *__s++ = traits_type::to_char_type(__c);
00681           __c = __sb->snextc();
00682           ++_M_gcount;
00683         }
00684           if (traits_type::eq_int_type(__c, __eof))
00685         this->setstate(ios_base::eofbit);
00686           else
00687         {
00688           if (traits_type::eq_int_type(__c, __idelim))
00689             {
00690               __sb->sbumpc();
00691               ++_M_gcount;
00692             }
00693           else
00694             this->setstate(ios_base::failbit);
00695         }
00696         }
00697       catch(exception& __fail)
00698         {
00699           // 27.6.1.3 paragraph 1
00700           // Turn this on without causing an ios::failure to be thrown.
00701           this->setstate(ios_base::badbit);
00702           if ((this->exceptions() & ios_base::badbit) != 0)
00703         __throw_exception_again;
00704         }
00705     }
00706       *__s = char_type();
00707       if (!_M_gcount)
00708     this->setstate(ios_base::failbit);
00709       return *this;
00710     }
00711   
00712   template<typename _CharT, typename _Traits>
00713     basic_istream<_CharT, _Traits>&
00714     basic_istream<_CharT, _Traits>::
00715     ignore(streamsize __n, int_type __delim)
00716     {
00717       _M_gcount = 0;
00718       sentry __cerb(*this, true);
00719       if (__cerb && __n > 0) 
00720     {
00721       try 
00722         {
00723           const int_type __eof = traits_type::eof();
00724           __streambuf_type* __sb = this->rdbuf();
00725           int_type __c;
00726           
00727           __n = min(__n, numeric_limits<streamsize>::max());
00728           while (_M_gcount < __n  
00729              && !traits_type::eq_int_type(__c = __sb->sbumpc(), __eof))
00730         {
00731           ++_M_gcount;
00732           if (traits_type::eq_int_type(__c, __delim))
00733             break;
00734         }
00735           if (traits_type::eq_int_type(__c, __eof))
00736         this->setstate(ios_base::eofbit);
00737         }
00738       catch(exception& __fail)
00739         {
00740           // 27.6.1.3 paragraph 1
00741           // Turn this on without causing an ios::failure to be thrown.
00742           this->setstate(ios_base::badbit);
00743           if ((this->exceptions() & ios_base::badbit) != 0)
00744         __throw_exception_again;
00745         }
00746     }
00747       return *this;
00748     }
00749   
00750   template<typename _CharT, typename _Traits>
00751     typename basic_istream<_CharT, _Traits>::int_type
00752     basic_istream<_CharT, _Traits>::
00753     peek(void)
00754     {
00755       int_type __c = traits_type::eof();
00756       _M_gcount = 0;
00757       sentry __cerb(*this, true);
00758       if (__cerb)
00759     {
00760       try 
00761         { __c = this->rdbuf()->sgetc(); }
00762       catch(exception& __fail)
00763         {
00764           // 27.6.1.3 paragraph 1
00765           // Turn this on without causing an ios::failure to be thrown.
00766           this->setstate(ios_base::badbit);
00767           if ((this->exceptions() & ios_base::badbit) != 0)
00768         __throw_exception_again;
00769         }
00770     } 
00771       return __c;
00772     }
00773 
00774   template<typename _CharT, typename _Traits>
00775     basic_istream<_CharT, _Traits>&
00776     basic_istream<_CharT, _Traits>::
00777     read(char_type* __s, streamsize __n)
00778     {
00779       _M_gcount = 0;
00780       sentry __cerb(*this, true);
00781       if (__cerb) 
00782     {
00783       try 
00784         {
00785           _M_gcount = this->rdbuf()->sgetn(__s, __n);
00786           if (_M_gcount != __n)
00787         this->setstate(ios_base::eofbit | ios_base::failbit);
00788         }       
00789       catch(exception& __fail)
00790         {
00791           // 27.6.1.3 paragraph 1
00792           // Turn this on without causing an ios::failure to be thrown.
00793           this->setstate(ios_base::badbit);
00794           if ((this->exceptions() & ios_base::badbit) != 0)
00795         __throw_exception_again;
00796         }
00797     }
00798       else
00799     this->setstate(ios_base::failbit);
00800       return *this;
00801     }
00802   
00803   template<typename _CharT, typename _Traits>
00804     streamsize 
00805     basic_istream<_CharT, _Traits>::
00806     readsome(char_type* __s, streamsize __n)
00807     {
00808       _M_gcount = 0;
00809       sentry __cerb(*this, true);
00810       if (__cerb) 
00811     {
00812       try 
00813         {
00814           // Cannot compare int_type with streamsize generically.
00815           streamsize __num = this->rdbuf()->in_avail();
00816           if (__num >= 0)
00817         {
00818           __num = min(__num, __n);
00819           if (__num)
00820             _M_gcount = this->rdbuf()->sgetn(__s, __num);
00821         }
00822           else
00823         this->setstate(ios_base::eofbit);           
00824         }
00825       catch(exception& __fail)
00826         {
00827           // 27.6.1.3 paragraph 1
00828           // Turn this on without causing an ios::failure to be thrown.
00829           this->setstate(ios_base::badbit);
00830           if ((this->exceptions() & ios_base::badbit) != 0)
00831         __throw_exception_again;
00832         }
00833     }
00834       else
00835     this->setstate(ios_base::failbit);
00836       return _M_gcount;
00837     }
00838       
00839   template<typename _CharT, typename _Traits>
00840     basic_istream<_CharT, _Traits>&
00841     basic_istream<_CharT, _Traits>::
00842     putback(char_type __c)
00843     {
00844       _M_gcount = 0;
00845       sentry __cerb(*this, true);
00846       if (__cerb) 
00847     {
00848       try 
00849         {
00850           const int_type __eof = traits_type::eof();
00851           __streambuf_type* __sb = this->rdbuf();
00852           if (!__sb 
00853           || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
00854         this->setstate(ios_base::badbit);           
00855         }
00856       catch(exception& __fail)
00857         {
00858           // 27.6.1.3 paragraph 1
00859           // Turn this on without causing an ios::failure to be thrown.
00860           this->setstate(ios_base::badbit);
00861           if ((this->exceptions() & ios_base::badbit) != 0)
00862         __throw_exception_again;
00863         }
00864     }
00865       else
00866     this->setstate(ios_base::failbit);
00867       return *this;
00868     }
00869   
00870   template<typename _CharT, typename _Traits>
00871     basic_istream<_CharT, _Traits>&
00872     basic_istream<_CharT, _Traits>::
00873     unget(void)
00874     {
00875       _M_gcount = 0;
00876       sentry __cerb(*this, true);
00877       if (__cerb) 
00878     {
00879       try 
00880         {
00881           const int_type __eof = traits_type::eof();
00882           __streambuf_type* __sb = this->rdbuf();
00883           if (!__sb 
00884           || traits_type::eq_int_type(__sb->sungetc(), __eof))
00885         this->setstate(ios_base::badbit);           
00886         }
00887       catch(exception& __fail)
00888         {
00889           // 27.6.1.3 paragraph 1
00890           // Turn this on without causing an ios::failure to be thrown.
00891           this->setstate(ios_base::badbit);
00892           if ((this->exceptions() & ios_base::badbit) != 0)
00893         __throw_exception_again;
00894         }
00895     }
00896       else
00897     this->setstate(ios_base::failbit);
00898       return *this;
00899     }
00900   
00901   template<typename _CharT, typename _Traits>
00902     int
00903     basic_istream<_CharT, _Traits>::
00904     sync(void)
00905     {
00906       int __ret = -1;
00907       sentry __cerb(*this, true);
00908       if (__cerb) 
00909     {
00910       try 
00911         {
00912           __streambuf_type* __sb = this->rdbuf();
00913           if (__sb)
00914         {
00915           if (__sb->pubsync() == -1)
00916             this->setstate(ios_base::badbit);           
00917           else 
00918             __ret = 0;
00919         }
00920         }
00921       catch(exception& __fail)
00922         {
00923           // 27.6.1.3 paragraph 1
00924           // Turn this on without causing an ios::failure to be thrown.
00925           this->setstate(ios_base::badbit);
00926           if ((this->exceptions() & ios_base::badbit) != 0)
00927         __throw_exception_again;
00928         }
00929     }
00930       return __ret;
00931     }
00932   
00933   template<typename _CharT, typename _Traits>
00934     typename basic_istream<_CharT, _Traits>::pos_type
00935     basic_istream<_CharT, _Traits>::
00936     tellg(void)
00937     {
00938       pos_type __ret = pos_type(-1);
00939       if (!this->fail())
00940     __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
00941       return __ret;
00942     }
00943 
00944 
00945   template<typename _CharT, typename _Traits>
00946     basic_istream<_CharT, _Traits>&
00947     basic_istream<_CharT, _Traits>::
00948     seekg(pos_type __pos)
00949     {
00950       if (!this->fail())
00951     {
00952 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00953 // 136.  seekp, seekg setting wrong streams?
00954       pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::in);
00955 
00956 // 129. Need error indication from seekp() and seekg()
00957       if (__err == pos_type(off_type(-1)))
00958         this->setstate(ios_base::failbit);
00959 #endif
00960     }
00961       return *this;
00962     }
00963 
00964   template<typename _CharT, typename _Traits>
00965     basic_istream<_CharT, _Traits>&
00966     basic_istream<_CharT, _Traits>::
00967     seekg(off_type __off, ios_base::seekdir __dir)
00968     {
00969       if (!this->fail())
00970     {
00971 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00972 // 136.  seekp, seekg setting wrong streams?
00973       pos_type __err = this->rdbuf()->pubseekoff(__off, __dir, 
00974                              ios_base::in);
00975 
00976 // 129. Need error indication from seekp() and seekg()
00977       if (__err == pos_type(off_type(-1)))
00978         this->setstate(ios_base::failbit);
00979 #endif
00980     }
00981       return *this;
00982     }
00983 
00984   // 27.6.1.2.3 Character extraction templates
00985   template<typename _CharT, typename _Traits>
00986     basic_istream<_CharT, _Traits>&
00987     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
00988     {
00989       typedef basic_istream<_CharT, _Traits>        __istream_type;
00990       typename __istream_type::sentry __cerb(__in, false);
00991       if (__cerb)
00992     {
00993       try 
00994         { __in.get(__c); }
00995       catch(exception& __fail)
00996         {
00997           // 27.6.1.2.1 Common requirements.
00998           // Turn this on without causing an ios::failure to be thrown.
00999           __in.setstate(ios_base::badbit);
01000           if ((__in.exceptions() & ios_base::badbit) != 0)
01001         __throw_exception_again;
01002         }
01003     }
01004       else
01005     __in.setstate(ios_base::failbit);
01006       return __in;
01007     }
01008 
01009   template<typename _CharT, typename _Traits>
01010     basic_istream<_CharT, _Traits>&
01011     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
01012     {
01013       typedef basic_istream<_CharT, _Traits>        __istream_type;
01014       typedef typename __istream_type::__streambuf_type __streambuf_type;
01015       typedef typename _Traits::int_type        int_type;
01016       typedef _CharT                            char_type;
01017       typedef ctype<_CharT>                 __ctype_type;
01018       streamsize __extracted = 0;
01019 
01020       typename __istream_type::sentry __cerb(__in, false);
01021       if (__cerb)
01022     {
01023       try 
01024         {
01025           // Figure out how many characters to extract.
01026           streamsize __num = __in.width();
01027           if (__num == 0)
01028         __num = numeric_limits<streamsize>::max();
01029           
01030           const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
01031           const int_type __eof = _Traits::eof();
01032           __streambuf_type* __sb = __in.rdbuf();
01033           int_type __c = __sb->sgetc();
01034           
01035           while (__extracted < __num - 1 
01036              && !_Traits::eq_int_type(__c, __eof)
01037              && !__ctype.is(ctype_base::space, __c))
01038         {
01039           *__s++ = __c;
01040           ++__extracted;
01041           __c = __sb->snextc();
01042         }
01043           if (_Traits::eq_int_type(__c, __eof))
01044         __in.setstate(ios_base::eofbit);
01045 
01046 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
01047 //68.  Extractors for char* should store null at end
01048           *__s = char_type();
01049 #endif
01050           __in.width(0);
01051         }
01052       catch(exception& __fail)
01053         {
01054           // 27.6.1.2.1 Common requirements.
01055           // Turn this on without causing an ios::failure to be thrown.
01056           __in.setstate(ios_base::badbit);
01057           if ((__in.exceptions() & ios_base::badbit) != 0)
01058         __throw_exception_again;
01059         }
01060     }
01061       if (!__extracted)
01062     __in.setstate(ios_base::failbit);
01063       return __in;
01064     }
01065 
01066   // 27.6.1.4 Standard basic_istream manipulators
01067   template<typename _CharT, typename _Traits>
01068     basic_istream<_CharT,_Traits>& 
01069     ws(basic_istream<_CharT,_Traits>& __in)
01070     {
01071       typedef basic_istream<_CharT, _Traits>        __istream_type;
01072       typedef typename __istream_type::__streambuf_type __streambuf_type;
01073       typedef typename __istream_type::__ctype_type     __ctype_type;
01074       typedef typename __istream_type::int_type     __int_type;
01075 
01076       const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
01077       const __int_type __eof = _Traits::eof();        
01078       __streambuf_type* __sb = __in.rdbuf();
01079       __int_type __c = __sb->sgetc();
01080 
01081       while (!_Traits::eq_int_type(__c, __eof) 
01082          && __ctype.is(ctype_base::space, __c))
01083     __c = __sb->snextc();
01084 
01085        if (_Traits::eq_int_type(__c, __eof))
01086     __in.setstate(ios_base::eofbit);
01087 
01088       return __in;
01089     }
01090 
01091   // 21.3.7.9 basic_string::getline and operators
01092   template<typename _CharT, typename _Traits, typename _Alloc>
01093     basic_istream<_CharT, _Traits>&
01094     operator>>(basic_istream<_CharT, _Traits>& __in,
01095            basic_string<_CharT, _Traits, _Alloc>& __str)
01096     {
01097       typedef basic_istream<_CharT, _Traits>        __istream_type;
01098       typedef typename __istream_type::int_type     __int_type;
01099       typedef typename __istream_type::__streambuf_type __streambuf_type;
01100       typedef typename __istream_type::__ctype_type     __ctype_type;
01101       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
01102       typedef typename __string_type::size_type     __size_type;
01103       __size_type __extracted = 0;
01104 
01105       typename __istream_type::sentry __cerb(__in, false);
01106       if (__cerb) 
01107     {
01108       __str.erase();
01109       streamsize __w = __in.width();
01110       __size_type __n;
01111       __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size();
01112 
01113       const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
01114       const __int_type __eof = _Traits::eof();
01115       __streambuf_type* __sb = __in.rdbuf();
01116       __int_type __c = __sb->sgetc();
01117       
01118       while (__extracted < __n 
01119          && !_Traits::eq_int_type(__c, __eof)
01120          && !__ctype.is(ctype_base::space, __c))
01121         {
01122           __str += _Traits::to_char_type(__c);
01123           ++__extracted;
01124           __c = __sb->snextc();
01125         }
01126       if (_Traits::eq_int_type(__c, __eof))
01127         __in.setstate(ios_base::eofbit);
01128       __in.width(0);
01129     }
01130 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
01131 //211.  operator>>(istream&, string&) doesn't set failbit
01132       if (!__extracted)
01133     __in.setstate (ios_base::failbit);
01134 #endif
01135       return __in;
01136     }
01137 
01138   template<typename _CharT, typename _Traits, typename _Alloc>
01139     basic_istream<_CharT, _Traits>&
01140     getline(basic_istream<_CharT, _Traits>& __in,
01141         basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
01142     {
01143       typedef basic_istream<_CharT, _Traits>        __istream_type;
01144       typedef typename __istream_type::int_type     __int_type;
01145       typedef typename __istream_type::__streambuf_type __streambuf_type;
01146       typedef typename __istream_type::__ctype_type     __ctype_type;
01147       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
01148       typedef typename __string_type::size_type     __size_type;
01149 
01150       __size_type __extracted = 0;
01151       bool __testdelim = false;
01152       typename __istream_type::sentry __cerb(__in, true);
01153       if (__cerb) 
01154     {
01155       __str.erase();
01156       __size_type __n = __str.max_size();
01157 
01158       __int_type __idelim = _Traits::to_int_type(__delim);
01159       __streambuf_type* __sb = __in.rdbuf();
01160       __int_type __c = __sb->sbumpc();
01161       const __int_type __eof = _Traits::eof();
01162       __testdelim = _Traits::eq_int_type(__c, __idelim);
01163 
01164       while (__extracted <= __n 
01165          && !_Traits::eq_int_type(__c, __eof)
01166          && !__testdelim)
01167         {
01168           __str += _Traits::to_char_type(__c);
01169           ++__extracted;
01170           __c = __sb->sbumpc();
01171           __testdelim = _Traits::eq_int_type(__c, __idelim);
01172         }
01173       if (_Traits::eq_int_type(__c, __eof))
01174         __in.setstate(ios_base::eofbit);
01175     }
01176       if (!__extracted && !__testdelim)
01177     __in.setstate(ios_base::failbit);
01178       return __in;
01179     }
01180 
01181   template<class _CharT, class _Traits, class _Alloc>
01182     inline basic_istream<_CharT,_Traits>&
01183     getline(basic_istream<_CharT, _Traits>& __in, 
01184         basic_string<_CharT,_Traits,_Alloc>& __str)
01185     { return getline(__in, __str, __in.widen('\n')); }
01186 
01187   // Inhibit implicit instantiations for required instantiations,
01188   // which are defined via explicit instantiations elsewhere.  
01189   // NB:  This syntax is a GNU extension.
01190   extern template class basic_istream<char>;
01191   extern template istream& ws(istream&);
01192   extern template istream& operator>>(istream&, char&);
01193   extern template istream& operator>>(istream&, char*);
01194   extern template istream& operator>>(istream&, unsigned char&);
01195   extern template istream& operator>>(istream&, signed char&);
01196   extern template istream& operator>>(istream&, unsigned char*);
01197   extern template istream& operator>>(istream&, signed char*);
01198 
01199 #ifdef _GLIBCPP_USE_WCHAR_T
01200   extern template class basic_istream<wchar_t>;
01201   extern template wistream& ws(wistream&);
01202   extern template wistream& operator>>(wistream&, wchar_t&);
01203   extern template wistream& operator>>(wistream&, wchar_t*);
01204 #endif
01205 } // namespace std

Generated on Thu Nov 21 03:12:48 2002 for libstdc++-v3 Source by doxygen1.2.18-20021030