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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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)
678 sentry __cerb(*this, true);
679 if (__cerb && __n > 0)
681 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
684 const int_type __eof = traits_type::eof();
685 __streambuf_type* __sb = this->rdbuf();
686 int_type __c = __sb->sgetc();
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.
697 while (_M_gcount < __n
698 && !traits_type::eq_int_type(__c, __eof))
701 __c = __sb->snextc();
703 if (__n == numeric_limits<streamsize>::max()
704 && !traits_type::eq_int_type(__c, __eof))
705 _M_gcount = numeric_limits<streamsize>::min();
710 if (traits_type::eq_int_type(__c, __eof))
711 __err |= ios_base::eofbit;
714 { this->_M_setstate(ios_base::badbit); }
716 this->setstate(__err);
721 template<typename _CharT, typename _Traits>
722 basic_istream<_CharT, _Traits>&
723 basic_istream<_CharT, _Traits>::
724 ignore(streamsize __n, int_type __delim)
726 if (traits_type::eq_int_type(__delim, traits_type::eof()))
730 sentry __cerb(*this, true);
731 if (__cerb && __n > 0)
733 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
736 const int_type __eof = traits_type::eof();
737 __streambuf_type* __sb = this->rdbuf();
738 int_type __c = __sb->sgetc();
740 // See comment above.
743 while (_M_gcount < __n
744 && !traits_type::eq_int_type(__c, __eof)
745 && !traits_type::eq_int_type(__c, __delim))
748 __c = __sb->snextc();
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();
758 if (traits_type::eq_int_type(__c, __eof))
759 __err |= ios_base::eofbit;
760 else if (traits_type::eq_int_type(__c, __delim))
767 { this->_M_setstate(ios_base::badbit); }
769 this->setstate(__err);
774 template<typename _CharT, typename _Traits>
775 typename basic_istream<_CharT, _Traits>::int_type
776 basic_istream<_CharT, _Traits>::
779 int_type __c = traits_type::eof();
781 sentry __cerb(*this, true);
784 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
787 __c = this->rdbuf()->sgetc();
788 if (traits_type::eq_int_type(__c, traits_type::eof()))
789 __err |= ios_base::eofbit;
792 { this->_M_setstate(ios_base::badbit); }
794 this->setstate(__err);
799 template<typename _CharT, typename _Traits>
800 basic_istream<_CharT, _Traits>&
801 basic_istream<_CharT, _Traits>::
802 read(char_type* __s, streamsize __n)
805 sentry __cerb(*this, true);
808 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
811 _M_gcount = this->rdbuf()->sgetn(__s, __n);
812 if (_M_gcount != __n)
813 __err |= (ios_base::eofbit | ios_base::failbit);
816 { this->_M_setstate(ios_base::badbit); }
818 this->setstate(__err);
823 template<typename _CharT, typename _Traits>
825 basic_istream<_CharT, _Traits>::
826 readsome(char_type* __s, streamsize __n)
829 sentry __cerb(*this, true);
832 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
835 // Cannot compare int_type with streamsize generically.
836 const streamsize __num = this->rdbuf()->in_avail();
838 _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
839 else if (__num == -1)
840 __err |= ios_base::eofbit;
843 { this->_M_setstate(ios_base::badbit); }
845 this->setstate(__err);
850 template<typename _CharT, typename _Traits>
851 basic_istream<_CharT, _Traits>&
852 basic_istream<_CharT, _Traits>::
853 putback(char_type __c)
855 // _GLIBCXX_RESOLVE_LIB_DEFECTS
856 // 60. What is a formatted input function?
858 sentry __cerb(*this, true);
861 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
864 const int_type __eof = traits_type::eof();
865 __streambuf_type* __sb = this->rdbuf();
867 || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
868 __err |= ios_base::badbit;
871 { this->_M_setstate(ios_base::badbit); }
873 this->setstate(__err);
878 template<typename _CharT, typename _Traits>
879 basic_istream<_CharT, _Traits>&
880 basic_istream<_CharT, _Traits>::
883 // _GLIBCXX_RESOLVE_LIB_DEFECTS
884 // 60. What is a formatted input function?
886 sentry __cerb(*this, true);
889 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
892 const int_type __eof = traits_type::eof();
893 __streambuf_type* __sb = this->rdbuf();
895 || traits_type::eq_int_type(__sb->sungetc(), __eof))
896 __err |= ios_base::badbit;
899 { this->_M_setstate(ios_base::badbit); }
901 this->setstate(__err);
906 template<typename _CharT, typename _Traits>
908 basic_istream<_CharT, _Traits>::
911 // _GLIBCXX_RESOLVE_LIB_DEFECTS
912 // DR60. Do not change _M_gcount.
914 sentry __cerb(*this, true);
917 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
920 __streambuf_type* __sb = this->rdbuf();
923 if (__sb->pubsync() == -1)
924 __err |= ios_base::badbit;
930 { this->_M_setstate(ios_base::badbit); }
932 this->setstate(__err);
937 template<typename _CharT, typename _Traits>
938 typename basic_istream<_CharT, _Traits>::pos_type
939 basic_istream<_CharT, _Traits>::
942 // _GLIBCXX_RESOLVE_LIB_DEFECTS
943 // DR60. Do not change _M_gcount.
944 pos_type __ret = pos_type(-1);
948 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
951 { this->_M_setstate(ios_base::badbit); }
955 template<typename _CharT, typename _Traits>
956 basic_istream<_CharT, _Traits>&
957 basic_istream<_CharT, _Traits>::
958 seekg(pos_type __pos)
960 // _GLIBCXX_RESOLVE_LIB_DEFECTS
961 // DR60. Do not change _M_gcount.
962 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
967 // 136. seekp, seekg setting wrong streams?
968 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
971 // 129. Need error indication from seekp() and seekg()
972 if (__p == pos_type(off_type(-1)))
973 __err |= ios_base::failbit;
977 { this->_M_setstate(ios_base::badbit); }
979 this->setstate(__err);
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)
988 // _GLIBCXX_RESOLVE_LIB_DEFECTS
989 // DR60. Do not change _M_gcount.
990 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
995 // 136. seekp, seekg setting wrong streams?
996 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
999 // 129. Need error indication from seekp() and seekg()
1000 if (__p == pos_type(off_type(-1)))
1001 __err |= ios_base::failbit;
1005 { this->_M_setstate(ios_base::badbit); }
1007 this->setstate(__err);
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)
1016 typedef basic_istream<_CharT, _Traits> __istream_type;
1017 typedef typename __istream_type::int_type __int_type;
1019 typename __istream_type::sentry __cerb(__in, false);
1022 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1025 const __int_type __cb = __in.rdbuf()->sbumpc();
1026 if (!_Traits::eq_int_type(__cb, _Traits::eof()))
1027 __c = _Traits::to_char_type(__cb);
1029 __err |= (ios_base::eofbit | ios_base::failbit);
1032 { __in._M_setstate(ios_base::badbit); }
1034 __in.setstate(__err);
1039 template<typename _CharT, typename _Traits>
1040 basic_istream<_CharT, _Traits>&
1041 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
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;
1049 streamsize __extracted = 0;
1050 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1051 typename __istream_type::sentry __cerb(__in, false);
1056 // Figure out how many characters to extract.
1057 streamsize __num = __in.width();
1059 __num = numeric_limits<streamsize>::max();
1061 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1063 const int_type __eof = _Traits::eof();
1064 __streambuf_type* __sb = __in.rdbuf();
1065 int_type __c = __sb->sgetc();
1067 while (__extracted < __num - 1
1068 && !_Traits::eq_int_type(__c, __eof)
1069 && !__ct.is(ctype_base::space,
1070 _Traits::to_char_type(__c)))
1072 *__s++ = _Traits::to_char_type(__c);
1074 __c = __sb->snextc();
1076 if (_Traits::eq_int_type(__c, __eof))
1077 __err |= ios_base::eofbit;
1079 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1080 // 68. Extractors for char* should store null at end
1085 { __in._M_setstate(ios_base::badbit); }
1088 __err |= ios_base::failbit;
1090 __in.setstate(__err);
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)
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;
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();
1109 while (!_Traits::eq_int_type(__c, __eof)
1110 && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1111 __c = __sb->snextc();
1113 if (_Traits::eq_int_type(__c, __eof))
1114 __in.setstate(ios_base::eofbit);
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)
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;
1131 __size_type __extracted = 0;
1132 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1133 typename __istream_type::sentry __cerb(__in, false);
1138 // Avoid reallocation for common case.
1141 __size_type __len = 0;
1142 const streamsize __w = __in.width();
1143 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
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();
1150 while (__extracted < __n
1151 && !_Traits::eq_int_type(__c, __eof)
1152 && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1154 if (__len == sizeof(__buf) / sizeof(_CharT))
1156 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1159 __buf[__len++] = _Traits::to_char_type(__c);
1161 __c = __sb->snextc();
1163 __str.append(__buf, __len);
1165 if (_Traits::eq_int_type(__c, __eof))
1166 __err |= ios_base::eofbit;
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);
1177 // 211. operator>>(istream&, string&) doesn't set failbit
1179 __err |= ios_base::failbit;
1181 __in.setstate(__err);
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)
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;
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);
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();
1211 while (__extracted < __n
1212 && !_Traits::eq_int_type(__c, __eof)
1213 && !_Traits::eq_int_type(__c, __idelim))
1215 __str += _Traits::to_char_type(__c);
1217 __c = __sb->snextc();
1220 if (_Traits::eq_int_type(__c, __eof))
1221 __err |= ios_base::eofbit;
1222 else if (_Traits::eq_int_type(__c, __idelim))
1228 __err |= ios_base::failbit;
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);
1239 __err |= ios_base::failbit;
1241 __in.setstate(__err);
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')); }
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*);
1264 extern template class basic_iostream<char>;
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*);
1272 extern template class basic_iostream<wchar_t>;