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, int_type __delim)
113 if (traits_type::eq_int_type(__delim, traits_type::eof()))
117 sentry __cerb(*this, true);
118 if (__cerb && __n > 0)
120 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
123 const char_type __cdelim = traits_type::to_char_type(__delim);
124 const int_type __eof = traits_type::eof();
125 __streambuf_type* __sb = this->rdbuf();
126 int_type __c = __sb->sgetc();
128 bool __large_ignore = false;
131 while (_M_gcount < __n
132 && !traits_type::eq_int_type(__c, __eof)
133 && !traits_type::eq_int_type(__c, __delim))
135 streamsize __size = std::min(streamsize(__sb->egptr()
137 streamsize(__n - _M_gcount));
140 const char_type* __p = traits_type::find(__sb->gptr(),
144 __size = __p - __sb->gptr();
152 __c = __sb->snextc();
155 if (__n == numeric_limits<streamsize>::max()
156 && !traits_type::eq_int_type(__c, __eof)
157 && !traits_type::eq_int_type(__c, __delim))
159 _M_gcount = numeric_limits<streamsize>::min();
160 __large_ignore = true;
167 _M_gcount = numeric_limits<streamsize>::max();
169 if (traits_type::eq_int_type(__c, __eof))
170 __err |= ios_base::eofbit;
171 else if (traits_type::eq_int_type(__c, __delim))
173 if (_M_gcount < numeric_limits<streamsize>::max())
179 { this->_M_setstate(ios_base::badbit); }
181 this->setstate(__err);
188 getline(basic_istream<char>& __in, basic_string<char>& __str,
191 typedef basic_istream<char> __istream_type;
192 typedef __istream_type::int_type __int_type;
193 typedef __istream_type::char_type __char_type;
194 typedef __istream_type::traits_type __traits_type;
195 typedef __istream_type::__streambuf_type __streambuf_type;
196 typedef __istream_type::__ctype_type __ctype_type;
197 typedef basic_string<char> __string_type;
198 typedef __string_type::size_type __size_type;
200 __size_type __extracted = 0;
201 const __size_type __n = __str.max_size();
202 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
203 __istream_type::sentry __cerb(__in, true);
209 const __int_type __idelim = __traits_type::to_int_type(__delim);
210 const __int_type __eof = __traits_type::eof();
211 __streambuf_type* __sb = __in.rdbuf();
212 __int_type __c = __sb->sgetc();
214 while (__extracted < __n
215 && !__traits_type::eq_int_type(__c, __eof)
216 && !__traits_type::eq_int_type(__c, __idelim))
218 streamsize __size = std::min(streamsize(__sb->egptr()
220 streamsize(__n - __extracted));
223 const __char_type* __p = __traits_type::find(__sb->gptr(),
227 __size = __p - __sb->gptr();
228 __str.append(__sb->gptr(), __size);
230 __extracted += __size;
235 __str += __traits_type::to_char_type(__c);
237 __c = __sb->snextc();
241 if (__traits_type::eq_int_type(__c, __eof))
242 __err |= ios_base::eofbit;
243 else if (__traits_type::eq_int_type(__c, __idelim))
249 __err |= ios_base::failbit;
253 // _GLIBCXX_RESOLVE_LIB_DEFECTS
254 // 91. Description of operator>> and getline() for string<>
255 // might cause endless loop
256 __in._M_setstate(ios_base::badbit);
260 __err |= ios_base::failbit;
262 __in.setstate(__err);
266 #ifdef _GLIBCXX_USE_WCHAR_T
268 basic_istream<wchar_t>&
269 basic_istream<wchar_t>::
270 getline(char_type* __s, streamsize __n, char_type __delim)
273 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
274 sentry __cerb(*this, true);
279 const int_type __idelim = traits_type::to_int_type(__delim);
280 const int_type __eof = traits_type::eof();
281 __streambuf_type* __sb = this->rdbuf();
282 int_type __c = __sb->sgetc();
284 while (_M_gcount + 1 < __n
285 && !traits_type::eq_int_type(__c, __eof)
286 && !traits_type::eq_int_type(__c, __idelim))
288 streamsize __size = std::min(streamsize(__sb->egptr()
290 streamsize(__n - _M_gcount
294 const char_type* __p = traits_type::find(__sb->gptr(),
298 __size = __p - __sb->gptr();
299 traits_type::copy(__s, __sb->gptr(), __size);
307 *__s++ = traits_type::to_char_type(__c);
309 __c = __sb->snextc();
313 if (traits_type::eq_int_type(__c, __eof))
314 __err |= ios_base::eofbit;
315 else if (traits_type::eq_int_type(__c, __idelim))
321 __err |= ios_base::failbit;
324 { this->_M_setstate(ios_base::badbit); }
326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
327 // 243. get and getline when sentry reports failure.
331 __err |= ios_base::failbit;
333 this->setstate(__err);
338 basic_istream<wchar_t>&
339 basic_istream<wchar_t>::
340 ignore(streamsize __n, int_type __delim)
342 if (traits_type::eq_int_type(__delim, traits_type::eof()))
346 sentry __cerb(*this, true);
347 if (__cerb && __n > 0)
349 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
352 const char_type __cdelim = traits_type::to_char_type(__delim);
353 const int_type __eof = traits_type::eof();
354 __streambuf_type* __sb = this->rdbuf();
355 int_type __c = __sb->sgetc();
357 bool __large_ignore = false;
360 while (_M_gcount < __n
361 && !traits_type::eq_int_type(__c, __eof)
362 && !traits_type::eq_int_type(__c, __delim))
364 streamsize __size = std::min(streamsize(__sb->egptr()
366 streamsize(__n - _M_gcount));
369 const char_type* __p = traits_type::find(__sb->gptr(),
373 __size = __p - __sb->gptr();
381 __c = __sb->snextc();
384 if (__n == numeric_limits<streamsize>::max()
385 && !traits_type::eq_int_type(__c, __eof)
386 && !traits_type::eq_int_type(__c, __delim))
388 _M_gcount = numeric_limits<streamsize>::min();
389 __large_ignore = true;
396 _M_gcount = numeric_limits<streamsize>::max();
398 if (traits_type::eq_int_type(__c, __eof))
399 __err |= ios_base::eofbit;
400 else if (traits_type::eq_int_type(__c, __delim))
402 if (_M_gcount < numeric_limits<streamsize>::max())
408 { this->_M_setstate(ios_base::badbit); }
410 this->setstate(__err);
416 basic_istream<wchar_t>&
417 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
420 typedef basic_istream<wchar_t> __istream_type;
421 typedef __istream_type::int_type __int_type;
422 typedef __istream_type::char_type __char_type;
423 typedef __istream_type::traits_type __traits_type;
424 typedef __istream_type::__streambuf_type __streambuf_type;
425 typedef __istream_type::__ctype_type __ctype_type;
426 typedef basic_string<wchar_t> __string_type;
427 typedef __string_type::size_type __size_type;
429 __size_type __extracted = 0;
430 const __size_type __n = __str.max_size();
431 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
432 __istream_type::sentry __cerb(__in, true);
438 const __int_type __idelim = __traits_type::to_int_type(__delim);
439 const __int_type __eof = __traits_type::eof();
440 __streambuf_type* __sb = __in.rdbuf();
441 __int_type __c = __sb->sgetc();
443 while (__extracted < __n
444 && !__traits_type::eq_int_type(__c, __eof)
445 && !__traits_type::eq_int_type(__c, __idelim))
447 streamsize __size = std::min(streamsize(__sb->egptr()
449 streamsize(__n - __extracted));
452 const __char_type* __p = __traits_type::find(__sb->gptr(),
456 __size = __p - __sb->gptr();
457 __str.append(__sb->gptr(), __size);
459 __extracted += __size;
464 __str += __traits_type::to_char_type(__c);
466 __c = __sb->snextc();
470 if (__traits_type::eq_int_type(__c, __eof))
471 __err |= ios_base::eofbit;
472 else if (__traits_type::eq_int_type(__c, __idelim))
478 __err |= ios_base::failbit;
482 // _GLIBCXX_RESOLVE_LIB_DEFECTS
483 // 91. Description of operator>> and getline() for string<>
484 // might cause endless loop
485 __in._M_setstate(ios_base::badbit);
489 __err |= ios_base::failbit;
491 __in.setstate(__err);