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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
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;
95 { this->_M_setstate(ios_base::badbit); }
97 // _GLIBCXX_RESOLVE_LIB_DEFECTS
98 // 243. get and getline when sentry reports failure.
102 __err |= ios_base::failbit;
104 this->setstate(__err);
110 basic_istream<char>::
111 ignore(streamsize __n)
117 sentry __cerb(*this, true);
118 if (__cerb && __n > 0)
120 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
123 const int_type __eof = traits_type::eof();
124 __streambuf_type* __sb = this->rdbuf();
125 int_type __c = __sb->sgetc();
127 // See comment in istream.tcc.
128 bool __large_ignore = false;
131 while (_M_gcount < __n
132 && !traits_type::eq_int_type(__c, __eof))
134 streamsize __size = std::min(streamsize(__sb->egptr()
136 streamsize(__n - _M_gcount));
146 __c = __sb->snextc();
149 if (__n == numeric_limits<streamsize>::max()
150 && !traits_type::eq_int_type(__c, __eof))
152 _M_gcount = numeric_limits<streamsize>::min();
153 __large_ignore = true;
160 _M_gcount = numeric_limits<streamsize>::max();
162 if (traits_type::eq_int_type(__c, __eof))
163 __err |= ios_base::eofbit;
166 { this->_M_setstate(ios_base::badbit); }
168 this->setstate(__err);
175 basic_istream<char>::
176 ignore(streamsize __n, int_type __delim)
178 if (traits_type::eq_int_type(__delim, traits_type::eof()))
182 sentry __cerb(*this, true);
183 if (__cerb && __n > 0)
185 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
188 const char_type __cdelim = traits_type::to_char_type(__delim);
189 const int_type __eof = traits_type::eof();
190 __streambuf_type* __sb = this->rdbuf();
191 int_type __c = __sb->sgetc();
193 bool __large_ignore = false;
196 while (_M_gcount < __n
197 && !traits_type::eq_int_type(__c, __eof)
198 && !traits_type::eq_int_type(__c, __delim))
200 streamsize __size = std::min(streamsize(__sb->egptr()
202 streamsize(__n - _M_gcount));
205 const char_type* __p = traits_type::find(__sb->gptr(),
209 __size = __p - __sb->gptr();
217 __c = __sb->snextc();
220 if (__n == numeric_limits<streamsize>::max()
221 && !traits_type::eq_int_type(__c, __eof)
222 && !traits_type::eq_int_type(__c, __delim))
224 _M_gcount = numeric_limits<streamsize>::min();
225 __large_ignore = true;
232 _M_gcount = numeric_limits<streamsize>::max();
234 if (traits_type::eq_int_type(__c, __eof))
235 __err |= ios_base::eofbit;
236 else if (traits_type::eq_int_type(__c, __delim))
238 if (_M_gcount < numeric_limits<streamsize>::max())
244 { this->_M_setstate(ios_base::badbit); }
246 this->setstate(__err);
253 getline(basic_istream<char>& __in, basic_string<char>& __str,
256 typedef basic_istream<char> __istream_type;
257 typedef __istream_type::int_type __int_type;
258 typedef __istream_type::char_type __char_type;
259 typedef __istream_type::traits_type __traits_type;
260 typedef __istream_type::__streambuf_type __streambuf_type;
261 typedef __istream_type::__ctype_type __ctype_type;
262 typedef basic_string<char> __string_type;
263 typedef __string_type::size_type __size_type;
265 __size_type __extracted = 0;
266 const __size_type __n = __str.max_size();
267 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
268 __istream_type::sentry __cerb(__in, true);
274 const __int_type __idelim = __traits_type::to_int_type(__delim);
275 const __int_type __eof = __traits_type::eof();
276 __streambuf_type* __sb = __in.rdbuf();
277 __int_type __c = __sb->sgetc();
279 while (__extracted < __n
280 && !__traits_type::eq_int_type(__c, __eof)
281 && !__traits_type::eq_int_type(__c, __idelim))
283 streamsize __size = std::min(streamsize(__sb->egptr()
285 streamsize(__n - __extracted));
288 const __char_type* __p = __traits_type::find(__sb->gptr(),
292 __size = __p - __sb->gptr();
293 __str.append(__sb->gptr(), __size);
295 __extracted += __size;
300 __str += __traits_type::to_char_type(__c);
302 __c = __sb->snextc();
306 if (__traits_type::eq_int_type(__c, __eof))
307 __err |= ios_base::eofbit;
308 else if (__traits_type::eq_int_type(__c, __idelim))
314 __err |= ios_base::failbit;
318 // _GLIBCXX_RESOLVE_LIB_DEFECTS
319 // 91. Description of operator>> and getline() for string<>
320 // might cause endless loop
321 __in._M_setstate(ios_base::badbit);
325 __err |= ios_base::failbit;
327 __in.setstate(__err);
331 #ifdef _GLIBCXX_USE_WCHAR_T
333 basic_istream<wchar_t>&
334 basic_istream<wchar_t>::
335 getline(char_type* __s, streamsize __n, char_type __delim)
338 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
339 sentry __cerb(*this, true);
344 const int_type __idelim = traits_type::to_int_type(__delim);
345 const int_type __eof = traits_type::eof();
346 __streambuf_type* __sb = this->rdbuf();
347 int_type __c = __sb->sgetc();
349 while (_M_gcount + 1 < __n
350 && !traits_type::eq_int_type(__c, __eof)
351 && !traits_type::eq_int_type(__c, __idelim))
353 streamsize __size = std::min(streamsize(__sb->egptr()
355 streamsize(__n - _M_gcount
359 const char_type* __p = traits_type::find(__sb->gptr(),
363 __size = __p - __sb->gptr();
364 traits_type::copy(__s, __sb->gptr(), __size);
372 *__s++ = traits_type::to_char_type(__c);
374 __c = __sb->snextc();
378 if (traits_type::eq_int_type(__c, __eof))
379 __err |= ios_base::eofbit;
380 else if (traits_type::eq_int_type(__c, __idelim))
386 __err |= ios_base::failbit;
389 { this->_M_setstate(ios_base::badbit); }
391 // _GLIBCXX_RESOLVE_LIB_DEFECTS
392 // 243. get and getline when sentry reports failure.
396 __err |= ios_base::failbit;
398 this->setstate(__err);
403 basic_istream<wchar_t>&
404 basic_istream<wchar_t>::
405 ignore(streamsize __n)
411 sentry __cerb(*this, true);
412 if (__cerb && __n > 0)
414 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
417 const int_type __eof = traits_type::eof();
418 __streambuf_type* __sb = this->rdbuf();
419 int_type __c = __sb->sgetc();
421 bool __large_ignore = false;
424 while (_M_gcount < __n
425 && !traits_type::eq_int_type(__c, __eof))
427 streamsize __size = std::min(streamsize(__sb->egptr()
429 streamsize(__n - _M_gcount));
439 __c = __sb->snextc();
442 if (__n == numeric_limits<streamsize>::max()
443 && !traits_type::eq_int_type(__c, __eof))
445 _M_gcount = numeric_limits<streamsize>::min();
446 __large_ignore = true;
453 _M_gcount = numeric_limits<streamsize>::max();
455 if (traits_type::eq_int_type(__c, __eof))
456 __err |= ios_base::eofbit;
459 { this->_M_setstate(ios_base::badbit); }
461 this->setstate(__err);
467 basic_istream<wchar_t>&
468 basic_istream<wchar_t>::
469 ignore(streamsize __n, int_type __delim)
471 if (traits_type::eq_int_type(__delim, traits_type::eof()))
475 sentry __cerb(*this, true);
476 if (__cerb && __n > 0)
478 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
481 const char_type __cdelim = traits_type::to_char_type(__delim);
482 const int_type __eof = traits_type::eof();
483 __streambuf_type* __sb = this->rdbuf();
484 int_type __c = __sb->sgetc();
486 bool __large_ignore = false;
489 while (_M_gcount < __n
490 && !traits_type::eq_int_type(__c, __eof)
491 && !traits_type::eq_int_type(__c, __delim))
493 streamsize __size = std::min(streamsize(__sb->egptr()
495 streamsize(__n - _M_gcount));
498 const char_type* __p = traits_type::find(__sb->gptr(),
502 __size = __p - __sb->gptr();
510 __c = __sb->snextc();
513 if (__n == numeric_limits<streamsize>::max()
514 && !traits_type::eq_int_type(__c, __eof)
515 && !traits_type::eq_int_type(__c, __delim))
517 _M_gcount = numeric_limits<streamsize>::min();
518 __large_ignore = true;
525 _M_gcount = numeric_limits<streamsize>::max();
527 if (traits_type::eq_int_type(__c, __eof))
528 __err |= ios_base::eofbit;
529 else if (traits_type::eq_int_type(__c, __delim))
531 if (_M_gcount < numeric_limits<streamsize>::max())
537 { this->_M_setstate(ios_base::badbit); }
539 this->setstate(__err);
545 basic_istream<wchar_t>&
546 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
549 typedef basic_istream<wchar_t> __istream_type;
550 typedef __istream_type::int_type __int_type;
551 typedef __istream_type::char_type __char_type;
552 typedef __istream_type::traits_type __traits_type;
553 typedef __istream_type::__streambuf_type __streambuf_type;
554 typedef __istream_type::__ctype_type __ctype_type;
555 typedef basic_string<wchar_t> __string_type;
556 typedef __string_type::size_type __size_type;
558 __size_type __extracted = 0;
559 const __size_type __n = __str.max_size();
560 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
561 __istream_type::sentry __cerb(__in, true);
567 const __int_type __idelim = __traits_type::to_int_type(__delim);
568 const __int_type __eof = __traits_type::eof();
569 __streambuf_type* __sb = __in.rdbuf();
570 __int_type __c = __sb->sgetc();
572 while (__extracted < __n
573 && !__traits_type::eq_int_type(__c, __eof)
574 && !__traits_type::eq_int_type(__c, __idelim))
576 streamsize __size = std::min(streamsize(__sb->egptr()
578 streamsize(__n - __extracted));
581 const __char_type* __p = __traits_type::find(__sb->gptr(),
585 __size = __p - __sb->gptr();
586 __str.append(__sb->gptr(), __size);
588 __extracted += __size;
593 __str += __traits_type::to_char_type(__c);
595 __c = __sb->snextc();
599 if (__traits_type::eq_int_type(__c, __eof))
600 __err |= ios_base::eofbit;
601 else if (__traits_type::eq_int_type(__c, __idelim))
607 __err |= ios_base::failbit;
611 // _GLIBCXX_RESOLVE_LIB_DEFECTS
612 // 91. Description of operator>> and getline() for string<>
613 // might cause endless loop
614 __in._M_setstate(ios_base::badbit);
618 __err |= ios_base::failbit;
620 __in.setstate(__err);