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