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