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 __noskipws)
49 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
54 if (!__noskipws && (__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)
79 __err |= ios_base::failbit;
84 template<typename _CharT, typename _Traits>
85 basic_istream<_CharT, _Traits>&
86 basic_istream<_CharT, _Traits>::
87 operator>>(__istream_type& (*__pf)(__istream_type&))
88 { return __pf(*this); }
90 template<typename _CharT, typename _Traits>
91 basic_istream<_CharT, _Traits>&
92 basic_istream<_CharT, _Traits>::
93 operator>>(__ios_type& (*__pf)(__ios_type&))
99 template<typename _CharT, typename _Traits>
100 basic_istream<_CharT, _Traits>&
101 basic_istream<_CharT, _Traits>::
102 operator>>(ios_base& (*__pf)(ios_base&))
108 template<typename _CharT, typename _Traits>
109 basic_istream<_CharT, _Traits>&
110 basic_istream<_CharT, _Traits>::
111 operator>>(bool& __n)
113 sentry __cerb(*this, false);
116 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
119 const __num_get_type& __ng = __check_facet(this->_M_num_get);
120 __ng.get(*this, 0, *this, __err, __n);
123 { this->_M_setstate(ios_base::badbit); }
125 this->setstate(__err);
130 template<typename _CharT, typename _Traits>
131 basic_istream<_CharT, _Traits>&
132 basic_istream<_CharT, _Traits>::
133 operator>>(short& __n)
135 sentry __cerb(*this, false);
138 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
142 const __num_get_type& __ng = __check_facet(this->_M_num_get);
143 __ng.get(*this, 0, *this, __err, __l);
144 // _GLIBCXX_RESOLVE_LIB_DEFECTS
145 // 118. basic_istream uses nonexistent num_get member functions.
146 if (!(__err & ios_base::failbit)
147 && (numeric_limits<short>::min() <= __l
148 && __l <= numeric_limits<short>::max()))
151 __err |= ios_base::failbit;
154 { this->_M_setstate(ios_base::badbit); }
156 this->setstate(__err);
161 template<typename _CharT, typename _Traits>
162 basic_istream<_CharT, _Traits>&
163 basic_istream<_CharT, _Traits>::
164 operator>>(unsigned short& __n)
166 sentry __cerb(*this, false);
169 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
172 const __num_get_type& __ng = __check_facet(this->_M_num_get);
173 __ng.get(*this, 0, *this, __err, __n);
176 { this->_M_setstate(ios_base::badbit); }
178 this->setstate(__err);
183 template<typename _CharT, typename _Traits>
184 basic_istream<_CharT, _Traits>&
185 basic_istream<_CharT, _Traits>::
188 sentry __cerb(*this, false);
191 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
195 const __num_get_type& __ng = __check_facet(this->_M_num_get);
196 __ng.get(*this, 0, *this, __err, __l);
197 // _GLIBCXX_RESOLVE_LIB_DEFECTS
198 // 118. basic_istream uses nonexistent num_get member functions.
199 if (!(__err & ios_base::failbit)
200 && (numeric_limits<int>::min() <= __l
201 && __l <= numeric_limits<int>::max()))
204 __err |= ios_base::failbit;
207 { this->_M_setstate(ios_base::badbit); }
209 this->setstate(__err);
214 template<typename _CharT, typename _Traits>
215 basic_istream<_CharT, _Traits>&
216 basic_istream<_CharT, _Traits>::
217 operator>>(unsigned int& __n)
219 sentry __cerb(*this, false);
222 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
225 const __num_get_type& __ng = __check_facet(this->_M_num_get);
226 __ng.get(*this, 0, *this, __err, __n);
229 { this->_M_setstate(ios_base::badbit); }
231 this->setstate(__err);
236 template<typename _CharT, typename _Traits>
237 basic_istream<_CharT, _Traits>&
238 basic_istream<_CharT, _Traits>::
239 operator>>(long& __n)
241 sentry __cerb(*this, false);
244 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
247 const __num_get_type& __ng = __check_facet(this->_M_num_get);
248 __ng.get(*this, 0, *this, __err, __n);
251 { this->_M_setstate(ios_base::badbit); }
253 this->setstate(__err);
258 template<typename _CharT, typename _Traits>
259 basic_istream<_CharT, _Traits>&
260 basic_istream<_CharT, _Traits>::
261 operator>>(unsigned long& __n)
263 sentry __cerb(*this, false);
266 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
269 const __num_get_type& __ng = __check_facet(this->_M_num_get);
270 __ng.get(*this, 0, *this, __err, __n);
273 { this->_M_setstate(ios_base::badbit); }
275 this->setstate(__err);
280 #ifdef _GLIBCXX_USE_LONG_LONG
281 template<typename _CharT, typename _Traits>
282 basic_istream<_CharT, _Traits>&
283 basic_istream<_CharT, _Traits>::
284 operator>>(long long& __n)
286 sentry __cerb(*this, false);
289 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
292 const __num_get_type& __ng = __check_facet(this->_M_num_get);
293 __ng.get(*this, 0, *this, __err, __n);
296 { this->_M_setstate(ios_base::badbit); }
298 this->setstate(__err);
303 template<typename _CharT, typename _Traits>
304 basic_istream<_CharT, _Traits>&
305 basic_istream<_CharT, _Traits>::
306 operator>>(unsigned long long& __n)
308 sentry __cerb(*this, false);
311 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
314 const __num_get_type& __ng = __check_facet(this->_M_num_get);
315 __ng.get(*this, 0, *this, __err, __n);
318 { this->_M_setstate(ios_base::badbit); }
320 this->setstate(__err);
326 template<typename _CharT, typename _Traits>
327 basic_istream<_CharT, _Traits>&
328 basic_istream<_CharT, _Traits>::
329 operator>>(float& __n)
331 sentry __cerb(*this, false);
334 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
337 const __num_get_type& __ng = __check_facet(this->_M_num_get);
338 __ng.get(*this, 0, *this, __err, __n);
341 { this->_M_setstate(ios_base::badbit); }
343 this->setstate(__err);
348 template<typename _CharT, typename _Traits>
349 basic_istream<_CharT, _Traits>&
350 basic_istream<_CharT, _Traits>::
351 operator>>(double& __n)
353 sentry __cerb(*this, false);
356 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
359 const __num_get_type& __ng = __check_facet(this->_M_num_get);
360 __ng.get(*this, 0, *this, __err, __n);
363 { this->_M_setstate(ios_base::badbit); }
365 this->setstate(__err);
370 template<typename _CharT, typename _Traits>
371 basic_istream<_CharT, _Traits>&
372 basic_istream<_CharT, _Traits>::
373 operator>>(long double& __n)
375 sentry __cerb(*this, false);
378 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
381 const __num_get_type& __ng = __check_facet(this->_M_num_get);
382 __ng.get(*this, 0, *this, __err, __n);
385 { this->_M_setstate(ios_base::badbit); }
387 this->setstate(__err);
392 template<typename _CharT, typename _Traits>
393 basic_istream<_CharT, _Traits>&
394 basic_istream<_CharT, _Traits>::
395 operator>>(void*& __n)
397 sentry __cerb(*this, false);
400 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
403 const __num_get_type& __ng = __check_facet(this->_M_num_get);
404 __ng.get(*this, 0, *this, __err, __n);
407 { this->_M_setstate(ios_base::badbit); }
409 this->setstate(__err);
414 template<typename _CharT, typename _Traits>
415 basic_istream<_CharT, _Traits>&
416 basic_istream<_CharT, _Traits>::
417 operator>>(__streambuf_type* __sbout)
419 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
420 sentry __cerb(*this, false);
421 if (__cerb && __sbout)
425 if (!__copy_streambufs(this->rdbuf(), __sbout))
426 __err |= ios_base::failbit;
429 { this->_M_setstate(ios_base::failbit); }
432 __err |= ios_base::failbit;
434 this->setstate(__err);
438 template<typename _CharT, typename _Traits>
439 typename basic_istream<_CharT, _Traits>::int_type
440 basic_istream<_CharT, _Traits>::
443 const int_type __eof = traits_type::eof();
444 int_type __c = __eof;
446 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
447 sentry __cerb(*this, true);
452 __c = this->rdbuf()->sbumpc();
453 // 27.6.1.1 paragraph 3
454 if (!traits_type::eq_int_type(__c, __eof))
457 __err |= ios_base::eofbit;
460 { this->_M_setstate(ios_base::badbit); }
463 __err |= ios_base::failbit;
465 this->setstate(__err);
469 template<typename _CharT, typename _Traits>
470 basic_istream<_CharT, _Traits>&
471 basic_istream<_CharT, _Traits>::
475 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
476 sentry __cerb(*this, true);
481 const int_type __cb = this->rdbuf()->sbumpc();
482 // 27.6.1.1 paragraph 3
483 if (!traits_type::eq_int_type(__cb, traits_type::eof()))
486 __c = traits_type::to_char_type(__cb);
489 __err |= ios_base::eofbit;
492 { this->_M_setstate(ios_base::badbit); }
495 __err |= ios_base::failbit;
497 this->setstate(__err);
501 template<typename _CharT, typename _Traits>
502 basic_istream<_CharT, _Traits>&
503 basic_istream<_CharT, _Traits>::
504 get(char_type* __s, streamsize __n, char_type __delim)
507 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
508 sentry __cerb(*this, true);
513 const int_type __idelim = traits_type::to_int_type(__delim);
514 const int_type __eof = traits_type::eof();
515 __streambuf_type* __sb = this->rdbuf();
516 int_type __c = __sb->sgetc();
518 while (_M_gcount + 1 < __n
519 && !traits_type::eq_int_type(__c, __eof)
520 && !traits_type::eq_int_type(__c, __idelim))
522 *__s++ = traits_type::to_char_type(__c);
523 __c = __sb->snextc();
526 if (traits_type::eq_int_type(__c, __eof))
527 __err |= ios_base::eofbit;
530 { this->_M_setstate(ios_base::badbit); }
534 __err |= ios_base::failbit;
536 this->setstate(__err);
540 template<typename _CharT, typename _Traits>
541 basic_istream<_CharT, _Traits>&
542 basic_istream<_CharT, _Traits>::
543 get(__streambuf_type& __sb, char_type __delim)
546 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
547 sentry __cerb(*this, true);
552 const int_type __idelim = traits_type::to_int_type(__delim);
553 const int_type __eof = traits_type::eof();
554 __streambuf_type* __this_sb = this->rdbuf();
555 int_type __c = __this_sb->sgetc();
556 char_type __c2 = traits_type::to_char_type(__c);
558 while (!traits_type::eq_int_type(__c, __eof)
559 && !traits_type::eq_int_type(__c, __idelim)
560 && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
563 __c = __this_sb->snextc();
564 __c2 = traits_type::to_char_type(__c);
566 if (traits_type::eq_int_type(__c, __eof))
567 __err |= ios_base::eofbit;
570 { this->_M_setstate(ios_base::badbit); }
573 __err |= ios_base::failbit;
575 this->setstate(__err);
579 template<typename _CharT, typename _Traits>
580 basic_istream<_CharT, _Traits>&
581 basic_istream<_CharT, _Traits>::
582 getline(char_type* __s, streamsize __n, char_type __delim)
585 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
586 sentry __cerb(*this, true);
591 const int_type __idelim = traits_type::to_int_type(__delim);
592 const int_type __eof = traits_type::eof();
593 __streambuf_type* __sb = this->rdbuf();
594 int_type __c = __sb->sgetc();
597 while (_M_gcount < __n
598 && !traits_type::eq_int_type(__c, __eof)
599 && !traits_type::eq_int_type(__c, __idelim))
601 streamsize __size = std::min(streamsize(__sb->egptr()
606 const char_type* __p = traits_type::find(__sb->gptr(),
610 __size = __p - __sb->gptr();
611 traits_type::copy(__s, __sb->gptr(), __size);
619 *__s++ = traits_type::to_char_type(__c);
620 __c = __sb->snextc();
625 if (traits_type::eq_int_type(__c, __eof))
626 __err |= ios_base::eofbit;
627 else if (traits_type::eq_int_type(__c, __idelim))
633 __err |= ios_base::failbit;
636 { this->_M_setstate(ios_base::badbit); }
640 __err |= ios_base::failbit;
642 this->setstate(__err);
646 template<typename _CharT, typename _Traits>
647 basic_istream<_CharT, _Traits>&
648 basic_istream<_CharT, _Traits>::
649 ignore(streamsize __n, int_type __delim)
652 sentry __cerb(*this, true);
653 if (__cerb && __n > 0)
655 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
658 const int_type __eof = traits_type::eof();
659 __streambuf_type* __sb = this->rdbuf();
662 while (_M_gcount < __n
663 && !traits_type::eq_int_type(__c = __sb->sbumpc(), __eof))
666 if (traits_type::eq_int_type(__c, __delim))
669 if (traits_type::eq_int_type(__c, __eof))
670 __err |= ios_base::eofbit;
673 { this->_M_setstate(ios_base::badbit); }
675 this->setstate(__err);
680 template<typename _CharT, typename _Traits>
681 typename basic_istream<_CharT, _Traits>::int_type
682 basic_istream<_CharT, _Traits>::
685 int_type __c = traits_type::eof();
687 sentry __cerb(*this, true);
690 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
693 __c = this->rdbuf()->sgetc();
694 if (traits_type::eq_int_type(__c, traits_type::eof()))
695 __err |= ios_base::eofbit;
698 { this->_M_setstate(ios_base::badbit); }
700 this->setstate(__err);
705 template<typename _CharT, typename _Traits>
706 basic_istream<_CharT, _Traits>&
707 basic_istream<_CharT, _Traits>::
708 read(char_type* __s, streamsize __n)
711 sentry __cerb(*this, true);
714 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
717 _M_gcount = this->rdbuf()->sgetn(__s, __n);
718 if (_M_gcount != __n)
719 __err |= (ios_base::eofbit | ios_base::failbit);
722 { this->_M_setstate(ios_base::badbit); }
724 this->setstate(__err);
729 template<typename _CharT, typename _Traits>
731 basic_istream<_CharT, _Traits>::
732 readsome(char_type* __s, streamsize __n)
735 sentry __cerb(*this, true);
738 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
741 // Cannot compare int_type with streamsize generically.
742 const streamsize __num = this->rdbuf()->in_avail();
744 _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
745 else if (__num == -1)
746 __err |= ios_base::eofbit;
749 { this->_M_setstate(ios_base::badbit); }
751 this->setstate(__err);
756 template<typename _CharT, typename _Traits>
757 basic_istream<_CharT, _Traits>&
758 basic_istream<_CharT, _Traits>::
759 putback(char_type __c)
761 // _GLIBCXX_RESOLVE_LIB_DEFECTS
762 // 60. What is a formatted input function?
764 sentry __cerb(*this, true);
767 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
770 const int_type __eof = traits_type::eof();
771 __streambuf_type* __sb = this->rdbuf();
773 || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
774 __err |= ios_base::badbit;
777 { this->_M_setstate(ios_base::badbit); }
779 this->setstate(__err);
784 template<typename _CharT, typename _Traits>
785 basic_istream<_CharT, _Traits>&
786 basic_istream<_CharT, _Traits>::
789 // _GLIBCXX_RESOLVE_LIB_DEFECTS
790 // 60. What is a formatted input function?
792 sentry __cerb(*this, true);
795 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
798 const int_type __eof = traits_type::eof();
799 __streambuf_type* __sb = this->rdbuf();
801 || traits_type::eq_int_type(__sb->sungetc(), __eof))
802 __err |= ios_base::badbit;
805 { this->_M_setstate(ios_base::badbit); }
807 this->setstate(__err);
812 template<typename _CharT, typename _Traits>
814 basic_istream<_CharT, _Traits>::
817 // _GLIBCXX_RESOLVE_LIB_DEFECTS
818 // DR60. Do not change _M_gcount.
820 sentry __cerb(*this, true);
823 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
826 __streambuf_type* __sb = this->rdbuf();
829 if (__sb->pubsync() == -1)
830 __err |= ios_base::badbit;
836 { this->_M_setstate(ios_base::badbit); }
838 this->setstate(__err);
843 template<typename _CharT, typename _Traits>
844 typename basic_istream<_CharT, _Traits>::pos_type
845 basic_istream<_CharT, _Traits>::
848 // _GLIBCXX_RESOLVE_LIB_DEFECTS
849 // DR60. Do not change _M_gcount.
850 pos_type __ret = pos_type(-1);
854 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
857 { this->_M_setstate(ios_base::badbit); }
861 template<typename _CharT, typename _Traits>
862 basic_istream<_CharT, _Traits>&
863 basic_istream<_CharT, _Traits>::
864 seekg(pos_type __pos)
866 // _GLIBCXX_RESOLVE_LIB_DEFECTS
867 // DR60. Do not change _M_gcount.
868 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
873 // 136. seekp, seekg setting wrong streams?
874 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
877 // 129. Need error indication from seekp() and seekg()
878 if (__p == pos_type(off_type(-1)))
879 __err |= ios_base::failbit;
883 { this->_M_setstate(ios_base::badbit); }
885 this->setstate(__err);
889 template<typename _CharT, typename _Traits>
890 basic_istream<_CharT, _Traits>&
891 basic_istream<_CharT, _Traits>::
892 seekg(off_type __off, ios_base::seekdir __dir)
894 // _GLIBCXX_RESOLVE_LIB_DEFECTS
895 // DR60. Do not change _M_gcount.
896 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
901 // 136. seekp, seekg setting wrong streams?
902 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
905 // 129. Need error indication from seekp() and seekg()
906 if (__p == pos_type(off_type(-1)))
907 __err |= ios_base::failbit;
911 { this->_M_setstate(ios_base::badbit); }
913 this->setstate(__err);
917 // 27.6.1.2.3 Character extraction templates
918 template<typename _CharT, typename _Traits>
919 basic_istream<_CharT, _Traits>&
920 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
922 typedef basic_istream<_CharT, _Traits> __istream_type;
923 typedef typename __istream_type::int_type __int_type;
925 typename __istream_type::sentry __cerb(__in, false);
928 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
931 const __int_type __cb = __in.rdbuf()->sbumpc();
932 if (!_Traits::eq_int_type(__cb, _Traits::eof()))
933 __c = _Traits::to_char_type(__cb);
935 __err |= (ios_base::eofbit | ios_base::failbit);
938 { __in._M_setstate(ios_base::badbit); }
940 __in.setstate(__err);
945 template<typename _CharT, typename _Traits>
946 basic_istream<_CharT, _Traits>&
947 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
949 typedef basic_istream<_CharT, _Traits> __istream_type;
950 typedef typename __istream_type::__streambuf_type __streambuf_type;
951 typedef typename _Traits::int_type int_type;
952 typedef _CharT char_type;
953 typedef ctype<_CharT> __ctype_type;
955 streamsize __extracted = 0;
956 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
957 typename __istream_type::sentry __cerb(__in, false);
962 // Figure out how many characters to extract.
963 streamsize __num = __in.width();
965 __num = numeric_limits<streamsize>::max();
967 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
969 const int_type __eof = _Traits::eof();
970 __streambuf_type* __sb = __in.rdbuf();
971 int_type __c = __sb->sgetc();
973 while (__extracted < __num - 1
974 && !_Traits::eq_int_type(__c, __eof)
975 && !__ct.is(ctype_base::space,
976 _Traits::to_char_type(__c)))
978 *__s++ = _Traits::to_char_type(__c);
980 __c = __sb->snextc();
982 if (_Traits::eq_int_type(__c, __eof))
983 __err |= ios_base::eofbit;
985 // _GLIBCXX_RESOLVE_LIB_DEFECTS
986 // 68. Extractors for char* should store null at end
991 { __in._M_setstate(ios_base::badbit); }
994 __err |= ios_base::failbit;
996 __in.setstate(__err);
1000 // 27.6.1.4 Standard basic_istream manipulators
1001 template<typename _CharT, typename _Traits>
1002 basic_istream<_CharT,_Traits>&
1003 ws(basic_istream<_CharT,_Traits>& __in)
1005 typedef basic_istream<_CharT, _Traits> __istream_type;
1006 typedef typename __istream_type::__streambuf_type __streambuf_type;
1007 typedef typename __istream_type::__ctype_type __ctype_type;
1008 typedef typename __istream_type::int_type __int_type;
1010 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1011 const __int_type __eof = _Traits::eof();
1012 __streambuf_type* __sb = __in.rdbuf();
1013 __int_type __c = __sb->sgetc();
1015 while (!_Traits::eq_int_type(__c, __eof)
1016 && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1017 __c = __sb->snextc();
1019 if (_Traits::eq_int_type(__c, __eof))
1020 __in.setstate(ios_base::eofbit);
1024 // 21.3.7.9 basic_string::getline and operators
1025 template<typename _CharT, typename _Traits, typename _Alloc>
1026 basic_istream<_CharT, _Traits>&
1027 operator>>(basic_istream<_CharT, _Traits>& __in,
1028 basic_string<_CharT, _Traits, _Alloc>& __str)
1030 typedef basic_istream<_CharT, _Traits> __istream_type;
1031 typedef typename __istream_type::int_type __int_type;
1032 typedef typename __istream_type::__streambuf_type __streambuf_type;
1033 typedef typename __istream_type::__ctype_type __ctype_type;
1034 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1035 typedef typename __string_type::size_type __size_type;
1037 __size_type __extracted = 0;
1038 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1039 typename __istream_type::sentry __cerb(__in, false);
1044 // Avoid reallocation for common case.
1047 __size_type __len = 0;
1048 const streamsize __w = __in.width();
1049 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
1051 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1052 const __int_type __eof = _Traits::eof();
1053 __streambuf_type* __sb = __in.rdbuf();
1054 __int_type __c = __sb->sgetc();
1056 while (__extracted < __n
1057 && !_Traits::eq_int_type(__c, __eof)
1058 && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1060 if (__len == sizeof(__buf) / sizeof(_CharT))
1062 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1065 __buf[__len++] = _Traits::to_char_type(__c);
1067 __c = __sb->snextc();
1069 __str.append(__buf, __len);
1071 if (_Traits::eq_int_type(__c, __eof))
1072 __err |= ios_base::eofbit;
1077 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1078 // 91. Description of operator>> and getline() for string<>
1079 // might cause endless loop
1080 __in._M_setstate(ios_base::badbit);
1083 // 211. operator>>(istream&, string&) doesn't set failbit
1085 __err |= ios_base::failbit;
1087 __in.setstate(__err);
1091 template<typename _CharT, typename _Traits, typename _Alloc>
1092 basic_istream<_CharT, _Traits>&
1093 getline(basic_istream<_CharT, _Traits>& __in,
1094 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1096 typedef basic_istream<_CharT, _Traits> __istream_type;
1097 typedef typename __istream_type::int_type __int_type;
1098 typedef typename __istream_type::__streambuf_type __streambuf_type;
1099 typedef typename __istream_type::__ctype_type __ctype_type;
1100 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1101 typedef typename __string_type::size_type __size_type;
1103 __size_type __extracted = 0;
1104 const __size_type __n = __str.max_size();
1105 bool __testdelim = false;
1106 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
1107 typename __istream_type::sentry __cerb(__in, true);
1112 // Avoid reallocation for common case.
1115 __size_type __len = 0;
1116 const __int_type __idelim = _Traits::to_int_type(__delim);
1117 const __int_type __eof = _Traits::eof();
1118 __streambuf_type* __sb = __in.rdbuf();
1119 __int_type __c = __sb->sgetc();
1121 while (__extracted < __n
1122 && !_Traits::eq_int_type(__c, __eof)
1123 && !_Traits::eq_int_type(__c, __idelim))
1125 if (__len == sizeof(__buf) / sizeof(_CharT))
1127 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
1130 __buf[__len++] = _Traits::to_char_type(__c);
1132 __c = __sb->snextc();
1134 __str.append(__buf, __len);
1136 if (_Traits::eq_int_type(__c, __eof))
1137 __err |= ios_base::eofbit;
1138 else if (_Traits::eq_int_type(__c, __idelim))
1144 __err |= ios_base::failbit;
1148 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1149 // 91. Description of operator>> and getline() for string<>
1150 // might cause endless loop
1151 __in._M_setstate(ios_base::badbit);
1155 __err |= ios_base::failbit;
1157 __in.setstate(__err);
1161 template<class _CharT, class _Traits, class _Alloc>
1162 inline basic_istream<_CharT,_Traits>&
1163 getline(basic_istream<_CharT, _Traits>& __in,
1164 basic_string<_CharT,_Traits,_Alloc>& __str)
1165 { return getline(__in, __str, __in.widen('\n')); }
1167 // Inhibit implicit instantiations for required instantiations,
1168 // which are defined via explicit instantiations elsewhere.
1169 // NB: This syntax is a GNU extension.
1170 #if _GLIBCXX_EXTERN_TEMPLATE
1171 extern template class basic_istream<char>;
1172 extern template istream& ws(istream&);
1173 extern template istream& operator>>(istream&, char&);
1174 extern template istream& operator>>(istream&, char*);
1175 extern template istream& operator>>(istream&, unsigned char&);
1176 extern template istream& operator>>(istream&, signed char&);
1177 extern template istream& operator>>(istream&, unsigned char*);
1178 extern template istream& operator>>(istream&, signed char*);
1180 #ifdef _GLIBCXX_USE_WCHAR_T
1181 extern template class basic_istream<wchar_t>;
1182 extern template wistream& ws(wistream&);
1183 extern template wistream& operator>>(wistream&, wchar_t&);
1184 extern template wistream& operator>>(wistream&, wchar_t*);