1 // istream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 // ISO C++ 14882: 27.6.1 Input streams
36 #define _ISTREAM_TCC 1
38 #pragma GCC system_header
41 #include <ostream> // For flush()
45 template<typename _CharT, typename _Traits>
46 basic_istream<_CharT, _Traits>::sentry::
47 sentry(basic_istream<_CharT, _Traits>& __in, bool __noskip) : _M_ok(false)
49 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
54 if (!__noskip && (__in.flags() & ios_base::skipws))
56 const __int_type __eof = traits_type::eof();
57 __streambuf_type* __sb = __in.rdbuf();
58 __int_type __c = __sb->sgetc();
60 const __ctype_type& __ct = __check_facet(__in._M_ctype);
61 while (!traits_type::eq_int_type(__c, __eof)
62 && __ct.is(ctype_base::space,
63 traits_type::to_char_type(__c)))
66 // _GLIBCXX_RESOLVE_LIB_DEFECTS
67 // 195. Should basic_istream::sentry's constructor ever
69 if (traits_type::eq_int_type(__c, __eof))
70 __err |= ios_base::eofbit;
74 if (__in.good() && __err == ios_base::goodbit)
78 __err |= ios_base::failbit;
83 template<typename _CharT, typename _Traits>
84 basic_istream<_CharT, _Traits>&
85 basic_istream<_CharT, _Traits>::
86 operator>>(__istream_type& (*__pf)(__istream_type&))
87 { return __pf(*this); }
89 template<typename _CharT, typename _Traits>
90 basic_istream<_CharT, _Traits>&
91 basic_istream<_CharT, _Traits>::
92 operator>>(__ios_type& (*__pf)(__ios_type&))
98 template<typename _CharT, typename _Traits>
99 basic_istream<_CharT, _Traits>&
100 basic_istream<_CharT, _Traits>::
101 operator>>(ios_base& (*__pf)(ios_base&))
107 template<typename _CharT, typename _Traits>
108 basic_istream<_CharT, _Traits>&
109 basic_istream<_CharT, _Traits>::
110 operator>>(bool& __n)
112 sentry __cerb(*this, false);
115 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
118 const __num_get_type& __ng = __check_facet(this->_M_num_get);
119 __ng.get(*this, 0, *this, __err, __n);
122 { this->_M_setstate(ios_base::badbit); }
124 this->setstate(__err);
129 template<typename _CharT, typename _Traits>
130 basic_istream<_CharT, _Traits>&
131 basic_istream<_CharT, _Traits>::
132 operator>>(short& __n)
134 sentry __cerb(*this, false);
137 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
141 const __num_get_type& __ng = __check_facet(this->_M_num_get);
142 __ng.get(*this, 0, *this, __err, __l);
143 // _GLIBCXX_RESOLVE_LIB_DEFECTS
144 // 118. basic_istream uses nonexistent num_get member functions.
145 if (!(__err & ios_base::failbit)
146 && (numeric_limits<short>::min() <= __l
147 && __l <= numeric_limits<short>::max()))
150 __err |= ios_base::failbit;
153 { this->_M_setstate(ios_base::badbit); }
155 this->setstate(__err);
160 template<typename _CharT, typename _Traits>
161 basic_istream<_CharT, _Traits>&
162 basic_istream<_CharT, _Traits>::
163 operator>>(unsigned short& __n)
165 sentry __cerb(*this, false);
168 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
171 const __num_get_type& __ng = __check_facet(this->_M_num_get);
172 __ng.get(*this, 0, *this, __err, __n);
175 { this->_M_setstate(ios_base::badbit); }
177 this->setstate(__err);
182 template<typename _CharT, typename _Traits>
183 basic_istream<_CharT, _Traits>&
184 basic_istream<_CharT, _Traits>::
187 sentry __cerb(*this, false);
190 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
194 const __num_get_type& __ng = __check_facet(this->_M_num_get);
195 __ng.get(*this, 0, *this, __err, __l);
196 // _GLIBCXX_RESOLVE_LIB_DEFECTS
197 // 118. basic_istream uses nonexistent num_get member functions.
198 if (!(__err & ios_base::failbit)
199 && (numeric_limits<int>::min() <= __l
200 && __l <= numeric_limits<int>::max()))
203 __err |= ios_base::failbit;
206 { this->_M_setstate(ios_base::badbit); }
208 this->setstate(__err);
213 template<typename _CharT, typename _Traits>
214 basic_istream<_CharT, _Traits>&
215 basic_istream<_CharT, _Traits>::
216 operator>>(unsigned int& __n)
218 sentry __cerb(*this, false);
221 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
224 const __num_get_type& __ng = __check_facet(this->_M_num_get);
225 __ng.get(*this, 0, *this, __err, __n);
228 { this->_M_setstate(ios_base::badbit); }
230 this->setstate(__err);
235 template<typename _CharT, typename _Traits>
236 basic_istream<_CharT, _Traits>&
237 basic_istream<_CharT, _Traits>::
238 operator>>(long& __n)
240 sentry __cerb(*this, false);
243 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
246 const __num_get_type& __ng = __check_facet(this->_M_num_get);
247 __ng.get(*this, 0, *this, __err, __n);
250 { this->_M_setstate(ios_base::badbit); }
252 this->setstate(__err);
257 template<typename _CharT, typename _Traits>
258 basic_istream<_CharT, _Traits>&
259 basic_istream<_CharT, _Traits>::
260 operator>>(unsigned long& __n)
262 sentry __cerb(*this, false);
265 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
268 const __num_get_type& __ng = __check_facet(this->_M_num_get);
269 __ng.get(*this, 0, *this, __err, __n);
272 { this->_M_setstate(ios_base::badbit); }
274 this->setstate(__err);
279 #ifdef _GLIBCXX_USE_LONG_LONG
280 template<typename _CharT, typename _Traits>
281 basic_istream<_CharT, _Traits>&
282 basic_istream<_CharT, _Traits>::
283 operator>>(long long& __n)
285 sentry __cerb(*this, false);
288 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
291 const __num_get_type& __ng = __check_facet(this->_M_num_get);
292 __ng.get(*this, 0, *this, __err, __n);
295 { this->_M_setstate(ios_base::badbit); }
297 this->setstate(__err);
302 template<typename _CharT, typename _Traits>
303 basic_istream<_CharT, _Traits>&
304 basic_istream<_CharT, _Traits>::
305 operator>>(unsigned long long& __n)
307 sentry __cerb(*this, false);
310 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
313 const __num_get_type& __ng = __check_facet(this->_M_num_get);
314 __ng.get(*this, 0, *this, __err, __n);
317 { this->_M_setstate(ios_base::badbit); }
319 this->setstate(__err);
325 template<typename _CharT, typename _Traits>
326 basic_istream<_CharT, _Traits>&
327 basic_istream<_CharT, _Traits>::
328 operator>>(float& __n)
330 sentry __cerb(*this, false);
333 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
336 const __num_get_type& __ng = __check_facet(this->_M_num_get);
337 __ng.get(*this, 0, *this, __err, __n);
340 { this->_M_setstate(ios_base::badbit); }
342 this->setstate(__err);
347 template<typename _CharT, typename _Traits>
348 basic_istream<_CharT, _Traits>&
349 basic_istream<_CharT, _Traits>::
350 operator>>(double& __n)
352 sentry __cerb(*this, false);
355 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
358 const __num_get_type& __ng = __check_facet(this->_M_num_get);
359 __ng.get(*this, 0, *this, __err, __n);
362 { this->_M_setstate(ios_base::badbit); }
364 this->setstate(__err);
369 template<typename _CharT, typename _Traits>
370 basic_istream<_CharT, _Traits>&
371 basic_istream<_CharT, _Traits>::
372 operator>>(long double& __n)
374 sentry __cerb(*this, false);
377 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
380 const __num_get_type& __ng = __check_facet(this->_M_num_get);
381 __ng.get(*this, 0, *this, __err, __n);
384 { this->_M_setstate(ios_base::badbit); }
386 this->setstate(__err);
391 template<typename _CharT, typename _Traits>
392 basic_istream<_CharT, _Traits>&
393 basic_istream<_CharT, _Traits>::
394 operator>>(void*& __n)
396 sentry __cerb(*this, false);
399 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
402 const __num_get_type& __ng = __check_facet(this->_M_num_get);
403 __ng.get(*this, 0, *this, __err, __n);
406 { this->_M_setstate(ios_base::badbit); }
408 this->setstate(__err);
413 template<typename _CharT, typename _Traits>
414 basic_istream<_CharT, _Traits>&
415 basic_istream<_CharT, _Traits>::
416 operator>>(__streambuf_type* __sbout)
418 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
419 sentry __cerb(*this, false);
420 if (__cerb && __sbout)
424 if (!__copy_streambufs(this->rdbuf(), __sbout))
425 __err |= ios_base::failbit;
428 { this->_M_setstate(ios_base::failbit); }
431 __err |= ios_base::failbit;
433 this->setstate(__err);
437 template<typename _CharT, typename _Traits>
438 typename basic_istream<_CharT, _Traits>::int_type
439 basic_istream<_CharT, _Traits>::
442 const int_type __eof = traits_type::eof();
443 int_type __c = __eof;
445 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
446 sentry __cerb(*this, true);
451 __c = this->rdbuf()->sbumpc();
452 // 27.6.1.1 paragraph 3
453 if (!traits_type::eq_int_type(__c, __eof))
456 __err |= ios_base::eofbit;
459 { this->_M_setstate(ios_base::badbit); }
462 __err |= ios_base::failbit;
464 this->setstate(__err);
468 template<typename _CharT, typename _Traits>
469 basic_istream<_CharT, _Traits>&
470 basic_istream<_CharT, _Traits>::
474 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
475 sentry __cerb(*this, true);
480 const int_type __cb = this->rdbuf()->sbumpc();
481 // 27.6.1.1 paragraph 3
482 if (!traits_type::eq_int_type(__cb, traits_type::eof()))
485 __c = traits_type::to_char_type(__cb);
488 __err |= ios_base::eofbit;
491 { this->_M_setstate(ios_base::badbit); }
494 __err |= ios_base::failbit;
496 this->setstate(__err);
500 template<typename _CharT, typename _Traits>
501 basic_istream<_CharT, _Traits>&
502 basic_istream<_CharT, _Traits>::
503 get(char_type* __s, streamsize __n, char_type __delim)
506 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
507 sentry __cerb(*this, true);
512 const int_type __idelim = traits_type::to_int_type(__delim);
513 const int_type __eof = traits_type::eof();
514 __streambuf_type* __sb = this->rdbuf();
515 int_type __c = __sb->sgetc();
517 while (_M_gcount + 1 < __n
518 && !traits_type::eq_int_type(__c, __eof)
519 && !traits_type::eq_int_type(__c, __idelim))
521 *__s++ = traits_type::to_char_type(__c);
523 __c = __sb->snextc();
525 if (traits_type::eq_int_type(__c, __eof))
526 __err |= ios_base::eofbit;
529 { this->_M_setstate(ios_base::badbit); }
533 __err |= ios_base::failbit;
535 this->setstate(__err);
539 template<typename _CharT, typename _Traits>
540 basic_istream<_CharT, _Traits>&
541 basic_istream<_CharT, _Traits>::
542 get(__streambuf_type& __sb, char_type __delim)
545 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
546 sentry __cerb(*this, true);
551 const int_type __idelim = traits_type::to_int_type(__delim);
552 const int_type __eof = traits_type::eof();
553 __streambuf_type* __this_sb = this->rdbuf();
554 int_type __c = __this_sb->sgetc();
555 char_type __c2 = traits_type::to_char_type(__c);
557 while (!traits_type::eq_int_type(__c, __eof)
558 && !traits_type::eq_int_type(__c, __idelim)
559 && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
562 __c = __this_sb->snextc();
563 __c2 = traits_type::to_char_type(__c);
565 if (traits_type::eq_int_type(__c, __eof))
566 __err |= ios_base::eofbit;
569 { this->_M_setstate(ios_base::badbit); }
572 __err |= ios_base::failbit;
574 this->setstate(__err);
578 template<typename _CharT, typename _Traits>
579 basic_istream<_CharT, _Traits>&
580 basic_istream<_CharT, _Traits>::
581 getline(char_type* __s, streamsize __n, char_type __delim)
584 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
585 sentry __cerb(*this, true);
590 const int_type __idelim = traits_type::to_int_type(__delim);
591 const int_type __eof = traits_type::eof();
592 __streambuf_type* __sb = this->rdbuf();
593 int_type __c = __sb->sgetc();
595 while (_M_gcount + 1 < __n
596 && !traits_type::eq_int_type(__c, __eof)
597 && !traits_type::eq_int_type(__c, __idelim))
599 streamsize __size = std::min(streamsize(__sb->egptr()
601 streamsize(__n - _M_gcount
605 const char_type* __p = traits_type::find(__sb->gptr(),
609 __size = __p - __sb->gptr();
610 traits_type::copy(__s, __sb->gptr(), __size);
618 *__s++ = traits_type::to_char_type(__c);
620 __c = __sb->snextc();
624 if (traits_type::eq_int_type(__c, __eof))
625 __err |= ios_base::eofbit;
626 else if (traits_type::eq_int_type(__c, __idelim))
632 __err |= ios_base::failbit;
635 { this->_M_setstate(ios_base::badbit); }
639 __err |= ios_base::failbit;
641 this->setstate(__err);
645 // We provide three overloads, since the first two are much simpler
646 // than the general case. Also, the latter two can thus adopt the
647 // same "batchy" strategy used by getline above.
648 template<typename _CharT, typename _Traits>
649 basic_istream<_CharT, _Traits>&
650 basic_istream<_CharT, _Traits>::
654 sentry __cerb(*this, true);
657 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
660 const int_type __eof = traits_type::eof();
661 __streambuf_type* __sb = this->rdbuf();
663 if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
664 __err |= ios_base::eofbit;
669 { this->_M_setstate(ios_base::badbit); }
671 this->setstate(__err);
676 template<typename _CharT, typename _Traits>
677 basic_istream<_CharT, _Traits>&
678 basic_istream<_CharT, _Traits>::
679 ignore(streamsize __n)
685 sentry __cerb(*this, true);
686 if (__cerb && __n > 0)
688 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
691 const int_type __eof = traits_type::eof();
692 __streambuf_type* __sb = this->rdbuf();
693 int_type __c = __sb->sgetc();
695 const bool __bound = __n != numeric_limits<streamsize>::max();
698 while (_M_gcount <= __n
699 && !traits_type::eq_int_type(__c, __eof))
701 streamsize __size = __sb->egptr() - __sb->gptr();
703 __size = std::min(__size, streamsize(__n - _M_gcount + 1));
714 __c = __sb->snextc();
717 if (traits_type::eq_int_type(__c, __eof))
718 __err |= ios_base::eofbit;
721 { this->_M_setstate(ios_base::badbit); }
723 this->setstate(__err);
728 template<typename _CharT, typename _Traits>
729 basic_istream<_CharT, _Traits>&
730 basic_istream<_CharT, _Traits>::
731 ignore(streamsize __n, int_type __delim)
733 if (traits_type::eq_int_type(__delim, traits_type::eof()))
737 sentry __cerb(*this, true);
738 if (__cerb && __n > 0)
740 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
743 const char_type __cdelim = traits_type::to_char_type(__delim);
744 const int_type __eof = traits_type::eof();
745 __streambuf_type* __sb = this->rdbuf();
746 int_type __c = __sb->sgetc();
748 const bool __bound = __n != numeric_limits<streamsize>::max();
751 while (_M_gcount <= __n
752 && !traits_type::eq_int_type(__c, __eof)
753 && !traits_type::eq_int_type(__c, __delim))
755 streamsize __size = __sb->egptr() - __sb->gptr();
757 __size = std::min(__size, streamsize(__n - _M_gcount + 1));
761 const char_type* __p = traits_type::find(__sb->gptr(),
765 __size = __p - __sb->gptr();
773 __c = __sb->snextc();
776 if (traits_type::eq_int_type(__c, __eof))
777 __err |= ios_base::eofbit;
778 else if (traits_type::eq_int_type(__c, __delim))
785 { this->_M_setstate(ios_base::badbit); }
787 this->setstate(__err);
792 template<typename _CharT, typename _Traits>
793 typename basic_istream<_CharT, _Traits>::int_type
794 basic_istream<_CharT, _Traits>::
797 int_type __c = traits_type::eof();
799 sentry __cerb(*this, true);
802 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
805 __c = this->rdbuf()->sgetc();
806 if (traits_type::eq_int_type(__c, traits_type::eof()))
807 __err |= ios_base::eofbit;
810 { this->_M_setstate(ios_base::badbit); }
812 this->setstate(__err);
817 template<typename _CharT, typename _Traits>
818 basic_istream<_CharT, _Traits>&
819 basic_istream<_CharT, _Traits>::
820 read(char_type* __s, streamsize __n)
823 sentry __cerb(*this, true);
826 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
829 _M_gcount = this->rdbuf()->sgetn(__s, __n);
830 if (_M_gcount != __n)
831 __err |= (ios_base::eofbit | ios_base::failbit);
834 { this->_M_setstate(ios_base::badbit); }
836 this->setstate(__err);
841 template<typename _CharT, typename _Traits>
843 basic_istream<_CharT, _Traits>::
844 readsome(char_type* __s, streamsize __n)
847 sentry __cerb(*this, true);
850 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
853 // Cannot compare int_type with streamsize generically.
854 const streamsize __num = this->rdbuf()->in_avail();
856 _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
857 else if (__num == -1)
858 __err |= ios_base::eofbit;
861 { this->_M_setstate(ios_base::badbit); }
863 this->setstate(__err);
868 template<typename _CharT, typename _Traits>
869 basic_istream<_CharT, _Traits>&
870 basic_istream<_CharT, _Traits>::
871 putback(char_type __c)
873 // _GLIBCXX_RESOLVE_LIB_DEFECTS
874 // 60. What is a formatted input function?
876 sentry __cerb(*this, true);
879 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
882 const int_type __eof = traits_type::eof();
883 __streambuf_type* __sb = this->rdbuf();
885 || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
886 __err |= ios_base::badbit;
889 { this->_M_setstate(ios_base::badbit); }
891 this->setstate(__err);
896 template<typename _CharT, typename _Traits>
897 basic_istream<_CharT, _Traits>&
898 basic_istream<_CharT, _Traits>::
901 // _GLIBCXX_RESOLVE_LIB_DEFECTS
902 // 60. What is a formatted input function?
904 sentry __cerb(*this, true);
907 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
910 const int_type __eof = traits_type::eof();
911 __streambuf_type* __sb = this->rdbuf();
913 || traits_type::eq_int_type(__sb->sungetc(), __eof))
914 __err |= ios_base::badbit;
917 { this->_M_setstate(ios_base::badbit); }
919 this->setstate(__err);
924 template<typename _CharT, typename _Traits>
926 basic_istream<_CharT, _Traits>::
929 // _GLIBCXX_RESOLVE_LIB_DEFECTS
930 // DR60. Do not change _M_gcount.
932 sentry __cerb(*this, true);
935 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
938 __streambuf_type* __sb = this->rdbuf();
941 if (__sb->pubsync() == -1)
942 __err |= ios_base::badbit;
948 { this->_M_setstate(ios_base::badbit); }
950 this->setstate(__err);
955 template<typename _CharT, typename _Traits>
956 typename basic_istream<_CharT, _Traits>::pos_type
957 basic_istream<_CharT, _Traits>::
960 // _GLIBCXX_RESOLVE_LIB_DEFECTS
961 // DR60. Do not change _M_gcount.
962 pos_type __ret = pos_type(-1);
966 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
969 { this->_M_setstate(ios_base::badbit); }
973 template<typename _CharT, typename _Traits>
974 basic_istream<_CharT, _Traits>&
975 basic_istream<_CharT, _Traits>::
976 seekg(pos_type __pos)
978 // _GLIBCXX_RESOLVE_LIB_DEFECTS
979 // DR60. Do not change _M_gcount.
980 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
985 // 136. seekp, seekg setting wrong streams?
986 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
989 // 129. Need error indication from seekp() and seekg()
990 if (__p == pos_type(off_type(-1)))
991 __err |= ios_base::failbit;
995 { this->_M_setstate(ios_base::badbit); }
997 this->setstate(__err);
1001 template<typename _CharT, typename _Traits>
1002 basic_istream<_CharT, _Traits>&
1003 basic_istream<_CharT, _Traits>::
1004 seekg(off_type __off, ios_base::seekdir __dir)
1006 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1007 // DR60. Do not change _M_gcount.
1008 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1013 // 136. seekp, seekg setting wrong streams?
1014 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
1017 // 129. Need error indication from seekp() and seekg()
1018 if (__p == pos_type(off_type(-1)))
1019 __err |= ios_base::failbit;
1023 { this->_M_setstate(ios_base::badbit); }
1025 this->setstate(__err);
1029 // 27.6.1.2.3 Character extraction templates
1030 template<typename _CharT, typename _Traits>
1031 basic_istream<_CharT, _Traits>&
1032 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
1034 typedef basic_istream<_CharT, _Traits> __istream_type;
1035 typedef typename __istream_type::int_type __int_type;
1037 typename __istream_type::sentry __cerb(__in, false);
1040 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1043 const __int_type __cb = __in.rdbuf()->sbumpc();
1044 if (!_Traits::eq_int_type(__cb, _Traits::eof()))
1045 __c = _Traits::to_char_type(__cb);
1047 __err |= (ios_base::eofbit | ios_base::failbit);
1050 { __in._M_setstate(ios_base::badbit); }
1052 __in.setstate(__err);
1057 template<typename _CharT, typename _Traits>
1058 basic_istream<_CharT, _Traits>&
1059 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
1061 typedef basic_istream<_CharT, _Traits> __istream_type;
1062 typedef typename __istream_type::__streambuf_type __streambuf_type;
1063 typedef typename _Traits::int_type int_type;
1064 typedef _CharT char_type;
1065 typedef ctype<_CharT> __ctype_type;
1067 streamsize __extracted = 0;
1068 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1069 typename __istream_type::sentry __cerb(__in, false);
1074 // Figure out how many characters to extract.
1075 streamsize __num = __in.width();
1077 __num = numeric_limits<streamsize>::max();
1079 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1081 const int_type __eof = _Traits::eof();
1082 __streambuf_type* __sb = __in.rdbuf();
1083 int_type __c = __sb->sgetc();
1085 while (__extracted < __num - 1
1086 && !_Traits::eq_int_type(__c, __eof)
1087 && !__ct.is(ctype_base::space,
1088 _Traits::to_char_type(__c)))
1090 *__s++ = _Traits::to_char_type(__c);
1092 __c = __sb->snextc();
1094 if (_Traits::eq_int_type(__c, __eof))
1095 __err |= ios_base::eofbit;
1097 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1098 // 68. Extractors for char* should store null at end
1103 { __in._M_setstate(ios_base::badbit); }
1106 __err |= ios_base::failbit;
1108 __in.setstate(__err);
1112 // 27.6.1.4 Standard basic_istream manipulators
1113 template<typename _CharT, typename _Traits>
1114 basic_istream<_CharT,_Traits>&
1115 ws(basic_istream<_CharT,_Traits>& __in)
1117 typedef basic_istream<_CharT, _Traits> __istream_type;
1118 typedef typename __istream_type::__streambuf_type __streambuf_type;
1119 typedef typename __istream_type::__ctype_type __ctype_type;
1120 typedef typename __istream_type::int_type __int_type;
1122 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1123 const __int_type __eof = _Traits::eof();
1124 __streambuf_type* __sb = __in.rdbuf();
1125 __int_type __c = __sb->sgetc();
1127 while (!_Traits::eq_int_type(__c, __eof)
1128 && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1129 __c = __sb->snextc();
1131 if (_Traits::eq_int_type(__c, __eof))
1132 __in.setstate(ios_base::eofbit);
1136 // 21.3.7.9 basic_string::getline and operators
1137 template<typename _CharT, typename _Traits, typename _Alloc>
1138 basic_istream<_CharT, _Traits>&
1139 operator>>(basic_istream<_CharT, _Traits>& __in,
1140 basic_string<_CharT, _Traits, _Alloc>& __str)
1142 typedef basic_istream<_CharT, _Traits> __istream_type;
1143 typedef typename __istream_type::int_type __int_type;
1144 typedef typename __istream_type::__streambuf_type __streambuf_type;
1145 typedef typename __istream_type::__ctype_type __ctype_type;
1146 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1147 typedef typename __string_type::size_type __size_type;
1149 __size_type __extracted = 0;
1150 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1151 typename __istream_type::sentry __cerb(__in, false);
1156 // Avoid reallocation for common case.
1159 __size_type __len = 0;
1160 const streamsize __w = __in.width();
1161 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
1163 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1164 const __int_type __eof = _Traits::eof();
1165 __streambuf_type* __sb = __in.rdbuf();
1166 __int_type __c = __sb->sgetc();
1168 while (__extracted < __n
1169 && !_Traits::eq_int_type(__c, __eof)
1170 && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1172 if (__len == sizeof(__buf) / sizeof(_CharT))
1174 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1177 __buf[__len++] = _Traits::to_char_type(__c);
1179 __c = __sb->snextc();
1181 __str.append(__buf, __len);
1183 if (_Traits::eq_int_type(__c, __eof))
1184 __err |= ios_base::eofbit;
1189 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1190 // 91. Description of operator>> and getline() for string<>
1191 // might cause endless loop
1192 __in._M_setstate(ios_base::badbit);
1195 // 211. operator>>(istream&, string&) doesn't set failbit
1197 __err |= ios_base::failbit;
1199 __in.setstate(__err);
1203 template<typename _CharT, typename _Traits, typename _Alloc>
1204 basic_istream<_CharT, _Traits>&
1205 getline(basic_istream<_CharT, _Traits>& __in,
1206 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1208 typedef basic_istream<_CharT, _Traits> __istream_type;
1209 typedef typename __istream_type::int_type __int_type;
1210 typedef typename __istream_type::__streambuf_type __streambuf_type;
1211 typedef typename __istream_type::__ctype_type __ctype_type;
1212 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1213 typedef typename __string_type::size_type __size_type;
1215 __size_type __extracted = 0;
1216 const __size_type __n = __str.max_size();
1217 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1218 typename __istream_type::sentry __cerb(__in, true);
1223 // Avoid reallocation for common case.
1226 __size_type __len = 0;
1227 const __int_type __idelim = _Traits::to_int_type(__delim);
1228 const __int_type __eof = _Traits::eof();
1229 __streambuf_type* __sb = __in.rdbuf();
1230 __int_type __c = __sb->sgetc();
1232 while (__extracted < __n
1233 && !_Traits::eq_int_type(__c, __eof)
1234 && !_Traits::eq_int_type(__c, __idelim))
1236 if (__len == sizeof(__buf) / sizeof(_CharT))
1238 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1241 __buf[__len++] = _Traits::to_char_type(__c);
1243 __c = __sb->snextc();
1245 __str.append(__buf, __len);
1247 if (_Traits::eq_int_type(__c, __eof))
1248 __err |= ios_base::eofbit;
1249 else if (_Traits::eq_int_type(__c, __idelim))
1255 __err |= ios_base::failbit;
1259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1260 // 91. Description of operator>> and getline() for string<>
1261 // might cause endless loop
1262 __in._M_setstate(ios_base::badbit);
1266 __err |= ios_base::failbit;
1268 __in.setstate(__err);
1272 template<class _CharT, class _Traits, class _Alloc>
1273 inline basic_istream<_CharT,_Traits>&
1274 getline(basic_istream<_CharT, _Traits>& __in,
1275 basic_string<_CharT,_Traits,_Alloc>& __str)
1276 { return getline(__in, __str, __in.widen('\n')); }
1278 // Inhibit implicit instantiations for required instantiations,
1279 // which are defined via explicit instantiations elsewhere.
1280 // NB: This syntax is a GNU extension.
1281 #if _GLIBCXX_EXTERN_TEMPLATE
1282 extern template class basic_istream<char>;
1283 extern template istream& ws(istream&);
1284 extern template istream& operator>>(istream&, char&);
1285 extern template istream& operator>>(istream&, char*);
1286 extern template istream& operator>>(istream&, unsigned char&);
1287 extern template istream& operator>>(istream&, signed char&);
1288 extern template istream& operator>>(istream&, unsigned char*);
1289 extern template istream& operator>>(istream&, signed char*);
1291 #ifdef _GLIBCXX_USE_WCHAR_T
1292 extern template class basic_istream<wchar_t>;
1293 extern template wistream& ws(wistream&);
1294 extern template wistream& operator>>(wistream&, wchar_t&);
1295 extern template wistream& operator>>(wistream&, wchar_t*);