1 // istream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
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)
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882: 27.6.1 Input streams
41 #define _ISTREAM_TCC 1
43 #pragma GCC system_header
46 #include <ostream> // For flush()
50 template<typename _CharT, typename _Traits>
51 basic_istream<_CharT, _Traits>::sentry::
52 sentry(basic_istream<_CharT, _Traits>& __in, bool __noskip) : _M_ok(false)
54 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
59 if (!__noskip && (__in.flags() & ios_base::skipws))
61 const __int_type __eof = traits_type::eof();
62 __streambuf_type* __sb = __in.rdbuf();
63 __int_type __c = __sb->sgetc();
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)))
71 // _GLIBCXX_RESOLVE_LIB_DEFECTS
72 // 195. Should basic_istream::sentry's constructor ever
74 if (traits_type::eq_int_type(__c, __eof))
75 __err |= ios_base::eofbit;
79 if (__in.good() && __err == ios_base::goodbit)
83 __err |= ios_base::failbit;
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); }
94 template<typename _CharT, typename _Traits>
95 basic_istream<_CharT, _Traits>&
96 basic_istream<_CharT, _Traits>::
97 operator>>(__ios_type& (*__pf)(__ios_type&))
103 template<typename _CharT, typename _Traits>
104 basic_istream<_CharT, _Traits>&
105 basic_istream<_CharT, _Traits>::
106 operator>>(ios_base& (*__pf)(ios_base&))
112 template<typename _CharT, typename _Traits>
113 basic_istream<_CharT, _Traits>&
114 basic_istream<_CharT, _Traits>::
115 operator>>(bool& __n)
117 sentry __cerb(*this, false);
120 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
123 const __num_get_type& __ng = __check_facet(this->_M_num_get);
124 __ng.get(*this, 0, *this, __err, __n);
127 { this->_M_setstate(ios_base::badbit); }
129 this->setstate(__err);
134 template<typename _CharT, typename _Traits>
135 basic_istream<_CharT, _Traits>&
136 basic_istream<_CharT, _Traits>::
137 operator>>(short& __n)
139 sentry __cerb(*this, false);
142 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
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()))
155 __err |= ios_base::failbit;
158 { this->_M_setstate(ios_base::badbit); }
160 this->setstate(__err);
165 template<typename _CharT, typename _Traits>
166 basic_istream<_CharT, _Traits>&
167 basic_istream<_CharT, _Traits>::
168 operator>>(unsigned short& __n)
170 sentry __cerb(*this, false);
173 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
176 const __num_get_type& __ng = __check_facet(this->_M_num_get);
177 __ng.get(*this, 0, *this, __err, __n);
180 { this->_M_setstate(ios_base::badbit); }
182 this->setstate(__err);
187 template<typename _CharT, typename _Traits>
188 basic_istream<_CharT, _Traits>&
189 basic_istream<_CharT, _Traits>::
192 sentry __cerb(*this, false);
195 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
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()))
208 __err |= ios_base::failbit;
211 { this->_M_setstate(ios_base::badbit); }
213 this->setstate(__err);
218 template<typename _CharT, typename _Traits>
219 basic_istream<_CharT, _Traits>&
220 basic_istream<_CharT, _Traits>::
221 operator>>(unsigned int& __n)
223 sentry __cerb(*this, false);
226 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
229 const __num_get_type& __ng = __check_facet(this->_M_num_get);
230 __ng.get(*this, 0, *this, __err, __n);
233 { this->_M_setstate(ios_base::badbit); }
235 this->setstate(__err);
240 template<typename _CharT, typename _Traits>
241 basic_istream<_CharT, _Traits>&
242 basic_istream<_CharT, _Traits>::
243 operator>>(long& __n)
245 sentry __cerb(*this, false);
248 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
251 const __num_get_type& __ng = __check_facet(this->_M_num_get);
252 __ng.get(*this, 0, *this, __err, __n);
255 { this->_M_setstate(ios_base::badbit); }
257 this->setstate(__err);
262 template<typename _CharT, typename _Traits>
263 basic_istream<_CharT, _Traits>&
264 basic_istream<_CharT, _Traits>::
265 operator>>(unsigned long& __n)
267 sentry __cerb(*this, false);
270 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);
277 { this->_M_setstate(ios_base::badbit); }
279 this->setstate(__err);
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)
290 sentry __cerb(*this, false);
293 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
296 const __num_get_type& __ng = __check_facet(this->_M_num_get);
297 __ng.get(*this, 0, *this, __err, __n);
300 { this->_M_setstate(ios_base::badbit); }
302 this->setstate(__err);
307 template<typename _CharT, typename _Traits>
308 basic_istream<_CharT, _Traits>&
309 basic_istream<_CharT, _Traits>::
310 operator>>(unsigned long long& __n)
312 sentry __cerb(*this, false);
315 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
318 const __num_get_type& __ng = __check_facet(this->_M_num_get);
319 __ng.get(*this, 0, *this, __err, __n);
322 { this->_M_setstate(ios_base::badbit); }
324 this->setstate(__err);
330 template<typename _CharT, typename _Traits>
331 basic_istream<_CharT, _Traits>&
332 basic_istream<_CharT, _Traits>::
333 operator>>(float& __n)
335 sentry __cerb(*this, false);
338 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
341 const __num_get_type& __ng = __check_facet(this->_M_num_get);
342 __ng.get(*this, 0, *this, __err, __n);
345 { this->_M_setstate(ios_base::badbit); }
347 this->setstate(__err);
352 template<typename _CharT, typename _Traits>
353 basic_istream<_CharT, _Traits>&
354 basic_istream<_CharT, _Traits>::
355 operator>>(double& __n)
357 sentry __cerb(*this, false);
360 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
363 const __num_get_type& __ng = __check_facet(this->_M_num_get);
364 __ng.get(*this, 0, *this, __err, __n);
367 { this->_M_setstate(ios_base::badbit); }
369 this->setstate(__err);
374 template<typename _CharT, typename _Traits>
375 basic_istream<_CharT, _Traits>&
376 basic_istream<_CharT, _Traits>::
377 operator>>(long double& __n)
379 sentry __cerb(*this, false);
382 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
385 const __num_get_type& __ng = __check_facet(this->_M_num_get);
386 __ng.get(*this, 0, *this, __err, __n);
389 { this->_M_setstate(ios_base::badbit); }
391 this->setstate(__err);
396 template<typename _CharT, typename _Traits>
397 basic_istream<_CharT, _Traits>&
398 basic_istream<_CharT, _Traits>::
399 operator>>(void*& __n)
401 sentry __cerb(*this, false);
404 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
407 const __num_get_type& __ng = __check_facet(this->_M_num_get);
408 __ng.get(*this, 0, *this, __err, __n);
411 { this->_M_setstate(ios_base::badbit); }
413 this->setstate(__err);
418 template<typename _CharT, typename _Traits>
419 basic_istream<_CharT, _Traits>&
420 basic_istream<_CharT, _Traits>::
421 operator>>(__streambuf_type* __sbout)
423 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
424 sentry __cerb(*this, false);
425 if (__cerb && __sbout)
429 if (!__copy_streambufs(this->rdbuf(), __sbout))
430 __err |= ios_base::failbit;
433 { this->_M_setstate(ios_base::failbit); }
436 __err |= ios_base::failbit;
438 this->setstate(__err);
442 template<typename _CharT, typename _Traits>
443 typename basic_istream<_CharT, _Traits>::int_type
444 basic_istream<_CharT, _Traits>::
447 const int_type __eof = traits_type::eof();
448 int_type __c = __eof;
450 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
451 sentry __cerb(*this, true);
456 __c = this->rdbuf()->sbumpc();
457 // 27.6.1.1 paragraph 3
458 if (!traits_type::eq_int_type(__c, __eof))
461 __err |= ios_base::eofbit;
464 { this->_M_setstate(ios_base::badbit); }
467 __err |= ios_base::failbit;
469 this->setstate(__err);
473 template<typename _CharT, typename _Traits>
474 basic_istream<_CharT, _Traits>&
475 basic_istream<_CharT, _Traits>::
479 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
480 sentry __cerb(*this, true);
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()))
490 __c = traits_type::to_char_type(__cb);
493 __err |= ios_base::eofbit;
496 { this->_M_setstate(ios_base::badbit); }
499 __err |= ios_base::failbit;
501 this->setstate(__err);
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)
511 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
512 sentry __cerb(*this, true);
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();
522 while (_M_gcount + 1 < __n
523 && !traits_type::eq_int_type(__c, __eof)
524 && !traits_type::eq_int_type(__c, __idelim))
526 *__s++ = traits_type::to_char_type(__c);
528 __c = __sb->snextc();
530 if (traits_type::eq_int_type(__c, __eof))
531 __err |= ios_base::eofbit;
534 { this->_M_setstate(ios_base::badbit); }
536 // _GLIBCXX_RESOLVE_LIB_DEFECTS
537 // 243. get and getline when sentry reports failure.
541 __err |= ios_base::failbit;
543 this->setstate(__err);
547 template<typename _CharT, typename _Traits>
548 basic_istream<_CharT, _Traits>&
549 basic_istream<_CharT, _Traits>::
550 get(__streambuf_type& __sb, char_type __delim)
553 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
554 sentry __cerb(*this, true);
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);
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))
570 __c = __this_sb->snextc();
571 __c2 = traits_type::to_char_type(__c);
573 if (traits_type::eq_int_type(__c, __eof))
574 __err |= ios_base::eofbit;
577 { this->_M_setstate(ios_base::badbit); }
580 __err |= ios_base::failbit;
582 this->setstate(__err);
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)
592 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
593 sentry __cerb(*this, true);
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();
603 while (_M_gcount + 1 < __n
604 && !traits_type::eq_int_type(__c, __eof)
605 && !traits_type::eq_int_type(__c, __idelim))
607 *__s++ = traits_type::to_char_type(__c);
608 __c = __sb->snextc();
611 if (traits_type::eq_int_type(__c, __eof))
612 __err |= ios_base::eofbit;
615 if (traits_type::eq_int_type(__c, __idelim))
621 __err |= ios_base::failbit;
625 { this->_M_setstate(ios_base::badbit); }
627 // _GLIBCXX_RESOLVE_LIB_DEFECTS
628 // 243. get and getline when sentry reports failure.
632 __err |= ios_base::failbit;
634 this->setstate(__err);
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>::
647 sentry __cerb(*this, true);
650 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
653 const int_type __eof = traits_type::eof();
654 __streambuf_type* __sb = this->rdbuf();
656 if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
657 __err |= ios_base::eofbit;
662 { this->_M_setstate(ios_base::badbit); }
664 this->setstate(__err);
669 template<typename _CharT, typename _Traits>
670 basic_istream<_CharT, _Traits>&
671 basic_istream<_CharT, _Traits>::
672 ignore(streamsize __n)
675 sentry __cerb(*this, true);
676 if (__cerb && __n > 0)
678 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
681 const int_type __eof = traits_type::eof();
682 __streambuf_type* __sb = this->rdbuf();
683 int_type __c = __sb->sgetc();
685 // N.B. On LFS-enabled platforms streamsize is still 32 bits
686 // wide: if we want to implement the standard mandated behavior
687 // for n == max() (see 27.6.1.3/24) we are at risk of signed
688 // integer overflow: thus these contortions. Also note that,
689 // by definition, when more than 2G chars are actually ignored,
690 // _M_gcount (the return value of gcount, that is) cannot be
691 // really correct, being unavoidably too small.
692 bool __large_ignore = false;
695 while (_M_gcount < __n
696 && !traits_type::eq_int_type(__c, __eof))
699 __c = __sb->snextc();
701 if (__n == numeric_limits<streamsize>::max()
702 && !traits_type::eq_int_type(__c, __eof))
704 _M_gcount = numeric_limits<streamsize>::min();
705 __large_ignore = true;
712 _M_gcount = numeric_limits<streamsize>::max();
714 if (traits_type::eq_int_type(__c, __eof))
715 __err |= ios_base::eofbit;
718 { this->_M_setstate(ios_base::badbit); }
720 this->setstate(__err);
725 template<typename _CharT, typename _Traits>
726 basic_istream<_CharT, _Traits>&
727 basic_istream<_CharT, _Traits>::
728 ignore(streamsize __n, int_type __delim)
731 sentry __cerb(*this, true);
732 if (__cerb && __n > 0)
734 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
737 const int_type __eof = traits_type::eof();
738 __streambuf_type* __sb = this->rdbuf();
739 int_type __c = __sb->sgetc();
741 // See comment above.
742 bool __large_ignore = false;
745 while (_M_gcount < __n
746 && !traits_type::eq_int_type(__c, __eof)
747 && !traits_type::eq_int_type(__c, __delim))
750 __c = __sb->snextc();
752 if (__n == numeric_limits<streamsize>::max()
753 && !traits_type::eq_int_type(__c, __eof)
754 && !traits_type::eq_int_type(__c, __delim))
756 _M_gcount = numeric_limits<streamsize>::min();
757 __large_ignore = true;
764 _M_gcount = numeric_limits<streamsize>::max();
766 if (traits_type::eq_int_type(__c, __eof))
767 __err |= ios_base::eofbit;
768 else if (traits_type::eq_int_type(__c, __delim))
770 if (_M_gcount < numeric_limits<streamsize>::max())
776 { this->_M_setstate(ios_base::badbit); }
778 this->setstate(__err);
783 template<typename _CharT, typename _Traits>
784 typename basic_istream<_CharT, _Traits>::int_type
785 basic_istream<_CharT, _Traits>::
788 int_type __c = traits_type::eof();
790 sentry __cerb(*this, true);
793 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
796 __c = this->rdbuf()->sgetc();
797 if (traits_type::eq_int_type(__c, traits_type::eof()))
798 __err |= ios_base::eofbit;
801 { this->_M_setstate(ios_base::badbit); }
803 this->setstate(__err);
808 template<typename _CharT, typename _Traits>
809 basic_istream<_CharT, _Traits>&
810 basic_istream<_CharT, _Traits>::
811 read(char_type* __s, streamsize __n)
814 sentry __cerb(*this, true);
817 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
820 _M_gcount = this->rdbuf()->sgetn(__s, __n);
821 if (_M_gcount != __n)
822 __err |= (ios_base::eofbit | ios_base::failbit);
825 { this->_M_setstate(ios_base::badbit); }
827 this->setstate(__err);
832 template<typename _CharT, typename _Traits>
834 basic_istream<_CharT, _Traits>::
835 readsome(char_type* __s, streamsize __n)
838 sentry __cerb(*this, true);
841 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
844 // Cannot compare int_type with streamsize generically.
845 const streamsize __num = this->rdbuf()->in_avail();
847 _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
848 else if (__num == -1)
849 __err |= ios_base::eofbit;
852 { this->_M_setstate(ios_base::badbit); }
854 this->setstate(__err);
859 template<typename _CharT, typename _Traits>
860 basic_istream<_CharT, _Traits>&
861 basic_istream<_CharT, _Traits>::
862 putback(char_type __c)
864 // _GLIBCXX_RESOLVE_LIB_DEFECTS
865 // 60. What is a formatted input function?
867 sentry __cerb(*this, true);
870 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
873 const int_type __eof = traits_type::eof();
874 __streambuf_type* __sb = this->rdbuf();
876 || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
877 __err |= ios_base::badbit;
880 { this->_M_setstate(ios_base::badbit); }
882 this->setstate(__err);
887 template<typename _CharT, typename _Traits>
888 basic_istream<_CharT, _Traits>&
889 basic_istream<_CharT, _Traits>::
892 // _GLIBCXX_RESOLVE_LIB_DEFECTS
893 // 60. What is a formatted input function?
895 sentry __cerb(*this, true);
898 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
901 const int_type __eof = traits_type::eof();
902 __streambuf_type* __sb = this->rdbuf();
904 || traits_type::eq_int_type(__sb->sungetc(), __eof))
905 __err |= ios_base::badbit;
908 { this->_M_setstate(ios_base::badbit); }
910 this->setstate(__err);
915 template<typename _CharT, typename _Traits>
917 basic_istream<_CharT, _Traits>::
920 // _GLIBCXX_RESOLVE_LIB_DEFECTS
921 // DR60. Do not change _M_gcount.
923 sentry __cerb(*this, true);
926 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
929 __streambuf_type* __sb = this->rdbuf();
932 if (__sb->pubsync() == -1)
933 __err |= ios_base::badbit;
939 { this->_M_setstate(ios_base::badbit); }
941 this->setstate(__err);
946 template<typename _CharT, typename _Traits>
947 typename basic_istream<_CharT, _Traits>::pos_type
948 basic_istream<_CharT, _Traits>::
951 // _GLIBCXX_RESOLVE_LIB_DEFECTS
952 // DR60. Do not change _M_gcount.
953 pos_type __ret = pos_type(-1);
957 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
960 { this->_M_setstate(ios_base::badbit); }
964 template<typename _CharT, typename _Traits>
965 basic_istream<_CharT, _Traits>&
966 basic_istream<_CharT, _Traits>::
967 seekg(pos_type __pos)
969 // _GLIBCXX_RESOLVE_LIB_DEFECTS
970 // DR60. Do not change _M_gcount.
971 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
976 // 136. seekp, seekg setting wrong streams?
977 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
980 // 129. Need error indication from seekp() and seekg()
981 if (__p == pos_type(off_type(-1)))
982 __err |= ios_base::failbit;
986 { this->_M_setstate(ios_base::badbit); }
988 this->setstate(__err);
992 template<typename _CharT, typename _Traits>
993 basic_istream<_CharT, _Traits>&
994 basic_istream<_CharT, _Traits>::
995 seekg(off_type __off, ios_base::seekdir __dir)
997 // _GLIBCXX_RESOLVE_LIB_DEFECTS
998 // DR60. Do not change _M_gcount.
999 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1004 // 136. seekp, seekg setting wrong streams?
1005 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
1008 // 129. Need error indication from seekp() and seekg()
1009 if (__p == pos_type(off_type(-1)))
1010 __err |= ios_base::failbit;
1014 { this->_M_setstate(ios_base::badbit); }
1016 this->setstate(__err);
1020 // 27.6.1.2.3 Character extraction templates
1021 template<typename _CharT, typename _Traits>
1022 basic_istream<_CharT, _Traits>&
1023 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
1025 typedef basic_istream<_CharT, _Traits> __istream_type;
1026 typedef typename __istream_type::int_type __int_type;
1028 typename __istream_type::sentry __cerb(__in, false);
1031 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1034 const __int_type __cb = __in.rdbuf()->sbumpc();
1035 if (!_Traits::eq_int_type(__cb, _Traits::eof()))
1036 __c = _Traits::to_char_type(__cb);
1038 __err |= (ios_base::eofbit | ios_base::failbit);
1041 { __in._M_setstate(ios_base::badbit); }
1043 __in.setstate(__err);
1048 template<typename _CharT, typename _Traits>
1049 basic_istream<_CharT, _Traits>&
1050 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
1052 typedef basic_istream<_CharT, _Traits> __istream_type;
1053 typedef typename __istream_type::__streambuf_type __streambuf_type;
1054 typedef typename _Traits::int_type int_type;
1055 typedef _CharT char_type;
1056 typedef ctype<_CharT> __ctype_type;
1058 streamsize __extracted = 0;
1059 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1060 typename __istream_type::sentry __cerb(__in, false);
1065 // Figure out how many characters to extract.
1066 streamsize __num = __in.width();
1068 __num = numeric_limits<streamsize>::max();
1070 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1072 const int_type __eof = _Traits::eof();
1073 __streambuf_type* __sb = __in.rdbuf();
1074 int_type __c = __sb->sgetc();
1076 while (__extracted < __num - 1
1077 && !_Traits::eq_int_type(__c, __eof)
1078 && !__ct.is(ctype_base::space,
1079 _Traits::to_char_type(__c)))
1081 *__s++ = _Traits::to_char_type(__c);
1083 __c = __sb->snextc();
1085 if (_Traits::eq_int_type(__c, __eof))
1086 __err |= ios_base::eofbit;
1088 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1089 // 68. Extractors for char* should store null at end
1094 { __in._M_setstate(ios_base::badbit); }
1097 __err |= ios_base::failbit;
1099 __in.setstate(__err);
1103 // 27.6.1.4 Standard basic_istream manipulators
1104 template<typename _CharT, typename _Traits>
1105 basic_istream<_CharT,_Traits>&
1106 ws(basic_istream<_CharT,_Traits>& __in)
1108 typedef basic_istream<_CharT, _Traits> __istream_type;
1109 typedef typename __istream_type::__streambuf_type __streambuf_type;
1110 typedef typename __istream_type::__ctype_type __ctype_type;
1111 typedef typename __istream_type::int_type __int_type;
1113 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1114 const __int_type __eof = _Traits::eof();
1115 __streambuf_type* __sb = __in.rdbuf();
1116 __int_type __c = __sb->sgetc();
1118 while (!_Traits::eq_int_type(__c, __eof)
1119 && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1120 __c = __sb->snextc();
1122 if (_Traits::eq_int_type(__c, __eof))
1123 __in.setstate(ios_base::eofbit);
1127 // 21.3.7.9 basic_string::getline and operators
1128 template<typename _CharT, typename _Traits, typename _Alloc>
1129 basic_istream<_CharT, _Traits>&
1130 operator>>(basic_istream<_CharT, _Traits>& __in,
1131 basic_string<_CharT, _Traits, _Alloc>& __str)
1133 typedef basic_istream<_CharT, _Traits> __istream_type;
1134 typedef typename __istream_type::int_type __int_type;
1135 typedef typename __istream_type::__streambuf_type __streambuf_type;
1136 typedef typename __istream_type::__ctype_type __ctype_type;
1137 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1138 typedef typename __string_type::size_type __size_type;
1140 __size_type __extracted = 0;
1141 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1142 typename __istream_type::sentry __cerb(__in, false);
1147 // Avoid reallocation for common case.
1150 __size_type __len = 0;
1151 const streamsize __w = __in.width();
1152 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
1154 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1155 const __int_type __eof = _Traits::eof();
1156 __streambuf_type* __sb = __in.rdbuf();
1157 __int_type __c = __sb->sgetc();
1159 while (__extracted < __n
1160 && !_Traits::eq_int_type(__c, __eof)
1161 && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1163 if (__len == sizeof(__buf) / sizeof(_CharT))
1165 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1168 __buf[__len++] = _Traits::to_char_type(__c);
1170 __c = __sb->snextc();
1172 __str.append(__buf, __len);
1174 if (_Traits::eq_int_type(__c, __eof))
1175 __err |= ios_base::eofbit;
1180 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1181 // 91. Description of operator>> and getline() for string<>
1182 // might cause endless loop
1183 __in._M_setstate(ios_base::badbit);
1186 // 211. operator>>(istream&, string&) doesn't set failbit
1188 __err |= ios_base::failbit;
1190 __in.setstate(__err);
1194 template<typename _CharT, typename _Traits, typename _Alloc>
1195 basic_istream<_CharT, _Traits>&
1196 getline(basic_istream<_CharT, _Traits>& __in,
1197 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1199 typedef basic_istream<_CharT, _Traits> __istream_type;
1200 typedef typename __istream_type::int_type __int_type;
1201 typedef typename __istream_type::__streambuf_type __streambuf_type;
1202 typedef typename __istream_type::__ctype_type __ctype_type;
1203 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1204 typedef typename __string_type::size_type __size_type;
1206 __size_type __extracted = 0;
1207 const __size_type __n = __str.max_size();
1208 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1209 typename __istream_type::sentry __cerb(__in, true);
1215 const __int_type __idelim = _Traits::to_int_type(__delim);
1216 const __int_type __eof = _Traits::eof();
1217 __streambuf_type* __sb = __in.rdbuf();
1218 __int_type __c = __sb->sgetc();
1220 while (__extracted < __n
1221 && !_Traits::eq_int_type(__c, __eof)
1222 && !_Traits::eq_int_type(__c, __idelim))
1224 __str += _Traits::to_char_type(__c);
1226 __c = __sb->snextc();
1229 if (_Traits::eq_int_type(__c, __eof))
1230 __err |= ios_base::eofbit;
1231 else if (_Traits::eq_int_type(__c, __idelim))
1237 __err |= ios_base::failbit;
1241 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1242 // 91. Description of operator>> and getline() for string<>
1243 // might cause endless loop
1244 __in._M_setstate(ios_base::badbit);
1248 __err |= ios_base::failbit;
1250 __in.setstate(__err);
1254 template<class _CharT, class _Traits, class _Alloc>
1255 inline basic_istream<_CharT,_Traits>&
1256 getline(basic_istream<_CharT, _Traits>& __in,
1257 basic_string<_CharT,_Traits,_Alloc>& __str)
1258 { return getline(__in, __str, __in.widen('\n')); }
1260 // Inhibit implicit instantiations for required instantiations,
1261 // which are defined via explicit instantiations elsewhere.
1262 // NB: This syntax is a GNU extension.
1263 #if _GLIBCXX_EXTERN_TEMPLATE
1264 extern template class basic_istream<char>;
1265 extern template istream& ws(istream&);
1266 extern template istream& operator>>(istream&, char&);
1267 extern template istream& operator>>(istream&, char*);
1268 extern template istream& operator>>(istream&, unsigned char&);
1269 extern template istream& operator>>(istream&, signed char&);
1270 extern template istream& operator>>(istream&, unsigned char*);
1271 extern template istream& operator>>(istream&, signed char*);
1273 extern template class basic_iostream<char>;
1275 #ifdef _GLIBCXX_USE_WCHAR_T
1276 extern template class basic_istream<wchar_t>;
1277 extern template wistream& ws(wistream&);
1278 extern template wistream& operator>>(wistream&, wchar_t&);
1279 extern template wistream& operator>>(wistream&, wchar_t*);
1281 extern template class basic_iostream<wchar_t>;