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