# istream.tcc

    00001 // istream classes -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
    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(...)
    00122         {
    00123           // 27.6.1.2.1 Common requirements.
    00124           // Turn this on without causing an ios::failure to be thrown.
    00125           this->_M_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(...)
    00159         {
    00160           // 27.6.1.2.1 Common requirements.
    00161           // Turn this on without causing an ios::failure to be thrown.
    00162           this->_M_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(...)
    00186         {
    00187           // 27.6.1.2.1 Common requirements.
    00188           // Turn this on without causing an ios::failure to be thrown.
    00189           this->_M_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(...)
    00223         {
    00224           // 27.6.1.2.1 Common requirements.
    00225           // Turn this on without causing an ios::failure to be thrown.
    00226           this->_M_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(...)
    00250         {
    00251           // 27.6.1.2.1 Common requirements.
    00252           // Turn this on without causing an ios::failure to be thrown.
    00253           this->_M_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(...)
    00277         {
    00278           // 27.6.1.2.1 Common requirements.
    00279           // Turn this on without causing an ios::failure to be thrown.
    00280           this->_M_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(...)
    00304         {
    00305           // 27.6.1.2.1 Common requirements.
    00306           // Turn this on without causing an ios::failure to be thrown.
    00307           this->_M_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(...)
    00332         {
    00333           // 27.6.1.2.1 Common requirements.
    00334           // Turn this on without causing an ios::failure to be thrown.
    00335           this->_M_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(...)
    00359         {
    00360           // 27.6.1.2.1 Common requirements.
    00361           // Turn this on without causing an ios::failure to be thrown.
    00362           this->_M_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(...)
    00387         {
    00388           // 27.6.1.2.1 Common requirements.
    00389           // Turn this on without causing an ios::failure to be thrown.
    00390           this->_M_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(...)
    00414         {
    00415           // 27.6.1.2.1 Common requirements.
    00416           // Turn this on without causing an ios::failure to be thrown.
    00417           this->_M_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(...)
    00441         {
    00442           // 27.6.1.2.1 Common requirements.
    00443           // Turn this on without causing an ios::failure to be thrown.
    00444           this->_M_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(...)
    00468         {
    00469           // 27.6.1.2.1 Common requirements.
    00470           // Turn this on without causing an ios::failure to be thrown.
    00471           this->_M_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(...)
    00499          {
    00500            // 27.6.2.5.1 Common requirements.
    00501            // Turn this on without causing an ios::failure to be thrown.
    00502            this->_M_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(...)
    00531         {
    00532           // 27.6.1.3 paragraph 1
    00533           // Turn this on without causing an ios::failure to be thrown.
    00534           this->_M_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(...)
    00565         {
    00566           // 27.6.1.3 paragraph 1
    00567           // Turn this on without causing an ios::failure to be thrown.
    00568           this->_M_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(...)
    00604         {
    00605           // 27.6.1.3 paragraph 1
    00606           // Turn this on without causing an ios::failure to be thrown.
    00607           this->_M_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(...)
    00647         {
    00648           // 27.6.1.3 paragraph 1
    00649           // Turn this on without causing an ios::failure to be thrown.
    00650           this->_M_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(...)
    00698         {
    00699           // 27.6.1.3 paragraph 1
    00700           // Turn this on without causing an ios::failure to be thrown.
    00701           this->_M_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(...)
    00739         {
    00740           // 27.6.1.3 paragraph 1
    00741           // Turn this on without causing an ios::failure to be thrown.
    00742           this->_M_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(...)
    00763         {
    00764           // 27.6.1.3 paragraph 1
    00765           // Turn this on without causing an ios::failure to be thrown.
    00766           this->_M_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(...)
    00790         {
    00791           // 27.6.1.3 paragraph 1
    00792           // Turn this on without causing an ios::failure to be thrown.
    00793           this->_M_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(...)
    00826         {
    00827           // 27.6.1.3 paragraph 1
    00828           // Turn this on without causing an ios::failure to be thrown.
    00829           this->_M_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 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00845 // 60. What is a formatted input function?
    00846       _M_gcount = 0;
    00847 #endif
    00848       sentry __cerb(*this, true);
    00849       if (__cerb)
    00850     {
    00851       try
    00852         {
    00853           const int_type __eof = traits_type::eof();
    00854           __streambuf_type* __sb = this->rdbuf();
    00855           if (!__sb
    00856           || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
    00857         this->setstate(ios_base::badbit);
    00858         }
    00859       catch(...)
    00860         {
    00861           // 27.6.1.3 paragraph 1
    00862           // Turn this on without causing an ios::failure to be thrown.
    00863           this->_M_setstate(ios_base::badbit);
    00864           if ((this->exceptions() & ios_base::badbit) != 0)
    00865         __throw_exception_again;
    00866         }
    00867     }
    00868       else
    00869     this->setstate(ios_base::failbit);
    00870       return *this;
    00871     }
    00872
    00873   template<typename _CharT, typename _Traits>
    00874     basic_istream<_CharT, _Traits>&
    00875     basic_istream<_CharT, _Traits>::
    00876     unget(void)
    00877     {
    00878 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00879 // 60. What is a formatted input function?
    00880       _M_gcount = 0;
    00881 #endif
    00882       sentry __cerb(*this, true);
    00883       if (__cerb)
    00884     {
    00885       try
    00886         {
    00887           const int_type __eof = traits_type::eof();
    00888           __streambuf_type* __sb = this->rdbuf();
    00889           if (!__sb
    00890           || traits_type::eq_int_type(__sb->sungetc(), __eof))
    00891         this->setstate(ios_base::badbit);
    00892         }
    00893       catch(...)
    00894         {
    00895           // 27.6.1.3 paragraph 1
    00896           // Turn this on without causing an ios::failure to be thrown.
    00897           this->_M_setstate(ios_base::badbit);
    00898           if ((this->exceptions() & ios_base::badbit) != 0)
    00899         __throw_exception_again;
    00900         }
    00901     }
    00902       else
    00903     this->setstate(ios_base::failbit);
    00904       return *this;
    00905     }
    00906
    00907   template<typename _CharT, typename _Traits>
    00908     int
    00909     basic_istream<_CharT, _Traits>::
    00910     sync(void)
    00911     {
    00912       // DR60.  Do not change _M_gcount.
    00913       int __ret = -1;
    00914       sentry __cerb(*this, true);
    00915       if (__cerb)
    00916     {
    00917       try
    00918         {
    00919           __streambuf_type* __sb = this->rdbuf();
    00920           if (__sb)
    00921         {
    00922           if (__sb->pubsync() == -1)
    00923             this->setstate(ios_base::badbit);
    00924           else
    00925             __ret = 0;
    00926         }
    00927         }
    00928       catch(...)
    00929         {
    00930           // 27.6.1.3 paragraph 1
    00931           // Turn this on without causing an ios::failure to be thrown.
    00932           this->_M_setstate(ios_base::badbit);
    00933           if ((this->exceptions() & ios_base::badbit) != 0)
    00934         __throw_exception_again;
    00935         }
    00936     }
    00937       return __ret;
    00938     }
    00939
    00940   template<typename _CharT, typename _Traits>
    00941     typename basic_istream<_CharT, _Traits>::pos_type
    00942     basic_istream<_CharT, _Traits>::
    00943     tellg(void)
    00944     {
    00945       // DR60.  Do not change _M_gcount.
    00946       pos_type __ret = pos_type(-1);
    00947       if (!this->fail())
    00948     __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
    00949       return __ret;
    00950     }
    00951
    00952
    00953   template<typename _CharT, typename _Traits>
    00954     basic_istream<_CharT, _Traits>&
    00955     basic_istream<_CharT, _Traits>::
    00956     seekg(pos_type __pos)
    00957     {
    00958       // DR60.  Do not change _M_gcount.
    00959       if (!this->fail())
    00960     {
    00961 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00962 // 136.  seekp, seekg setting wrong streams?
    00963       pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::in);
    00964
    00965 // 129. Need error indication from seekp() and seekg()
    00966       if (__err == pos_type(off_type(-1)))
    00967         this->setstate(ios_base::failbit);
    00968 #endif
    00969     }
    00970       return *this;
    00971     }
    00972
    00973   template<typename _CharT, typename _Traits>
    00974     basic_istream<_CharT, _Traits>&
    00975     basic_istream<_CharT, _Traits>::
    00976     seekg(off_type __off, ios_base::seekdir __dir)
    00977     {
    00978       // DR60.  Do not change _M_gcount.
    00979       if (!this->fail())
    00980     {
    00981 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00982 // 136.  seekp, seekg setting wrong streams?
    00983       pos_type __err = this->rdbuf()->pubseekoff(__off, __dir,
    00984                              ios_base::in);
    00985
    00986 // 129. Need error indication from seekp() and seekg()
    00987       if (__err == pos_type(off_type(-1)))
    00988         this->setstate(ios_base::failbit);
    00989 #endif
    00990     }
    00991       return *this;
    00992     }
    00993
    00994   // 27.6.1.2.3 Character extraction templates
    00995   template<typename _CharT, typename _Traits>
    00996     basic_istream<_CharT, _Traits>&
    00997     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
    00998     {
    00999       typedef basic_istream<_CharT, _Traits>        __istream_type;
    01000       typename __istream_type::sentry __cerb(__in, false);
    01001       if (__cerb)
    01002     {
    01003       try
    01004         { __in.get(__c); }
    01005       catch(...)
    01006         {
    01007           // 27.6.1.2.1 Common requirements.
    01008           // Turn this on without causing an ios::failure to be thrown.
    01009           __in._M_setstate(ios_base::badbit);
    01010           if ((__in.exceptions() & ios_base::badbit) != 0)
    01011         __throw_exception_again;
    01012         }
    01013     }
    01014       else
    01015     __in.setstate(ios_base::failbit);
    01016       return __in;
    01017     }
    01018
    01019   template<typename _CharT, typename _Traits>
    01020     basic_istream<_CharT, _Traits>&
    01021     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
    01022     {
    01023       typedef basic_istream<_CharT, _Traits>        __istream_type;
    01024       typedef typename __istream_type::__streambuf_type __streambuf_type;
    01025       typedef typename _Traits::int_type        int_type;
    01026       typedef _CharT                            char_type;
    01027       typedef ctype<_CharT>                 __ctype_type;
    01028       streamsize __extracted = 0;
    01029
    01030       typename __istream_type::sentry __cerb(__in, false);
    01031       if (__cerb)
    01032     {
    01033       try
    01034         {
    01035           // Figure out how many characters to extract.
    01036           streamsize __num = __in.width();
    01037           if (__num == 0)
    01038         __num = numeric_limits<streamsize>::max();
    01039
    01040           const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
    01041           const int_type __eof = _Traits::eof();
    01042           __streambuf_type* __sb = __in.rdbuf();
    01043           int_type __c = __sb->sgetc();
    01044
    01045           while (__extracted < __num - 1
    01046              && !_Traits::eq_int_type(__c, __eof)
    01047              && !__ctype.is(ctype_base::space, _Traits::to_char_type(__c)))
    01048         {
    01049           *__s++ = _Traits::to_char_type(__c);
    01050           ++__extracted;
    01051           __c = __sb->snextc();
    01052         }
    01053           if (_Traits::eq_int_type(__c, __eof))
    01054         __in.setstate(ios_base::eofbit);
    01055
    01056 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    01057 //68.  Extractors for char* should store null at end
    01058           *__s = char_type();
    01059 #endif
    01060           __in.width(0);
    01061         }
    01062       catch(...)
    01063         {
    01064           // 27.6.1.2.1 Common requirements.
    01065           // Turn this on without causing an ios::failure to be thrown.
    01066           __in._M_setstate(ios_base::badbit);
    01067           if ((__in.exceptions() & ios_base::badbit) != 0)
    01068         __throw_exception_again;
    01069         }
    01070     }
    01071       if (!__extracted)
    01072     __in.setstate(ios_base::failbit);
    01073       return __in;
    01074     }
    01075
    01076   // 27.6.1.4 Standard basic_istream manipulators
    01077   template<typename _CharT, typename _Traits>
    01078     basic_istream<_CharT,_Traits>&
    01079     ws(basic_istream<_CharT,_Traits>& __in)
    01080     {
    01081       typedef basic_istream<_CharT, _Traits>        __istream_type;
    01082       typedef typename __istream_type::__streambuf_type __streambuf_type;
    01083       typedef typename __istream_type::__ctype_type     __ctype_type;
    01084       typedef typename __istream_type::int_type     __int_type;
    01085
    01086       const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
    01087       const __int_type __eof = _Traits::eof();
    01088       __streambuf_type* __sb = __in.rdbuf();
    01089       __int_type __c = __sb->sgetc();
    01090
    01091       while (!_Traits::eq_int_type(__c, __eof)
    01092          && __ctype.is(ctype_base::space, _Traits::to_char_type(__c)))
    01093     __c = __sb->snextc();
    01094
    01095        if (_Traits::eq_int_type(__c, __eof))
    01096     __in.setstate(ios_base::eofbit);
    01097
    01098       return __in;
    01099     }
    01100
    01101   // 21.3.7.9 basic_string::getline and operators
    01102   template<typename _CharT, typename _Traits, typename _Alloc>
    01103     basic_istream<_CharT, _Traits>&
    01104     operator>>(basic_istream<_CharT, _Traits>& __in,
    01105            basic_string<_CharT, _Traits, _Alloc>& __str)
    01106     {
    01107       typedef basic_istream<_CharT, _Traits>        __istream_type;
    01108       typedef typename __istream_type::int_type     __int_type;
    01109       typedef typename __istream_type::__streambuf_type __streambuf_type;
    01110       typedef typename __istream_type::__ctype_type     __ctype_type;
    01111       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
    01112       typedef typename __string_type::size_type     __size_type;
    01113       __size_type __extracted = 0;
    01114
    01115       typename __istream_type::sentry __cerb(__in, false);
    01116       if (__cerb)
    01117     {
    01118       __str.erase();
    01119       streamsize __w = __in.width();
    01120       __size_type __n;
    01121       __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size();
    01122
    01123       const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
    01124       const __int_type __eof = _Traits::eof();
    01125       __streambuf_type* __sb = __in.rdbuf();
    01126       __int_type __c = __sb->sgetc();
    01127
    01128       while (__extracted < __n
    01129          && !_Traits::eq_int_type(__c, __eof)
    01130          && !__ctype.is(ctype_base::space, _Traits::to_char_type(__c)))
    01131         {
    01132           __str += _Traits::to_char_type(__c);
    01133           ++__extracted;
    01134           __c = __sb->snextc();
    01135         }
    01136       if (_Traits::eq_int_type(__c, __eof))
    01137         __in.setstate(ios_base::eofbit);
    01138       __in.width(0);
    01139     }
    01140 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    01141 //211.  operator>>(istream&, string&) doesn't set failbit
    01142       if (!__extracted)
    01143     __in.setstate (ios_base::failbit);
    01144 #endif
    01145       return __in;
    01146     }
    01147
    01148   template<typename _CharT, typename _Traits, typename _Alloc>
    01149     basic_istream<_CharT, _Traits>&
    01150     getline(basic_istream<_CharT, _Traits>& __in,
    01151         basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
    01152     {
    01153       typedef basic_istream<_CharT, _Traits>        __istream_type;
    01154       typedef typename __istream_type::int_type     __int_type;
    01155       typedef typename __istream_type::__streambuf_type __streambuf_type;
    01156       typedef typename __istream_type::__ctype_type     __ctype_type;
    01157       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
    01158       typedef typename __string_type::size_type     __size_type;
    01159
    01160       __size_type __extracted = 0;
    01161       bool __testdelim = false;
    01162       typename __istream_type::sentry __cerb(__in, true);
    01163       if (__cerb)
    01164     {
    01165       __str.erase();
    01166       __size_type __n = __str.max_size();
    01167
    01168       __int_type __idelim = _Traits::to_int_type(__delim);
    01169       __streambuf_type* __sb = __in.rdbuf();
    01170       __int_type __c = __sb->sbumpc();
    01171       const __int_type __eof = _Traits::eof();
    01172       __testdelim = _Traits::eq_int_type(__c, __idelim);
    01173
    01174       while (__extracted <= __n
    01175          && !_Traits::eq_int_type(__c, __eof)
    01176          && !__testdelim)
    01177         {
    01178           __str += _Traits::to_char_type(__c);
    01179           ++__extracted;
    01180           __c = __sb->sbumpc();
    01181           __testdelim = _Traits::eq_int_type(__c, __idelim);
    01182         }
    01183       if (_Traits::eq_int_type(__c, __eof))
    01184         __in.setstate(ios_base::eofbit);
    01185     }
    01186       if (!__extracted && !__testdelim)
    01187     __in.setstate(ios_base::failbit);
    01188       return __in;
    01189     }
    01190
    01191   template<class _CharT, class _Traits, class _Alloc>
    01192     inline basic_istream<_CharT,_Traits>&
    01193     getline(basic_istream<_CharT, _Traits>& __in,
    01194         basic_string<_CharT,_Traits,_Alloc>& __str)
    01195     { return getline(__in, __str, __in.widen('\n')); }
    01196
    01197   // Inhibit implicit instantiations for required instantiations,
    01198   // which are defined via explicit instantiations elsewhere.
    01199   // NB:  This syntax is a GNU extension.
    01200 #if _GLIBCPP_EXTERN_TEMPLATE
    01201   extern template class basic_istream<char>;
    01202   extern template istream& ws(istream&);
    01203   extern template istream& operator>>(istream&, char&);
    01204   extern template istream& operator>>(istream&, char*);
    01205   extern template istream& operator>>(istream&, unsigned char&);
    01206   extern template istream& operator>>(istream&, signed char&);
    01207   extern template istream& operator>>(istream&, unsigned char*);
    01208   extern template istream& operator>>(istream&, signed char*);
    01209
    01210 #ifdef _GLIBCPP_USE_WCHAR_T
    01211   extern template class basic_istream<wchar_t>;
    01212   extern template wistream& ws(wistream&);
    01213   extern template wistream& operator>>(wistream&, wchar_t&);
    01214   extern template wistream& operator>>(wistream&, wchar_t*);
    01215 #endif
    01216 #endif
    01217 } // namespace std