# istream.tcc

Go to the documentation of this file.

    00001 // istream classes -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    00004 // 2006, 2007, 2008, 2009
    00005 // Free Software Foundation, Inc.
    00006 //
    00007 // This file is part of the GNU ISO C++ Library.  This library is free
    00008 // software; you can redistribute it and/or modify it under the
    00009 // terms of the GNU General Public License as published by the
    00010 // Free Software Foundation; either version 3, or (at your option)
    00011 // any later version.
    00012
    00013 // This library is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00016 // GNU General Public License for more details.
    00017
    00018 // Under Section 7 of GPL version 3, you are granted additional
    00019 // permissions described in the GCC Runtime Library Exception, version
    00020 // 3.1, as published by the Free Software Foundation.
    00021
    00022 // You should have received a copy of the GNU General Public License and
    00023 // a copy of the GCC Runtime Library Exception along with this program;
    00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00025 // <http://www.gnu.org/licenses/>.
    00026
    00027 /** @file istream.tcc
    00028  *  This is an internal header file, included by other library headers.
    00029  *  You should not attempt to use it directly.
    00030  */
    00031
    00032 //
    00033 // ISO C++ 14882: 27.6.1  Input streams
    00034 //
    00035
    00036 #ifndef _ISTREAM_TCC
    00037 #define _ISTREAM_TCC 1
    00038
    00039 #pragma GCC system_header
    00040
    00041 #include <cxxabi-forced.h>
    00042
    00043 _GLIBCXX_BEGIN_NAMESPACE(std)
    00044
    00045   template<typename _CharT, typename _Traits>
    00046     basic_istream<_CharT, _Traits>::sentry::
    00047     sentry(basic_istream<_CharT, _Traits>& __in, bool __noskip) : _M_ok(false)
    00048     {
    00049       ios_base::iostate __err = ios_base::goodbit;
    00050       if (__in.good())
    00051     {
    00052       if (__in.tie())
    00053         __in.tie()->flush();
    00054       if (!__noskip && bool(__in.flags() & ios_base::skipws))
    00055         {
    00056           const __int_type __eof = traits_type::eof();
    00057           __streambuf_type* __sb = __in.rdbuf();
    00058           __int_type __c = __sb->sgetc();
    00059
    00060           const __ctype_type& __ct = __check_facet(__in._M_ctype);
    00061           while (!traits_type::eq_int_type(__c, __eof)
    00062              && __ct.is(ctype_base::space,
    00063                 traits_type::to_char_type(__c)))
    00064         __c = __sb->snextc();
    00065
    00066           // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00067           // 195. Should basic_istream::sentry's constructor ever
    00068           // set eofbit?
    00069           if (traits_type::eq_int_type(__c, __eof))
    00070         __err |= ios_base::eofbit;
    00071         }
    00072     }
    00073
    00074       if (__in.good() && __err == ios_base::goodbit)
    00075     _M_ok = true;
    00076       else
    00077     {
    00078       __err |= ios_base::failbit;
    00079       __in.setstate(__err);
    00080     }
    00081     }
    00082
    00083   template<typename _CharT, typename _Traits>
    00084     template<typename _ValueT>
    00085       basic_istream<_CharT, _Traits>&
    00086       basic_istream<_CharT, _Traits>::
    00087       _M_extract(_ValueT& __v)
    00088       {
    00089     sentry __cerb(*this, false);
    00090     if (__cerb)
    00091       {
    00092         ios_base::iostate __err = ios_base::goodbit;
    00093         __try
    00094           {
    00095         const __num_get_type& __ng = __check_facet(this->_M_num_get);
    00096         __ng.get(*this, 0, *this, __err, __v);
    00097           }
    00098         __catch(__cxxabiv1::__forced_unwind&)
    00099           {
    00100         this->_M_setstate(ios_base::badbit);
    00101         __throw_exception_again;
    00102           }
    00103         __catch(...)
    00104           { this->_M_setstate(ios_base::badbit); }
    00105         if (__err)
    00106           this->setstate(__err);
    00107       }
    00108     return *this;
    00109       }
    00110
    00111   template<typename _CharT, typename _Traits>
    00112     basic_istream<_CharT, _Traits>&
    00113     basic_istream<_CharT, _Traits>::
    00114     operator>>(short& __n)
    00115     {
    00116       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00117       // 118. basic_istream uses nonexistent num_get member functions.
    00118       sentry __cerb(*this, false);
    00119       if (__cerb)
    00120     {
    00121       ios_base::iostate __err = ios_base::goodbit;
    00122       __try
    00123         {
    00124           long __l;
    00125           const __num_get_type& __ng = __check_facet(this->_M_num_get);
    00126           __ng.get(*this, 0, *this, __err, __l);
    00127
    00128           // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00129           // 696. istream::operator>>(int&) broken.
    00130           if (__l < __gnu_cxx::__numeric_traits<short>::__min)
    00131         {
    00132           __err |= ios_base::failbit;
    00133           __n = __gnu_cxx::__numeric_traits<short>::__min;
    00134         }
    00135           else if (__l > __gnu_cxx::__numeric_traits<short>::__max)
    00136         {
    00137           __err |= ios_base::failbit;
    00138           __n = __gnu_cxx::__numeric_traits<short>::__max;
    00139         }
    00140           else
    00141         __n = short(__l);
    00142         }
    00143       __catch(__cxxabiv1::__forced_unwind&)
    00144         {
    00145           this->_M_setstate(ios_base::badbit);
    00146           __throw_exception_again;
    00147         }
    00148       __catch(...)
    00149         { this->_M_setstate(ios_base::badbit); }
    00150       if (__err)
    00151         this->setstate(__err);
    00152     }
    00153       return *this;
    00154     }
    00155
    00156   template<typename _CharT, typename _Traits>
    00157     basic_istream<_CharT, _Traits>&
    00158     basic_istream<_CharT, _Traits>::
    00159     operator>>(int& __n)
    00160     {
    00161       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00162       // 118. basic_istream uses nonexistent num_get member functions.
    00163       sentry __cerb(*this, false);
    00164       if (__cerb)
    00165     {
    00166       ios_base::iostate __err = ios_base::goodbit;
    00167       __try
    00168         {
    00169           long __l;
    00170           const __num_get_type& __ng = __check_facet(this->_M_num_get);
    00171           __ng.get(*this, 0, *this, __err, __l);
    00172
    00173           // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00174           // 696. istream::operator>>(int&) broken.
    00175           if (__l < __gnu_cxx::__numeric_traits<int>::__min)
    00176         {
    00177           __err |= ios_base::failbit;
    00178           __n = __gnu_cxx::__numeric_traits<int>::__min;
    00179         }
    00180           else if (__l > __gnu_cxx::__numeric_traits<int>::__max)
    00181         {
    00182           __err |= ios_base::failbit;
    00183           __n = __gnu_cxx::__numeric_traits<int>::__max;
    00184         }
    00185           else
    00186         __n = int(__l);
    00187         }
    00188       __catch(__cxxabiv1::__forced_unwind&)
    00189         {
    00190           this->_M_setstate(ios_base::badbit);
    00191           __throw_exception_again;
    00192         }
    00193       __catch(...)
    00194         { this->_M_setstate(ios_base::badbit); }
    00195       if (__err)
    00196         this->setstate(__err);
    00197     }
    00198       return *this;
    00199     }
    00200
    00201   template<typename _CharT, typename _Traits>
    00202     basic_istream<_CharT, _Traits>&
    00203     basic_istream<_CharT, _Traits>::
    00204     operator>>(__streambuf_type* __sbout)
    00205     {
    00206       ios_base::iostate __err = ios_base::goodbit;
    00207       sentry __cerb(*this, false);
    00208       if (__cerb && __sbout)
    00209     {
    00210       __try
    00211         {
    00212           bool __ineof;
    00213           if (!__copy_streambufs_eof(this->rdbuf(), __sbout, __ineof))
    00214         __err |= ios_base::failbit;
    00215           if (__ineof)
    00216         __err |= ios_base::eofbit;
    00217         }
    00218       __catch(__cxxabiv1::__forced_unwind&)
    00219         {
    00220           this->_M_setstate(ios_base::failbit);
    00221           __throw_exception_again;
    00222         }
    00223       __catch(...)
    00224         { this->_M_setstate(ios_base::failbit); }
    00225     }
    00226       else if (!__sbout)
    00227     __err |= ios_base::failbit;
    00228       if (__err)
    00229     this->setstate(__err);
    00230       return *this;
    00231     }
    00232
    00233   template<typename _CharT, typename _Traits>
    00234     typename basic_istream<_CharT, _Traits>::int_type
    00235     basic_istream<_CharT, _Traits>::
    00236     get(void)
    00237     {
    00238       const int_type __eof = traits_type::eof();
    00239       int_type __c = __eof;
    00240       _M_gcount = 0;
    00241       ios_base::iostate __err = ios_base::goodbit;
    00242       sentry __cerb(*this, true);
    00243       if (__cerb)
    00244     {
    00245       __try
    00246         {
    00247           __c = this->rdbuf()->sbumpc();
    00248           // 27.6.1.1 paragraph 3
    00249           if (!traits_type::eq_int_type(__c, __eof))
    00250         _M_gcount = 1;
    00251           else
    00252         __err |= ios_base::eofbit;
    00253         }
    00254       __catch(__cxxabiv1::__forced_unwind&)
    00255         {
    00256           this->_M_setstate(ios_base::badbit);
    00257           __throw_exception_again;
    00258         }
    00259       __catch(...)
    00260         { this->_M_setstate(ios_base::badbit); }
    00261     }
    00262       if (!_M_gcount)
    00263     __err |= ios_base::failbit;
    00264       if (__err)
    00265     this->setstate(__err);
    00266       return __c;
    00267     }
    00268
    00269   template<typename _CharT, typename _Traits>
    00270     basic_istream<_CharT, _Traits>&
    00271     basic_istream<_CharT, _Traits>::
    00272     get(char_type& __c)
    00273     {
    00274       _M_gcount = 0;
    00275       ios_base::iostate __err = ios_base::goodbit;
    00276       sentry __cerb(*this, true);
    00277       if (__cerb)
    00278     {
    00279       __try
    00280         {
    00281           const int_type __cb = this->rdbuf()->sbumpc();
    00282           // 27.6.1.1 paragraph 3
    00283           if (!traits_type::eq_int_type(__cb, traits_type::eof()))
    00284         {
    00285           _M_gcount = 1;
    00286           __c = traits_type::to_char_type(__cb);
    00287         }
    00288           else
    00289         __err |= ios_base::eofbit;
    00290         }
    00291       __catch(__cxxabiv1::__forced_unwind&)
    00292         {
    00293           this->_M_setstate(ios_base::badbit);
    00294           __throw_exception_again;
    00295         }
    00296       __catch(...)
    00297         { this->_M_setstate(ios_base::badbit); }
    00298     }
    00299       if (!_M_gcount)
    00300     __err |= ios_base::failbit;
    00301       if (__err)
    00302     this->setstate(__err);
    00303       return *this;
    00304     }
    00305
    00306   template<typename _CharT, typename _Traits>
    00307     basic_istream<_CharT, _Traits>&
    00308     basic_istream<_CharT, _Traits>::
    00309     get(char_type* __s, streamsize __n, char_type __delim)
    00310     {
    00311       _M_gcount = 0;
    00312       ios_base::iostate __err = ios_base::goodbit;
    00313       sentry __cerb(*this, true);
    00314       if (__cerb)
    00315     {
    00316       __try
    00317         {
    00318           const int_type __idelim = traits_type::to_int_type(__delim);
    00319           const int_type __eof = traits_type::eof();
    00320           __streambuf_type* __sb = this->rdbuf();
    00321           int_type __c = __sb->sgetc();
    00322
    00323           while (_M_gcount + 1 < __n
    00324              && !traits_type::eq_int_type(__c, __eof)
    00325              && !traits_type::eq_int_type(__c, __idelim))
    00326         {
    00327           *__s++ = traits_type::to_char_type(__c);
    00328           ++_M_gcount;
    00329           __c = __sb->snextc();
    00330         }
    00331           if (traits_type::eq_int_type(__c, __eof))
    00332         __err |= ios_base::eofbit;
    00333         }
    00334       __catch(__cxxabiv1::__forced_unwind&)
    00335         {
    00336           this->_M_setstate(ios_base::badbit);
    00337           __throw_exception_again;
    00338         }
    00339       __catch(...)
    00340         { this->_M_setstate(ios_base::badbit); }
    00341     }
    00342       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00343       // 243. get and getline when sentry reports failure.
    00344       if (__n > 0)
    00345     *__s = char_type();
    00346       if (!_M_gcount)
    00347     __err |= ios_base::failbit;
    00348       if (__err)
    00349     this->setstate(__err);
    00350       return *this;
    00351     }
    00352
    00353   template<typename _CharT, typename _Traits>
    00354     basic_istream<_CharT, _Traits>&
    00355     basic_istream<_CharT, _Traits>::
    00356     get(__streambuf_type& __sb, char_type __delim)
    00357     {
    00358       _M_gcount = 0;
    00359       ios_base::iostate __err = ios_base::goodbit;
    00360       sentry __cerb(*this, true);
    00361       if (__cerb)
    00362     {
    00363       __try
    00364         {
    00365           const int_type __idelim = traits_type::to_int_type(__delim);
    00366           const int_type __eof = traits_type::eof();
    00367           __streambuf_type* __this_sb = this->rdbuf();
    00368           int_type __c = __this_sb->sgetc();
    00369           char_type __c2 = traits_type::to_char_type(__c);
    00370
    00371           while (!traits_type::eq_int_type(__c, __eof)
    00372              && !traits_type::eq_int_type(__c, __idelim)
    00373              && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
    00374         {
    00375           ++_M_gcount;
    00376           __c = __this_sb->snextc();
    00377           __c2 = traits_type::to_char_type(__c);
    00378         }
    00379           if (traits_type::eq_int_type(__c, __eof))
    00380         __err |= ios_base::eofbit;
    00381         }
    00382       __catch(__cxxabiv1::__forced_unwind&)
    00383         {
    00384           this->_M_setstate(ios_base::badbit);
    00385           __throw_exception_again;
    00386         }
    00387       __catch(...)
    00388         { this->_M_setstate(ios_base::badbit); }
    00389     }
    00390       if (!_M_gcount)
    00391     __err |= ios_base::failbit;
    00392       if (__err)
    00393     this->setstate(__err);
    00394       return *this;
    00395     }
    00396
    00397   template<typename _CharT, typename _Traits>
    00398     basic_istream<_CharT, _Traits>&
    00399     basic_istream<_CharT, _Traits>::
    00400     getline(char_type* __s, streamsize __n, char_type __delim)
    00401     {
    00402       _M_gcount = 0;
    00403       ios_base::iostate __err = ios_base::goodbit;
    00404       sentry __cerb(*this, true);
    00405       if (__cerb)
    00406         {
    00407           __try
    00408             {
    00409               const int_type __idelim = traits_type::to_int_type(__delim);
    00410               const int_type __eof = traits_type::eof();
    00411               __streambuf_type* __sb = this->rdbuf();
    00412               int_type __c = __sb->sgetc();
    00413
    00414               while (_M_gcount + 1 < __n
    00415                      && !traits_type::eq_int_type(__c, __eof)
    00416                      && !traits_type::eq_int_type(__c, __idelim))
    00417                 {
    00418                   *__s++ = traits_type::to_char_type(__c);
    00419                   __c = __sb->snextc();
    00420                   ++_M_gcount;
    00421                 }
    00422               if (traits_type::eq_int_type(__c, __eof))
    00423                 __err |= ios_base::eofbit;
    00424               else
    00425                 {
    00426                   if (traits_type::eq_int_type(__c, __idelim))
    00427                     {
    00428                       __sb->sbumpc();
    00429                       ++_M_gcount;
    00430                     }
    00431                   else
    00432                     __err |= ios_base::failbit;
    00433                 }
    00434             }
    00435       __catch(__cxxabiv1::__forced_unwind&)
    00436         {
    00437           this->_M_setstate(ios_base::badbit);
    00438           __throw_exception_again;
    00439         }
    00440           __catch(...)
    00441             { this->_M_setstate(ios_base::badbit); }
    00442         }
    00443       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00444       // 243. get and getline when sentry reports failure.
    00445       if (__n > 0)
    00446     *__s = char_type();
    00447       if (!_M_gcount)
    00448         __err |= ios_base::failbit;
    00449       if (__err)
    00450         this->setstate(__err);
    00451       return *this;
    00452     }
    00453
    00454   // We provide three overloads, since the first two are much simpler
    00455   // than the general case. Also, the latter two can thus adopt the
    00456   // same "batchy" strategy used by getline above.
    00457   template<typename _CharT, typename _Traits>
    00458     basic_istream<_CharT, _Traits>&
    00459     basic_istream<_CharT, _Traits>::
    00460     ignore(void)
    00461     {
    00462       _M_gcount = 0;
    00463       sentry __cerb(*this, true);
    00464       if (__cerb)
    00465     {
    00466       ios_base::iostate __err = ios_base::goodbit;
    00467       __try
    00468         {
    00469           const int_type __eof = traits_type::eof();
    00470           __streambuf_type* __sb = this->rdbuf();
    00471
    00472           if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
    00473         __err |= ios_base::eofbit;
    00474           else
    00475         _M_gcount = 1;
    00476         }
    00477       __catch(__cxxabiv1::__forced_unwind&)
    00478         {
    00479           this->_M_setstate(ios_base::badbit);
    00480           __throw_exception_again;
    00481         }
    00482       __catch(...)
    00483         { this->_M_setstate(ios_base::badbit); }
    00484       if (__err)
    00485         this->setstate(__err);
    00486     }
    00487       return *this;
    00488     }
    00489
    00490   template<typename _CharT, typename _Traits>
    00491     basic_istream<_CharT, _Traits>&
    00492     basic_istream<_CharT, _Traits>::
    00493     ignore(streamsize __n)
    00494     {
    00495       _M_gcount = 0;
    00496       sentry __cerb(*this, true);
    00497       if (__cerb && __n > 0)
    00498         {
    00499           ios_base::iostate __err = ios_base::goodbit;
    00500           __try
    00501             {
    00502               const int_type __eof = traits_type::eof();
    00503               __streambuf_type* __sb = this->rdbuf();
    00504               int_type __c = __sb->sgetc();
    00505
    00506           // N.B. On LFS-enabled platforms streamsize is still 32 bits
    00507           // wide: if we want to implement the standard mandated behavior
    00508           // for n == max() (see 27.6.1.3/24) we are at risk of signed
    00509           // integer overflow: thus these contortions. Also note that,
    00510           // by definition, when more than 2G chars are actually ignored,
    00511           // _M_gcount (the return value of gcount, that is) cannot be
    00512           // really correct, being unavoidably too small.
    00513           bool __large_ignore = false;
    00514           while (true)
    00515         {
    00516           while (_M_gcount < __n
    00517              && !traits_type::eq_int_type(__c, __eof))
    00518             {
    00519               ++_M_gcount;
    00520               __c = __sb->snextc();
    00521             }
    00522           if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
    00523               && !traits_type::eq_int_type(__c, __eof))
    00524             {
    00525               _M_gcount =
    00526             __gnu_cxx::__numeric_traits<streamsize>::__min;
    00527               __large_ignore = true;
    00528             }
    00529           else
    00530             break;
    00531         }
    00532
    00533           if (__large_ignore)
    00534         _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
    00535
    00536           if (traits_type::eq_int_type(__c, __eof))
    00537                 __err |= ios_base::eofbit;
    00538             }
    00539       __catch(__cxxabiv1::__forced_unwind&)
    00540         {
    00541           this->_M_setstate(ios_base::badbit);
    00542           __throw_exception_again;
    00543         }
    00544           __catch(...)
    00545             { this->_M_setstate(ios_base::badbit); }
    00546           if (__err)
    00547             this->setstate(__err);
    00548         }
    00549       return *this;
    00550     }
    00551
    00552   template<typename _CharT, typename _Traits>
    00553     basic_istream<_CharT, _Traits>&
    00554     basic_istream<_CharT, _Traits>::
    00555     ignore(streamsize __n, int_type __delim)
    00556     {
    00557       _M_gcount = 0;
    00558       sentry __cerb(*this, true);
    00559       if (__cerb && __n > 0)
    00560         {
    00561           ios_base::iostate __err = ios_base::goodbit;
    00562           __try
    00563             {
    00564               const int_type __eof = traits_type::eof();
    00565               __streambuf_type* __sb = this->rdbuf();
    00566               int_type __c = __sb->sgetc();
    00567
    00568           // See comment above.
    00569           bool __large_ignore = false;
    00570           while (true)
    00571         {
    00572           while (_M_gcount < __n
    00573              && !traits_type::eq_int_type(__c, __eof)
    00574              && !traits_type::eq_int_type(__c, __delim))
    00575             {
    00576               ++_M_gcount;
    00577               __c = __sb->snextc();
    00578             }
    00579           if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
    00580               && !traits_type::eq_int_type(__c, __eof)
    00581               && !traits_type::eq_int_type(__c, __delim))
    00582             {
    00583               _M_gcount =
    00584             __gnu_cxx::__numeric_traits<streamsize>::__min;
    00585               __large_ignore = true;
    00586             }
    00587           else
    00588             break;
    00589         }
    00590
    00591           if (__large_ignore)
    00592         _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
    00593
    00594               if (traits_type::eq_int_type(__c, __eof))
    00595                 __err |= ios_base::eofbit;
    00596           else if (traits_type::eq_int_type(__c, __delim))
    00597         {
    00598           if (_M_gcount
    00599               < __gnu_cxx::__numeric_traits<streamsize>::__max)
    00600             ++_M_gcount;
    00601           __sb->sbumpc();
    00602         }
    00603             }
    00604       __catch(__cxxabiv1::__forced_unwind&)
    00605         {
    00606           this->_M_setstate(ios_base::badbit);
    00607           __throw_exception_again;
    00608         }
    00609           __catch(...)
    00610             { this->_M_setstate(ios_base::badbit); }
    00611           if (__err)
    00612             this->setstate(__err);
    00613         }
    00614       return *this;
    00615     }
    00616
    00617   template<typename _CharT, typename _Traits>
    00618     typename basic_istream<_CharT, _Traits>::int_type
    00619     basic_istream<_CharT, _Traits>::
    00620     peek(void)
    00621     {
    00622       int_type __c = traits_type::eof();
    00623       _M_gcount = 0;
    00624       sentry __cerb(*this, true);
    00625       if (__cerb)
    00626     {
    00627       ios_base::iostate __err = ios_base::goodbit;
    00628       __try
    00629         {
    00630           __c = this->rdbuf()->sgetc();
    00631           if (traits_type::eq_int_type(__c, traits_type::eof()))
    00632         __err |= ios_base::eofbit;
    00633         }
    00634       __catch(__cxxabiv1::__forced_unwind&)
    00635         {
    00636           this->_M_setstate(ios_base::badbit);
    00637           __throw_exception_again;
    00638         }
    00639       __catch(...)
    00640         { this->_M_setstate(ios_base::badbit); }
    00641       if (__err)
    00642         this->setstate(__err);
    00643     }
    00644       return __c;
    00645     }
    00646
    00647   template<typename _CharT, typename _Traits>
    00648     basic_istream<_CharT, _Traits>&
    00649     basic_istream<_CharT, _Traits>::
    00650     read(char_type* __s, streamsize __n)
    00651     {
    00652       _M_gcount = 0;
    00653       sentry __cerb(*this, true);
    00654       if (__cerb)
    00655     {
    00656       ios_base::iostate __err = ios_base::goodbit;
    00657       __try
    00658         {
    00659           _M_gcount = this->rdbuf()->sgetn(__s, __n);
    00660           if (_M_gcount != __n)
    00661         __err |= (ios_base::eofbit | ios_base::failbit);
    00662         }
    00663       __catch(__cxxabiv1::__forced_unwind&)
    00664         {
    00665           this->_M_setstate(ios_base::badbit);
    00666           __throw_exception_again;
    00667         }
    00668       __catch(...)
    00669         { this->_M_setstate(ios_base::badbit); }
    00670       if (__err)
    00671         this->setstate(__err);
    00672     }
    00673       return *this;
    00674     }
    00675
    00676   template<typename _CharT, typename _Traits>
    00677     streamsize
    00678     basic_istream<_CharT, _Traits>::
    00679     readsome(char_type* __s, streamsize __n)
    00680     {
    00681       _M_gcount = 0;
    00682       sentry __cerb(*this, true);
    00683       if (__cerb)
    00684     {
    00685       ios_base::iostate __err = ios_base::goodbit;
    00686       __try
    00687         {
    00688           // Cannot compare int_type with streamsize generically.
    00689           const streamsize __num = this->rdbuf()->in_avail();
    00690           if (__num > 0)
    00691         _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
    00692           else if (__num == -1)
    00693         __err |= ios_base::eofbit;
    00694         }
    00695       __catch(__cxxabiv1::__forced_unwind&)
    00696         {
    00697           this->_M_setstate(ios_base::badbit);
    00698           __throw_exception_again;
    00699         }
    00700       __catch(...)
    00701         { this->_M_setstate(ios_base::badbit); }
    00702       if (__err)
    00703         this->setstate(__err);
    00704     }
    00705       return _M_gcount;
    00706     }
    00707
    00708   template<typename _CharT, typename _Traits>
    00709     basic_istream<_CharT, _Traits>&
    00710     basic_istream<_CharT, _Traits>::
    00711     putback(char_type __c)
    00712     {
    00713       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00714       // 60. What is a formatted input function?
    00715       _M_gcount = 0;
    00716       sentry __cerb(*this, true);
    00717       if (__cerb)
    00718     {
    00719       ios_base::iostate __err = ios_base::goodbit;
    00720       __try
    00721         {
    00722           const int_type __eof = traits_type::eof();
    00723           __streambuf_type* __sb = this->rdbuf();
    00724           if (!__sb
    00725           || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
    00726         __err |= ios_base::badbit;
    00727         }
    00728       __catch(__cxxabiv1::__forced_unwind&)
    00729         {
    00730           this->_M_setstate(ios_base::badbit);
    00731           __throw_exception_again;
    00732         }
    00733       __catch(...)
    00734         { this->_M_setstate(ios_base::badbit); }
    00735       if (__err)
    00736         this->setstate(__err);
    00737     }
    00738       return *this;
    00739     }
    00740
    00741   template<typename _CharT, typename _Traits>
    00742     basic_istream<_CharT, _Traits>&
    00743     basic_istream<_CharT, _Traits>::
    00744     unget(void)
    00745     {
    00746       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00747       // 60. What is a formatted input function?
    00748       _M_gcount = 0;
    00749       sentry __cerb(*this, true);
    00750       if (__cerb)
    00751     {
    00752       ios_base::iostate __err = ios_base::goodbit;
    00753       __try
    00754         {
    00755           const int_type __eof = traits_type::eof();
    00756           __streambuf_type* __sb = this->rdbuf();
    00757           if (!__sb
    00758           || traits_type::eq_int_type(__sb->sungetc(), __eof))
    00759         __err |= ios_base::badbit;
    00760         }
    00761       __catch(__cxxabiv1::__forced_unwind&)
    00762         {
    00763           this->_M_setstate(ios_base::badbit);
    00764           __throw_exception_again;
    00765         }
    00766       __catch(...)
    00767         { this->_M_setstate(ios_base::badbit); }
    00768       if (__err)
    00769         this->setstate(__err);
    00770     }
    00771       return *this;
    00772     }
    00773
    00774   template<typename _CharT, typename _Traits>
    00775     int
    00776     basic_istream<_CharT, _Traits>::
    00777     sync(void)
    00778     {
    00779       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00780       // DR60.  Do not change _M_gcount.
    00781       int __ret = -1;
    00782       sentry __cerb(*this, true);
    00783       if (__cerb)
    00784     {
    00785       ios_base::iostate __err = ios_base::goodbit;
    00786       __try
    00787         {
    00788           __streambuf_type* __sb = this->rdbuf();
    00789           if (__sb)
    00790         {
    00791           if (__sb->pubsync() == -1)
    00792             __err |= ios_base::badbit;
    00793           else
    00794             __ret = 0;
    00795         }
    00796         }
    00797       __catch(__cxxabiv1::__forced_unwind&)
    00798         {
    00799           this->_M_setstate(ios_base::badbit);
    00800           __throw_exception_again;
    00801         }
    00802       __catch(...)
    00803         { this->_M_setstate(ios_base::badbit); }
    00804       if (__err)
    00805         this->setstate(__err);
    00806     }
    00807       return __ret;
    00808     }
    00809
    00810   template<typename _CharT, typename _Traits>
    00811     typename basic_istream<_CharT, _Traits>::pos_type
    00812     basic_istream<_CharT, _Traits>::
    00813     tellg(void)
    00814     {
    00815       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00816       // DR60.  Do not change _M_gcount.
    00817       pos_type __ret = pos_type(-1);
    00818       __try
    00819     {
    00820       if (!this->fail())
    00821         __ret = this->rdbuf()->pubseekoff(0, ios_base::cur,
    00822                           ios_base::in);
    00823     }
    00824       __catch(__cxxabiv1::__forced_unwind&)
    00825     {
    00826       this->_M_setstate(ios_base::badbit);
    00827       __throw_exception_again;
    00828     }
    00829       __catch(...)
    00830     { this->_M_setstate(ios_base::badbit); }
    00831       return __ret;
    00832     }
    00833
    00834   template<typename _CharT, typename _Traits>
    00835     basic_istream<_CharT, _Traits>&
    00836     basic_istream<_CharT, _Traits>::
    00837     seekg(pos_type __pos)
    00838     {
    00839       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00840       // DR60.  Do not change _M_gcount.
    00841       ios_base::iostate __err = ios_base::goodbit;
    00842       __try
    00843     {
    00844       if (!this->fail())
    00845         {
    00846           // 136.  seekp, seekg setting wrong streams?
    00847           const pos_type __p = this->rdbuf()->pubseekpos(__pos,
    00848                                  ios_base::in);
    00849
    00850           // 129.  Need error indication from seekp() and seekg()
    00851           if (__p == pos_type(off_type(-1)))
    00852         __err |= ios_base::failbit;
    00853         }
    00854     }
    00855       __catch(__cxxabiv1::__forced_unwind&)
    00856     {
    00857       this->_M_setstate(ios_base::badbit);
    00858       __throw_exception_again;
    00859     }
    00860       __catch(...)
    00861     { this->_M_setstate(ios_base::badbit); }
    00862       if (__err)
    00863     this->setstate(__err);
    00864       return *this;
    00865     }
    00866
    00867   template<typename _CharT, typename _Traits>
    00868     basic_istream<_CharT, _Traits>&
    00869     basic_istream<_CharT, _Traits>::
    00870     seekg(off_type __off, ios_base::seekdir __dir)
    00871     {
    00872       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00873       // DR60.  Do not change _M_gcount.
    00874       ios_base::iostate __err = ios_base::goodbit;
    00875       __try
    00876     {
    00877       if (!this->fail())
    00878         {
    00879           // 136.  seekp, seekg setting wrong streams?
    00880           const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
    00881                                  ios_base::in);
    00882
    00883           // 129.  Need error indication from seekp() and seekg()
    00884           if (__p == pos_type(off_type(-1)))
    00885         __err |= ios_base::failbit;
    00886         }
    00887     }
    00888       __catch(__cxxabiv1::__forced_unwind&)
    00889     {
    00890       this->_M_setstate(ios_base::badbit);
    00891       __throw_exception_again;
    00892     }
    00893       __catch(...)
    00894     { this->_M_setstate(ios_base::badbit); }
    00895       if (__err)
    00896     this->setstate(__err);
    00897       return *this;
    00898     }
    00899
    00900   // 27.6.1.2.3 Character extraction templates
    00901   template<typename _CharT, typename _Traits>
    00902     basic_istream<_CharT, _Traits>&
    00903     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
    00904     {
    00905       typedef basic_istream<_CharT, _Traits>        __istream_type;
    00906       typedef typename __istream_type::int_type         __int_type;
    00907
    00908       typename __istream_type::sentry __cerb(__in, false);
    00909       if (__cerb)
    00910     {
    00911       ios_base::iostate __err = ios_base::goodbit;
    00912       __try
    00913         {
    00914           const __int_type __cb = __in.rdbuf()->sbumpc();
    00915           if (!_Traits::eq_int_type(__cb, _Traits::eof()))
    00916         __c = _Traits::to_char_type(__cb);
    00917           else
    00918         __err |= (ios_base::eofbit | ios_base::failbit);
    00919         }
    00920       __catch(__cxxabiv1::__forced_unwind&)
    00921         {
    00922           __in._M_setstate(ios_base::badbit);
    00923           __throw_exception_again;
    00924         }
    00925       __catch(...)
    00926         { __in._M_setstate(ios_base::badbit); }
    00927       if (__err)
    00928         __in.setstate(__err);
    00929     }
    00930       return __in;
    00931     }
    00932
    00933   template<typename _CharT, typename _Traits>
    00934     basic_istream<_CharT, _Traits>&
    00935     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
    00936     {
    00937       typedef basic_istream<_CharT, _Traits>        __istream_type;
    00938       typedef basic_streambuf<_CharT, _Traits>          __streambuf_type;
    00939       typedef typename _Traits::int_type        int_type;
    00940       typedef _CharT                    char_type;
    00941       typedef ctype<_CharT>             __ctype_type;
    00942
    00943       streamsize __extracted = 0;
    00944       ios_base::iostate __err = ios_base::goodbit;
    00945       typename __istream_type::sentry __cerb(__in, false);
    00946       if (__cerb)
    00947     {
    00948       __try
    00949         {
    00950           // Figure out how many characters to extract.
    00951           streamsize __num = __in.width();
    00952           if (__num <= 0)
    00953         __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
    00954
    00955           const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
    00956
    00957           const int_type __eof = _Traits::eof();
    00958           __streambuf_type* __sb = __in.rdbuf();
    00959           int_type __c = __sb->sgetc();
    00960
    00961           while (__extracted < __num - 1
    00962              && !_Traits::eq_int_type(__c, __eof)
    00963              && !__ct.is(ctype_base::space,
    00964                  _Traits::to_char_type(__c)))
    00965         {
    00966           *__s++ = _Traits::to_char_type(__c);
    00967           ++__extracted;
    00968           __c = __sb->snextc();
    00969         }
    00970           if (_Traits::eq_int_type(__c, __eof))
    00971         __err |= ios_base::eofbit;
    00972
    00973           // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00974           // 68.  Extractors for char* should store null at end
    00975           *__s = char_type();
    00976           __in.width(0);
    00977         }
    00978       __catch(__cxxabiv1::__forced_unwind&)
    00979         {
    00980           __in._M_setstate(ios_base::badbit);
    00981           __throw_exception_again;
    00982         }
    00983       __catch(...)
    00984         { __in._M_setstate(ios_base::badbit); }
    00985     }
    00986       if (!__extracted)
    00987     __err |= ios_base::failbit;
    00988       if (__err)
    00989     __in.setstate(__err);
    00990       return __in;
    00991     }
    00992
    00993   // 27.6.1.4 Standard basic_istream manipulators
    00994   template<typename _CharT, typename _Traits>
    00995     basic_istream<_CharT, _Traits>&
    00996     ws(basic_istream<_CharT, _Traits>& __in)
    00997     {
    00998       typedef basic_istream<_CharT, _Traits>        __istream_type;
    00999       typedef basic_streambuf<_CharT, _Traits>          __streambuf_type;
    01000       typedef typename __istream_type::int_type     __int_type;
    01001       typedef ctype<_CharT>             __ctype_type;
    01002
    01003       const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
    01004       const __int_type __eof = _Traits::eof();
    01005       __streambuf_type* __sb = __in.rdbuf();
    01006       __int_type __c = __sb->sgetc();
    01007
    01008       while (!_Traits::eq_int_type(__c, __eof)
    01009          && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
    01010     __c = __sb->snextc();
    01011
    01012        if (_Traits::eq_int_type(__c, __eof))
    01013      __in.setstate(ios_base::eofbit);
    01014       return __in;
    01015     }
    01016
    01017   // Inhibit implicit instantiations for required instantiations,
    01018   // which are defined via explicit instantiations elsewhere.
    01019   // NB:  This syntax is a GNU extension.
    01020 #if _GLIBCXX_EXTERN_TEMPLATE
    01021   extern template class basic_istream<char>;
    01022   extern template istream& ws(istream&);
    01023   extern template istream& operator>>(istream&, char&);
    01024   extern template istream& operator>>(istream&, char*);
    01025   extern template istream& operator>>(istream&, unsigned char&);
    01026   extern template istream& operator>>(istream&, signed char&);
    01027   extern template istream& operator>>(istream&, unsigned char*);
    01028   extern template istream& operator>>(istream&, signed char*);
    01029
    01030   extern template istream& istream::_M_extract(unsigned short&);
    01031   extern template istream& istream::_M_extract(unsigned int&);
    01032   extern template istream& istream::_M_extract(long&);
    01033   extern template istream& istream::_M_extract(unsigned long&);
    01034   extern template istream& istream::_M_extract(bool&);
    01035 #ifdef _GLIBCXX_USE_LONG_LONG
    01036   extern template istream& istream::_M_extract(long long&);
    01037   extern template istream& istream::_M_extract(unsigned long long&);
    01038 #endif
    01039   extern template istream& istream::_M_extract(float&);
    01040   extern template istream& istream::_M_extract(double&);
    01041   extern template istream& istream::_M_extract(long double&);
    01042   extern template istream& istream::_M_extract(void*&);
    01043
    01044   extern template class basic_iostream<char>;
    01045
    01046 #ifdef _GLIBCXX_USE_WCHAR_T
    01047   extern template class basic_istream<wchar_t>;
    01048   extern template wistream& ws(wistream&);
    01049   extern template wistream& operator>>(wistream&, wchar_t&);
    01050   extern template wistream& operator>>(wistream&, wchar_t*);
    01051
    01052   extern template wistream& wistream::_M_extract(unsigned short&);
    01053   extern template wistream& wistream::_M_extract(unsigned int&);
    01054   extern template wistream& wistream::_M_extract(long&);
    01055   extern template wistream& wistream::_M_extract(unsigned long&);
    01056   extern template wistream& wistream::_M_extract(bool&);
    01057 #ifdef _GLIBCXX_USE_LONG_LONG
    01058   extern template wistream& wistream::_M_extract(long long&);
    01059   extern template wistream& wistream::_M_extract(unsigned long long&);
    01060 #endif
    01061   extern template wistream& wistream::_M_extract(float&);
    01062   extern template wistream& wistream::_M_extract(double&);
    01063   extern template wistream& wistream::_M_extract(long double&);
    01064   extern template wistream& wistream::_M_extract(void*&);
    01065
    01066   extern template class basic_iostream<wchar_t>;
    01067 #endif
    01068 #endif
    01069
    01070 _GLIBCXX_END_NAMESPACE
    01071
    01072 #endif