1 // Input streams -*- C++ -*-
3 // Copyright (C) 2004, 2005 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.6.1 Input streams
36 _GLIBCXX_BEGIN_NAMESPACE(std)
41 getline(char_type* __s, streamsize __n, char_type __delim)
44 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
45 sentry __cerb(*this, true);
50 const int_type __idelim = traits_type::to_int_type(__delim);
51 const int_type __eof = traits_type::eof();
52 __streambuf_type* __sb = this->rdbuf();
53 int_type __c = __sb->sgetc();
55 while (_M_gcount + 1 < __n
56 && !traits_type::eq_int_type(__c, __eof)
57 && !traits_type::eq_int_type(__c, __idelim))
59 streamsize __size = std::min(streamsize(__sb->egptr()
61 streamsize(__n - _M_gcount
65 const char_type* __p = traits_type::find(__sb->gptr(),
69 __size = __p - __sb->gptr();
70 traits_type::copy(__s, __sb->gptr(), __size);
78 *__s++ = traits_type::to_char_type(__c);
84 if (traits_type::eq_int_type(__c, __eof))
85 __err |= ios_base::eofbit;
86 else if (traits_type::eq_int_type(__c, __idelim))
92 __err |= ios_base::failbit;
94 catch(__cxxabiv1::__forced_unwind&)
96 this->_M_setstate(ios_base::badbit);
97 __throw_exception_again;
100 { this->_M_setstate(ios_base::badbit); }
102 // _GLIBCXX_RESOLVE_LIB_DEFECTS
103 // 243. get and getline when sentry reports failure.
107 __err |= ios_base::failbit;
109 this->setstate(__err);
115 basic_istream<char>::
116 ignore(streamsize __n, int_type __delim)
118 if (traits_type::eq_int_type(__delim, traits_type::eof()))
122 sentry __cerb(*this, true);
123 if (__cerb && __n > 0)
125 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
128 const char_type __cdelim = traits_type::to_char_type(__delim);
129 const int_type __eof = traits_type::eof();
130 __streambuf_type* __sb = this->rdbuf();
131 int_type __c = __sb->sgetc();
133 bool __large_ignore = false;
136 while (_M_gcount < __n
137 && !traits_type::eq_int_type(__c, __eof)
138 && !traits_type::eq_int_type(__c, __delim))
140 streamsize __size = std::min(streamsize(__sb->egptr()
142 streamsize(__n - _M_gcount));
145 const char_type* __p = traits_type::find(__sb->gptr(),
149 __size = __p - __sb->gptr();
157 __c = __sb->snextc();
160 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
161 && !traits_type::eq_int_type(__c, __eof)
162 && !traits_type::eq_int_type(__c, __delim))
165 __gnu_cxx::__numeric_traits<streamsize>::__min;
166 __large_ignore = true;
173 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
175 if (traits_type::eq_int_type(__c, __eof))
176 __err |= ios_base::eofbit;
177 else if (traits_type::eq_int_type(__c, __delim))
180 < __gnu_cxx::__numeric_traits<streamsize>::__max)
185 catch(__cxxabiv1::__forced_unwind&)
187 this->_M_setstate(ios_base::badbit);
188 __throw_exception_again;
191 { this->_M_setstate(ios_base::badbit); }
193 this->setstate(__err);
200 operator>>(basic_istream<char>& __in, char* __s)
202 typedef basic_istream<char> __istream_type;
203 typedef __istream_type::int_type __int_type;
204 typedef __istream_type::char_type __char_type;
205 typedef __istream_type::traits_type __traits_type;
206 typedef __istream_type::__streambuf_type __streambuf_type;
207 typedef __istream_type::__ctype_type __ctype_type;
209 streamsize __extracted = 0;
210 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
211 __istream_type::sentry __cerb(__in, false);
216 // Figure out how many characters to extract.
217 streamsize __num = __in.width();
219 __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
221 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
223 const __int_type __eof = __traits_type::eof();
224 __streambuf_type* __sb = __in.rdbuf();
225 __int_type __c = __sb->sgetc();
227 while (__extracted < __num - 1
228 && !__traits_type::eq_int_type(__c, __eof)
229 && !__ct.is(ctype_base::space,
230 __traits_type::to_char_type(__c)))
232 streamsize __size = std::min(streamsize(__sb->egptr()
234 streamsize(__num - __extracted
238 __size = (__ct.scan_is(ctype_base::space,
240 __sb->gptr() + __size)
242 __traits_type::copy(__s, __sb->gptr(), __size);
245 __extracted += __size;
250 *__s++ = __traits_type::to_char_type(__c);
252 __c = __sb->snextc();
256 if (__traits_type::eq_int_type(__c, __eof))
257 __err |= ios_base::eofbit;
259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
260 // 68. Extractors for char* should store null at end
261 *__s = __char_type();
264 catch(__cxxabiv1::__forced_unwind&)
266 __in._M_setstate(ios_base::badbit);
267 __throw_exception_again;
270 { __in._M_setstate(ios_base::badbit); }
273 __err |= ios_base::failbit;
275 __in.setstate(__err);
281 operator>>(basic_istream<char>& __in, basic_string<char>& __str)
283 typedef basic_istream<char> __istream_type;
284 typedef __istream_type::int_type __int_type;
285 typedef __istream_type::char_type __char_type;
286 typedef __istream_type::traits_type __traits_type;
287 typedef __istream_type::__streambuf_type __streambuf_type;
288 typedef __istream_type::__ctype_type __ctype_type;
289 typedef basic_string<char> __string_type;
290 typedef __string_type::size_type __size_type;
292 __size_type __extracted = 0;
293 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
294 __istream_type::sentry __cerb(__in, false);
300 const streamsize __w = __in.width();
301 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
303 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
304 const __int_type __eof = __traits_type::eof();
305 __streambuf_type* __sb = __in.rdbuf();
306 __int_type __c = __sb->sgetc();
308 while (__extracted < __n
309 && !__traits_type::eq_int_type(__c, __eof)
310 && !__ct.is(ctype_base::space,
311 __traits_type::to_char_type(__c)))
313 streamsize __size = std::min(streamsize(__sb->egptr()
315 streamsize(__n - __extracted));
318 __size = (__ct.scan_is(ctype_base::space,
320 __sb->gptr() + __size)
322 __str.append(__sb->gptr(), __size);
324 __extracted += __size;
329 __str += __traits_type::to_char_type(__c);
331 __c = __sb->snextc();
335 if (__traits_type::eq_int_type(__c, __eof))
336 __err |= ios_base::eofbit;
339 catch(__cxxabiv1::__forced_unwind&)
341 __in._M_setstate(ios_base::badbit);
342 __throw_exception_again;
346 // _GLIBCXX_RESOLVE_LIB_DEFECTS
347 // 91. Description of operator>> and getline() for string<>
348 // might cause endless loop
349 __in._M_setstate(ios_base::badbit);
353 __err |= ios_base::failbit;
355 __in.setstate(__err);
361 getline(basic_istream<char>& __in, basic_string<char>& __str,
364 typedef basic_istream<char> __istream_type;
365 typedef __istream_type::int_type __int_type;
366 typedef __istream_type::char_type __char_type;
367 typedef __istream_type::traits_type __traits_type;
368 typedef __istream_type::__streambuf_type __streambuf_type;
369 typedef __istream_type::__ctype_type __ctype_type;
370 typedef basic_string<char> __string_type;
371 typedef __string_type::size_type __size_type;
373 __size_type __extracted = 0;
374 const __size_type __n = __str.max_size();
375 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
376 __istream_type::sentry __cerb(__in, true);
382 const __int_type __idelim = __traits_type::to_int_type(__delim);
383 const __int_type __eof = __traits_type::eof();
384 __streambuf_type* __sb = __in.rdbuf();
385 __int_type __c = __sb->sgetc();
387 while (__extracted < __n
388 && !__traits_type::eq_int_type(__c, __eof)
389 && !__traits_type::eq_int_type(__c, __idelim))
391 streamsize __size = std::min(streamsize(__sb->egptr()
393 streamsize(__n - __extracted));
396 const __char_type* __p = __traits_type::find(__sb->gptr(),
400 __size = __p - __sb->gptr();
401 __str.append(__sb->gptr(), __size);
403 __extracted += __size;
408 __str += __traits_type::to_char_type(__c);
410 __c = __sb->snextc();
414 if (__traits_type::eq_int_type(__c, __eof))
415 __err |= ios_base::eofbit;
416 else if (__traits_type::eq_int_type(__c, __idelim))
422 __err |= ios_base::failbit;
424 catch(__cxxabiv1::__forced_unwind&)
426 __in._M_setstate(ios_base::badbit);
427 __throw_exception_again;
431 // _GLIBCXX_RESOLVE_LIB_DEFECTS
432 // 91. Description of operator>> and getline() for string<>
433 // might cause endless loop
434 __in._M_setstate(ios_base::badbit);
438 __err |= ios_base::failbit;
440 __in.setstate(__err);
444 #ifdef _GLIBCXX_USE_WCHAR_T
446 basic_istream<wchar_t>&
447 basic_istream<wchar_t>::
448 getline(char_type* __s, streamsize __n, char_type __delim)
451 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
452 sentry __cerb(*this, true);
457 const int_type __idelim = traits_type::to_int_type(__delim);
458 const int_type __eof = traits_type::eof();
459 __streambuf_type* __sb = this->rdbuf();
460 int_type __c = __sb->sgetc();
462 while (_M_gcount + 1 < __n
463 && !traits_type::eq_int_type(__c, __eof)
464 && !traits_type::eq_int_type(__c, __idelim))
466 streamsize __size = std::min(streamsize(__sb->egptr()
468 streamsize(__n - _M_gcount
472 const char_type* __p = traits_type::find(__sb->gptr(),
476 __size = __p - __sb->gptr();
477 traits_type::copy(__s, __sb->gptr(), __size);
485 *__s++ = traits_type::to_char_type(__c);
487 __c = __sb->snextc();
491 if (traits_type::eq_int_type(__c, __eof))
492 __err |= ios_base::eofbit;
493 else if (traits_type::eq_int_type(__c, __idelim))
499 __err |= ios_base::failbit;
501 catch(__cxxabiv1::__forced_unwind&)
503 this->_M_setstate(ios_base::badbit);
504 __throw_exception_again;
507 { this->_M_setstate(ios_base::badbit); }
509 // _GLIBCXX_RESOLVE_LIB_DEFECTS
510 // 243. get and getline when sentry reports failure.
514 __err |= ios_base::failbit;
516 this->setstate(__err);
521 basic_istream<wchar_t>&
522 basic_istream<wchar_t>::
523 ignore(streamsize __n, int_type __delim)
525 if (traits_type::eq_int_type(__delim, traits_type::eof()))
529 sentry __cerb(*this, true);
530 if (__cerb && __n > 0)
532 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
535 const char_type __cdelim = traits_type::to_char_type(__delim);
536 const int_type __eof = traits_type::eof();
537 __streambuf_type* __sb = this->rdbuf();
538 int_type __c = __sb->sgetc();
540 bool __large_ignore = false;
543 while (_M_gcount < __n
544 && !traits_type::eq_int_type(__c, __eof)
545 && !traits_type::eq_int_type(__c, __delim))
547 streamsize __size = std::min(streamsize(__sb->egptr()
549 streamsize(__n - _M_gcount));
552 const char_type* __p = traits_type::find(__sb->gptr(),
556 __size = __p - __sb->gptr();
564 __c = __sb->snextc();
567 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
568 && !traits_type::eq_int_type(__c, __eof)
569 && !traits_type::eq_int_type(__c, __delim))
572 __gnu_cxx::__numeric_traits<streamsize>::__min;
573 __large_ignore = true;
580 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
582 if (traits_type::eq_int_type(__c, __eof))
583 __err |= ios_base::eofbit;
584 else if (traits_type::eq_int_type(__c, __delim))
587 < __gnu_cxx::__numeric_traits<streamsize>::__max)
592 catch(__cxxabiv1::__forced_unwind&)
594 this->_M_setstate(ios_base::badbit);
595 __throw_exception_again;
598 { this->_M_setstate(ios_base::badbit); }
600 this->setstate(__err);
606 basic_istream<wchar_t>&
607 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
610 typedef basic_istream<wchar_t> __istream_type;
611 typedef __istream_type::int_type __int_type;
612 typedef __istream_type::char_type __char_type;
613 typedef __istream_type::traits_type __traits_type;
614 typedef __istream_type::__streambuf_type __streambuf_type;
615 typedef __istream_type::__ctype_type __ctype_type;
616 typedef basic_string<wchar_t> __string_type;
617 typedef __string_type::size_type __size_type;
619 __size_type __extracted = 0;
620 const __size_type __n = __str.max_size();
621 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
622 __istream_type::sentry __cerb(__in, true);
628 const __int_type __idelim = __traits_type::to_int_type(__delim);
629 const __int_type __eof = __traits_type::eof();
630 __streambuf_type* __sb = __in.rdbuf();
631 __int_type __c = __sb->sgetc();
633 while (__extracted < __n
634 && !__traits_type::eq_int_type(__c, __eof)
635 && !__traits_type::eq_int_type(__c, __idelim))
637 streamsize __size = std::min(streamsize(__sb->egptr()
639 streamsize(__n - __extracted));
642 const __char_type* __p = __traits_type::find(__sb->gptr(),
646 __size = __p - __sb->gptr();
647 __str.append(__sb->gptr(), __size);
649 __extracted += __size;
654 __str += __traits_type::to_char_type(__c);
656 __c = __sb->snextc();
660 if (__traits_type::eq_int_type(__c, __eof))
661 __err |= ios_base::eofbit;
662 else if (__traits_type::eq_int_type(__c, __idelim))
668 __err |= ios_base::failbit;
670 catch(__cxxabiv1::__forced_unwind&)
672 __in._M_setstate(ios_base::badbit);
673 __throw_exception_again;
677 // _GLIBCXX_RESOLVE_LIB_DEFECTS
678 // 91. Description of operator>> and getline() for string<>
679 // might cause endless loop
680 __in._M_setstate(ios_base::badbit);
684 __err |= ios_base::failbit;
686 __in.setstate(__err);
691 _GLIBCXX_END_NAMESPACE