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       sentry __cerb(*this, true);
00845       if (__cerb) 
00846     {
00847       try 
00848         {
00849           const int_type __eof = traits_type::eof();
00850           __streambuf_type* __sb = this->rdbuf();
00851           if (!__sb 
00852           || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
00853         this->setstate(ios_base::badbit);           
00854         }
00855       catch(exception& __fail)
00856         {
00857           // 27.6.1.3 paragraph 1
00858           // Turn this on without causing an ios::failure to be thrown.
00859           this->setstate(ios_base::badbit);
00860           if ((this->exceptions() & ios_base::badbit) != 0)
00861         __throw_exception_again;
00862         }
00863     }
00864       else
00865     this->setstate(ios_base::failbit);
00866       return *this;
00867     }
00868   
00869   template<typename _CharT, typename _Traits>
00870     basic_istream<_CharT, _Traits>&
00871     basic_istream<_CharT, _Traits>::
00872     unget(void)
00873     {
00874       _M_gcount = 0;
00875       sentry __cerb(*this, true);
00876       if (__cerb) 
00877     {
00878       try 
00879         {
00880           const int_type __eof = traits_type::eof();
00881           __streambuf_type* __sb = this->rdbuf();
00882           if (!__sb 
00883           || traits_type::eq_int_type(__sb->sungetc(), __eof))
00884         this->setstate(ios_base::badbit);           
00885         }
00886       catch(exception& __fail)
00887         {
00888           // 27.6.1.3 paragraph 1
00889           // Turn this on without causing an ios::failure to be thrown.
00890           this->setstate(ios_base::badbit);
00891           if ((this->exceptions() & ios_base::badbit) != 0)
00892         __throw_exception_again;
00893         }
00894     }
00895       else
00896     this->setstate(ios_base::failbit);
00897       return *this;
00898     }
00899   
00900   template<typename _CharT, typename _Traits>
00901     int
00902     basic_istream<_CharT, _Traits>::
00903     sync(void)
00904     {
00905       int __ret = -1;
00906       _M_gcount = 0;
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       _M_gcount = 0;
00951       if (!this->fail())
00952     {
00953 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00954 // 136.  seekp, seekg setting wrong streams?
00955       pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::in);
00956 
00957 // 129. Need error indication from seekp() and seekg()
00958       if (__err == pos_type(off_type(-1)))
00959         this->setstate(ios_base::failbit);
00960 #endif
00961     }
00962       return *this;
00963     }
00964 
00965   template<typename _CharT, typename _Traits>
00966     basic_istream<_CharT, _Traits>&
00967     basic_istream<_CharT, _Traits>::
00968     seekg(off_type __off, ios_base::seekdir __dir)
00969     {
00970       _M_gcount = 0;
00971       if (!this->fail())
00972     {
00973 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00974 // 136.  seekp, seekg setting wrong streams?
00975       pos_type __err = this->rdbuf()->pubseekoff(__off, __dir, 
00976                              ios_base::in);
00977 
00978 // 129. Need error indication from seekp() and seekg()
00979       if (__err == pos_type(off_type(-1)))
00980         this->setstate(ios_base::failbit);
00981 #endif
00982     }
00983       return *this;
00984     }
00985 
00986   // 27.6.1.2.3 Character extraction templates
00987   template<typename _CharT, typename _Traits>
00988     basic_istream<_CharT, _Traits>&
00989     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
00990     {
00991       typedef basic_istream<_CharT, _Traits>        __istream_type;
00992       typename __istream_type::sentry __cerb(__in, false);
00993       if (__cerb)
00994     {
00995       try 
00996         { __in.get(__c); }
00997       catch(exception& __fail)
00998         {
00999           // 27.6.1.2.1 Common requirements.
01000           // Turn this on without causing an ios::failure to be thrown.
01001           __in.setstate(ios_base::badbit);
01002           if ((__in.exceptions() & ios_base::badbit) != 0)
01003         __throw_exception_again;
01004         }
01005     }
01006       else
01007     __in.setstate(ios_base::failbit);
01008       return __in;
01009     }
01010 
01011   template<typename _CharT, typename _Traits>
01012     basic_istream<_CharT, _Traits>&
01013     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
01014     {
01015       typedef basic_istream<_CharT, _Traits>        __istream_type;
01016       typedef typename __istream_type::__streambuf_type __streambuf_type;
01017       typedef typename _Traits::int_type        int_type;
01018       typedef _CharT                            char_type;
01019       typedef ctype<_CharT>                 __ctype_type;
01020       streamsize __extracted = 0;
01021 
01022       typename __istream_type::sentry __cerb(__in, false);
01023       if (__cerb)
01024     {
01025       try 
01026         {
01027           // Figure out how many characters to extract.
01028           streamsize __num = __in.width();
01029           if (__num == 0)
01030         __num = numeric_limits<streamsize>::max();
01031           
01032           const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
01033           const int_type __eof = _Traits::eof();
01034           __streambuf_type* __sb = __in.rdbuf();
01035           int_type __c = __sb->sgetc();
01036           
01037           while (__extracted < __num - 1 
01038              && !_Traits::eq_int_type(__c, __eof)
01039              && !__ctype.is(ctype_base::space, __c))
01040         {
01041           *__s++ = __c;
01042           ++__extracted;
01043           __c = __sb->snextc();
01044         }
01045           if (_Traits::eq_int_type(__c, __eof))
01046         __in.setstate(ios_base::eofbit);
01047 
01048 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
01049 //68.  Extractors for char* should store null at end
01050           *__s = char_type();
01051 #endif
01052           __in.width(0);
01053         }
01054       catch(exception& __fail)
01055         {
01056           // 27.6.1.2.1 Common requirements.
01057           // Turn this on without causing an ios::failure to be thrown.
01058           __in.setstate(ios_base::badbit);
01059           if ((__in.exceptions() & ios_base::badbit) != 0)
01060         __throw_exception_again;
01061         }
01062     }
01063       if (!__extracted)
01064     __in.setstate(ios_base::failbit);
01065       return __in;
01066     }
01067 
01068   // 27.6.1.4 Standard basic_istream manipulators
01069   template<typename _CharT, typename _Traits>
01070     basic_istream<_CharT,_Traits>& 
01071     ws(basic_istream<_CharT,_Traits>& __in)
01072     {
01073       typedef basic_istream<_CharT, _Traits>        __istream_type;
01074       typedef typename __istream_type::__streambuf_type __streambuf_type;
01075       typedef typename __istream_type::__ctype_type     __ctype_type;
01076       typedef typename __istream_type::int_type     __int_type;
01077 
01078       const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
01079       const __int_type __eof = _Traits::eof();        
01080       __streambuf_type* __sb = __in.rdbuf();
01081       __int_type __c = __sb->sgetc();
01082 
01083       while (!_Traits::eq_int_type(__c, __eof) 
01084          && __ctype.is(ctype_base::space, __c))
01085     __c = __sb->snextc();
01086 
01087        if (_Traits::eq_int_type(__c, __eof))
01088     __in.setstate(ios_base::eofbit);
01089 
01090       return __in;
01091     }
01092 
01093   // 21.3.7.9 basic_string::getline and operators
01094   template<typename _CharT, typename _Traits, typename _Alloc>
01095     basic_istream<_CharT, _Traits>&
01096     operator>>(basic_istream<_CharT, _Traits>& __in,
01097            basic_string<_CharT, _Traits, _Alloc>& __str)
01098     {
01099       typedef basic_istream<_CharT, _Traits>        __istream_type;
01100       typedef typename __istream_type::int_type     __int_type;
01101       typedef typename __istream_type::__streambuf_type __streambuf_type;
01102       typedef typename __istream_type::__ctype_type     __ctype_type;
01103       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
01104       typedef typename __string_type::size_type     __size_type;
01105       __size_type __extracted = 0;
01106 
01107       typename __istream_type::sentry __cerb(__in, false);
01108       if (__cerb) 
01109     {
01110       __str.erase();
01111       streamsize __w = __in.width();
01112       __size_type __n;
01113       __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size();
01114 
01115       const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
01116       const __int_type __eof = _Traits::eof();
01117       __streambuf_type* __sb = __in.rdbuf();
01118       __int_type __c = __sb->sgetc();
01119       
01120       while (__extracted < __n 
01121          && !_Traits::eq_int_type(__c, __eof)
01122          && !__ctype.is(ctype_base::space, __c))
01123         {
01124           __str += _Traits::to_char_type(__c);
01125           ++__extracted;
01126           __c = __sb->snextc();
01127         }
01128       if (_Traits::eq_int_type(__c, __eof))
01129         __in.setstate(ios_base::eofbit);
01130       __in.width(0);
01131     }
01132 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
01133 //211.  operator>>(istream&, string&) doesn't set failbit
01134       if (!__extracted)
01135     __in.setstate (ios_base::failbit);
01136 #endif
01137       return __in;
01138     }
01139 
01140   template<typename _CharT, typename _Traits, typename _Alloc>
01141     basic_istream<_CharT, _Traits>&
01142     getline(basic_istream<_CharT, _Traits>& __in,
01143         basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
01144     {
01145       typedef basic_istream<_CharT, _Traits>        __istream_type;
01146       typedef typename __istream_type::int_type     __int_type;
01147       typedef typename __istream_type::__streambuf_type __streambuf_type;
01148       typedef typename __istream_type::__ctype_type     __ctype_type;
01149       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
01150       typedef typename __string_type::size_type     __size_type;
01151 
01152       __size_type __extracted = 0;
01153       bool __testdelim = false;
01154       typename __istream_type::sentry __cerb(__in, true);
01155       if (__cerb) 
01156     {
01157       __str.erase();
01158       __size_type __n = __str.max_size();
01159 
01160       __int_type __idelim = _Traits::to_int_type(__delim);
01161       __streambuf_type* __sb = __in.rdbuf();
01162       __int_type __c = __sb->sbumpc();
01163       const __int_type __eof = _Traits::eof();
01164       __testdelim = _Traits::eq_int_type(__c, __idelim);
01165 
01166       while (__extracted <= __n 
01167          && !_Traits::eq_int_type(__c, __eof)
01168          && !__testdelim)
01169         {
01170           __str += _Traits::to_char_type(__c);
01171           ++__extracted;
01172           __c = __sb->sbumpc();
01173           __testdelim = _Traits::eq_int_type(__c, __idelim);
01174         }
01175       if (_Traits::eq_int_type(__c, __eof))
01176         __in.setstate(ios_base::eofbit);
01177     }
01178       if (!__extracted && !__testdelim)
01179     __in.setstate(ios_base::failbit);
01180       return __in;
01181     }
01182 
01183   template<class _CharT, class _Traits, class _Alloc>
01184     inline basic_istream<_CharT,_Traits>&
01185     getline(basic_istream<_CharT, _Traits>& __in, 
01186         basic_string<_CharT,_Traits,_Alloc>& __str)
01187     { return getline(__in, __str, __in.widen('\n')); }
01188 
01189   // Inhibit implicit instantiations for required instantiations,
01190   // which are defined via explicit instantiations elsewhere.  
01191   // NB:  This syntax is a GNU extension.
01192   extern template class basic_istream<char>;
01193   extern template istream& ws(istream&);
01194   extern template istream& operator>>(istream&, char&);
01195   extern template istream& operator>>(istream&, char*);
01196   extern template istream& operator>>(istream&, unsigned char&);
01197   extern template istream& operator>>(istream&, signed char&);
01198   extern template istream& operator>>(istream&, unsigned char*);
01199   extern template istream& operator>>(istream&, signed char*);
01200 
01201 #ifdef _GLIBCPP_USE_WCHAR_T
01202   extern template class basic_istream<wchar_t>;
01203   extern template wistream& ws(wistream&);
01204   extern template wistream& operator>>(wistream&, wchar_t&);
01205   extern template wistream& operator>>(wistream&, wchar_t*);
01206 #endif
01207 } // namespace std

Generated on Tue Dec 23 12:34:01 2003 for libstdc++-v3 Source by doxygen 1.3.4