1 // Input streams -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 // ISO C++ 14882: 27.6.1 Input streams
32 _GLIBCXX_BEGIN_NAMESPACE(std)
37 getline(char_type* __s, streamsize __n, char_type __delim)
40 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
41 sentry __cerb(*this, true);
46 const int_type __idelim = traits_type::to_int_type(__delim);
47 const int_type __eof = traits_type::eof();
48 __streambuf_type* __sb = this->rdbuf();
49 int_type __c = __sb->sgetc();
51 while (_M_gcount + 1 < __n
52 && !traits_type::eq_int_type(__c, __eof)
53 && !traits_type::eq_int_type(__c, __idelim))
55 streamsize __size = std::min(streamsize(__sb->egptr()
57 streamsize(__n - _M_gcount
61 const char_type* __p = traits_type::find(__sb->gptr(),
65 __size = __p - __sb->gptr();
66 traits_type::copy(__s, __sb->gptr(), __size);
74 *__s++ = traits_type::to_char_type(__c);
80 if (traits_type::eq_int_type(__c, __eof))
81 __err |= ios_base::eofbit;
82 else if (traits_type::eq_int_type(__c, __idelim))
88 __err |= ios_base::failbit;
90 __catch(__cxxabiv1::__forced_unwind&)
92 this->_M_setstate(ios_base::badbit);
93 __throw_exception_again;
96 { this->_M_setstate(ios_base::badbit); }
98 // _GLIBCXX_RESOLVE_LIB_DEFECTS
99 // 243. get and getline when sentry reports failure.
103 __err |= ios_base::failbit;
105 this->setstate(__err);
111 basic_istream<char>::
112 ignore(streamsize __n, int_type __delim)
114 if (traits_type::eq_int_type(__delim, traits_type::eof()))
118 sentry __cerb(*this, true);
119 if (__cerb && __n > 0)
121 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
124 const char_type __cdelim = traits_type::to_char_type(__delim);
125 const int_type __eof = traits_type::eof();
126 __streambuf_type* __sb = this->rdbuf();
127 int_type __c = __sb->sgetc();
129 bool __large_ignore = false;
132 while (_M_gcount < __n
133 && !traits_type::eq_int_type(__c, __eof)
134 && !traits_type::eq_int_type(__c, __delim))
136 streamsize __size = std::min(streamsize(__sb->egptr()
138 streamsize(__n - _M_gcount));
141 const char_type* __p = traits_type::find(__sb->gptr(),
145 __size = __p - __sb->gptr();
153 __c = __sb->snextc();
156 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
157 && !traits_type::eq_int_type(__c, __eof)
158 && !traits_type::eq_int_type(__c, __delim))
161 __gnu_cxx::__numeric_traits<streamsize>::__min;
162 __large_ignore = true;
169 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
171 if (traits_type::eq_int_type(__c, __eof))
172 __err |= ios_base::eofbit;
173 else if (traits_type::eq_int_type(__c, __delim))
176 < __gnu_cxx::__numeric_traits<streamsize>::__max)
181 __catch(__cxxabiv1::__forced_unwind&)
183 this->_M_setstate(ios_base::badbit);
184 __throw_exception_again;
187 { this->_M_setstate(ios_base::badbit); }
189 this->setstate(__err);
196 operator>>(basic_istream<char>& __in, char* __s)
198 typedef basic_istream<char> __istream_type;
199 typedef __istream_type::int_type __int_type;
200 typedef __istream_type::char_type __char_type;
201 typedef __istream_type::traits_type __traits_type;
202 typedef __istream_type::__streambuf_type __streambuf_type;
203 typedef __istream_type::__ctype_type __ctype_type;
205 streamsize __extracted = 0;
206 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
207 __istream_type::sentry __cerb(__in, false);
212 // Figure out how many characters to extract.
213 streamsize __num = __in.width();
215 __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
217 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
219 const __int_type __eof = __traits_type::eof();
220 __streambuf_type* __sb = __in.rdbuf();
221 __int_type __c = __sb->sgetc();
223 while (__extracted < __num - 1
224 && !__traits_type::eq_int_type(__c, __eof)
225 && !__ct.is(ctype_base::space,
226 __traits_type::to_char_type(__c)))
228 streamsize __size = std::min(streamsize(__sb->egptr()
230 streamsize(__num - __extracted
234 __size = (__ct.scan_is(ctype_base::space,
236 __sb->gptr() + __size)
238 __traits_type::copy(__s, __sb->gptr(), __size);
241 __extracted += __size;
246 *__s++ = __traits_type::to_char_type(__c);
248 __c = __sb->snextc();
252 if (__traits_type::eq_int_type(__c, __eof))
253 __err |= ios_base::eofbit;
255 // _GLIBCXX_RESOLVE_LIB_DEFECTS
256 // 68. Extractors for char* should store null at end
257 *__s = __char_type();
260 __catch(__cxxabiv1::__forced_unwind&)
262 __in._M_setstate(ios_base::badbit);
263 __throw_exception_again;
266 { __in._M_setstate(ios_base::badbit); }
269 __err |= ios_base::failbit;
271 __in.setstate(__err);
277 operator>>(basic_istream<char>& __in, basic_string<char>& __str)
279 typedef basic_istream<char> __istream_type;
280 typedef __istream_type::int_type __int_type;
281 typedef __istream_type::char_type __char_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::iostate(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);
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 __istream_type::__ctype_type __ctype_type;
366 typedef basic_string<char> __string_type;
367 typedef __string_type::size_type __size_type;
369 __size_type __extracted = 0;
370 const __size_type __n = __str.max_size();
371 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
372 __istream_type::sentry __cerb(__in, true);
378 const __int_type __idelim = __traits_type::to_int_type(__delim);
379 const __int_type __eof = __traits_type::eof();
380 __streambuf_type* __sb = __in.rdbuf();
381 __int_type __c = __sb->sgetc();
383 while (__extracted < __n
384 && !__traits_type::eq_int_type(__c, __eof)
385 && !__traits_type::eq_int_type(__c, __idelim))
387 streamsize __size = std::min(streamsize(__sb->egptr()
389 streamsize(__n - __extracted));
392 const __char_type* __p = __traits_type::find(__sb->gptr(),
396 __size = __p - __sb->gptr();
397 __str.append(__sb->gptr(), __size);
399 __extracted += __size;
404 __str += __traits_type::to_char_type(__c);
406 __c = __sb->snextc();
410 if (__traits_type::eq_int_type(__c, __eof))
411 __err |= ios_base::eofbit;
412 else if (__traits_type::eq_int_type(__c, __idelim))
418 __err |= ios_base::failbit;
420 __catch(__cxxabiv1::__forced_unwind&)
422 __in._M_setstate(ios_base::badbit);
423 __throw_exception_again;
427 // _GLIBCXX_RESOLVE_LIB_DEFECTS
428 // 91. Description of operator>> and getline() for string<>
429 // might cause endless loop
430 __in._M_setstate(ios_base::badbit);
434 __err |= ios_base::failbit;
436 __in.setstate(__err);
440 #ifdef _GLIBCXX_USE_WCHAR_T
442 basic_istream<wchar_t>&
443 basic_istream<wchar_t>::
444 getline(char_type* __s, streamsize __n, char_type __delim)
447 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
448 sentry __cerb(*this, true);
453 const int_type __idelim = traits_type::to_int_type(__delim);
454 const int_type __eof = traits_type::eof();
455 __streambuf_type* __sb = this->rdbuf();
456 int_type __c = __sb->sgetc();
458 while (_M_gcount + 1 < __n
459 && !traits_type::eq_int_type(__c, __eof)
460 && !traits_type::eq_int_type(__c, __idelim))
462 streamsize __size = std::min(streamsize(__sb->egptr()
464 streamsize(__n - _M_gcount
468 const char_type* __p = traits_type::find(__sb->gptr(),
472 __size = __p - __sb->gptr();
473 traits_type::copy(__s, __sb->gptr(), __size);
481 *__s++ = traits_type::to_char_type(__c);
483 __c = __sb->snextc();
487 if (traits_type::eq_int_type(__c, __eof))
488 __err |= ios_base::eofbit;
489 else if (traits_type::eq_int_type(__c, __idelim))
495 __err |= ios_base::failbit;
497 __catch(__cxxabiv1::__forced_unwind&)
499 this->_M_setstate(ios_base::badbit);
500 __throw_exception_again;
503 { this->_M_setstate(ios_base::badbit); }
505 // _GLIBCXX_RESOLVE_LIB_DEFECTS
506 // 243. get and getline when sentry reports failure.
510 __err |= ios_base::failbit;
512 this->setstate(__err);
517 basic_istream<wchar_t>&
518 basic_istream<wchar_t>::
519 ignore(streamsize __n, int_type __delim)
521 if (traits_type::eq_int_type(__delim, traits_type::eof()))
525 sentry __cerb(*this, true);
526 if (__cerb && __n > 0)
528 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
531 const char_type __cdelim = traits_type::to_char_type(__delim);
532 const int_type __eof = traits_type::eof();
533 __streambuf_type* __sb = this->rdbuf();
534 int_type __c = __sb->sgetc();
536 bool __large_ignore = false;
539 while (_M_gcount < __n
540 && !traits_type::eq_int_type(__c, __eof)
541 && !traits_type::eq_int_type(__c, __delim))
543 streamsize __size = std::min(streamsize(__sb->egptr()
545 streamsize(__n - _M_gcount));
548 const char_type* __p = traits_type::find(__sb->gptr(),
552 __size = __p - __sb->gptr();
560 __c = __sb->snextc();
563 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
564 && !traits_type::eq_int_type(__c, __eof)
565 && !traits_type::eq_int_type(__c, __delim))
568 __gnu_cxx::__numeric_traits<streamsize>::__min;
569 __large_ignore = true;
576 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
578 if (traits_type::eq_int_type(__c, __eof))
579 __err |= ios_base::eofbit;
580 else if (traits_type::eq_int_type(__c, __delim))
583 < __gnu_cxx::__numeric_traits<streamsize>::__max)
588 __catch(__cxxabiv1::__forced_unwind&)
590 this->_M_setstate(ios_base::badbit);
591 __throw_exception_again;
594 { this->_M_setstate(ios_base::badbit); }
596 this->setstate(__err);
602 basic_istream<wchar_t>&
603 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
606 typedef basic_istream<wchar_t> __istream_type;
607 typedef __istream_type::int_type __int_type;
608 typedef __istream_type::char_type __char_type;
609 typedef __istream_type::traits_type __traits_type;
610 typedef __istream_type::__streambuf_type __streambuf_type;
611 typedef __istream_type::__ctype_type __ctype_type;
612 typedef basic_string<wchar_t> __string_type;
613 typedef __string_type::size_type __size_type;
615 __size_type __extracted = 0;
616 const __size_type __n = __str.max_size();
617 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
618 __istream_type::sentry __cerb(__in, true);
624 const __int_type __idelim = __traits_type::to_int_type(__delim);
625 const __int_type __eof = __traits_type::eof();
626 __streambuf_type* __sb = __in.rdbuf();
627 __int_type __c = __sb->sgetc();
629 while (__extracted < __n
630 && !__traits_type::eq_int_type(__c, __eof)
631 && !__traits_type::eq_int_type(__c, __idelim))
633 streamsize __size = std::min(streamsize(__sb->egptr()
635 streamsize(__n - __extracted));
638 const __char_type* __p = __traits_type::find(__sb->gptr(),
642 __size = __p - __sb->gptr();
643 __str.append(__sb->gptr(), __size);
645 __extracted += __size;
650 __str += __traits_type::to_char_type(__c);
652 __c = __sb->snextc();
656 if (__traits_type::eq_int_type(__c, __eof))
657 __err |= ios_base::eofbit;
658 else if (__traits_type::eq_int_type(__c, __idelim))
664 __err |= ios_base::failbit;
666 __catch(__cxxabiv1::__forced_unwind&)
668 __in._M_setstate(ios_base::badbit);
669 __throw_exception_again;
673 // _GLIBCXX_RESOLVE_LIB_DEFECTS
674 // 91. Description of operator>> and getline() for string<>
675 // might cause endless loop
676 __in._M_setstate(ios_base::badbit);
680 __err |= ios_base::failbit;
682 __in.setstate(__err);
687 _GLIBCXX_END_NAMESPACE