1 // istream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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()
48 _GLIBCXX_BEGIN_NAMESPACE(std)
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 template<typename _ValueT>
114 basic_istream<_CharT, _Traits>&
115 basic_istream<_CharT, _Traits>::
116 _M_extract(_ValueT& __v)
118 sentry __cerb(*this, false);
121 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
124 const __num_get_type& __ng = __check_facet(this->_M_num_get);
125 __ng.get(*this, 0, *this, __err, __v);
128 { this->_M_setstate(ios_base::badbit); }
130 this->setstate(__err);
135 template<typename _CharT, typename _Traits>
136 basic_istream<_CharT, _Traits>&
137 basic_istream<_CharT, _Traits>::
138 operator>>(short& __n)
140 // _GLIBCXX_RESOLVE_LIB_DEFECTS
141 // 118. basic_istream uses nonexistent num_get member functions.
146 if (numeric_limits<short>::min() <= __l
147 && __l <= numeric_limits<short>::max())
150 this->setstate(ios_base::failbit);
155 template<typename _CharT, typename _Traits>
156 basic_istream<_CharT, _Traits>&
157 basic_istream<_CharT, _Traits>::
160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
161 // 118. basic_istream uses nonexistent num_get member functions.
166 if (numeric_limits<int>::min() <= __l
167 && __l <= numeric_limits<int>::max())
170 this->setstate(ios_base::failbit);
175 template<typename _CharT, typename _Traits>
176 basic_istream<_CharT, _Traits>&
177 basic_istream<_CharT, _Traits>::
178 operator>>(__streambuf_type* __sbout)
180 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
181 sentry __cerb(*this, false);
182 if (__cerb && __sbout)
187 if (!__copy_streambufs_eof(this->rdbuf(), __sbout, __ineof))
188 __err |= ios_base::failbit;
190 __err |= ios_base::eofbit;
193 { this->_M_setstate(ios_base::failbit); }
196 __err |= ios_base::failbit;
198 this->setstate(__err);
202 template<typename _CharT, typename _Traits>
203 typename basic_istream<_CharT, _Traits>::int_type
204 basic_istream<_CharT, _Traits>::
207 const int_type __eof = traits_type::eof();
208 int_type __c = __eof;
210 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
211 sentry __cerb(*this, true);
216 __c = this->rdbuf()->sbumpc();
217 // 27.6.1.1 paragraph 3
218 if (!traits_type::eq_int_type(__c, __eof))
221 __err |= ios_base::eofbit;
224 { this->_M_setstate(ios_base::badbit); }
227 __err |= ios_base::failbit;
229 this->setstate(__err);
233 template<typename _CharT, typename _Traits>
234 basic_istream<_CharT, _Traits>&
235 basic_istream<_CharT, _Traits>::
239 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
240 sentry __cerb(*this, true);
245 const int_type __cb = this->rdbuf()->sbumpc();
246 // 27.6.1.1 paragraph 3
247 if (!traits_type::eq_int_type(__cb, traits_type::eof()))
250 __c = traits_type::to_char_type(__cb);
253 __err |= ios_base::eofbit;
256 { this->_M_setstate(ios_base::badbit); }
259 __err |= ios_base::failbit;
261 this->setstate(__err);
265 template<typename _CharT, typename _Traits>
266 basic_istream<_CharT, _Traits>&
267 basic_istream<_CharT, _Traits>::
268 get(char_type* __s, streamsize __n, char_type __delim)
271 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
272 sentry __cerb(*this, true);
277 const int_type __idelim = traits_type::to_int_type(__delim);
278 const int_type __eof = traits_type::eof();
279 __streambuf_type* __sb = this->rdbuf();
280 int_type __c = __sb->sgetc();
282 while (_M_gcount + 1 < __n
283 && !traits_type::eq_int_type(__c, __eof)
284 && !traits_type::eq_int_type(__c, __idelim))
286 *__s++ = traits_type::to_char_type(__c);
288 __c = __sb->snextc();
290 if (traits_type::eq_int_type(__c, __eof))
291 __err |= ios_base::eofbit;
294 { this->_M_setstate(ios_base::badbit); }
296 // _GLIBCXX_RESOLVE_LIB_DEFECTS
297 // 243. get and getline when sentry reports failure.
301 __err |= ios_base::failbit;
303 this->setstate(__err);
307 template<typename _CharT, typename _Traits>
308 basic_istream<_CharT, _Traits>&
309 basic_istream<_CharT, _Traits>::
310 get(__streambuf_type& __sb, char_type __delim)
313 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
314 sentry __cerb(*this, true);
319 const int_type __idelim = traits_type::to_int_type(__delim);
320 const int_type __eof = traits_type::eof();
321 __streambuf_type* __this_sb = this->rdbuf();
322 int_type __c = __this_sb->sgetc();
323 char_type __c2 = traits_type::to_char_type(__c);
325 while (!traits_type::eq_int_type(__c, __eof)
326 && !traits_type::eq_int_type(__c, __idelim)
327 && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
330 __c = __this_sb->snextc();
331 __c2 = traits_type::to_char_type(__c);
333 if (traits_type::eq_int_type(__c, __eof))
334 __err |= ios_base::eofbit;
337 { this->_M_setstate(ios_base::badbit); }
340 __err |= ios_base::failbit;
342 this->setstate(__err);
346 template<typename _CharT, typename _Traits>
347 basic_istream<_CharT, _Traits>&
348 basic_istream<_CharT, _Traits>::
349 getline(char_type* __s, streamsize __n, char_type __delim)
352 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
353 sentry __cerb(*this, true);
358 const int_type __idelim = traits_type::to_int_type(__delim);
359 const int_type __eof = traits_type::eof();
360 __streambuf_type* __sb = this->rdbuf();
361 int_type __c = __sb->sgetc();
363 while (_M_gcount + 1 < __n
364 && !traits_type::eq_int_type(__c, __eof)
365 && !traits_type::eq_int_type(__c, __idelim))
367 *__s++ = traits_type::to_char_type(__c);
368 __c = __sb->snextc();
371 if (traits_type::eq_int_type(__c, __eof))
372 __err |= ios_base::eofbit;
375 if (traits_type::eq_int_type(__c, __idelim))
381 __err |= ios_base::failbit;
385 { this->_M_setstate(ios_base::badbit); }
387 // _GLIBCXX_RESOLVE_LIB_DEFECTS
388 // 243. get and getline when sentry reports failure.
392 __err |= ios_base::failbit;
394 this->setstate(__err);
398 // We provide three overloads, since the first two are much simpler
399 // than the general case. Also, the latter two can thus adopt the
400 // same "batchy" strategy used by getline above.
401 template<typename _CharT, typename _Traits>
402 basic_istream<_CharT, _Traits>&
403 basic_istream<_CharT, _Traits>::
407 sentry __cerb(*this, true);
410 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
413 const int_type __eof = traits_type::eof();
414 __streambuf_type* __sb = this->rdbuf();
416 if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
417 __err |= ios_base::eofbit;
422 { this->_M_setstate(ios_base::badbit); }
424 this->setstate(__err);
429 template<typename _CharT, typename _Traits>
430 basic_istream<_CharT, _Traits>&
431 basic_istream<_CharT, _Traits>::
432 ignore(streamsize __n)
435 sentry __cerb(*this, true);
436 if (__cerb && __n > 0)
438 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
441 const int_type __eof = traits_type::eof();
442 __streambuf_type* __sb = this->rdbuf();
443 int_type __c = __sb->sgetc();
445 // N.B. On LFS-enabled platforms streamsize is still 32 bits
446 // wide: if we want to implement the standard mandated behavior
447 // for n == max() (see 27.6.1.3/24) we are at risk of signed
448 // integer overflow: thus these contortions. Also note that,
449 // by definition, when more than 2G chars are actually ignored,
450 // _M_gcount (the return value of gcount, that is) cannot be
451 // really correct, being unavoidably too small.
452 bool __large_ignore = false;
455 while (_M_gcount < __n
456 && !traits_type::eq_int_type(__c, __eof))
459 __c = __sb->snextc();
461 if (__n == numeric_limits<streamsize>::max()
462 && !traits_type::eq_int_type(__c, __eof))
464 _M_gcount = numeric_limits<streamsize>::min();
465 __large_ignore = true;
472 _M_gcount = numeric_limits<streamsize>::max();
474 if (traits_type::eq_int_type(__c, __eof))
475 __err |= ios_base::eofbit;
478 { this->_M_setstate(ios_base::badbit); }
480 this->setstate(__err);
485 template<typename _CharT, typename _Traits>
486 basic_istream<_CharT, _Traits>&
487 basic_istream<_CharT, _Traits>::
488 ignore(streamsize __n, int_type __delim)
491 sentry __cerb(*this, true);
492 if (__cerb && __n > 0)
494 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
497 const int_type __eof = traits_type::eof();
498 __streambuf_type* __sb = this->rdbuf();
499 int_type __c = __sb->sgetc();
501 // See comment above.
502 bool __large_ignore = false;
505 while (_M_gcount < __n
506 && !traits_type::eq_int_type(__c, __eof)
507 && !traits_type::eq_int_type(__c, __delim))
510 __c = __sb->snextc();
512 if (__n == numeric_limits<streamsize>::max()
513 && !traits_type::eq_int_type(__c, __eof)
514 && !traits_type::eq_int_type(__c, __delim))
516 _M_gcount = numeric_limits<streamsize>::min();
517 __large_ignore = true;
524 _M_gcount = numeric_limits<streamsize>::max();
526 if (traits_type::eq_int_type(__c, __eof))
527 __err |= ios_base::eofbit;
528 else if (traits_type::eq_int_type(__c, __delim))
530 if (_M_gcount < numeric_limits<streamsize>::max())
536 { this->_M_setstate(ios_base::badbit); }
538 this->setstate(__err);
543 template<typename _CharT, typename _Traits>
544 typename basic_istream<_CharT, _Traits>::int_type
545 basic_istream<_CharT, _Traits>::
548 int_type __c = traits_type::eof();
550 sentry __cerb(*this, true);
553 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
556 __c = this->rdbuf()->sgetc();
557 if (traits_type::eq_int_type(__c, traits_type::eof()))
558 __err |= ios_base::eofbit;
561 { this->_M_setstate(ios_base::badbit); }
563 this->setstate(__err);
568 template<typename _CharT, typename _Traits>
569 basic_istream<_CharT, _Traits>&
570 basic_istream<_CharT, _Traits>::
571 read(char_type* __s, streamsize __n)
574 sentry __cerb(*this, true);
577 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
580 _M_gcount = this->rdbuf()->sgetn(__s, __n);
581 if (_M_gcount != __n)
582 __err |= (ios_base::eofbit | ios_base::failbit);
585 { this->_M_setstate(ios_base::badbit); }
587 this->setstate(__err);
592 template<typename _CharT, typename _Traits>
594 basic_istream<_CharT, _Traits>::
595 readsome(char_type* __s, streamsize __n)
598 sentry __cerb(*this, true);
601 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
604 // Cannot compare int_type with streamsize generically.
605 const streamsize __num = this->rdbuf()->in_avail();
607 _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
608 else if (__num == -1)
609 __err |= ios_base::eofbit;
612 { this->_M_setstate(ios_base::badbit); }
614 this->setstate(__err);
619 template<typename _CharT, typename _Traits>
620 basic_istream<_CharT, _Traits>&
621 basic_istream<_CharT, _Traits>::
622 putback(char_type __c)
624 // _GLIBCXX_RESOLVE_LIB_DEFECTS
625 // 60. What is a formatted input function?
627 sentry __cerb(*this, true);
630 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
633 const int_type __eof = traits_type::eof();
634 __streambuf_type* __sb = this->rdbuf();
636 || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
637 __err |= ios_base::badbit;
640 { this->_M_setstate(ios_base::badbit); }
642 this->setstate(__err);
647 template<typename _CharT, typename _Traits>
648 basic_istream<_CharT, _Traits>&
649 basic_istream<_CharT, _Traits>::
652 // _GLIBCXX_RESOLVE_LIB_DEFECTS
653 // 60. What is a formatted input function?
655 sentry __cerb(*this, true);
658 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
661 const int_type __eof = traits_type::eof();
662 __streambuf_type* __sb = this->rdbuf();
664 || traits_type::eq_int_type(__sb->sungetc(), __eof))
665 __err |= ios_base::badbit;
668 { this->_M_setstate(ios_base::badbit); }
670 this->setstate(__err);
675 template<typename _CharT, typename _Traits>
677 basic_istream<_CharT, _Traits>::
680 // _GLIBCXX_RESOLVE_LIB_DEFECTS
681 // DR60. Do not change _M_gcount.
683 sentry __cerb(*this, true);
686 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
689 __streambuf_type* __sb = this->rdbuf();
692 if (__sb->pubsync() == -1)
693 __err |= ios_base::badbit;
699 { this->_M_setstate(ios_base::badbit); }
701 this->setstate(__err);
706 template<typename _CharT, typename _Traits>
707 typename basic_istream<_CharT, _Traits>::pos_type
708 basic_istream<_CharT, _Traits>::
711 // _GLIBCXX_RESOLVE_LIB_DEFECTS
712 // DR60. Do not change _M_gcount.
713 pos_type __ret = pos_type(-1);
714 sentry __cerb(*this, true);
720 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur,
724 { this->_M_setstate(ios_base::badbit); }
729 template<typename _CharT, typename _Traits>
730 basic_istream<_CharT, _Traits>&
731 basic_istream<_CharT, _Traits>::
732 seekg(pos_type __pos)
734 // _GLIBCXX_RESOLVE_LIB_DEFECTS
735 // DR60. Do not change _M_gcount.
736 sentry __cerb(*this, true);
739 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
744 // 136. seekp, seekg setting wrong streams?
745 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
748 // 129. Need error indication from seekp() and seekg()
749 if (__p == pos_type(off_type(-1)))
750 __err |= ios_base::failbit;
754 { this->_M_setstate(ios_base::badbit); }
756 this->setstate(__err);
761 template<typename _CharT, typename _Traits>
762 basic_istream<_CharT, _Traits>&
763 basic_istream<_CharT, _Traits>::
764 seekg(off_type __off, ios_base::seekdir __dir)
766 // _GLIBCXX_RESOLVE_LIB_DEFECTS
767 // DR60. Do not change _M_gcount.
768 sentry __cerb(*this, true);
771 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
776 // 136. seekp, seekg setting wrong streams?
777 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
780 // 129. Need error indication from seekp() and seekg()
781 if (__p == pos_type(off_type(-1)))
782 __err |= ios_base::failbit;
786 { this->_M_setstate(ios_base::badbit); }
788 this->setstate(__err);
793 // 27.6.1.2.3 Character extraction templates
794 template<typename _CharT, typename _Traits>
795 basic_istream<_CharT, _Traits>&
796 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
798 typedef basic_istream<_CharT, _Traits> __istream_type;
799 typedef typename __istream_type::int_type __int_type;
801 typename __istream_type::sentry __cerb(__in, false);
804 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
807 const __int_type __cb = __in.rdbuf()->sbumpc();
808 if (!_Traits::eq_int_type(__cb, _Traits::eof()))
809 __c = _Traits::to_char_type(__cb);
811 __err |= (ios_base::eofbit | ios_base::failbit);
814 { __in._M_setstate(ios_base::badbit); }
816 __in.setstate(__err);
821 template<typename _CharT, typename _Traits>
822 basic_istream<_CharT, _Traits>&
823 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
825 typedef basic_istream<_CharT, _Traits> __istream_type;
826 typedef typename __istream_type::__streambuf_type __streambuf_type;
827 typedef typename _Traits::int_type int_type;
828 typedef _CharT char_type;
829 typedef ctype<_CharT> __ctype_type;
831 streamsize __extracted = 0;
832 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
833 typename __istream_type::sentry __cerb(__in, false);
838 // Figure out how many characters to extract.
839 streamsize __num = __in.width();
841 __num = numeric_limits<streamsize>::max();
843 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
845 const int_type __eof = _Traits::eof();
846 __streambuf_type* __sb = __in.rdbuf();
847 int_type __c = __sb->sgetc();
849 while (__extracted < __num - 1
850 && !_Traits::eq_int_type(__c, __eof)
851 && !__ct.is(ctype_base::space,
852 _Traits::to_char_type(__c)))
854 *__s++ = _Traits::to_char_type(__c);
856 __c = __sb->snextc();
858 if (_Traits::eq_int_type(__c, __eof))
859 __err |= ios_base::eofbit;
861 // _GLIBCXX_RESOLVE_LIB_DEFECTS
862 // 68. Extractors for char* should store null at end
867 { __in._M_setstate(ios_base::badbit); }
870 __err |= ios_base::failbit;
872 __in.setstate(__err);
876 // 27.6.1.4 Standard basic_istream manipulators
877 template<typename _CharT, typename _Traits>
878 basic_istream<_CharT,_Traits>&
879 ws(basic_istream<_CharT,_Traits>& __in)
881 typedef basic_istream<_CharT, _Traits> __istream_type;
882 typedef typename __istream_type::__streambuf_type __streambuf_type;
883 typedef typename __istream_type::__ctype_type __ctype_type;
884 typedef typename __istream_type::int_type __int_type;
886 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
887 const __int_type __eof = _Traits::eof();
888 __streambuf_type* __sb = __in.rdbuf();
889 __int_type __c = __sb->sgetc();
891 while (!_Traits::eq_int_type(__c, __eof)
892 && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
893 __c = __sb->snextc();
895 if (_Traits::eq_int_type(__c, __eof))
896 __in.setstate(ios_base::eofbit);
900 // 21.3.7.9 basic_string::getline and operators
901 template<typename _CharT, typename _Traits, typename _Alloc>
902 basic_istream<_CharT, _Traits>&
903 operator>>(basic_istream<_CharT, _Traits>& __in,
904 basic_string<_CharT, _Traits, _Alloc>& __str)
906 typedef basic_istream<_CharT, _Traits> __istream_type;
907 typedef typename __istream_type::int_type __int_type;
908 typedef typename __istream_type::__streambuf_type __streambuf_type;
909 typedef typename __istream_type::__ctype_type __ctype_type;
910 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
911 typedef typename __string_type::size_type __size_type;
913 __size_type __extracted = 0;
914 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
915 typename __istream_type::sentry __cerb(__in, false);
920 // Avoid reallocation for common case.
923 __size_type __len = 0;
924 const streamsize __w = __in.width();
925 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
927 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
928 const __int_type __eof = _Traits::eof();
929 __streambuf_type* __sb = __in.rdbuf();
930 __int_type __c = __sb->sgetc();
932 while (__extracted < __n
933 && !_Traits::eq_int_type(__c, __eof)
934 && !__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
936 if (__len == sizeof(__buf) / sizeof(_CharT))
938 __str.append(__buf, sizeof(__buf) / sizeof(_CharT));
941 __buf[__len++] = _Traits::to_char_type(__c);
943 __c = __sb->snextc();
945 __str.append(__buf, __len);
947 if (_Traits::eq_int_type(__c, __eof))
948 __err |= ios_base::eofbit;
953 // _GLIBCXX_RESOLVE_LIB_DEFECTS
954 // 91. Description of operator>> and getline() for string<>
955 // might cause endless loop
956 __in._M_setstate(ios_base::badbit);
959 // 211. operator>>(istream&, string&) doesn't set failbit
961 __err |= ios_base::failbit;
963 __in.setstate(__err);
967 template<typename _CharT, typename _Traits, typename _Alloc>
968 basic_istream<_CharT, _Traits>&
969 getline(basic_istream<_CharT, _Traits>& __in,
970 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
972 typedef basic_istream<_CharT, _Traits> __istream_type;
973 typedef typename __istream_type::int_type __int_type;
974 typedef typename __istream_type::__streambuf_type __streambuf_type;
975 typedef typename __istream_type::__ctype_type __ctype_type;
976 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
977 typedef typename __string_type::size_type __size_type;
979 __size_type __extracted = 0;
980 const __size_type __n = __str.max_size();
981 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
982 typename __istream_type::sentry __cerb(__in, true);
988 const __int_type __idelim = _Traits::to_int_type(__delim);
989 const __int_type __eof = _Traits::eof();
990 __streambuf_type* __sb = __in.rdbuf();
991 __int_type __c = __sb->sgetc();
993 while (__extracted < __n
994 && !_Traits::eq_int_type(__c, __eof)
995 && !_Traits::eq_int_type(__c, __idelim))
997 __str += _Traits::to_char_type(__c);
999 __c = __sb->snextc();
1002 if (_Traits::eq_int_type(__c, __eof))
1003 __err |= ios_base::eofbit;
1004 else if (_Traits::eq_int_type(__c, __idelim))
1010 __err |= ios_base::failbit;
1014 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1015 // 91. Description of operator>> and getline() for string<>
1016 // might cause endless loop
1017 __in._M_setstate(ios_base::badbit);
1021 __err |= ios_base::failbit;
1023 __in.setstate(__err);
1027 template<class _CharT, class _Traits, class _Alloc>
1028 inline basic_istream<_CharT,_Traits>&
1029 getline(basic_istream<_CharT, _Traits>& __in,
1030 basic_string<_CharT,_Traits,_Alloc>& __str)
1031 { return getline(__in, __str, __in.widen('\n')); }
1033 // Inhibit implicit instantiations for required instantiations,
1034 // which are defined via explicit instantiations elsewhere.
1035 // NB: This syntax is a GNU extension.
1036 #if _GLIBCXX_EXTERN_TEMPLATE
1037 extern template class basic_istream<char>;
1038 extern template istream& ws(istream&);
1039 extern template istream& operator>>(istream&, char&);
1040 extern template istream& operator>>(istream&, char*);
1041 extern template istream& operator>>(istream&, unsigned char&);
1042 extern template istream& operator>>(istream&, signed char&);
1043 extern template istream& operator>>(istream&, unsigned char*);
1044 extern template istream& operator>>(istream&, signed char*);
1046 extern template class basic_iostream<char>;
1048 #ifdef _GLIBCXX_USE_WCHAR_T
1049 extern template class basic_istream<wchar_t>;
1050 extern template wistream& ws(wistream&);
1051 extern template wistream& operator>>(wistream&, wchar_t&);
1052 extern template wistream& operator>>(wistream&, wchar_t*);
1054 extern template class basic_iostream<wchar_t>;
1058 _GLIBCXX_END_NAMESPACE