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();
129 while (_M_gcount < __n
130 && !traits_type::eq_int_type(__c, __eof))
132 streamsize __size = std::min(streamsize(__sb->egptr()
134 streamsize(__n - _M_gcount));
144 __c = __sb->snextc();
147 if (__n == numeric_limits<streamsize>::max()
148 && !traits_type::eq_int_type(__c, __eof))
149 _M_gcount = numeric_limits<streamsize>::min();
154 if (traits_type::eq_int_type(__c, __eof))
155 __err |= ios_base::eofbit;
158 { this->_M_setstate(ios_base::badbit); }
160 this->setstate(__err);
167 basic_istream<char>::
168 ignore(streamsize __n, int_type __delim)
170 if (traits_type::eq_int_type(__delim, traits_type::eof()))
174 sentry __cerb(*this, true);
175 if (__cerb && __n > 0)
177 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
180 const char_type __cdelim = traits_type::to_char_type(__delim);
181 const int_type __eof = traits_type::eof();
182 __streambuf_type* __sb = this->rdbuf();
183 int_type __c = __sb->sgetc();
187 while (_M_gcount < __n
188 && !traits_type::eq_int_type(__c, __eof)
189 && !traits_type::eq_int_type(__c, __delim))
191 streamsize __size = std::min(streamsize(__sb->egptr()
193 streamsize(__n - _M_gcount));
196 const char_type* __p = traits_type::find(__sb->gptr(),
200 __size = __p - __sb->gptr();
208 __c = __sb->snextc();
211 if (__n == numeric_limits<streamsize>::max()
212 && !traits_type::eq_int_type(__c, __eof)
213 && !traits_type::eq_int_type(__c, __delim))
214 _M_gcount = numeric_limits<streamsize>::min();
219 if (traits_type::eq_int_type(__c, __eof))
220 __err |= ios_base::eofbit;
221 else if (traits_type::eq_int_type(__c, __delim))
228 { this->_M_setstate(ios_base::badbit); }
230 this->setstate(__err);
237 getline(basic_istream<char>& __in, basic_string<char>& __str,
240 typedef basic_istream<char> __istream_type;
241 typedef __istream_type::int_type __int_type;
242 typedef __istream_type::char_type __char_type;
243 typedef __istream_type::traits_type __traits_type;
244 typedef __istream_type::__streambuf_type __streambuf_type;
245 typedef __istream_type::__ctype_type __ctype_type;
246 typedef basic_string<char> __string_type;
247 typedef __string_type::size_type __size_type;
249 __size_type __extracted = 0;
250 const __size_type __n = __str.max_size();
251 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
252 __istream_type::sentry __cerb(__in, true);
258 const __int_type __idelim = __traits_type::to_int_type(__delim);
259 const __int_type __eof = __traits_type::eof();
260 __streambuf_type* __sb = __in.rdbuf();
261 __int_type __c = __sb->sgetc();
263 while (__extracted < __n
264 && !__traits_type::eq_int_type(__c, __eof)
265 && !__traits_type::eq_int_type(__c, __idelim))
267 streamsize __size = std::min(streamsize(__sb->egptr()
269 streamsize(__n - __extracted));
272 const __char_type* __p = __traits_type::find(__sb->gptr(),
276 __size = __p - __sb->gptr();
277 __str.append(__sb->gptr(), __size);
279 __extracted += __size;
284 __str += __traits_type::to_char_type(__c);
286 __c = __sb->snextc();
290 if (__traits_type::eq_int_type(__c, __eof))
291 __err |= ios_base::eofbit;
292 else if (__traits_type::eq_int_type(__c, __idelim))
298 __err |= ios_base::failbit;
302 // _GLIBCXX_RESOLVE_LIB_DEFECTS
303 // 91. Description of operator>> and getline() for string<>
304 // might cause endless loop
305 __in._M_setstate(ios_base::badbit);
309 __err |= ios_base::failbit;
311 __in.setstate(__err);
315 #ifdef _GLIBCXX_USE_WCHAR_T
317 basic_istream<wchar_t>&
318 basic_istream<wchar_t>::
319 getline(char_type* __s, streamsize __n, char_type __delim)
322 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
323 sentry __cerb(*this, true);
328 const int_type __idelim = traits_type::to_int_type(__delim);
329 const int_type __eof = traits_type::eof();
330 __streambuf_type* __sb = this->rdbuf();
331 int_type __c = __sb->sgetc();
333 while (_M_gcount + 1 < __n
334 && !traits_type::eq_int_type(__c, __eof)
335 && !traits_type::eq_int_type(__c, __idelim))
337 streamsize __size = std::min(streamsize(__sb->egptr()
339 streamsize(__n - _M_gcount
343 const char_type* __p = traits_type::find(__sb->gptr(),
347 __size = __p - __sb->gptr();
348 traits_type::copy(__s, __sb->gptr(), __size);
356 *__s++ = traits_type::to_char_type(__c);
358 __c = __sb->snextc();
362 if (traits_type::eq_int_type(__c, __eof))
363 __err |= ios_base::eofbit;
364 else if (traits_type::eq_int_type(__c, __idelim))
370 __err |= ios_base::failbit;
373 { this->_M_setstate(ios_base::badbit); }
375 // _GLIBCXX_RESOLVE_LIB_DEFECTS
376 // 243. get and getline when sentry reports failure.
380 __err |= ios_base::failbit;
382 this->setstate(__err);
387 basic_istream<wchar_t>&
388 basic_istream<wchar_t>::
389 ignore(streamsize __n)
395 sentry __cerb(*this, true);
396 if (__cerb && __n > 0)
398 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
401 const int_type __eof = traits_type::eof();
402 __streambuf_type* __sb = this->rdbuf();
403 int_type __c = __sb->sgetc();
407 while (_M_gcount < __n
408 && !traits_type::eq_int_type(__c, __eof))
410 streamsize __size = std::min(streamsize(__sb->egptr()
412 streamsize(__n - _M_gcount));
422 __c = __sb->snextc();
425 if (__n == numeric_limits<streamsize>::max()
426 && !traits_type::eq_int_type(__c, __eof))
427 _M_gcount = numeric_limits<streamsize>::min();
432 if (traits_type::eq_int_type(__c, __eof))
433 __err |= ios_base::eofbit;
436 { this->_M_setstate(ios_base::badbit); }
438 this->setstate(__err);
444 basic_istream<wchar_t>&
445 basic_istream<wchar_t>::
446 ignore(streamsize __n, int_type __delim)
448 if (traits_type::eq_int_type(__delim, traits_type::eof()))
452 sentry __cerb(*this, true);
453 if (__cerb && __n > 0)
455 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
458 const char_type __cdelim = traits_type::to_char_type(__delim);
459 const int_type __eof = traits_type::eof();
460 __streambuf_type* __sb = this->rdbuf();
461 int_type __c = __sb->sgetc();
465 while (_M_gcount < __n
466 && !traits_type::eq_int_type(__c, __eof)
467 && !traits_type::eq_int_type(__c, __delim))
469 streamsize __size = std::min(streamsize(__sb->egptr()
471 streamsize(__n - _M_gcount));
474 const char_type* __p = traits_type::find(__sb->gptr(),
478 __size = __p - __sb->gptr();
486 __c = __sb->snextc();
489 if (__n == numeric_limits<streamsize>::max()
490 && !traits_type::eq_int_type(__c, __eof)
491 && !traits_type::eq_int_type(__c, __delim))
492 _M_gcount = numeric_limits<streamsize>::min();
497 if (traits_type::eq_int_type(__c, __eof))
498 __err |= ios_base::eofbit;
499 else if (traits_type::eq_int_type(__c, __delim))
506 { this->_M_setstate(ios_base::badbit); }
508 this->setstate(__err);
514 basic_istream<wchar_t>&
515 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
518 typedef basic_istream<wchar_t> __istream_type;
519 typedef __istream_type::int_type __int_type;
520 typedef __istream_type::char_type __char_type;
521 typedef __istream_type::traits_type __traits_type;
522 typedef __istream_type::__streambuf_type __streambuf_type;
523 typedef __istream_type::__ctype_type __ctype_type;
524 typedef basic_string<wchar_t> __string_type;
525 typedef __string_type::size_type __size_type;
527 __size_type __extracted = 0;
528 const __size_type __n = __str.max_size();
529 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
530 __istream_type::sentry __cerb(__in, true);
536 const __int_type __idelim = __traits_type::to_int_type(__delim);
537 const __int_type __eof = __traits_type::eof();
538 __streambuf_type* __sb = __in.rdbuf();
539 __int_type __c = __sb->sgetc();
541 while (__extracted < __n
542 && !__traits_type::eq_int_type(__c, __eof)
543 && !__traits_type::eq_int_type(__c, __idelim))
545 streamsize __size = std::min(streamsize(__sb->egptr()
547 streamsize(__n - __extracted));
550 const __char_type* __p = __traits_type::find(__sb->gptr(),
554 __size = __p - __sb->gptr();
555 __str.append(__sb->gptr(), __size);
557 __extracted += __size;
562 __str += __traits_type::to_char_type(__c);
564 __c = __sb->snextc();
568 if (__traits_type::eq_int_type(__c, __eof))
569 __err |= ios_base::eofbit;
570 else if (__traits_type::eq_int_type(__c, __idelim))
576 __err |= ios_base::failbit;
580 // _GLIBCXX_RESOLVE_LIB_DEFECTS
581 // 91. Description of operator>> and getline() for string<>
582 // might cause endless loop
583 __in._M_setstate(ios_base::badbit);
587 __err |= ios_base::failbit;
589 __in.setstate(__err);