Home | History | Annotate | Download | only in bits
      1 // istream classes -*- C++ -*-
      2 
      3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
      4 // 2006, 2007, 2008, 2009, 2010, 2011
      5 // Free Software Foundation, Inc.
      6 //
      7 // This file is part of the GNU ISO C++ Library.  This library is free
      8 // software; you can redistribute it and/or modify it under the
      9 // terms of the GNU General Public License as published by the
     10 // Free Software Foundation; either version 3, or (at your option)
     11 // any later version.
     12 
     13 // This library is distributed in the hope that it will be useful,
     14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 // GNU General Public License for more details.
     17 
     18 // Under Section 7 of GPL version 3, you are granted additional
     19 // permissions described in the GCC Runtime Library Exception, version
     20 // 3.1, as published by the Free Software Foundation.
     21 
     22 // You should have received a copy of the GNU General Public License and
     23 // a copy of the GCC Runtime Library Exception along with this program;
     24 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     25 // <http://www.gnu.org/licenses/>.
     26 
     27 /** @file bits/istream.tcc
     28  *  This is an internal header file, included by other library headers.
     29  *  Do not attempt to use it directly. @headername{istream}
     30  */
     31 
     32 //
     33 // ISO C++ 14882: 27.6.1  Input streams
     34 //
     35 
     36 #ifndef _ISTREAM_TCC
     37 #define _ISTREAM_TCC 1
     38 
     39 #pragma GCC system_header
     40 
     41 #include <bits/cxxabi_forced.h>
     42 
     43 namespace std _GLIBCXX_VISIBILITY(default)
     44 {
     45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     46 
     47   template<typename _CharT, typename _Traits>
     48     basic_istream<_CharT, _Traits>::sentry::
     49     sentry(basic_istream<_CharT, _Traits>& __in, bool __noskip) : _M_ok(false)
     50     {
     51       ios_base::iostate __err = ios_base::goodbit;
     52       if (__in.good())
     53 	{
     54 	  if (__in.tie())
     55 	    __in.tie()->flush();
     56 	  if (!__noskip && bool(__in.flags() & ios_base::skipws))
     57 	    {
     58 	      const __int_type __eof = traits_type::eof();
     59 	      __streambuf_type* __sb = __in.rdbuf();
     60 	      __int_type __c = __sb->sgetc();
     61 
     62 	      const __ctype_type& __ct = __check_facet(__in._M_ctype);
     63 	      while (!traits_type::eq_int_type(__c, __eof)
     64 		     && __ct.is(ctype_base::space, 
     65 				traits_type::to_char_type(__c)))
     66 		__c = __sb->snextc();
     67 
     68 	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
     69 	      // 195. Should basic_istream::sentry's constructor ever
     70 	      // set eofbit?
     71 	      if (traits_type::eq_int_type(__c, __eof))
     72 		__err |= ios_base::eofbit;
     73 	    }
     74 	}
     75 
     76       if (__in.good() && __err == ios_base::goodbit)
     77 	_M_ok = true;
     78       else
     79 	{
     80 	  __err |= ios_base::failbit;
     81 	  __in.setstate(__err);
     82 	}
     83     }
     84 
     85   template<typename _CharT, typename _Traits>
     86     template<typename _ValueT>
     87       basic_istream<_CharT, _Traits>&
     88       basic_istream<_CharT, _Traits>::
     89       _M_extract(_ValueT& __v)
     90       {
     91 	sentry __cerb(*this, false);
     92 	if (__cerb)
     93 	  {
     94 	    ios_base::iostate __err = ios_base::goodbit;
     95 	    __try
     96 	      {
     97 		const __num_get_type& __ng = __check_facet(this->_M_num_get);
     98 		__ng.get(*this, 0, *this, __err, __v);
     99 	      }
    100 	    __catch(__cxxabiv1::__forced_unwind&)
    101 	      {
    102 		this->_M_setstate(ios_base::badbit);
    103 		__throw_exception_again;
    104 	      }
    105 	    __catch(...)
    106 	      { this->_M_setstate(ios_base::badbit); }
    107 	    if (__err)
    108 	      this->setstate(__err);
    109 	  }
    110 	return *this;
    111       }
    112 
    113   template<typename _CharT, typename _Traits>
    114     basic_istream<_CharT, _Traits>&
    115     basic_istream<_CharT, _Traits>::
    116     operator>>(short& __n)
    117     {
    118       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    119       // 118. basic_istream uses nonexistent num_get member functions.
    120       sentry __cerb(*this, false);
    121       if (__cerb)
    122 	{
    123 	  ios_base::iostate __err = ios_base::goodbit;
    124 	  __try
    125 	    {
    126 	      long __l;
    127 	      const __num_get_type& __ng = __check_facet(this->_M_num_get);
    128 	      __ng.get(*this, 0, *this, __err, __l);
    129 
    130 	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
    131 	      // 696. istream::operator>>(int&) broken.
    132 	      if (__l < __gnu_cxx::__numeric_traits<short>::__min)
    133 		{
    134 		  __err |= ios_base::failbit;
    135 		  __n = __gnu_cxx::__numeric_traits<short>::__min;
    136 		}
    137 	      else if (__l > __gnu_cxx::__numeric_traits<short>::__max)
    138 		{
    139 		  __err |= ios_base::failbit;
    140 		  __n = __gnu_cxx::__numeric_traits<short>::__max;
    141 		}
    142 	      else
    143 		__n = short(__l);
    144 	    }
    145 	  __catch(__cxxabiv1::__forced_unwind&)
    146 	    {
    147 	      this->_M_setstate(ios_base::badbit);
    148 	      __throw_exception_again;
    149 	    }
    150 	  __catch(...)
    151 	    { this->_M_setstate(ios_base::badbit); }
    152 	  if (__err)
    153 	    this->setstate(__err);
    154 	}
    155       return *this;
    156     }
    157 
    158   template<typename _CharT, typename _Traits>
    159     basic_istream<_CharT, _Traits>&
    160     basic_istream<_CharT, _Traits>::
    161     operator>>(int& __n)
    162     {
    163       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    164       // 118. basic_istream uses nonexistent num_get member functions.
    165       sentry __cerb(*this, false);
    166       if (__cerb)
    167 	{
    168 	  ios_base::iostate __err = ios_base::goodbit;
    169 	  __try
    170 	    {
    171 	      long __l;
    172 	      const __num_get_type& __ng = __check_facet(this->_M_num_get);
    173 	      __ng.get(*this, 0, *this, __err, __l);
    174 
    175 	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
    176 	      // 696. istream::operator>>(int&) broken.
    177 	      if (__l < __gnu_cxx::__numeric_traits<int>::__min)
    178 		{
    179 		  __err |= ios_base::failbit;
    180 		  __n = __gnu_cxx::__numeric_traits<int>::__min;
    181 		}
    182 	      else if (__l > __gnu_cxx::__numeric_traits<int>::__max)
    183 		{
    184 		  __err |= ios_base::failbit;	      
    185 		  __n = __gnu_cxx::__numeric_traits<int>::__max;
    186 		}
    187 	      else
    188 		__n = int(__l);
    189 	    }
    190 	  __catch(__cxxabiv1::__forced_unwind&)
    191 	    {
    192 	      this->_M_setstate(ios_base::badbit);
    193 	      __throw_exception_again;
    194 	    }
    195 	  __catch(...)
    196 	    { this->_M_setstate(ios_base::badbit); }
    197 	  if (__err)
    198 	    this->setstate(__err);
    199 	}
    200       return *this;
    201     }
    202 
    203   template<typename _CharT, typename _Traits>
    204     basic_istream<_CharT, _Traits>&
    205     basic_istream<_CharT, _Traits>::
    206     operator>>(__streambuf_type* __sbout)
    207     {
    208       ios_base::iostate __err = ios_base::goodbit;
    209       sentry __cerb(*this, false);
    210       if (__cerb && __sbout)
    211 	{
    212 	  __try
    213 	    {
    214 	      bool __ineof;
    215 	      if (!__copy_streambufs_eof(this->rdbuf(), __sbout, __ineof))
    216 		__err |= ios_base::failbit;
    217 	      if (__ineof)
    218 		__err |= ios_base::eofbit;
    219 	    }
    220 	  __catch(__cxxabiv1::__forced_unwind&)
    221 	    {
    222 	      this->_M_setstate(ios_base::failbit);
    223 	      __throw_exception_again;
    224 	    }
    225 	  __catch(...)
    226 	    { this->_M_setstate(ios_base::failbit); }
    227 	}
    228       else if (!__sbout)
    229 	__err |= ios_base::failbit;
    230       if (__err)
    231 	this->setstate(__err);
    232       return *this;
    233     }
    234 
    235   template<typename _CharT, typename _Traits>
    236     typename basic_istream<_CharT, _Traits>::int_type
    237     basic_istream<_CharT, _Traits>::
    238     get(void)
    239     {
    240       const int_type __eof = traits_type::eof();
    241       int_type __c = __eof;
    242       _M_gcount = 0;
    243       ios_base::iostate __err = ios_base::goodbit;
    244       sentry __cerb(*this, true);
    245       if (__cerb)
    246 	{
    247 	  __try
    248 	    {
    249 	      __c = this->rdbuf()->sbumpc();
    250 	      // 27.6.1.1 paragraph 3
    251 	      if (!traits_type::eq_int_type(__c, __eof))
    252 		_M_gcount = 1;
    253 	      else
    254 		__err |= ios_base::eofbit;
    255 	    }
    256 	  __catch(__cxxabiv1::__forced_unwind&)
    257 	    {
    258 	      this->_M_setstate(ios_base::badbit);
    259 	      __throw_exception_again;
    260 	    }
    261 	  __catch(...)
    262 	    { this->_M_setstate(ios_base::badbit); }
    263 	}
    264       if (!_M_gcount)
    265 	__err |= ios_base::failbit;
    266       if (__err)
    267 	this->setstate(__err);
    268       return __c;
    269     }
    270 
    271   template<typename _CharT, typename _Traits>
    272     basic_istream<_CharT, _Traits>&
    273     basic_istream<_CharT, _Traits>::
    274     get(char_type& __c)
    275     {
    276       _M_gcount = 0;
    277       ios_base::iostate __err = ios_base::goodbit;
    278       sentry __cerb(*this, true);
    279       if (__cerb)
    280 	{
    281 	  __try
    282 	    {
    283 	      const int_type __cb = this->rdbuf()->sbumpc();
    284 	      // 27.6.1.1 paragraph 3
    285 	      if (!traits_type::eq_int_type(__cb, traits_type::eof()))
    286 		{
    287 		  _M_gcount = 1;
    288 		  __c = traits_type::to_char_type(__cb);
    289 		}
    290 	      else
    291 		__err |= ios_base::eofbit;
    292 	    }
    293 	  __catch(__cxxabiv1::__forced_unwind&)
    294 	    {
    295 	      this->_M_setstate(ios_base::badbit);
    296 	      __throw_exception_again;
    297 	    }
    298 	  __catch(...)
    299 	    { this->_M_setstate(ios_base::badbit); }
    300 	}
    301       if (!_M_gcount)
    302 	__err |= ios_base::failbit;
    303       if (__err)
    304 	this->setstate(__err);
    305       return *this;
    306     }
    307 
    308   template<typename _CharT, typename _Traits>
    309     basic_istream<_CharT, _Traits>&
    310     basic_istream<_CharT, _Traits>::
    311     get(char_type* __s, streamsize __n, char_type __delim)
    312     {
    313       _M_gcount = 0;
    314       ios_base::iostate __err = ios_base::goodbit;
    315       sentry __cerb(*this, true);
    316       if (__cerb)
    317 	{
    318 	  __try
    319 	    {
    320 	      const int_type __idelim = traits_type::to_int_type(__delim);
    321 	      const int_type __eof = traits_type::eof();
    322 	      __streambuf_type* __sb = this->rdbuf();
    323 	      int_type __c = __sb->sgetc();
    324 
    325 	      while (_M_gcount + 1 < __n
    326 		     && !traits_type::eq_int_type(__c, __eof)
    327 		     && !traits_type::eq_int_type(__c, __idelim))
    328 		{
    329 		  *__s++ = traits_type::to_char_type(__c);
    330 		  ++_M_gcount;
    331 		  __c = __sb->snextc();
    332 		}
    333 	      if (traits_type::eq_int_type(__c, __eof))
    334 		__err |= ios_base::eofbit;
    335 	    }
    336 	  __catch(__cxxabiv1::__forced_unwind&)
    337 	    {
    338 	      this->_M_setstate(ios_base::badbit);
    339 	      __throw_exception_again;
    340 	    }
    341 	  __catch(...)
    342 	    { this->_M_setstate(ios_base::badbit); }
    343 	}
    344       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    345       // 243. get and getline when sentry reports failure.
    346       if (__n > 0)
    347 	*__s = char_type();
    348       if (!_M_gcount)
    349 	__err |= ios_base::failbit;
    350       if (__err)
    351 	this->setstate(__err);
    352       return *this;
    353     }
    354 
    355   template<typename _CharT, typename _Traits>
    356     basic_istream<_CharT, _Traits>&
    357     basic_istream<_CharT, _Traits>::
    358     get(__streambuf_type& __sb, char_type __delim)
    359     {
    360       _M_gcount = 0;
    361       ios_base::iostate __err = ios_base::goodbit;
    362       sentry __cerb(*this, true);
    363       if (__cerb)
    364 	{
    365 	  __try
    366 	    {
    367 	      const int_type __idelim = traits_type::to_int_type(__delim);
    368 	      const int_type __eof = traits_type::eof();
    369 	      __streambuf_type* __this_sb = this->rdbuf();
    370 	      int_type __c = __this_sb->sgetc();
    371 	      char_type __c2 = traits_type::to_char_type(__c);
    372 
    373 	      while (!traits_type::eq_int_type(__c, __eof)
    374 		     && !traits_type::eq_int_type(__c, __idelim)
    375 		     && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
    376 		{
    377 		  ++_M_gcount;
    378 		  __c = __this_sb->snextc();
    379 		  __c2 = traits_type::to_char_type(__c);
    380 		}
    381 	      if (traits_type::eq_int_type(__c, __eof))
    382 		__err |= ios_base::eofbit;
    383 	    }
    384 	  __catch(__cxxabiv1::__forced_unwind&)
    385 	    {
    386 	      this->_M_setstate(ios_base::badbit);
    387 	      __throw_exception_again;
    388 	    }
    389 	  __catch(...)
    390 	    { this->_M_setstate(ios_base::badbit); }
    391 	}
    392       if (!_M_gcount)
    393 	__err |= ios_base::failbit;
    394       if (__err)
    395 	this->setstate(__err);
    396       return *this;
    397     }
    398 
    399   template<typename _CharT, typename _Traits>
    400     basic_istream<_CharT, _Traits>&
    401     basic_istream<_CharT, _Traits>::
    402     getline(char_type* __s, streamsize __n, char_type __delim)
    403     {
    404       _M_gcount = 0;
    405       ios_base::iostate __err = ios_base::goodbit;
    406       sentry __cerb(*this, true);
    407       if (__cerb)
    408         {
    409           __try
    410             {
    411               const int_type __idelim = traits_type::to_int_type(__delim);
    412               const int_type __eof = traits_type::eof();
    413               __streambuf_type* __sb = this->rdbuf();
    414               int_type __c = __sb->sgetc();
    415 
    416               while (_M_gcount + 1 < __n
    417                      && !traits_type::eq_int_type(__c, __eof)
    418                      && !traits_type::eq_int_type(__c, __idelim))
    419                 {
    420                   *__s++ = traits_type::to_char_type(__c);
    421                   __c = __sb->snextc();
    422                   ++_M_gcount;
    423                 }
    424               if (traits_type::eq_int_type(__c, __eof))
    425                 __err |= ios_base::eofbit;
    426               else
    427                 {
    428                   if (traits_type::eq_int_type(__c, __idelim))
    429                     {
    430                       __sb->sbumpc();
    431                       ++_M_gcount;
    432                     }
    433                   else
    434                     __err |= ios_base::failbit;
    435                 }
    436             }
    437 	  __catch(__cxxabiv1::__forced_unwind&)
    438 	    {
    439 	      this->_M_setstate(ios_base::badbit);
    440 	      __throw_exception_again;
    441 	    }
    442           __catch(...)
    443             { this->_M_setstate(ios_base::badbit); }
    444         }
    445       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    446       // 243. get and getline when sentry reports failure.
    447       if (__n > 0)
    448 	*__s = char_type();
    449       if (!_M_gcount)
    450         __err |= ios_base::failbit;
    451       if (__err)
    452         this->setstate(__err);
    453       return *this;
    454     }
    455 
    456   // We provide three overloads, since the first two are much simpler
    457   // than the general case. Also, the latter two can thus adopt the
    458   // same "batchy" strategy used by getline above.
    459   template<typename _CharT, typename _Traits>
    460     basic_istream<_CharT, _Traits>&
    461     basic_istream<_CharT, _Traits>::
    462     ignore(void)
    463     {
    464       _M_gcount = 0;
    465       sentry __cerb(*this, true);
    466       if (__cerb)
    467 	{
    468 	  ios_base::iostate __err = ios_base::goodbit;
    469 	  __try
    470 	    {
    471 	      const int_type __eof = traits_type::eof();
    472 	      __streambuf_type* __sb = this->rdbuf();
    473 
    474 	      if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
    475 		__err |= ios_base::eofbit;
    476 	      else
    477 		_M_gcount = 1;
    478 	    }
    479 	  __catch(__cxxabiv1::__forced_unwind&)
    480 	    {
    481 	      this->_M_setstate(ios_base::badbit);
    482 	      __throw_exception_again;
    483 	    }
    484 	  __catch(...)
    485 	    { this->_M_setstate(ios_base::badbit); }
    486 	  if (__err)
    487 	    this->setstate(__err);
    488 	}
    489       return *this;
    490     }
    491 
    492   template<typename _CharT, typename _Traits>
    493     basic_istream<_CharT, _Traits>&
    494     basic_istream<_CharT, _Traits>::
    495     ignore(streamsize __n)
    496     {
    497       _M_gcount = 0;
    498       sentry __cerb(*this, true);
    499       if (__cerb && __n > 0)
    500         {
    501           ios_base::iostate __err = ios_base::goodbit;
    502           __try
    503             {
    504               const int_type __eof = traits_type::eof();
    505               __streambuf_type* __sb = this->rdbuf();
    506               int_type __c = __sb->sgetc();
    507 
    508 	      // N.B. On LFS-enabled platforms streamsize is still 32 bits
    509 	      // wide: if we want to implement the standard mandated behavior
    510 	      // for n == max() (see 27.6.1.3/24) we are at risk of signed
    511 	      // integer overflow: thus these contortions. Also note that,
    512 	      // by definition, when more than 2G chars are actually ignored,
    513 	      // _M_gcount (the return value of gcount, that is) cannot be
    514 	      // really correct, being unavoidably too small.
    515 	      bool __large_ignore = false;
    516 	      while (true)
    517 		{
    518 		  while (_M_gcount < __n
    519 			 && !traits_type::eq_int_type(__c, __eof))
    520 		    {
    521 		      ++_M_gcount;
    522 		      __c = __sb->snextc();
    523 		    }
    524 		  if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
    525 		      && !traits_type::eq_int_type(__c, __eof))
    526 		    {
    527 		      _M_gcount =
    528 			__gnu_cxx::__numeric_traits<streamsize>::__min;
    529 		      __large_ignore = true;
    530 		    }
    531 		  else
    532 		    break;
    533 		}
    534 
    535 	      if (__large_ignore)
    536 		_M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
    537 
    538 	      if (traits_type::eq_int_type(__c, __eof))
    539                 __err |= ios_base::eofbit;
    540             }
    541 	  __catch(__cxxabiv1::__forced_unwind&)
    542 	    {
    543 	      this->_M_setstate(ios_base::badbit);
    544 	      __throw_exception_again;
    545 	    }
    546           __catch(...)
    547             { this->_M_setstate(ios_base::badbit); }
    548           if (__err)
    549             this->setstate(__err);
    550         }
    551       return *this;
    552     }
    553 
    554   template<typename _CharT, typename _Traits>
    555     basic_istream<_CharT, _Traits>&
    556     basic_istream<_CharT, _Traits>::
    557     ignore(streamsize __n, int_type __delim)
    558     {
    559       _M_gcount = 0;
    560       sentry __cerb(*this, true);
    561       if (__cerb && __n > 0)
    562         {
    563           ios_base::iostate __err = ios_base::goodbit;
    564           __try
    565             {
    566               const int_type __eof = traits_type::eof();
    567               __streambuf_type* __sb = this->rdbuf();
    568               int_type __c = __sb->sgetc();
    569 
    570 	      // See comment above.
    571 	      bool __large_ignore = false;
    572 	      while (true)
    573 		{
    574 		  while (_M_gcount < __n
    575 			 && !traits_type::eq_int_type(__c, __eof)
    576 			 && !traits_type::eq_int_type(__c, __delim))
    577 		    {
    578 		      ++_M_gcount;
    579 		      __c = __sb->snextc();
    580 		    }
    581 		  if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
    582 		      && !traits_type::eq_int_type(__c, __eof)
    583 		      && !traits_type::eq_int_type(__c, __delim))
    584 		    {
    585 		      _M_gcount =
    586 			__gnu_cxx::__numeric_traits<streamsize>::__min;
    587 		      __large_ignore = true;
    588 		    }
    589 		  else
    590 		    break;
    591 		}
    592 
    593 	      if (__large_ignore)
    594 		_M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
    595 
    596               if (traits_type::eq_int_type(__c, __eof))
    597                 __err |= ios_base::eofbit;
    598 	      else if (traits_type::eq_int_type(__c, __delim))
    599 		{
    600 		  if (_M_gcount
    601 		      < __gnu_cxx::__numeric_traits<streamsize>::__max)
    602 		    ++_M_gcount;
    603 		  __sb->sbumpc();
    604 		}
    605             }
    606 	  __catch(__cxxabiv1::__forced_unwind&)
    607 	    {
    608 	      this->_M_setstate(ios_base::badbit);
    609 	      __throw_exception_again;
    610 	    }
    611           __catch(...)
    612             { this->_M_setstate(ios_base::badbit); }
    613           if (__err)
    614             this->setstate(__err);
    615         }
    616       return *this;
    617     }
    618 
    619   template<typename _CharT, typename _Traits>
    620     typename basic_istream<_CharT, _Traits>::int_type
    621     basic_istream<_CharT, _Traits>::
    622     peek(void)
    623     {
    624       int_type __c = traits_type::eof();
    625       _M_gcount = 0;
    626       sentry __cerb(*this, true);
    627       if (__cerb)
    628 	{
    629 	  ios_base::iostate __err = ios_base::goodbit;
    630 	  __try
    631 	    {
    632 	      __c = this->rdbuf()->sgetc();
    633 	      if (traits_type::eq_int_type(__c, traits_type::eof()))
    634 		__err |= ios_base::eofbit;
    635 	    }
    636 	  __catch(__cxxabiv1::__forced_unwind&)
    637 	    {
    638 	      this->_M_setstate(ios_base::badbit);
    639 	      __throw_exception_again;
    640 	    }
    641 	  __catch(...)
    642 	    { this->_M_setstate(ios_base::badbit); }
    643 	  if (__err)
    644 	    this->setstate(__err);
    645 	}
    646       return __c;
    647     }
    648 
    649   template<typename _CharT, typename _Traits>
    650     basic_istream<_CharT, _Traits>&
    651     basic_istream<_CharT, _Traits>::
    652     read(char_type* __s, streamsize __n)
    653     {
    654       _M_gcount = 0;
    655       sentry __cerb(*this, true);
    656       if (__cerb)
    657 	{
    658 	  ios_base::iostate __err = ios_base::goodbit;
    659 	  __try
    660 	    {
    661 	      _M_gcount = this->rdbuf()->sgetn(__s, __n);
    662 	      if (_M_gcount != __n)
    663 		__err |= (ios_base::eofbit | ios_base::failbit);
    664 	    }
    665 	  __catch(__cxxabiv1::__forced_unwind&)
    666 	    {
    667 	      this->_M_setstate(ios_base::badbit);
    668 	      __throw_exception_again;
    669 	    }
    670 	  __catch(...)
    671 	    { this->_M_setstate(ios_base::badbit); }
    672 	  if (__err)
    673 	    this->setstate(__err);
    674 	}
    675       return *this;
    676     }
    677 
    678   template<typename _CharT, typename _Traits>
    679     streamsize
    680     basic_istream<_CharT, _Traits>::
    681     readsome(char_type* __s, streamsize __n)
    682     {
    683       _M_gcount = 0;
    684       sentry __cerb(*this, true);
    685       if (__cerb)
    686 	{
    687 	  ios_base::iostate __err = ios_base::goodbit;
    688 	  __try
    689 	    {
    690 	      // Cannot compare int_type with streamsize generically.
    691 	      const streamsize __num = this->rdbuf()->in_avail();
    692 	      if (__num > 0)
    693 		_M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
    694 	      else if (__num == -1)
    695 		__err |= ios_base::eofbit;
    696 	    }
    697 	  __catch(__cxxabiv1::__forced_unwind&)
    698 	    {
    699 	      this->_M_setstate(ios_base::badbit);
    700 	      __throw_exception_again;
    701 	    }
    702 	  __catch(...)
    703 	    { this->_M_setstate(ios_base::badbit); }
    704 	  if (__err)
    705 	    this->setstate(__err);
    706 	}
    707       return _M_gcount;
    708     }
    709 
    710   template<typename _CharT, typename _Traits>
    711     basic_istream<_CharT, _Traits>&
    712     basic_istream<_CharT, _Traits>::
    713     putback(char_type __c)
    714     {
    715       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    716       // 60. What is a formatted input function?
    717       _M_gcount = 0;
    718       // Clear eofbit per N3168.
    719       this->clear(this->rdstate() & ~ios_base::eofbit);
    720       sentry __cerb(*this, true);
    721       if (__cerb)
    722 	{
    723 	  ios_base::iostate __err = ios_base::goodbit;
    724 	  __try
    725 	    {
    726 	      const int_type __eof = traits_type::eof();
    727 	      __streambuf_type* __sb = this->rdbuf();
    728 	      if (!__sb
    729 		  || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
    730 		__err |= ios_base::badbit;
    731 	    }
    732 	  __catch(__cxxabiv1::__forced_unwind&)
    733 	    {
    734 	      this->_M_setstate(ios_base::badbit);
    735 	      __throw_exception_again;
    736 	    }
    737 	  __catch(...)
    738 	    { this->_M_setstate(ios_base::badbit); }
    739 	  if (__err)
    740 	    this->setstate(__err);
    741 	}
    742       return *this;
    743     }
    744 
    745   template<typename _CharT, typename _Traits>
    746     basic_istream<_CharT, _Traits>&
    747     basic_istream<_CharT, _Traits>::
    748     unget(void)
    749     {
    750       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    751       // 60. What is a formatted input function?
    752       _M_gcount = 0;
    753       // Clear eofbit per N3168.
    754       this->clear(this->rdstate() & ~ios_base::eofbit);
    755       sentry __cerb(*this, true);
    756       if (__cerb)
    757 	{
    758 	  ios_base::iostate __err = ios_base::goodbit;
    759 	  __try
    760 	    {
    761 	      const int_type __eof = traits_type::eof();
    762 	      __streambuf_type* __sb = this->rdbuf();
    763 	      if (!__sb
    764 		  || traits_type::eq_int_type(__sb->sungetc(), __eof))
    765 		__err |= ios_base::badbit;
    766 	    }
    767 	  __catch(__cxxabiv1::__forced_unwind&)
    768 	    {
    769 	      this->_M_setstate(ios_base::badbit);
    770 	      __throw_exception_again;
    771 	    }
    772 	  __catch(...)
    773 	    { this->_M_setstate(ios_base::badbit); }
    774 	  if (__err)
    775 	    this->setstate(__err);
    776 	}
    777       return *this;
    778     }
    779 
    780   template<typename _CharT, typename _Traits>
    781     int
    782     basic_istream<_CharT, _Traits>::
    783     sync(void)
    784     {
    785       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    786       // DR60.  Do not change _M_gcount.
    787       int __ret = -1;
    788       sentry __cerb(*this, true);
    789       if (__cerb)
    790 	{
    791 	  ios_base::iostate __err = ios_base::goodbit;
    792 	  __try
    793 	    {
    794 	      __streambuf_type* __sb = this->rdbuf();
    795 	      if (__sb)
    796 		{
    797 		  if (__sb->pubsync() == -1)
    798 		    __err |= ios_base::badbit;
    799 		  else
    800 		    __ret = 0;
    801 		}
    802 	    }
    803 	  __catch(__cxxabiv1::__forced_unwind&)
    804 	    {
    805 	      this->_M_setstate(ios_base::badbit);
    806 	      __throw_exception_again;
    807 	    }
    808 	  __catch(...)
    809 	    { this->_M_setstate(ios_base::badbit); }
    810 	  if (__err)
    811 	    this->setstate(__err);
    812 	}
    813       return __ret;
    814     }
    815 
    816   template<typename _CharT, typename _Traits>
    817     typename basic_istream<_CharT, _Traits>::pos_type
    818     basic_istream<_CharT, _Traits>::
    819     tellg(void)
    820     {
    821       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    822       // DR60.  Do not change _M_gcount.
    823       pos_type __ret = pos_type(-1);
    824       sentry __cerb(*this, true);
    825       if (__cerb)
    826 	{
    827 	  __try
    828 	    {
    829 	      if (!this->fail())
    830 		__ret = this->rdbuf()->pubseekoff(0, ios_base::cur,
    831 						  ios_base::in);
    832 	    }
    833 	  __catch(__cxxabiv1::__forced_unwind&)
    834 	    {
    835 	      this->_M_setstate(ios_base::badbit);
    836 	      __throw_exception_again;
    837 	    }
    838 	  __catch(...)
    839 	    { this->_M_setstate(ios_base::badbit); }
    840 	}
    841       return __ret;
    842     }
    843 
    844   template<typename _CharT, typename _Traits>
    845     basic_istream<_CharT, _Traits>&
    846     basic_istream<_CharT, _Traits>::
    847     seekg(pos_type __pos)
    848     {
    849       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    850       // DR60.  Do not change _M_gcount.
    851       // Clear eofbit per N3168.
    852       this->clear(this->rdstate() & ~ios_base::eofbit);
    853       sentry __cerb(*this, true);
    854       if (__cerb)
    855 	{
    856 	  ios_base::iostate __err = ios_base::goodbit;
    857 	  __try
    858 	    {
    859 	      if (!this->fail())
    860 		{
    861 		  // 136.  seekp, seekg setting wrong streams?
    862 		  const pos_type __p = this->rdbuf()->pubseekpos(__pos,
    863 								 ios_base::in);
    864 		  
    865 		  // 129.  Need error indication from seekp() and seekg()
    866 		  if (__p == pos_type(off_type(-1)))
    867 		    __err |= ios_base::failbit;
    868 		}
    869 	    }
    870 	  __catch(__cxxabiv1::__forced_unwind&)
    871 	    {
    872 	      this->_M_setstate(ios_base::badbit);
    873 	      __throw_exception_again;
    874 	    }
    875 	  __catch(...)
    876 	    { this->_M_setstate(ios_base::badbit); }
    877 	  if (__err)
    878 	    this->setstate(__err);
    879 	}
    880       return *this;
    881     }
    882 
    883   template<typename _CharT, typename _Traits>
    884     basic_istream<_CharT, _Traits>&
    885     basic_istream<_CharT, _Traits>::
    886     seekg(off_type __off, ios_base::seekdir __dir)
    887     {
    888       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    889       // DR60.  Do not change _M_gcount.
    890       // Clear eofbit per N3168.
    891       this->clear(this->rdstate() & ~ios_base::eofbit);
    892       sentry __cerb(*this, true);
    893       if (__cerb)
    894 	{
    895 	  ios_base::iostate __err = ios_base::goodbit;
    896 	  __try
    897 	    {
    898 	      if (!this->fail())
    899 		{
    900 		  // 136.  seekp, seekg setting wrong streams?
    901 		  const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
    902 								 ios_base::in);
    903 	      
    904 		  // 129.  Need error indication from seekp() and seekg()
    905 		  if (__p == pos_type(off_type(-1)))
    906 		    __err |= ios_base::failbit;
    907 		}
    908 	    }
    909 	  __catch(__cxxabiv1::__forced_unwind&)
    910 	    {
    911 	      this->_M_setstate(ios_base::badbit);
    912 	      __throw_exception_again;
    913 	    }
    914 	  __catch(...)
    915 	    { this->_M_setstate(ios_base::badbit); }
    916 	  if (__err)
    917 	    this->setstate(__err);
    918 	}
    919       return *this;
    920     }
    921 
    922   // 27.6.1.2.3 Character extraction templates
    923   template<typename _CharT, typename _Traits>
    924     basic_istream<_CharT, _Traits>&
    925     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
    926     {
    927       typedef basic_istream<_CharT, _Traits>		__istream_type;
    928       typedef typename __istream_type::int_type         __int_type;
    929 
    930       typename __istream_type::sentry __cerb(__in, false);
    931       if (__cerb)
    932 	{
    933 	  ios_base::iostate __err = ios_base::goodbit;
    934 	  __try
    935 	    {
    936 	      const __int_type __cb = __in.rdbuf()->sbumpc();
    937 	      if (!_Traits::eq_int_type(__cb, _Traits::eof()))
    938 		__c = _Traits::to_char_type(__cb);
    939 	      else
    940 		__err |= (ios_base::eofbit | ios_base::failbit);
    941 	    }
    942 	  __catch(__cxxabiv1::__forced_unwind&)
    943 	    {
    944 	      __in._M_setstate(ios_base::badbit);
    945 	      __throw_exception_again;
    946 	    }
    947 	  __catch(...)
    948 	    { __in._M_setstate(ios_base::badbit); }
    949 	  if (__err)
    950 	    __in.setstate(__err);
    951 	}
    952       return __in;
    953     }
    954 
    955   template<typename _CharT, typename _Traits>
    956     basic_istream<_CharT, _Traits>&
    957     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
    958     {
    959       typedef basic_istream<_CharT, _Traits>		__istream_type;
    960       typedef basic_streambuf<_CharT, _Traits>          __streambuf_type;
    961       typedef typename _Traits::int_type		int_type;
    962       typedef _CharT					char_type;
    963       typedef ctype<_CharT>				__ctype_type;
    964 
    965       streamsize __extracted = 0;
    966       ios_base::iostate __err = ios_base::goodbit;
    967       typename __istream_type::sentry __cerb(__in, false);
    968       if (__cerb)
    969 	{
    970 	  __try
    971 	    {
    972 	      // Figure out how many characters to extract.
    973 	      streamsize __num = __in.width();
    974 	      if (__num <= 0)
    975 		__num = __gnu_cxx::__numeric_traits<streamsize>::__max;
    976 
    977 	      const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
    978 
    979 	      const int_type __eof = _Traits::eof();
    980 	      __streambuf_type* __sb = __in.rdbuf();
    981 	      int_type __c = __sb->sgetc();
    982 
    983 	      while (__extracted < __num - 1
    984 		     && !_Traits::eq_int_type(__c, __eof)
    985 		     && !__ct.is(ctype_base::space,
    986 				 _Traits::to_char_type(__c)))
    987 		{
    988 		  *__s++ = _Traits::to_char_type(__c);
    989 		  ++__extracted;
    990 		  __c = __sb->snextc();
    991 		}
    992 	      if (_Traits::eq_int_type(__c, __eof))
    993 		__err |= ios_base::eofbit;
    994 
    995 	      // _GLIBCXX_RESOLVE_LIB_DEFECTS
    996 	      // 68.  Extractors for char* should store null at end
    997 	      *__s = char_type();
    998 	      __in.width(0);
    999 	    }
   1000 	  __catch(__cxxabiv1::__forced_unwind&)
   1001 	    {
   1002 	      __in._M_setstate(ios_base::badbit);
   1003 	      __throw_exception_again;
   1004 	    }
   1005 	  __catch(...)
   1006 	    { __in._M_setstate(ios_base::badbit); }
   1007 	}
   1008       if (!__extracted)
   1009 	__err |= ios_base::failbit;
   1010       if (__err)
   1011 	__in.setstate(__err);
   1012       return __in;
   1013     }
   1014 
   1015   // 27.6.1.4 Standard basic_istream manipulators
   1016   template<typename _CharT, typename _Traits>
   1017     basic_istream<_CharT, _Traits>&
   1018     ws(basic_istream<_CharT, _Traits>& __in)
   1019     {
   1020       typedef basic_istream<_CharT, _Traits>		__istream_type;
   1021       typedef basic_streambuf<_CharT, _Traits>          __streambuf_type;
   1022       typedef typename __istream_type::int_type		__int_type;
   1023       typedef ctype<_CharT>				__ctype_type;
   1024 
   1025       const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
   1026       const __int_type __eof = _Traits::eof();
   1027       __streambuf_type* __sb = __in.rdbuf();
   1028       __int_type __c = __sb->sgetc();
   1029 
   1030       while (!_Traits::eq_int_type(__c, __eof)
   1031 	     && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
   1032 	__c = __sb->snextc();
   1033 
   1034        if (_Traits::eq_int_type(__c, __eof))
   1035 	 __in.setstate(ios_base::eofbit);
   1036       return __in;
   1037     }
   1038 
   1039   // Inhibit implicit instantiations for required instantiations,
   1040   // which are defined via explicit instantiations elsewhere.
   1041 #if _GLIBCXX_EXTERN_TEMPLATE
   1042   extern template class basic_istream<char>;
   1043   extern template istream& ws(istream&);
   1044   extern template istream& operator>>(istream&, char&);
   1045   extern template istream& operator>>(istream&, char*);
   1046   extern template istream& operator>>(istream&, unsigned char&);
   1047   extern template istream& operator>>(istream&, signed char&);
   1048   extern template istream& operator>>(istream&, unsigned char*);
   1049   extern template istream& operator>>(istream&, signed char*);
   1050 
   1051   extern template istream& istream::_M_extract(unsigned short&);
   1052   extern template istream& istream::_M_extract(unsigned int&);  
   1053   extern template istream& istream::_M_extract(long&);
   1054   extern template istream& istream::_M_extract(unsigned long&);
   1055   extern template istream& istream::_M_extract(bool&);
   1056 #ifdef _GLIBCXX_USE_LONG_LONG
   1057   extern template istream& istream::_M_extract(long long&);
   1058   extern template istream& istream::_M_extract(unsigned long long&);
   1059 #endif
   1060   extern template istream& istream::_M_extract(float&);
   1061   extern template istream& istream::_M_extract(double&);
   1062   extern template istream& istream::_M_extract(long double&);
   1063   extern template istream& istream::_M_extract(void*&);
   1064 
   1065   extern template class basic_iostream<char>;
   1066 
   1067 #ifdef _GLIBCXX_USE_WCHAR_T
   1068   extern template class basic_istream<wchar_t>;
   1069   extern template wistream& ws(wistream&);
   1070   extern template wistream& operator>>(wistream&, wchar_t&);
   1071   extern template wistream& operator>>(wistream&, wchar_t*);
   1072 
   1073   extern template wistream& wistream::_M_extract(unsigned short&);
   1074   extern template wistream& wistream::_M_extract(unsigned int&);  
   1075   extern template wistream& wistream::_M_extract(long&);
   1076   extern template wistream& wistream::_M_extract(unsigned long&);
   1077   extern template wistream& wistream::_M_extract(bool&);
   1078 #ifdef _GLIBCXX_USE_LONG_LONG
   1079   extern template wistream& wistream::_M_extract(long long&);
   1080   extern template wistream& wistream::_M_extract(unsigned long long&);
   1081 #endif
   1082   extern template wistream& wistream::_M_extract(float&);
   1083   extern template wistream& wistream::_M_extract(double&);
   1084   extern template wistream& wistream::_M_extract(long double&);
   1085   extern template wistream& wistream::_M_extract(void*&);
   1086 
   1087   extern template class basic_iostream<wchar_t>;
   1088 #endif
   1089 #endif
   1090 
   1091 _GLIBCXX_END_NAMESPACE_VERSION
   1092 } // namespace std
   1093 
   1094 #endif
   1095