1 // Input streams -*- C++ -*-
3 // Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 // ISO C++ 14882: 27.6.1 Input streams
31 namespace std _GLIBCXX_VISIBILITY(default)
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 getline(char_type* __s, streamsize __n, char_type __delim)
41 ios_base::iostate __err = ios_base::goodbit;
42 sentry __cerb(*this, true);
47 const int_type __idelim = traits_type::to_int_type(__delim);
48 const int_type __eof = traits_type::eof();
49 __streambuf_type* __sb = this->rdbuf();
50 int_type __c = __sb->sgetc();
52 while (_M_gcount + 1 < __n
53 && !traits_type::eq_int_type(__c, __eof)
54 && !traits_type::eq_int_type(__c, __idelim))
56 streamsize __size = std::min(streamsize(__sb->egptr()
58 streamsize(__n - _M_gcount
62 const char_type* __p = traits_type::find(__sb->gptr(),
66 __size = __p - __sb->gptr();
67 traits_type::copy(__s, __sb->gptr(), __size);
69 __sb->__safe_gbump(__size);
75 *__s++ = traits_type::to_char_type(__c);
81 if (traits_type::eq_int_type(__c, __eof))
82 __err |= ios_base::eofbit;
83 else if (traits_type::eq_int_type(__c, __idelim))
89 __err |= ios_base::failbit;
91 __catch(__cxxabiv1::__forced_unwind&)
93 this->_M_setstate(ios_base::badbit);
94 __throw_exception_again;
97 { this->_M_setstate(ios_base::badbit); }
99 // _GLIBCXX_RESOLVE_LIB_DEFECTS
100 // 243. get and getline when sentry reports failure.
104 __err |= ios_base::failbit;
106 this->setstate(__err);
112 basic_istream<char>::
113 ignore(streamsize __n, int_type __delim)
115 if (traits_type::eq_int_type(__delim, traits_type::eof()))
119 sentry __cerb(*this, true);
120 if (__n > 0 && __cerb)
122 ios_base::iostate __err = ios_base::goodbit;
125 const char_type __cdelim = traits_type::to_char_type(__delim);
126 const int_type __eof = traits_type::eof();
127 __streambuf_type* __sb = this->rdbuf();
128 int_type __c = __sb->sgetc();
130 bool __large_ignore = false;
133 while (_M_gcount < __n
134 && !traits_type::eq_int_type(__c, __eof)
135 && !traits_type::eq_int_type(__c, __delim))
137 streamsize __size = std::min(streamsize(__sb->egptr()
139 streamsize(__n - _M_gcount));
142 const char_type* __p = traits_type::find(__sb->gptr(),
146 __size = __p - __sb->gptr();
147 __sb->__safe_gbump(__size);
154 __c = __sb->snextc();
157 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
158 && !traits_type::eq_int_type(__c, __eof)
159 && !traits_type::eq_int_type(__c, __delim))
162 __gnu_cxx::__numeric_traits<streamsize>::__min;
163 __large_ignore = true;
170 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
172 if (traits_type::eq_int_type(__c, __eof))
173 __err |= ios_base::eofbit;
174 else if (traits_type::eq_int_type(__c, __delim))
177 < __gnu_cxx::__numeric_traits<streamsize>::__max)
182 __catch(__cxxabiv1::__forced_unwind&)
184 this->_M_setstate(ios_base::badbit);
185 __throw_exception_again;
188 { this->_M_setstate(ios_base::badbit); }
190 this->setstate(__err);
197 operator>>(basic_istream<char>& __in, char* __s)
199 typedef basic_istream<char> __istream_type;
200 typedef __istream_type::int_type __int_type;
201 typedef __istream_type::char_type __char_type;
202 typedef __istream_type::traits_type __traits_type;
203 typedef __istream_type::__streambuf_type __streambuf_type;
204 typedef __istream_type::__ctype_type __ctype_type;
206 streamsize __extracted = 0;
207 ios_base::iostate __err = ios_base::goodbit;
208 __istream_type::sentry __cerb(__in, false);
213 // Figure out how many characters to extract.
214 streamsize __num = __in.width();
216 __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
218 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
220 const __int_type __eof = __traits_type::eof();
221 __streambuf_type* __sb = __in.rdbuf();
222 __int_type __c = __sb->sgetc();
224 while (__extracted < __num - 1
225 && !__traits_type::eq_int_type(__c, __eof)
226 && !__ct.is(ctype_base::space,
227 __traits_type::to_char_type(__c)))
229 streamsize __size = std::min(streamsize(__sb->egptr()
231 streamsize(__num - __extracted
235 __size = (__ct.scan_is(ctype_base::space,
237 __sb->gptr() + __size)
239 __traits_type::copy(__s, __sb->gptr(), __size);
241 __sb->__safe_gbump(__size);
242 __extracted += __size;
247 *__s++ = __traits_type::to_char_type(__c);
249 __c = __sb->snextc();
253 if (__traits_type::eq_int_type(__c, __eof))
254 __err |= ios_base::eofbit;
256 // _GLIBCXX_RESOLVE_LIB_DEFECTS
257 // 68. Extractors for char* should store null at end
258 *__s = __char_type();
261 __catch(__cxxabiv1::__forced_unwind&)
263 __in._M_setstate(ios_base::badbit);
264 __throw_exception_again;
267 { __in._M_setstate(ios_base::badbit); }
270 __err |= ios_base::failbit;
272 __in.setstate(__err);
278 operator>>(basic_istream<char>& __in, basic_string<char>& __str)
280 typedef basic_istream<char> __istream_type;
281 typedef __istream_type::int_type __int_type;
282 typedef __istream_type::traits_type __traits_type;
283 typedef __istream_type::__streambuf_type __streambuf_type;
284 typedef __istream_type::__ctype_type __ctype_type;
285 typedef basic_string<char> __string_type;
286 typedef __string_type::size_type __size_type;
288 __size_type __extracted = 0;
289 ios_base::iostate __err = ios_base::goodbit;
290 __istream_type::sentry __cerb(__in, false);
296 const streamsize __w = __in.width();
297 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
299 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
300 const __int_type __eof = __traits_type::eof();
301 __streambuf_type* __sb = __in.rdbuf();
302 __int_type __c = __sb->sgetc();
304 while (__extracted < __n
305 && !__traits_type::eq_int_type(__c, __eof)
306 && !__ct.is(ctype_base::space,
307 __traits_type::to_char_type(__c)))
309 streamsize __size = std::min(streamsize(__sb->egptr()
311 streamsize(__n - __extracted));
314 __size = (__ct.scan_is(ctype_base::space,
316 __sb->gptr() + __size)
318 __str.append(__sb->gptr(), __size);
319 __sb->__safe_gbump(__size);
320 __extracted += __size;
325 __str += __traits_type::to_char_type(__c);
327 __c = __sb->snextc();
331 if (__traits_type::eq_int_type(__c, __eof))
332 __err |= ios_base::eofbit;
335 __catch(__cxxabiv1::__forced_unwind&)
337 __in._M_setstate(ios_base::badbit);
338 __throw_exception_again;
342 // _GLIBCXX_RESOLVE_LIB_DEFECTS
343 // 91. Description of operator>> and getline() for string<>
344 // might cause endless loop
345 __in._M_setstate(ios_base::badbit);
349 __err |= ios_base::failbit;
351 __in.setstate(__err);
357 getline(basic_istream<char>& __in, basic_string<char>& __str,
360 typedef basic_istream<char> __istream_type;
361 typedef __istream_type::int_type __int_type;
362 typedef __istream_type::char_type __char_type;
363 typedef __istream_type::traits_type __traits_type;
364 typedef __istream_type::__streambuf_type __streambuf_type;
365 typedef basic_string<char> __string_type;
366 typedef __string_type::size_type __size_type;
368 __size_type __extracted = 0;
369 const __size_type __n = __str.max_size();
370 ios_base::iostate __err = ios_base::goodbit;
371 __istream_type::sentry __cerb(__in, true);
377 const __int_type __idelim = __traits_type::to_int_type(__delim);
378 const __int_type __eof = __traits_type::eof();
379 __streambuf_type* __sb = __in.rdbuf();
380 __int_type __c = __sb->sgetc();
382 while (__extracted < __n
383 && !__traits_type::eq_int_type(__c, __eof)
384 && !__traits_type::eq_int_type(__c, __idelim))
386 streamsize __size = std::min(streamsize(__sb->egptr()
388 streamsize(__n - __extracted));
391 const __char_type* __p = __traits_type::find(__sb->gptr(),
395 __size = __p - __sb->gptr();
396 __str.append(__sb->gptr(), __size);
397 __sb->__safe_gbump(__size);
398 __extracted += __size;
403 __str += __traits_type::to_char_type(__c);
405 __c = __sb->snextc();
409 if (__traits_type::eq_int_type(__c, __eof))
410 __err |= ios_base::eofbit;
411 else if (__traits_type::eq_int_type(__c, __idelim))
417 __err |= ios_base::failbit;
419 __catch(__cxxabiv1::__forced_unwind&)
421 __in._M_setstate(ios_base::badbit);
422 __throw_exception_again;
426 // _GLIBCXX_RESOLVE_LIB_DEFECTS
427 // 91. Description of operator>> and getline() for string<>
428 // might cause endless loop
429 __in._M_setstate(ios_base::badbit);
433 __err |= ios_base::failbit;
435 __in.setstate(__err);
439 #ifdef _GLIBCXX_USE_WCHAR_T
441 basic_istream<wchar_t>&
442 basic_istream<wchar_t>::
443 getline(char_type* __s, streamsize __n, char_type __delim)
446 ios_base::iostate __err = ios_base::goodbit;
447 sentry __cerb(*this, true);
452 const int_type __idelim = traits_type::to_int_type(__delim);
453 const int_type __eof = traits_type::eof();
454 __streambuf_type* __sb = this->rdbuf();
455 int_type __c = __sb->sgetc();
457 while (_M_gcount + 1 < __n
458 && !traits_type::eq_int_type(__c, __eof)
459 && !traits_type::eq_int_type(__c, __idelim))
461 streamsize __size = std::min(streamsize(__sb->egptr()
463 streamsize(__n - _M_gcount
467 const char_type* __p = traits_type::find(__sb->gptr(),
471 __size = __p - __sb->gptr();
472 traits_type::copy(__s, __sb->gptr(), __size);
474 __sb->__safe_gbump(__size);
480 *__s++ = traits_type::to_char_type(__c);
482 __c = __sb->snextc();
486 if (traits_type::eq_int_type(__c, __eof))
487 __err |= ios_base::eofbit;
488 else if (traits_type::eq_int_type(__c, __idelim))
494 __err |= ios_base::failbit;
496 __catch(__cxxabiv1::__forced_unwind&)
498 this->_M_setstate(ios_base::badbit);
499 __throw_exception_again;
502 { this->_M_setstate(ios_base::badbit); }
504 // _GLIBCXX_RESOLVE_LIB_DEFECTS
505 // 243. get and getline when sentry reports failure.
509 __err |= ios_base::failbit;
511 this->setstate(__err);
516 basic_istream<wchar_t>&
517 basic_istream<wchar_t>::
518 ignore(streamsize __n, int_type __delim)
520 if (traits_type::eq_int_type(__delim, traits_type::eof()))
524 sentry __cerb(*this, true);
525 if (__n > 0 && __cerb)
527 ios_base::iostate __err = ios_base::goodbit;
530 const char_type __cdelim = traits_type::to_char_type(__delim);
531 const int_type __eof = traits_type::eof();
532 __streambuf_type* __sb = this->rdbuf();
533 int_type __c = __sb->sgetc();
535 bool __large_ignore = false;
538 while (_M_gcount < __n
539 && !traits_type::eq_int_type(__c, __eof)
540 && !traits_type::eq_int_type(__c, __delim))
542 streamsize __size = std::min(streamsize(__sb->egptr()
544 streamsize(__n - _M_gcount));
547 const char_type* __p = traits_type::find(__sb->gptr(),
551 __size = __p - __sb->gptr();
552 __sb->__safe_gbump(__size);
559 __c = __sb->snextc();
562 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
563 && !traits_type::eq_int_type(__c, __eof)
564 && !traits_type::eq_int_type(__c, __delim))
567 __gnu_cxx::__numeric_traits<streamsize>::__min;
568 __large_ignore = true;
575 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
577 if (traits_type::eq_int_type(__c, __eof))
578 __err |= ios_base::eofbit;
579 else if (traits_type::eq_int_type(__c, __delim))
582 < __gnu_cxx::__numeric_traits<streamsize>::__max)
587 __catch(__cxxabiv1::__forced_unwind&)
589 this->_M_setstate(ios_base::badbit);
590 __throw_exception_again;
593 { this->_M_setstate(ios_base::badbit); }
595 this->setstate(__err);
601 basic_istream<wchar_t>&
602 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
605 typedef basic_istream<wchar_t> __istream_type;
606 typedef __istream_type::int_type __int_type;
607 typedef __istream_type::char_type __char_type;
608 typedef __istream_type::traits_type __traits_type;
609 typedef __istream_type::__streambuf_type __streambuf_type;
610 typedef basic_string<wchar_t> __string_type;
611 typedef __string_type::size_type __size_type;
613 __size_type __extracted = 0;
614 const __size_type __n = __str.max_size();
615 ios_base::iostate __err = ios_base::goodbit;
616 __istream_type::sentry __cerb(__in, true);
622 const __int_type __idelim = __traits_type::to_int_type(__delim);
623 const __int_type __eof = __traits_type::eof();
624 __streambuf_type* __sb = __in.rdbuf();
625 __int_type __c = __sb->sgetc();
627 while (__extracted < __n
628 && !__traits_type::eq_int_type(__c, __eof)
629 && !__traits_type::eq_int_type(__c, __idelim))
631 streamsize __size = std::min(streamsize(__sb->egptr()
633 streamsize(__n - __extracted));
636 const __char_type* __p = __traits_type::find(__sb->gptr(),
640 __size = __p - __sb->gptr();
641 __str.append(__sb->gptr(), __size);
642 __sb->__safe_gbump(__size);
643 __extracted += __size;
648 __str += __traits_type::to_char_type(__c);
650 __c = __sb->snextc();
654 if (__traits_type::eq_int_type(__c, __eof))
655 __err |= ios_base::eofbit;
656 else if (__traits_type::eq_int_type(__c, __idelim))
662 __err |= ios_base::failbit;
664 __catch(__cxxabiv1::__forced_unwind&)
666 __in._M_setstate(ios_base::badbit);
667 __throw_exception_again;
671 // _GLIBCXX_RESOLVE_LIB_DEFECTS
672 // 91. Description of operator>> and getline() for string<>
673 // might cause endless loop
674 __in._M_setstate(ios_base::badbit);
678 __err |= ios_base::failbit;
680 __in.setstate(__err);
685 _GLIBCXX_END_NAMESPACE_VERSION