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