]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/istream.tcc
concept-inst.cc (vector<locale::facet*>): Remove instantiations.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / istream.tcc
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 && __n > 0)
712 {
713 try
714 {
715 const int_type __idelim = traits_type::to_int_type(__delim);
716 const int_type __eof = traits_type::eof();
717 __streambuf_type* __sb = this->rdbuf();
718 int_type __c = __sb->sbumpc();
719 bool __testdelim = __c == __idelim;
720 bool __testeof = __c == __eof;
721
722 __n = min(__n, numeric_limits<streamsize>::max());
723 while (_M_gcount < __n - 1 && !__testeof && !__testdelim)
724 {
725 ++_M_gcount;
726 __c = __sb->sbumpc();
727 __testeof = __c == __eof;
728 __testdelim = __c == __idelim;
729 }
730 if ((_M_gcount == __n - 1 && !__testeof) || __testdelim)
731 ++_M_gcount;
732 if (__testeof)
733 this->setstate(ios_base::eofbit);
734 }
735 catch(exception& __fail)
736 {
737 // 27.6.1.3 paragraph 1
738 // Turn this on without causing an ios::failure to be thrown.
739 this->setstate(ios_base::badbit);
740 if ((this->exceptions() & ios_base::badbit) != 0)
741 __throw_exception_again;
742 }
743 }
744 return *this;
745 }
746
747 template<typename _CharT, typename _Traits>
748 typename basic_istream<_CharT, _Traits>::int_type
749 basic_istream<_CharT, _Traits>::
750 peek(void)
751 {
752 int_type __c = traits_type::eof();
753 _M_gcount = 0;
754 sentry __cerb(*this, true);
755 if (__cerb)
756 {
757 try
758 { __c = this->rdbuf()->sgetc(); }
759 catch(exception& __fail)
760 {
761 // 27.6.1.3 paragraph 1
762 // Turn this on without causing an ios::failure to be thrown.
763 this->setstate(ios_base::badbit);
764 if ((this->exceptions() & ios_base::badbit) != 0)
765 __throw_exception_again;
766 }
767 }
768 return __c;
769 }
770
771 template<typename _CharT, typename _Traits>
772 basic_istream<_CharT, _Traits>&
773 basic_istream<_CharT, _Traits>::
774 read(char_type* __s, streamsize __n)
775 {
776 _M_gcount = 0;
777 sentry __cerb(*this, true);
778 if (__cerb)
779 {
780 if (__n > 0)
781 {
782 try
783 {
784 const int_type __eof = traits_type::eof();
785 __streambuf_type* __sb = this->rdbuf();
786 int_type __c = __sb->sbumpc();
787 bool __testeof = __c == __eof;
788
789 while (_M_gcount < __n - 1 && !__testeof)
790 {
791 *__s++ = traits_type::to_char_type(__c);
792 ++_M_gcount;
793 __c = __sb->sbumpc();
794 __testeof = __c == __eof;
795 }
796 if (__testeof)
797 this->setstate(ios_base::eofbit | ios_base::failbit);
798 else
799 {
800 // _M_gcount == __n - 1
801 *__s++ = traits_type::to_char_type(__c);
802 ++_M_gcount;
803 }
804 }
805 catch(exception& __fail)
806 {
807 // 27.6.1.3 paragraph 1
808 // Turn this on without causing an ios::failure to be thrown.
809 this->setstate(ios_base::badbit);
810 if ((this->exceptions() & ios_base::badbit) != 0)
811 __throw_exception_again;
812 }
813 }
814 }
815 else
816 this->setstate(ios_base::failbit);
817 return *this;
818 }
819
820 template<typename _CharT, typename _Traits>
821 streamsize
822 basic_istream<_CharT, _Traits>::
823 readsome(char_type* __s, streamsize __n)
824 {
825 const int_type __eof = traits_type::eof();
826 _M_gcount = 0;
827 sentry __cerb(*this, true);
828 if (__cerb)
829 {
830 if (__n > 0)
831 {
832 try
833 {
834 streamsize __num = this->rdbuf()->in_avail();
835 if (__num != static_cast<streamsize>(__eof))
836 {
837 __num = min(__num, __n);
838 _M_gcount = this->rdbuf()->sgetn(__s, __num);
839 }
840 else
841 this->setstate(ios_base::eofbit);
842 }
843 catch(exception& __fail)
844 {
845 // 27.6.1.3 paragraph 1
846 // Turn this on without causing an ios::failure to be thrown.
847 this->setstate(ios_base::badbit);
848 if ((this->exceptions() & ios_base::badbit) != 0)
849 __throw_exception_again;
850 }
851 }
852 }
853 else
854 this->setstate(ios_base::failbit);
855 return _M_gcount;
856 }
857
858 template<typename _CharT, typename _Traits>
859 basic_istream<_CharT, _Traits>&
860 basic_istream<_CharT, _Traits>::
861 putback(char_type __c)
862 {
863 sentry __cerb(*this, true);
864 if (__cerb)
865 {
866 try
867 {
868 const int_type __eof = traits_type::eof();
869 __streambuf_type* __sb = this->rdbuf();
870 if (!__sb || __sb->sputbackc(__c) == __eof)
871 this->setstate(ios_base::badbit);
872 }
873 catch(exception& __fail)
874 {
875 // 27.6.1.3 paragraph 1
876 // Turn this on without causing an ios::failure to be thrown.
877 this->setstate(ios_base::badbit);
878 if ((this->exceptions() & ios_base::badbit) != 0)
879 __throw_exception_again;
880 }
881 }
882 else
883 this->setstate(ios_base::failbit);
884 return *this;
885 }
886
887 template<typename _CharT, typename _Traits>
888 basic_istream<_CharT, _Traits>&
889 basic_istream<_CharT, _Traits>::
890 unget(void)
891 {
892 _M_gcount = 0;
893 sentry __cerb(*this, true);
894 if (__cerb)
895 {
896 try
897 {
898 const int_type __eof = traits_type::eof();
899 __streambuf_type* __sb = this->rdbuf();
900 if (!__sb || __eof == __sb->sungetc())
901 this->setstate(ios_base::badbit);
902 }
903 catch(exception& __fail)
904 {
905 // 27.6.1.3 paragraph 1
906 // Turn this on without causing an ios::failure to be thrown.
907 this->setstate(ios_base::badbit);
908 if ((this->exceptions() & ios_base::badbit) != 0)
909 __throw_exception_again;
910 }
911 }
912 else
913 this->setstate(ios_base::failbit);
914 return *this;
915 }
916
917 template<typename _CharT, typename _Traits>
918 int
919 basic_istream<_CharT, _Traits>::
920 sync(void)
921 {
922 int __ret = traits_type::eof();
923 _M_gcount = 0;
924 sentry __cerb(*this, true);
925 if (__cerb)
926 {
927 try
928 {
929 __streambuf_type* __sb = this->rdbuf();
930 if (!__sb || __ret == __sb->pubsync())
931 this->setstate(ios_base::badbit);
932 else
933 __ret = 0;
934 }
935 catch(exception& __fail)
936 {
937 // 27.6.1.3 paragraph 1
938 // Turn this on without causing an ios::failure to be thrown.
939 this->setstate(ios_base::badbit);
940 if ((this->exceptions() & ios_base::badbit) != 0)
941 __throw_exception_again;
942 }
943 }
944 return __ret;
945 }
946
947 template<typename _CharT, typename _Traits>
948 typename basic_istream<_CharT, _Traits>::pos_type
949 basic_istream<_CharT, _Traits>::
950 tellg(void)
951 {
952 pos_type __ret = pos_type(-1);
953 _M_gcount = 0;
954 sentry __cerb(*this, true);
955 if (__cerb)
956 {
957 try
958 {
959 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
960 }
961 catch(exception& __fail)
962 {
963 // 27.6.1.3 paragraph 1
964 // Turn this on without causing an ios::failure to be thrown.
965 this->setstate(ios_base::badbit);
966 if ((this->exceptions() & ios_base::badbit) != 0)
967 __throw_exception_again;
968 }
969 }
970 return __ret;
971 }
972
973
974 template<typename _CharT, typename _Traits>
975 basic_istream<_CharT, _Traits>&
976 basic_istream<_CharT, _Traits>::
977 seekg(pos_type __pos)
978 {
979 _M_gcount = 0;
980 sentry __cerb(*this, true);
981 if (__cerb)
982 {
983 try
984 {
985 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
986 // 136. seekp, seekg setting wrong streams?
987 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::in);
988
989 // 129. Need error indication from seekp() and seekg()
990 if (__err == pos_type(off_type(-1)))
991 this->setstate(ios_base::failbit);
992 #endif
993 }
994 catch(exception& __fail)
995 {
996 // 27.6.1.3 paragraph 1
997 // Turn this on without causing an ios::failure to be thrown.
998 this->setstate(ios_base::badbit);
999 if ((this->exceptions() & ios_base::badbit) != 0)
1000 __throw_exception_again;
1001 }
1002 }
1003 return *this;
1004 }
1005
1006 template<typename _CharT, typename _Traits>
1007 basic_istream<_CharT, _Traits>&
1008 basic_istream<_CharT, _Traits>::
1009 seekg(off_type __off, ios_base::seekdir __dir)
1010 {
1011 _M_gcount = 0;
1012 sentry __cerb(*this, true);
1013 if (__cerb)
1014 {
1015 try
1016 {
1017 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1018 // 136. seekp, seekg setting wrong streams?
1019 pos_type __err = this->rdbuf()->pubseekoff(__off, __dir,
1020 ios_base::in);
1021
1022 // 129. Need error indication from seekp() and seekg()
1023 if (__err == pos_type(off_type(-1)))
1024 this->setstate(ios_base::failbit);
1025 #endif
1026 }
1027 catch(exception& __fail)
1028 {
1029 // 27.6.1.3 paragraph 1
1030 // Turn this on without causing an ios::failure to be thrown.
1031 this->setstate(ios_base::badbit);
1032 if ((this->exceptions() & ios_base::badbit) != 0)
1033 __throw_exception_again;
1034 }
1035 }
1036 return *this;
1037 }
1038
1039 // 27.6.1.2.3 Character extraction templates
1040 template<typename _CharT, typename _Traits>
1041 basic_istream<_CharT, _Traits>&
1042 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
1043 {
1044 typedef basic_istream<_CharT, _Traits> __istream_type;
1045 typename __istream_type::sentry __cerb(__in, false);
1046 if (__cerb)
1047 {
1048 try
1049 { __in.get(__c); }
1050 catch(exception& __fail)
1051 {
1052 // 27.6.1.2.1 Common requirements.
1053 // Turn this on without causing an ios::failure to be thrown.
1054 __in.setstate(ios_base::badbit);
1055 if ((__in.exceptions() & ios_base::badbit) != 0)
1056 __throw_exception_again;
1057 }
1058 }
1059 else
1060 __in.setstate(ios_base::failbit);
1061 return __in;
1062 }
1063
1064 template<typename _CharT, typename _Traits>
1065 basic_istream<_CharT, _Traits>&
1066 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
1067 {
1068 typedef basic_istream<_CharT, _Traits> __istream_type;
1069 typedef typename __istream_type::__streambuf_type __streambuf_type;
1070 typedef typename _Traits::int_type int_type;
1071 typedef _CharT char_type;
1072 typedef ctype<_CharT> __ctype_type;
1073 streamsize __extracted = 0;
1074
1075 typename __istream_type::sentry __cerb(__in, false);
1076 if (__cerb)
1077 {
1078 try
1079 {
1080 // Figure out how many characters to extract.
1081 streamsize __num = __in.width();
1082 if (__num == 0)
1083 __num = numeric_limits<streamsize>::max();
1084
1085 const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
1086 const int_type __eof = _Traits::eof();
1087 __streambuf_type* __sb = __in.rdbuf();
1088 int_type __c = __sb->sgetc();
1089
1090 while (__extracted < __num - 1
1091 && __c != __eof && !__ctype.is(ctype_base::space, __c))
1092 {
1093 *__s++ = __c;
1094 ++__extracted;
1095 __c = __sb->snextc();
1096 }
1097 if (__c == __eof)
1098 __in.setstate(ios_base::eofbit);
1099
1100 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1101 //68. Extractors for char* should store null at end
1102 *__s = char_type();
1103 #endif
1104 __in.width(0);
1105 }
1106 catch(exception& __fail)
1107 {
1108 // 27.6.1.2.1 Common requirements.
1109 // Turn this on without causing an ios::failure to be thrown.
1110 __in.setstate(ios_base::badbit);
1111 if ((__in.exceptions() & ios_base::badbit) != 0)
1112 __throw_exception_again;
1113 }
1114 }
1115 if (!__extracted)
1116 __in.setstate(ios_base::failbit);
1117 return __in;
1118 }
1119
1120 // 27.6.1.4 Standard basic_istream manipulators
1121 template<typename _CharT, typename _Traits>
1122 basic_istream<_CharT,_Traits>&
1123 ws(basic_istream<_CharT,_Traits>& __in)
1124 {
1125 typedef basic_istream<_CharT, _Traits> __istream_type;
1126 typedef typename __istream_type::__streambuf_type __streambuf_type;
1127 typedef typename __istream_type::__ctype_type __ctype_type;
1128 typedef typename __istream_type::int_type __int_type;
1129
1130 const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
1131 const __int_type __eof = _Traits::eof();
1132 __streambuf_type* __sb = __in.rdbuf();
1133 __int_type __c = __sb->sgetc();
1134
1135 while (__c != __eof && __ctype.is(ctype_base::space, __c))
1136 __c = __sb->snextc();
1137 if (__c == __eof)
1138 __in.setstate(ios_base::eofbit);
1139
1140 return __in;
1141 }
1142
1143 // 21.3.7.9 basic_string::getline and operators
1144 template<typename _CharT, typename _Traits, typename _Alloc>
1145 basic_istream<_CharT, _Traits>&
1146 operator>>(basic_istream<_CharT, _Traits>& __in,
1147 basic_string<_CharT, _Traits, _Alloc>& __str)
1148 {
1149 typedef basic_istream<_CharT, _Traits> __istream_type;
1150 typedef typename __istream_type::int_type __int_type;
1151 typedef typename __istream_type::__streambuf_type __streambuf_type;
1152 typedef typename __istream_type::__ctype_type __ctype_type;
1153 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1154 typedef typename __string_type::size_type __size_type;
1155 __size_type __extracted = 0;
1156
1157 typename __istream_type::sentry __cerb(__in, false);
1158 if (__cerb)
1159 {
1160 __str.erase();
1161 streamsize __w = __in.width();
1162 __size_type __n;
1163 __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size();
1164
1165 const __ctype_type& __ctype = use_facet<__ctype_type>(__in.getloc());
1166 const __int_type __eof = _Traits::eof();
1167 __streambuf_type* __sb = __in.rdbuf();
1168 __int_type __c = __sb->sgetc();
1169
1170 while (__extracted < __n
1171 && __c != __eof && !__ctype.is(ctype_base::space, __c))
1172 {
1173 __str += _Traits::to_char_type(__c);
1174 ++__extracted;
1175 __c = __sb->snextc();
1176 }
1177 if (__c == __eof)
1178 __in.setstate(ios_base::eofbit);
1179 __in.width(0);
1180 }
1181 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1182 //211. operator>>(istream&, string&) doesn't set failbit
1183 if (!__extracted)
1184 __in.setstate (ios_base::failbit);
1185 #endif
1186 return __in;
1187 }
1188
1189 template<typename _CharT, typename _Traits, typename _Alloc>
1190 basic_istream<_CharT, _Traits>&
1191 getline(basic_istream<_CharT, _Traits>& __in,
1192 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1193 {
1194 typedef basic_istream<_CharT, _Traits> __istream_type;
1195 typedef typename __istream_type::int_type __int_type;
1196 typedef typename __istream_type::__streambuf_type __streambuf_type;
1197 typedef typename __istream_type::__ctype_type __ctype_type;
1198 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1199 typedef typename __string_type::size_type __size_type;
1200
1201 __size_type __extracted = 0;
1202 bool __testdelim = false;
1203 typename __istream_type::sentry __cerb(__in, true);
1204 if (__cerb)
1205 {
1206 __str.erase();
1207 __size_type __n = __str.max_size();
1208
1209 __int_type __idelim = _Traits::to_int_type(__delim);
1210 __streambuf_type* __sb = __in.rdbuf();
1211 __int_type __c = __sb->sbumpc();
1212 const __int_type __eof = _Traits::eof();
1213 __testdelim = __c == __idelim;
1214
1215 while (__extracted <= __n && __c != __eof && !__testdelim)
1216 {
1217 __str += _Traits::to_char_type(__c);
1218 ++__extracted;
1219 __c = __sb->sbumpc();
1220 __testdelim = __c == __idelim;
1221 }
1222 if (__c == __eof)
1223 __in.setstate(ios_base::eofbit);
1224 }
1225 if (!__extracted && !__testdelim)
1226 __in.setstate(ios_base::failbit);
1227 return __in;
1228 }
1229
1230 template<class _CharT, class _Traits, class _Alloc>
1231 inline basic_istream<_CharT,_Traits>&
1232 getline(basic_istream<_CharT, _Traits>& __in,
1233 basic_string<_CharT,_Traits,_Alloc>& __str)
1234 { return getline(__in, __str, __in.widen('\n')); }
1235
1236 // Inhibit implicit instantiations for required instantiations,
1237 // which are defined via explicit instantiations elsewhere.
1238 // NB: This syntax is a GNU extension.
1239 extern template class basic_istream<char>;
1240 extern template istream& ws(istream&);
1241 extern template istream& operator>>(istream&, char&);
1242 extern template istream& operator>>(istream&, char*);
1243 extern template istream& operator>>(istream&, unsigned char&);
1244 extern template istream& operator>>(istream&, signed char&);
1245 extern template istream& operator>>(istream&, unsigned char*);
1246 extern template istream& operator>>(istream&, signed char*);
1247
1248 extern template class basic_istream<wchar_t>;
1249 extern template wistream& ws(wistream&);
1250 extern template wistream& operator>>(wistream&, wchar_t&);
1251 extern template wistream& operator>>(wistream&, wchar_t*);
1252 } // namespace std