istream.tcc

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

Generated on Wed May 1 19:19:31 2002 for libstdc++-v3 Source by doxygen1.2.15