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 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 // ISO C++ 14882: 27.6.1 Input streams
37 _GLIBCXX_BEGIN_NAMESPACE(std)
42 getline(char_type* __s, streamsize __n, char_type __delim)
45 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
46 sentry __cerb(*this, true);
51 const int_type __idelim = traits_type::to_int_type(__delim);
52 const int_type __eof = traits_type::eof();
53 __streambuf_type* __sb = this->rdbuf();
54 int_type __c = __sb->sgetc();
56 while (_M_gcount + 1 < __n
57 && !traits_type::eq_int_type(__c, __eof)
58 && !traits_type::eq_int_type(__c, __idelim))
60 streamsize __size = std::min(streamsize(__sb->egptr()
62 streamsize(__n - _M_gcount
66 const char_type* __p = traits_type::find(__sb->gptr(),
70 __size = __p - __sb->gptr();
71 traits_type::copy(__s, __sb->gptr(), __size);
79 *__s++ = traits_type::to_char_type(__c);
85 if (traits_type::eq_int_type(__c, __eof))
86 __err |= ios_base::eofbit;
87 else if (traits_type::eq_int_type(__c, __idelim))
93 __err |= ios_base::failbit;
95 __catch(__cxxabiv1::__forced_unwind&)
97 this->_M_setstate(ios_base::badbit);
98 __throw_exception_again;
101 { this->_M_setstate(ios_base::badbit); }
103 // _GLIBCXX_RESOLVE_LIB_DEFECTS
104 // 243. get and getline when sentry reports failure.
108 __err |= ios_base::failbit;
110 this->setstate(__err);
116 basic_istream<char>::
117 ignore(streamsize __n, int_type __delim)
119 if (traits_type::eq_int_type(__delim, traits_type::eof()))
123 sentry __cerb(*this, true);
124 if (__cerb && __n > 0)
126 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
129 const char_type __cdelim = traits_type::to_char_type(__delim);
130 const int_type __eof = traits_type::eof();
131 __streambuf_type* __sb = this->rdbuf();
132 int_type __c = __sb->sgetc();
134 bool __large_ignore = false;
137 while (_M_gcount < __n
138 && !traits_type::eq_int_type(__c, __eof)
139 && !traits_type::eq_int_type(__c, __delim))
141 streamsize __size = std::min(streamsize(__sb->egptr()
143 streamsize(__n - _M_gcount));
146 const char_type* __p = traits_type::find(__sb->gptr(),
150 __size = __p - __sb->gptr();
158 __c = __sb->snextc();
161 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
162 && !traits_type::eq_int_type(__c, __eof)
163 && !traits_type::eq_int_type(__c, __delim))
166 __gnu_cxx::__numeric_traits<streamsize>::__min;
167 __large_ignore = true;
174 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
176 if (traits_type::eq_int_type(__c, __eof))
177 __err |= ios_base::eofbit;
178 else if (traits_type::eq_int_type(__c, __delim))
181 < __gnu_cxx::__numeric_traits<streamsize>::__max)
186 __catch(__cxxabiv1::__forced_unwind&)
188 this->_M_setstate(ios_base::badbit);
189 __throw_exception_again;
192 { this->_M_setstate(ios_base::badbit); }
194 this->setstate(__err);
201 operator>>(basic_istream<char>& __in, char* __s)
203 typedef basic_istream<char> __istream_type;
204 typedef __istream_type::int_type __int_type;
205 typedef __istream_type::char_type __char_type;
206 typedef __istream_type::traits_type __traits_type;
207 typedef __istream_type::__streambuf_type __streambuf_type;
208 typedef __istream_type::__ctype_type __ctype_type;
210 streamsize __extracted = 0;
211 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
212 __istream_type::sentry __cerb(__in, false);
217 // Figure out how many characters to extract.
218 streamsize __num = __in.width();
220 __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
222 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
224 const __int_type __eof = __traits_type::eof();
225 __streambuf_type* __sb = __in.rdbuf();
226 __int_type __c = __sb->sgetc();
228 while (__extracted < __num - 1
229 && !__traits_type::eq_int_type(__c, __eof)
230 && !__ct.is(ctype_base::space,
231 __traits_type::to_char_type(__c)))
233 streamsize __size = std::min(streamsize(__sb->egptr()
235 streamsize(__num - __extracted
239 __size = (__ct.scan_is(ctype_base::space,
241 __sb->gptr() + __size)
243 __traits_type::copy(__s, __sb->gptr(), __size);
246 __extracted += __size;
251 *__s++ = __traits_type::to_char_type(__c);
253 __c = __sb->snextc();
257 if (__traits_type::eq_int_type(__c, __eof))
258 __err |= ios_base::eofbit;
260 // _GLIBCXX_RESOLVE_LIB_DEFECTS
261 // 68. Extractors for char* should store null at end
262 *__s = __char_type();
265 __catch(__cxxabiv1::__forced_unwind&)
267 __in._M_setstate(ios_base::badbit);
268 __throw_exception_again;
271 { __in._M_setstate(ios_base::badbit); }
274 __err |= ios_base::failbit;
276 __in.setstate(__err);
282 operator>>(basic_istream<char>& __in, basic_string<char>& __str)
284 typedef basic_istream<char> __istream_type;
285 typedef __istream_type::int_type __int_type;
286 typedef __istream_type::char_type __char_type;
287 typedef __istream_type::traits_type __traits_type;
288 typedef __istream_type::__streambuf_type __streambuf_type;
289 typedef __istream_type::__ctype_type __ctype_type;
290 typedef basic_string<char> __string_type;
291 typedef __string_type::size_type __size_type;
293 __size_type __extracted = 0;
294 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
295 __istream_type::sentry __cerb(__in, false);
301 const streamsize __w = __in.width();
302 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
304 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
305 const __int_type __eof = __traits_type::eof();
306 __streambuf_type* __sb = __in.rdbuf();
307 __int_type __c = __sb->sgetc();
309 while (__extracted < __n
310 && !__traits_type::eq_int_type(__c, __eof)
311 && !__ct.is(ctype_base::space,
312 __traits_type::to_char_type(__c)))
314 streamsize __size = std::min(streamsize(__sb->egptr()
316 streamsize(__n - __extracted));
319 __size = (__ct.scan_is(ctype_base::space,
321 __sb->gptr() + __size)
323 __str.append(__sb->gptr(), __size);
325 __extracted += __size;
330 __str += __traits_type::to_char_type(__c);
332 __c = __sb->snextc();
336 if (__traits_type::eq_int_type(__c, __eof))
337 __err |= ios_base::eofbit;
340 __catch(__cxxabiv1::__forced_unwind&)
342 __in._M_setstate(ios_base::badbit);
343 __throw_exception_again;
347 // _GLIBCXX_RESOLVE_LIB_DEFECTS
348 // 91. Description of operator>> and getline() for string<>
349 // might cause endless loop
350 __in._M_setstate(ios_base::badbit);
354 __err |= ios_base::failbit;
356 __in.setstate(__err);
362 getline(basic_istream<char>& __in, basic_string<char>& __str,
365 typedef basic_istream<char> __istream_type;
366 typedef __istream_type::int_type __int_type;
367 typedef __istream_type::char_type __char_type;
368 typedef __istream_type::traits_type __traits_type;
369 typedef __istream_type::__streambuf_type __streambuf_type;
370 typedef __istream_type::__ctype_type __ctype_type;
371 typedef basic_string<char> __string_type;
372 typedef __string_type::size_type __size_type;
374 __size_type __extracted = 0;
375 const __size_type __n = __str.max_size();
376 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
377 __istream_type::sentry __cerb(__in, true);
383 const __int_type __idelim = __traits_type::to_int_type(__delim);
384 const __int_type __eof = __traits_type::eof();
385 __streambuf_type* __sb = __in.rdbuf();
386 __int_type __c = __sb->sgetc();
388 while (__extracted < __n
389 && !__traits_type::eq_int_type(__c, __eof)
390 && !__traits_type::eq_int_type(__c, __idelim))
392 streamsize __size = std::min(streamsize(__sb->egptr()
394 streamsize(__n - __extracted));
397 const __char_type* __p = __traits_type::find(__sb->gptr(),
401 __size = __p - __sb->gptr();
402 __str.append(__sb->gptr(), __size);
404 __extracted += __size;
409 __str += __traits_type::to_char_type(__c);
411 __c = __sb->snextc();
415 if (__traits_type::eq_int_type(__c, __eof))
416 __err |= ios_base::eofbit;
417 else if (__traits_type::eq_int_type(__c, __idelim))
423 __err |= ios_base::failbit;
425 __catch(__cxxabiv1::__forced_unwind&)
427 __in._M_setstate(ios_base::badbit);
428 __throw_exception_again;
432 // _GLIBCXX_RESOLVE_LIB_DEFECTS
433 // 91. Description of operator>> and getline() for string<>
434 // might cause endless loop
435 __in._M_setstate(ios_base::badbit);
439 __err |= ios_base::failbit;
441 __in.setstate(__err);
445 #ifdef _GLIBCXX_USE_WCHAR_T
447 basic_istream<wchar_t>&
448 basic_istream<wchar_t>::
449 getline(char_type* __s, streamsize __n, char_type __delim)
452 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
453 sentry __cerb(*this, true);
458 const int_type __idelim = traits_type::to_int_type(__delim);
459 const int_type __eof = traits_type::eof();
460 __streambuf_type* __sb = this->rdbuf();
461 int_type __c = __sb->sgetc();
463 while (_M_gcount + 1 < __n
464 && !traits_type::eq_int_type(__c, __eof)
465 && !traits_type::eq_int_type(__c, __idelim))
467 streamsize __size = std::min(streamsize(__sb->egptr()
469 streamsize(__n - _M_gcount
473 const char_type* __p = traits_type::find(__sb->gptr(),
477 __size = __p - __sb->gptr();
478 traits_type::copy(__s, __sb->gptr(), __size);
486 *__s++ = traits_type::to_char_type(__c);
488 __c = __sb->snextc();
492 if (traits_type::eq_int_type(__c, __eof))
493 __err |= ios_base::eofbit;
494 else if (traits_type::eq_int_type(__c, __idelim))
500 __err |= ios_base::failbit;
502 __catch(__cxxabiv1::__forced_unwind&)
504 this->_M_setstate(ios_base::badbit);
505 __throw_exception_again;
508 { this->_M_setstate(ios_base::badbit); }
510 // _GLIBCXX_RESOLVE_LIB_DEFECTS
511 // 243. get and getline when sentry reports failure.
515 __err |= ios_base::failbit;
517 this->setstate(__err);
522 basic_istream<wchar_t>&
523 basic_istream<wchar_t>::
524 ignore(streamsize __n, int_type __delim)
526 if (traits_type::eq_int_type(__delim, traits_type::eof()))
530 sentry __cerb(*this, true);
531 if (__cerb && __n > 0)
533 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
536 const char_type __cdelim = traits_type::to_char_type(__delim);
537 const int_type __eof = traits_type::eof();
538 __streambuf_type* __sb = this->rdbuf();
539 int_type __c = __sb->sgetc();
541 bool __large_ignore = false;
544 while (_M_gcount < __n
545 && !traits_type::eq_int_type(__c, __eof)
546 && !traits_type::eq_int_type(__c, __delim))
548 streamsize __size = std::min(streamsize(__sb->egptr()
550 streamsize(__n - _M_gcount));
553 const char_type* __p = traits_type::find(__sb->gptr(),
557 __size = __p - __sb->gptr();
565 __c = __sb->snextc();
568 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
569 && !traits_type::eq_int_type(__c, __eof)
570 && !traits_type::eq_int_type(__c, __delim))
573 __gnu_cxx::__numeric_traits<streamsize>::__min;
574 __large_ignore = true;
581 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
583 if (traits_type::eq_int_type(__c, __eof))
584 __err |= ios_base::eofbit;
585 else if (traits_type::eq_int_type(__c, __delim))
588 < __gnu_cxx::__numeric_traits<streamsize>::__max)
593 __catch(__cxxabiv1::__forced_unwind&)
595 this->_M_setstate(ios_base::badbit);
596 __throw_exception_again;
599 { this->_M_setstate(ios_base::badbit); }
601 this->setstate(__err);
607 basic_istream<wchar_t>&
608 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
611 typedef basic_istream<wchar_t> __istream_type;
612 typedef __istream_type::int_type __int_type;
613 typedef __istream_type::char_type __char_type;
614 typedef __istream_type::traits_type __traits_type;
615 typedef __istream_type::__streambuf_type __streambuf_type;
616 typedef __istream_type::__ctype_type __ctype_type;
617 typedef basic_string<wchar_t> __string_type;
618 typedef __string_type::size_type __size_type;
620 __size_type __extracted = 0;
621 const __size_type __n = __str.max_size();
622 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
623 __istream_type::sentry __cerb(__in, true);
629 const __int_type __idelim = __traits_type::to_int_type(__delim);
630 const __int_type __eof = __traits_type::eof();
631 __streambuf_type* __sb = __in.rdbuf();
632 __int_type __c = __sb->sgetc();
634 while (__extracted < __n
635 && !__traits_type::eq_int_type(__c, __eof)
636 && !__traits_type::eq_int_type(__c, __idelim))
638 streamsize __size = std::min(streamsize(__sb->egptr()
640 streamsize(__n - __extracted));
643 const __char_type* __p = __traits_type::find(__sb->gptr(),
647 __size = __p - __sb->gptr();
648 __str.append(__sb->gptr(), __size);
650 __extracted += __size;
655 __str += __traits_type::to_char_type(__c);
657 __c = __sb->snextc();
661 if (__traits_type::eq_int_type(__c, __eof))
662 __err |= ios_base::eofbit;
663 else if (__traits_type::eq_int_type(__c, __idelim))
669 __err |= ios_base::failbit;
671 __catch(__cxxabiv1::__forced_unwind&)
673 __in._M_setstate(ios_base::badbit);
674 __throw_exception_again;
678 // _GLIBCXX_RESOLVE_LIB_DEFECTS
679 // 91. Description of operator>> and getline() for string<>
680 // might cause endless loop
681 __in._M_setstate(ios_base::badbit);
685 __err |= ios_base::failbit;
687 __in.setstate(__err);
692 _GLIBCXX_END_NAMESPACE