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

Generated on Fri Oct 20 15:45:41 2006 for libstdc++-v3 Source by  doxygen 1.4.7