1 // istream classes -*- C++ -*-
2 
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30 
31 //
32 // ISO C++ 14882: 27.6.2  Output streams
33 //
34 
35 #pragma GCC system_header
36 
37 #include <locale>
38 #include <ostream> // For flush()
39 
40 namespace std
41 {
42   template<typename _CharT, typename _Traits>
43     basic_istream<_CharT, _Traits>::sentry::
44     sentry(basic_istream<_CharT, _Traits>& __in, bool __noskipws)
45     {
46       if (__in.good())
47 	{
48 	  if (__in.tie())
49 	    __in.tie()->flush();
50 	  if (!__noskipws && (__in.flags() & ios_base::skipws))
51 	    {
52 	      const __int_type __eof = traits_type::eof();
53 	      __streambuf_type* __sb = __in.rdbuf();
54 	      __int_type __c = __sb->sgetc();
55 
56 	      if (__in._M_check_facet(__in._M_fctype))
57 		while (!traits_type::eq_int_type(__c, __eof)
58 		       && __in._M_fctype->is(ctype_base::space,
59 					     traits_type::to_char_type(__c)))
60 		  __c = __sb->snextc();
61 
62 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
63 //195.  Should basic_istream::sentry's constructor ever set eofbit?
64 	      if (traits_type::eq_int_type(__c, __eof))
65 		__in.setstate(ios_base::eofbit);
66 #endif
67 	    }
68 	}
69 
70       if (__in.good())
71 	_M_ok = true;
72       else
73 	{
74 	  _M_ok = false;
75 	  __in.setstate(ios_base::failbit);
76 	}
77     }
78 
79   template<typename _CharT, typename _Traits>
80     basic_istream<_CharT, _Traits>&
81     basic_istream<_CharT, _Traits>::
82     operator>>(__istream_type& (*__pf)(__istream_type&))
83     {
84       __pf(*this);
85       return *this;
86     }
87 
88   template<typename _CharT, typename _Traits>
89     basic_istream<_CharT, _Traits>&
90     basic_istream<_CharT, _Traits>::
91     operator>>(__ios_type& (*__pf)(__ios_type&))
92     {
93       __pf(*this);
94       return *this;
95     }
96 
97   template<typename _CharT, typename _Traits>
98     basic_istream<_CharT, _Traits>&
99     basic_istream<_CharT, _Traits>::
100     operator>>(ios_base& (*__pf)(ios_base&))
101     {
102       __pf(*this);
103       return *this;
104     }
105 
106   template<typename _CharT, typename _Traits>
107     basic_istream<_CharT, _Traits>&
108     basic_istream<_CharT, _Traits>::
109     operator>>(bool& __n)
110     {
111       sentry __cerb(*this, false);
112       if (__cerb)
113 	{
114 	  try
115 	    {
116 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
117 	      if (_M_check_facet(_M_fnumget))
118 		_M_fnumget->get(*this, 0, *this, __err, __n);
119 	      this->setstate(__err);
120 	    }
121 	  catch(exception& __fail)
122 	    {
123 	      // 27.6.1.2.1 Common requirements.
124 	      // Turn this on without causing an ios::failure to be thrown.
125 	      this->setstate(ios_base::badbit);
126 	      if ((this->exceptions() & ios_base::badbit) != 0)
127 		__throw_exception_again;
128 	    }
129 	}
130       return *this;
131     }
132 
133   template<typename _CharT, typename _Traits>
134     basic_istream<_CharT, _Traits>&
135     basic_istream<_CharT, _Traits>::
136     operator>>(short& __n)
137     {
138       sentry __cerb(*this, false);
139       if (__cerb)
140 	{
141 	  try
142 	    {
143 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
144 	      long __l;
145 	      if (_M_check_facet(_M_fnumget))
146 		_M_fnumget->get(*this, 0, *this, __err, __l);
147 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
148 	      // 118. basic_istream uses nonexistent num_get member functions.
149 	      if (!(__err & ios_base::failbit)
150 		  && (numeric_limits<short>::min() <= __l
151 		      && __l <= numeric_limits<short>::max()))
152 		__n = __l;
153 	      else
154                 __err |= ios_base::failbit;
155 #endif
156 	      this->setstate(__err);
157 	    }
158 	  catch(exception& __fail)
159 	    {
160 	      // 27.6.1.2.1 Common requirements.
161 	      // Turn this on without causing an ios::failure to be thrown.
162 	      this->setstate(ios_base::badbit);
163 	      if ((this->exceptions() & ios_base::badbit) != 0)
164 		__throw_exception_again;
165 	    }
166 	}
167       return *this;
168     }
169 
170   template<typename _CharT, typename _Traits>
171     basic_istream<_CharT, _Traits>&
172     basic_istream<_CharT, _Traits>::
173     operator>>(unsigned short& __n)
174     {
175       sentry __cerb(*this, false);
176       if (__cerb)
177 	{
178 	  try
179 	    {
180 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
181 	      if (_M_check_facet(_M_fnumget))
182 		_M_fnumget->get(*this, 0, *this, __err, __n);
183 	      this->setstate(__err);
184 	    }
185 	  catch(exception& __fail)
186 	    {
187 	      // 27.6.1.2.1 Common requirements.
188 	      // Turn this on without causing an ios::failure to be thrown.
189 	      this->setstate(ios_base::badbit);
190 	      if ((this->exceptions() & ios_base::badbit) != 0)
191 		__throw_exception_again;
192 	    }
193 	}
194       return *this;
195     }
196 
197   template<typename _CharT, typename _Traits>
198     basic_istream<_CharT, _Traits>&
199     basic_istream<_CharT, _Traits>::
200     operator>>(int& __n)
201     {
202       sentry __cerb(*this, false);
203       if (__cerb)
204 	{
205 	  try
206 	    {
207 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
208 	      long __l;
209 	      if (_M_check_facet(_M_fnumget))
210 		_M_fnumget->get(*this, 0, *this, __err, __l);
211 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
212 	      // 118. basic_istream uses nonexistent num_get member functions.
213 	      if (!(__err & ios_base::failbit)
214 		  && (numeric_limits<int>::min() <= __l
215 		      && __l <= numeric_limits<int>::max()))
216 		__n = __l;
217 	      else
218                 __err |= ios_base::failbit;
219 #endif
220 	      this->setstate(__err);
221 	    }
222 	  catch(exception& __fail)
223 	    {
224 	      // 27.6.1.2.1 Common requirements.
225 	      // Turn this on without causing an ios::failure to be thrown.
226 	      this->setstate(ios_base::badbit);
227 	      if ((this->exceptions() & ios_base::badbit) != 0)
228 		__throw_exception_again;
229 	    }
230 	}
231       return *this;
232     }
233 
234   template<typename _CharT, typename _Traits>
235     basic_istream<_CharT, _Traits>&
236     basic_istream<_CharT, _Traits>::
237     operator>>(unsigned int& __n)
238     {
239       sentry __cerb(*this, false);
240       if (__cerb)
241 	{
242 	  try
243 	    {
244 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
245 	      if (_M_check_facet(_M_fnumget))
246 		_M_fnumget->get(*this, 0, *this, __err, __n);
247 	      this->setstate(__err);
248 	    }
249 	  catch(exception& __fail)
250 	    {
251 	      // 27.6.1.2.1 Common requirements.
252 	      // Turn this on without causing an ios::failure to be thrown.
253 	      this->setstate(ios_base::badbit);
254 	      if ((this->exceptions() & ios_base::badbit) != 0)
255 		__throw_exception_again;
256 	    }
257 	}
258       return *this;
259     }
260 
261   template<typename _CharT, typename _Traits>
262     basic_istream<_CharT, _Traits>&
263     basic_istream<_CharT, _Traits>::
264     operator>>(long& __n)
265     {
266       sentry __cerb(*this, false);
267       if (__cerb)
268 	{
269 	  try
270 	    {
271 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
272 	      if (_M_check_facet(_M_fnumget))
273 		_M_fnumget->get(*this, 0, *this, __err, __n);
274 	      this->setstate(__err);
275 	    }
276 	  catch(exception& __fail)
277 	    {
278 	      // 27.6.1.2.1 Common requirements.
279 	      // Turn this on without causing an ios::failure to be thrown.
280 	      this->setstate(ios_base::badbit);
281 	      if ((this->exceptions() & ios_base::badbit) != 0)
282 		__throw_exception_again;
283 	    }
284 	}
285       return *this;
286     }
287 
288   template<typename _CharT, typename _Traits>
289     basic_istream<_CharT, _Traits>&
290     basic_istream<_CharT, _Traits>::
291     operator>>(unsigned long& __n)
292     {
293       sentry __cerb(*this, false);
294       if (__cerb)
295 	{
296 	  try
297 	    {
298 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
299 	      if (_M_check_facet(_M_fnumget))
300 		_M_fnumget->get(*this, 0, *this, __err, __n);
301 	      this->setstate(__err);
302 	    }
303 	  catch(exception& __fail)
304 	    {
305 	      // 27.6.1.2.1 Common requirements.
306 	      // Turn this on without causing an ios::failure to be thrown.
307 	      this->setstate(ios_base::badbit);
308 	      if ((this->exceptions() & ios_base::badbit) != 0)
309 		__throw_exception_again;
310 	    }
311 	}
312       return *this;
313     }
314 
315 #ifdef _GLIBCPP_USE_LONG_LONG
316   template<typename _CharT, typename _Traits>
317     basic_istream<_CharT, _Traits>&
318     basic_istream<_CharT, _Traits>::
319     operator>>(long long& __n)
320     {
321       sentry __cerb(*this, false);
322       if (__cerb)
323 	{
324 	  try
325 	    {
326 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
327 	      if (_M_check_facet(_M_fnumget))
328 		_M_fnumget->get(*this, 0, *this, __err, __n);
329 	      this->setstate(__err);
330 	    }
331 	  catch(exception& __fail)
332 	    {
333 	      // 27.6.1.2.1 Common requirements.
334 	      // Turn this on without causing an ios::failure to be thrown.
335 	      this->setstate(ios_base::badbit);
336 	      if ((this->exceptions() & ios_base::badbit) != 0)
337 	      __throw_exception_again;
338 	    }
339 	}
340       return *this;
341     }
342 
343   template<typename _CharT, typename _Traits>
344     basic_istream<_CharT, _Traits>&
345     basic_istream<_CharT, _Traits>::
346     operator>>(unsigned long long& __n)
347     {
348       sentry __cerb(*this, false);
349       if (__cerb)
350 	{
351 	  try
352 	    {
353 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
354 	      if (_M_check_facet(_M_fnumget))
355 		_M_fnumget->get(*this, 0, *this, __err, __n);
356 	      this->setstate(__err);
357 	    }
358 	  catch(exception& __fail)
359 	    {
360 	      // 27.6.1.2.1 Common requirements.
361 	      // Turn this on without causing an ios::failure to be thrown.
362 	      this->setstate(ios_base::badbit);
363 	      if ((this->exceptions() & ios_base::badbit) != 0)
364 		__throw_exception_again;
365 	    }
366 	}
367       return *this;
368     }
369 #endif
370 
371   template<typename _CharT, typename _Traits>
372     basic_istream<_CharT, _Traits>&
373     basic_istream<_CharT, _Traits>::
374     operator>>(float& __n)
375     {
376       sentry __cerb(*this, false);
377       if (__cerb)
378 	{
379 	  try
380 	    {
381 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
382 	      if (_M_check_facet(_M_fnumget))
383 		_M_fnumget->get(*this, 0, *this, __err, __n);
384 	      this->setstate(__err);
385 	    }
386 	  catch(exception& __fail)
387 	    {
388 	      // 27.6.1.2.1 Common requirements.
389 	      // Turn this on without causing an ios::failure to be thrown.
390 	      this->setstate(ios_base::badbit);
391 	      if ((this->exceptions() & ios_base::badbit) != 0)
392 		__throw_exception_again;
393 	    }
394 	}
395       return *this;
396     }
397 
398   template<typename _CharT, typename _Traits>
399     basic_istream<_CharT, _Traits>&
400     basic_istream<_CharT, _Traits>::
401     operator>>(double& __n)
402     {
403       sentry __cerb(*this, false);
404       if (__cerb)
405 	{
406 	  try
407 	    {
408 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
409 	      if (_M_check_facet(_M_fnumget))
410 		_M_fnumget->get(*this, 0, *this, __err, __n);
411 	      this->setstate(__err);
412 	    }
413 	  catch(exception& __fail)
414 	    {
415 	      // 27.6.1.2.1 Common requirements.
416 	      // Turn this on without causing an ios::failure to be thrown.
417 	      this->setstate(ios_base::badbit);
418 	      if ((this->exceptions() & ios_base::badbit) != 0)
419 		__throw_exception_again;
420 	    }
421 	}
422       return *this;
423     }
424 
425   template<typename _CharT, typename _Traits>
426     basic_istream<_CharT, _Traits>&
427     basic_istream<_CharT, _Traits>::
428     operator>>(long double& __n)
429     {
430       sentry __cerb(*this, false);
431       if (__cerb)
432 	{
433 	  try
434 	    {
435 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
436 	      if (_M_check_facet(_M_fnumget))
437 		_M_fnumget->get(*this, 0, *this, __err, __n);
438 	      this->setstate(__err);
439 	    }
440 	  catch(exception& __fail)
441 	    {
442 	      // 27.6.1.2.1 Common requirements.
443 	      // Turn this on without causing an ios::failure to be thrown.
444 	      this->setstate(ios_base::badbit);
445 	      if ((this->exceptions() & ios_base::badbit) != 0)
446 		__throw_exception_again;
447 	    }
448 	}
449       return *this;
450     }
451 
452   template<typename _CharT, typename _Traits>
453     basic_istream<_CharT, _Traits>&
454     basic_istream<_CharT, _Traits>::
455     operator>>(void*& __n)
456     {
457       sentry __cerb(*this, false);
458       if (__cerb)
459 	{
460 	  try
461 	    {
462 	      ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
463 	      if (_M_check_facet(_M_fnumget))
464 		_M_fnumget->get(*this, 0, *this, __err, __n);
465 	      this->setstate(__err);
466 	    }
467 	  catch(exception& __fail)
468 	    {
469 	      // 27.6.1.2.1 Common requirements.
470 	      // Turn this on without causing an ios::failure to be thrown.
471 	      this->setstate(ios_base::badbit);
472 	      if ((this->exceptions() & ios_base::badbit) != 0)
473 		__throw_exception_again;
474 	    }
475 	}
476       return *this;
477     }
478 
479   template<typename _CharT, typename _Traits>
480     basic_istream<_CharT, _Traits>&
481     basic_istream<_CharT, _Traits>::
482     operator>>(__streambuf_type* __sbout)
483     {
484        sentry __cerb(*this, false);
485        if (__cerb)
486 	 {
487 	   try
488 	     {
489 	       streamsize __xtrct = 0;
490 	       if (__sbout)
491 		 {
492 		   __streambuf_type* __sbin = this->rdbuf();
493 		   __xtrct = __copy_streambufs(*this, __sbin, __sbout);
494 		 }
495 	       if (!__sbout || !__xtrct)
496 		 this->setstate(ios_base::failbit);
497 	     }
498 	   catch(exception& __fail)
499 	     {
500 	       // 27.6.2.5.1 Common requirements.
501 	       // Turn this on without causing an ios::failure to be thrown.
502 	       this->setstate(ios_base::badbit);
503 	       if ((this->exceptions() & ios_base::badbit) != 0)
504 		 __throw_exception_again;
505 	     }
506 	 }
507        return *this;
508     }
509 
510   template<typename _CharT, typename _Traits>
511     typename basic_istream<_CharT, _Traits>::int_type
512     basic_istream<_CharT, _Traits>::
513     get(void)
514     {
515       const int_type __eof = traits_type::eof();
516       int_type __c = __eof;
517       _M_gcount = 0;
518       sentry __cerb(*this, true);
519       if (__cerb)
520 	{
521 	  try
522 	    {
523 	      __c = this->rdbuf()->sbumpc();
524 	      // 27.6.1.1 paragraph 3
525 	      if (!traits_type::eq_int_type(__c, __eof))
526 		_M_gcount = 1;
527 	      else
528 		this->setstate(ios_base::eofbit | ios_base::failbit);
529 	    }
530 	  catch(exception& __fail)
531 	    {
532 	      // 27.6.1.3 paragraph 1
533 	      // Turn this on without causing an ios::failure to be thrown.
534 	      this->setstate(ios_base::badbit);
535 	      if ((this->exceptions() & ios_base::badbit) != 0)
536 		__throw_exception_again;
537 	    }
538 	}
539       return __c;
540     }
541 
542   template<typename _CharT, typename _Traits>
543     basic_istream<_CharT, _Traits>&
544     basic_istream<_CharT, _Traits>::
545     get(char_type& __c)
546     {
547       _M_gcount = 0;
548       sentry __cerb(*this, true);
549       if (__cerb)
550 	{
551  	  try
552 	    {
553 	      const int_type __eof = traits_type::eof();
554 	      int_type __bufval = this->rdbuf()->sbumpc();
555 	      // 27.6.1.1 paragraph 3
556 	      if (!traits_type::eq_int_type(__bufval, __eof))
557 		{
558 		  _M_gcount = 1;
559 		  __c = traits_type::to_char_type(__bufval);
560 		}
561 	      else
562 		this->setstate(ios_base::eofbit | ios_base::failbit);
563 	    }
564 	  catch(exception& __fail)
565 	    {
566 	      // 27.6.1.3 paragraph 1
567 	      // Turn this on without causing an ios::failure to be thrown.
568 	      this->setstate(ios_base::badbit);
569 	      if ((this->exceptions() & ios_base::badbit) != 0)
570 		__throw_exception_again;
571 	    }
572 	}
573       return *this;
574     }
575 
576   template<typename _CharT, typename _Traits>
577     basic_istream<_CharT, _Traits>&
578     basic_istream<_CharT, _Traits>::
579     get(char_type* __s, streamsize __n, char_type __delim)
580     {
581       _M_gcount = 0;
582       sentry __cerb(*this, true);
583       if (__cerb)
584 	{
585 	  try
586 	    {
587 	      const int_type __idelim = traits_type::to_int_type(__delim);
588 	      const int_type __eof = traits_type::eof();
589 	      __streambuf_type* __sb = this->rdbuf();
590 	      int_type __c = __sb->sgetc();
591 
592 	      while (_M_gcount + 1 < __n
593 		     && !traits_type::eq_int_type(__c, __eof)
594 		     && !traits_type::eq_int_type(__c, __idelim))
595 		{
596 		  *__s++ = traits_type::to_char_type(__c);
597 		  __c = __sb->snextc();
598 		  ++_M_gcount;
599 		}
600 	      if (traits_type::eq_int_type(__c, __eof))
601 		this->setstate(ios_base::eofbit);
602 	    }
603 	  catch(exception& __fail)
604 	    {
605 	      // 27.6.1.3 paragraph 1
606 	      // Turn this on without causing an ios::failure to be thrown.
607 	      this->setstate(ios_base::badbit);
608 	      if ((this->exceptions() & ios_base::badbit) != 0)
609 		__throw_exception_again;
610 	    }
611 	}
612       *__s = char_type();
613       if (!_M_gcount)
614 	this->setstate(ios_base::failbit);
615       return *this;
616     }
617 
618   template<typename _CharT, typename _Traits>
619     basic_istream<_CharT, _Traits>&
620     basic_istream<_CharT, _Traits>::
621     get(__streambuf_type& __sb, char_type __delim)
622     {
623       _M_gcount = 0;
624       sentry __cerb(*this, true);
625       if (__cerb)
626 	{
627 	  try
628 	    {
629 	      const int_type __idelim = traits_type::to_int_type(__delim);
630 	      const int_type __eof = traits_type::eof();
631 	      __streambuf_type* __this_sb = this->rdbuf();
632 	      int_type __c = __this_sb->sgetc();
633 	      char_type __c2 = traits_type::to_char_type(__c);
634 
635 	      while (!traits_type::eq_int_type(__c, __eof)
636 		     && !traits_type::eq_int_type(__c, __idelim)
637 		     && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
638 		{
639 		  ++_M_gcount;
640 		  __c = __this_sb->snextc();
641 		  __c2 = traits_type::to_char_type(__c);
642 		}
643 	      if (traits_type::eq_int_type(__c, __eof))
644 		this->setstate(ios_base::eofbit);
645 	    }
646 	  catch(exception& __fail)
647 	    {
648 	      // 27.6.1.3 paragraph 1
649 	      // Turn this on without causing an ios::failure to be thrown.
650 	      this->setstate(ios_base::badbit);
651 	      if ((this->exceptions() & ios_base::badbit) != 0)
652 		__throw_exception_again;
653 	    }
654 	}
655       if (!_M_gcount)
656 	this->setstate(ios_base::failbit);
657       return *this;
658     }
659 
660   template<typename _CharT, typename _Traits>
661     basic_istream<_CharT, _Traits>&
662     basic_istream<_CharT, _Traits>::
663     getline(char_type* __s, streamsize __n, char_type __delim)
664     {
665       _M_gcount = 0;
666       sentry __cerb(*this, true);
667       if (__cerb)
668 	{
669           try
670 	    {
671 	      const int_type __idelim = traits_type::to_int_type(__delim);
672 	      const int_type __eof = traits_type::eof();
673 	      __streambuf_type* __sb = this->rdbuf();
674 	      int_type __c = __sb->sgetc();
675 
676 	      while (_M_gcount + 1 < __n
677 		     && !traits_type::eq_int_type(__c, __eof)
678 		     && !traits_type::eq_int_type(__c, __idelim))
679 		{
680 		  *__s++ = traits_type::to_char_type(__c);
681 		  __c = __sb->snextc();
682 		  ++_M_gcount;
683 		}
684 	      if (traits_type::eq_int_type(__c, __eof))
685 		this->setstate(ios_base::eofbit);
686 	      else
687 		{
688 		  if (traits_type::eq_int_type(__c, __idelim))
689 		    {
690 		      __sb->sbumpc();
691 		      ++_M_gcount;
692 		    }
693 		  else
694 		    this->setstate(ios_base::failbit);
695 		}
696 	    }
697 	  catch(exception& __fail)
698 	    {
699 	      // 27.6.1.3 paragraph 1
700 	      // Turn this on without causing an ios::failure to be thrown.
701 	      this->setstate(ios_base::badbit);
702 	      if ((this->exceptions() & ios_base::badbit) != 0)
703 		__throw_exception_again;
704 	    }
705 	}
706       *__s = char_type();
707       if (!_M_gcount)
708 	this->setstate(ios_base::failbit);
709       return *this;
710     }
711 
712   template<typename _CharT, typename _Traits>
713     basic_istream<_CharT, _Traits>&
714     basic_istream<_CharT, _Traits>::
715     ignore(streamsize __n, int_type __delim)
716     {
717       _M_gcount = 0;
718       sentry __cerb(*this, true);
719       if (__cerb && __n > 0)
720 	{
721 	  try
722 	    {
723 	      const int_type __eof = traits_type::eof();
724 	      __streambuf_type* __sb = this->rdbuf();
725 	      int_type __c;
726 
727 	      __n = min(__n, numeric_limits<streamsize>::max());
728 	      while (_M_gcount < __n
729 		     && !traits_type::eq_int_type(__c = __sb->sbumpc(), __eof))
730 		{
731 		  ++_M_gcount;
732 		  if (traits_type::eq_int_type(__c, __delim))
733 		    break;
734 		}
735 	      if (traits_type::eq_int_type(__c, __eof))
736 		this->setstate(ios_base::eofbit);
737 	    }
738 	  catch(exception& __fail)
739 	    {
740 	      // 27.6.1.3 paragraph 1
741 	      // Turn this on without causing an ios::failure to be thrown.
742 	      this->setstate(ios_base::badbit);
743 	      if ((this->exceptions() & ios_base::badbit) != 0)
744 		__throw_exception_again;
745 	    }
746 	}
747       return *this;
748     }
749 
750   template<typename _CharT, typename _Traits>
751     typename basic_istream<_CharT, _Traits>::int_type
752     basic_istream<_CharT, _Traits>::
753     peek(void)
754     {
755       int_type __c = traits_type::eof();
756       _M_gcount = 0;
757       sentry __cerb(*this, true);
758       if (__cerb)
759 	{
760 	  try
761 	    { __c = this->rdbuf()->sgetc(); }
762 	  catch(exception& __fail)
763 	    {
764 	      // 27.6.1.3 paragraph 1
765 	      // Turn this on without causing an ios::failure to be thrown.
766 	      this->setstate(ios_base::badbit);
767 	      if ((this->exceptions() & ios_base::badbit) != 0)
768 		__throw_exception_again;
769 	    }
770 	}
771       return __c;
772     }
773 
774   template<typename _CharT, typename _Traits>
775     basic_istream<_CharT, _Traits>&
776     basic_istream<_CharT, _Traits>::
777     read(char_type* __s, streamsize __n)
778     {
779       _M_gcount = 0;
780       sentry __cerb(*this, true);
781       if (__cerb)
782 	{
783 	  try
784 	    {
785 	      _M_gcount = this->rdbuf()->sgetn(__s, __n);
786 	      if (_M_gcount != __n)
787 		this->setstate(ios_base::eofbit | ios_base::failbit);
788 	    }
789 	  catch(exception& __fail)
790 	    {
791 	      // 27.6.1.3 paragraph 1
792 	      // Turn this on without causing an ios::failure to be thrown.
793 	      this->setstate(ios_base::badbit);
794 	      if ((this->exceptions() & ios_base::badbit) != 0)
795 		__throw_exception_again;
796 	    }
797 	}
798       else
799 	this->setstate(ios_base::failbit);
800       return *this;
801     }
802 
803   template<typename _CharT, typename _Traits>
804     streamsize
805     basic_istream<_CharT, _Traits>::
806     readsome(char_type* __s, streamsize __n)
807     {
808       _M_gcount = 0;
809       sentry __cerb(*this, true);
810       if (__cerb)
811 	{
812 	  try
813 	    {
814 	      streamsize __num = this->rdbuf()->in_avail();
815 	      if (__num > 0)
816 		{
817 		  __num = min(__num, __n);
818 		  if (__num)
819 		    _M_gcount = this->rdbuf()->sgetn(__s, __num);
820 		}
821 	      else
822 		this->setstate(ios_base::eofbit);
823 	    }
824 	  catch(exception& __fail)
825 	    {
826 	      // 27.6.1.3 paragraph 1
827 	      // Turn this on without causing an ios::failure to be thrown.
828 	      this->setstate(ios_base::badbit);
829 	      if ((this->exceptions() & ios_base::badbit) != 0)
830 		__throw_exception_again;
831 	    }
832 	}
833       else
834 	this->setstate(ios_base::failbit);
835       return _M_gcount;
836     }
837 
838   template<typename _CharT, typename _Traits>
839     basic_istream<_CharT, _Traits>&
840     basic_istream<_CharT, _Traits>::
841     putback(char_type __c)
842     {
843       sentry __cerb(*this, true);
844       if (__cerb)
845 	{
846 	  try
847 	    {
848 	      const int_type __eof = traits_type::eof();
849 	      __streambuf_type* __sb = this->rdbuf();
850 	      if (!__sb
851 		  || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
852 		this->setstate(ios_base::badbit);
853 	    }
854 	  catch(exception& __fail)
855 	    {
856 	      // 27.6.1.3 paragraph 1
857 	      // Turn this on without causing an ios::failure to be thrown.
858 	      this->setstate(ios_base::badbit);
859 	      if ((this->exceptions() & ios_base::badbit) != 0)
860 		__throw_exception_again;
861 	    }
862 	}
863       else
864 	this->setstate(ios_base::failbit);
865       return *this;
866     }
867 
868   template<typename _CharT, typename _Traits>
869     basic_istream<_CharT, _Traits>&
870     basic_istream<_CharT, _Traits>::
871     unget(void)
872     {
873       _M_gcount = 0;
874       sentry __cerb(*this, true);
875       if (__cerb)
876 	{
877 	  try
878 	    {
879 	      const int_type __eof = traits_type::eof();
880 	      __streambuf_type* __sb = this->rdbuf();
881 	      if (!__sb
882 		  || traits_type::eq_int_type(__sb->sungetc(), __eof))
883 		this->setstate(ios_base::badbit);
884 	    }
885 	  catch(exception& __fail)
886 	    {
887 	      // 27.6.1.3 paragraph 1
888 	      // Turn this on without causing an ios::failure to be thrown.
889 	      this->setstate(ios_base::badbit);
890 	      if ((this->exceptions() & ios_base::badbit) != 0)
891 		__throw_exception_again;
892 	    }
893 	}
894       else
895 	this->setstate(ios_base::failbit);
896       return *this;
897     }
898 
899   template<typename _CharT, typename _Traits>
900     int
901     basic_istream<_CharT, _Traits>::
902     sync(void)
903     {
904       int __ret = -1;
905       _M_gcount = 0;
906       sentry __cerb(*this, true);
907       if (__cerb)
908 	{
909 	  try
910 	    {
911 	      __streambuf_type* __sb = this->rdbuf();
912 	      if (__sb)
913 		{
914 		  if (__sb->pubsync() == -1)
915 		    this->setstate(ios_base::badbit);
916 		  else
917 		    __ret = 0;
918 		}
919 	    }
920 	  catch(exception& __fail)
921 	    {
922 	      // 27.6.1.3 paragraph 1
923 	      // Turn this on without causing an ios::failure to be thrown.
924 	      this->setstate(ios_base::badbit);
925 	      if ((this->exceptions() & ios_base::badbit) != 0)
926 		__throw_exception_again;
927 	    }
928 	}
929       return __ret;
930     }
931 
932   template<typename _CharT, typename _Traits>
933     typename basic_istream<_CharT, _Traits>::pos_type
934     basic_istream<_CharT, _Traits>::
935     tellg(void)
936     {
937       pos_type __ret = pos_type(-1);
938       _M_gcount = 0;
939       sentry __cerb(*this, true);
940       if (__cerb)
941 	{
942 	  try
943 	    {
944 	     __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
945 	    }
946 	  catch(exception& __fail)
947 	    {
948 	      // 27.6.1.3 paragraph 1
949 	      // Turn this on without causing an ios::failure to be thrown.
950 	      this->setstate(ios_base::badbit);
951 	      if ((this->exceptions() & ios_base::badbit) != 0)
952 		__throw_exception_again;
953 	    }
954 	}
955       return __ret;
956     }
957 
958 
959   template<typename _CharT, typename _Traits>
960     basic_istream<_CharT, _Traits>&
961     basic_istream<_CharT, _Traits>::
962     seekg(pos_type __pos)
963     {
964       _M_gcount = 0;
965       sentry __cerb(*this, true);
966       if (__cerb)
967 	{
968 	  try
969 	    {
970 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
971 // 136.  seekp, seekg setting wrong streams?
972 	      pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::in);
973 
974 // 129. Need error indication from seekp() and seekg()
975 	      if (__err == pos_type(off_type(-1)))
976 		this->setstate(ios_base::failbit);
977 #endif
978 	    }
979 	  catch(exception& __fail)
980 	    {
981 	      // 27.6.1.3 paragraph 1
982 	      // Turn this on without causing an ios::failure to be thrown.
983 	      this->setstate(ios_base::badbit);
984 	      if ((this->exceptions() & ios_base::badbit) != 0)
985 		__throw_exception_again;
986 	    }
987 	}
988       return *this;
989     }
990 
991   template<typename _CharT, typename _Traits>
992     basic_istream<_CharT, _Traits>&
993     basic_istream<_CharT, _Traits>::
994     seekg(off_type __off, ios_base::seekdir __dir)
995     {
996       _M_gcount = 0;
997       sentry __cerb(*this, true);
998       if (__cerb)
999 	{
1000 	  try
1001 	    {
1002 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1003 // 136.  seekp, seekg setting wrong streams?
1004 	      pos_type __err = this->rdbuf()->pubseekoff(__off, __dir,
1005 							 ios_base::in);
1006 
1007 // 129. Need error indication from seekp() and seekg()
1008 	      if (__err == pos_type(off_type(-1)))
1009 		this->setstate(ios_base::failbit);
1010 #endif
1011 	    }
1012 	  catch(exception& __fail)
1013 	    {
1014 	      // 27.6.1.3 paragraph 1
1015 	      // Turn this on without causing an ios::failure to be thrown.
1016 	      this->setstate(ios_base::badbit);
1017 	      if ((this->exceptions() & ios_base::badbit) != 0)
1018 		__throw_exception_again;
1019 	    }
1020 	}
1021       return *this;
1022     }
1023 
1024   // 27.6.1.2.3 Character extraction templates
1025   template<typename _CharT, typename _Traits>
1026     basic_istream<_CharT, _Traits>&
1027     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
1028     {
1029       typedef basic_istream<_CharT, _Traits> 		__istream_type;
1030       typename __istream_type::sentry __cerb(__in, false);
1031       if (__cerb)
1032 	{
1033 	  try
1034 	    { __in.get(__c); }
1035 	  catch(exception& __fail)
1036 	    {
1037 	      // 27.6.1.2.1 Common requirements.
1038 	      // Turn this on without causing an ios::failure to be thrown.
1039 	      __in.setstate(ios_base::badbit);
1040 	      if ((__in.exceptions() & ios_base::badbit) != 0)
1041 		__throw_exception_again;
1042 	    }
1043 	}
1044       else
1045 	__in.setstate(ios_base::failbit);
1046       return __in;
1047     }
1048 
1049   template<typename _CharT, typename _Traits>
1050     basic_istream<_CharT, _Traits>&
1051     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
1052     {
1053       typedef basic_istream<_CharT, _Traits> 		__istream_type;
1054       typedef typename __istream_type::__streambuf_type __streambuf_type;
1055       typedef typename _Traits::int_type 		int_type;
1056       typedef _CharT                     		char_type;
1057       typedef ctype<_CharT>     			__ctype_type;
1058       streamsize __extracted = 0;
1059 
1060       typename __istream_type::sentry __cerb(__in, false);
1061       if (__cerb)
1062 	{
1063 	  try
1064 	    {
1065 	      // Figure out how many characters to extract.
1066 	      streamsize __num = __in.width();
1067 	      if (__num == 0)
1068 		__num = numeric_limits<streamsize>::max();
1069 
1070 	      const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
1071 	      const int_type __eof = _Traits::eof();
1072 	      __streambuf_type* __sb = __in.rdbuf();
1073 	      int_type __c = __sb->sgetc();
1074 
1075 	      while (__extracted < __num - 1
1076 		     && __c != __eof && !__ctype.is(ctype_base::space, __c))
1077 		{
1078 		  *__s++ = __c;
1079 		  ++__extracted;
1080 		  __c = __sb->snextc();
1081 		}
1082 	      if (__c == __eof)
1083 		__in.setstate(ios_base::eofbit);
1084 
1085 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1086 //68.  Extractors for char* should store null at end
1087 	      *__s = char_type();
1088 #endif
1089 	      __in.width(0);
1090 	    }
1091 	  catch(exception& __fail)
1092 	    {
1093 	      // 27.6.1.2.1 Common requirements.
1094 	      // Turn this on without causing an ios::failure to be thrown.
1095 	      __in.setstate(ios_base::badbit);
1096 	      if ((__in.exceptions() & ios_base::badbit) != 0)
1097 		__throw_exception_again;
1098 	    }
1099 	}
1100       if (!__extracted)
1101 	__in.setstate(ios_base::failbit);
1102       return __in;
1103     }
1104 
1105   // 27.6.1.4 Standard basic_istream manipulators
1106   template<typename _CharT, typename _Traits>
1107     basic_istream<_CharT,_Traits>&
1108     ws(basic_istream<_CharT,_Traits>& __in)
1109     {
1110       typedef basic_istream<_CharT, _Traits> 		__istream_type;
1111       typedef typename __istream_type::__streambuf_type __streambuf_type;
1112       typedef typename __istream_type::__ctype_type 	__ctype_type;
1113       typedef typename __istream_type::int_type 	__int_type;
1114 
1115       const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
1116       const __int_type __eof = _Traits::eof();
1117       __streambuf_type* __sb = __in.rdbuf();
1118       __int_type __c = __sb->sgetc();
1119 
1120       while (__c != __eof && __ctype.is(ctype_base::space, __c))
1121 	__c = __sb->snextc();
1122       if (__c == __eof)
1123 	__in.setstate(ios_base::eofbit);
1124 
1125       return __in;
1126     }
1127 
1128   // 21.3.7.9 basic_string::getline and operators
1129   template<typename _CharT, typename _Traits, typename _Alloc>
1130     basic_istream<_CharT, _Traits>&
1131     operator>>(basic_istream<_CharT, _Traits>& __in,
1132 	       basic_string<_CharT, _Traits, _Alloc>& __str)
1133     {
1134       typedef basic_istream<_CharT, _Traits> 		__istream_type;
1135       typedef typename __istream_type::int_type 	__int_type;
1136       typedef typename __istream_type::__streambuf_type __streambuf_type;
1137       typedef typename __istream_type::__ctype_type 	__ctype_type;
1138       typedef basic_string<_CharT, _Traits, _Alloc> 	__string_type;
1139       typedef typename __string_type::size_type		__size_type;
1140       __size_type __extracted = 0;
1141 
1142       typename __istream_type::sentry __cerb(__in, false);
1143       if (__cerb)
1144 	{
1145 	  __str.erase();
1146 	  streamsize __w = __in.width();
1147 	  __size_type __n;
1148 	  __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size();
1149 
1150 	  const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
1151 	  const __int_type __eof = _Traits::eof();
1152 	  __streambuf_type* __sb = __in.rdbuf();
1153 	  __int_type __c = __sb->sgetc();
1154 
1155 	  while (__extracted < __n
1156 		 && __c != __eof && !__ctype.is(ctype_base::space, __c))
1157 	    {
1158 	      __str += _Traits::to_char_type(__c);
1159 	      ++__extracted;
1160 	      __c = __sb->snextc();
1161 	    }
1162 	  if (__c == __eof)
1163 	    __in.setstate(ios_base::eofbit);
1164 	  __in.width(0);
1165 	}
1166 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1167 //211.  operator>>(istream&, string&) doesn't set failbit
1168       if (!__extracted)
1169 	__in.setstate (ios_base::failbit);
1170 #endif
1171       return __in;
1172     }
1173 
1174   template<typename _CharT, typename _Traits, typename _Alloc>
1175     basic_istream<_CharT, _Traits>&
1176     getline(basic_istream<_CharT, _Traits>& __in,
1177 	    basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1178     {
1179       typedef basic_istream<_CharT, _Traits> 		__istream_type;
1180       typedef typename __istream_type::int_type 	__int_type;
1181       typedef typename __istream_type::__streambuf_type __streambuf_type;
1182       typedef typename __istream_type::__ctype_type 	__ctype_type;
1183       typedef basic_string<_CharT, _Traits, _Alloc> 	__string_type;
1184       typedef typename __string_type::size_type		__size_type;
1185 
1186       __size_type __extracted = 0;
1187       bool __testdelim = false;
1188       typename __istream_type::sentry __cerb(__in, true);
1189       if (__cerb)
1190 	{
1191 	  __str.erase();
1192 	  __size_type __n = __str.max_size();
1193 
1194 	  __int_type __idelim = _Traits::to_int_type(__delim);
1195 	  __streambuf_type* __sb = __in.rdbuf();
1196 	  __int_type __c = __sb->sbumpc();
1197 	  const __int_type __eof = _Traits::eof();
1198 	  __testdelim = _Traits::eq_int_type(__c, __idelim);
1199 
1200 	  while (__extracted <= __n
1201 		 && !_Traits::eq_int_type(__c, __eof)
1202 		 && !__testdelim)
1203 	    {
1204 	      __str += _Traits::to_char_type(__c);
1205 	      ++__extracted;
1206 	      __c = __sb->sbumpc();
1207 	      __testdelim = _Traits::eq_int_type(__c, __idelim);
1208 	    }
1209 	  if (_Traits::eq_int_type(__c, __eof))
1210 	    __in.setstate(ios_base::eofbit);
1211 	}
1212       if (!__extracted && !__testdelim)
1213 	__in.setstate(ios_base::failbit);
1214       return __in;
1215     }
1216 
1217   template<class _CharT, class _Traits, class _Alloc>
1218     inline basic_istream<_CharT,_Traits>&
1219     getline(basic_istream<_CharT, _Traits>& __in,
1220 	    basic_string<_CharT,_Traits,_Alloc>& __str)
1221     { return getline(__in, __str, __in.widen('\n')); }
1222 
1223   // Inhibit implicit instantiations for required instantiations,
1224   // which are defined via explicit instantiations elsewhere.
1225   // NB:  This syntax is a GNU extension.
1226   extern template class basic_istream<char>;
1227   extern template istream& ws(istream&);
1228   extern template istream& operator>>(istream&, char&);
1229   extern template istream& operator>>(istream&, char*);
1230   extern template istream& operator>>(istream&, unsigned char&);
1231   extern template istream& operator>>(istream&, signed char&);
1232   extern template istream& operator>>(istream&, unsigned char*);
1233   extern template istream& operator>>(istream&, signed char*);
1234 
1235 #ifdef _GLIBCPP_USE_WCHAR_T
1236   extern template class basic_istream<wchar_t>;
1237   extern template wistream& ws(wistream&);
1238   extern template wistream& operator>>(wistream&, wchar_t&);
1239   extern template wistream& operator>>(wistream&, wchar_t*);
1240 #endif
1241 } // namespace std
1242