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;
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 operator>>(basic_istream<char>& __in, char* __s)
190 typedef basic_istream<char> __istream_type;
191 typedef __istream_type::int_type __int_type;
192 typedef __istream_type::char_type __char_type;
193 typedef __istream_type::traits_type __traits_type;
194 typedef __istream_type::__streambuf_type __streambuf_type;
195 typedef __istream_type::__ctype_type __ctype_type;
197 streamsize __extracted = 0;
198 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
199 __istream_type::sentry __cerb(__in, false);
204 // Figure out how many characters to extract.
205 streamsize __num = __in.width();
207 __num = numeric_limits<streamsize>::max();
209 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
211 const __int_type __eof = __traits_type::eof();
212 __streambuf_type* __sb = __in.rdbuf();
213 __int_type __c = __sb->sgetc();
215 while (__extracted < __num - 1
216 && !__traits_type::eq_int_type(__c, __eof)
217 && !__ct.is(ctype_base::space,
218 __traits_type::to_char_type(__c)))
220 streamsize __size = std::min(streamsize(__sb->egptr()
222 streamsize(__num - __extracted
226 __size = (__ct.scan_is(ctype_base::space,
228 __sb->gptr() + __size)
230 __traits_type::copy(__s, __sb->gptr(), __size);
233 __extracted += __size;
238 *__s++ = __traits_type::to_char_type(__c);
240 __c = __sb->snextc();
244 if (__traits_type::eq_int_type(__c, __eof))
245 __err |= ios_base::eofbit;
247 // _GLIBCXX_RESOLVE_LIB_DEFECTS
248 // 68. Extractors for char* should store null at end
249 *__s = __char_type();
253 { __in._M_setstate(ios_base::badbit); }
256 __err |= ios_base::failbit;
258 __in.setstate(__err);
264 operator>>(basic_istream<char>& __in, basic_string<char>& __str)
266 typedef basic_istream<char> __istream_type;
267 typedef __istream_type::int_type __int_type;
268 typedef __istream_type::char_type __char_type;
269 typedef __istream_type::traits_type __traits_type;
270 typedef __istream_type::__streambuf_type __streambuf_type;
271 typedef __istream_type::__ctype_type __ctype_type;
272 typedef basic_string<char> __string_type;
273 typedef __string_type::size_type __size_type;
275 __size_type __extracted = 0;
276 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
277 __istream_type::sentry __cerb(__in, false);
283 const streamsize __w = __in.width();
284 const __size_type __n = __w > 0 ? static_cast<__size_type>(__w)
286 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
287 const __int_type __eof = __traits_type::eof();
288 __streambuf_type* __sb = __in.rdbuf();
289 __int_type __c = __sb->sgetc();
291 while (__extracted < __n
292 && !__traits_type::eq_int_type(__c, __eof)
293 && !__ct.is(ctype_base::space,
294 __traits_type::to_char_type(__c)))
296 streamsize __size = std::min(streamsize(__sb->egptr()
298 streamsize(__n - __extracted));
301 __size = (__ct.scan_is(ctype_base::space,
303 __sb->gptr() + __size)
305 __str.append(__sb->gptr(), __size);
307 __extracted += __size;
312 __str += __traits_type::to_char_type(__c);
314 __c = __sb->snextc();
318 if (__traits_type::eq_int_type(__c, __eof))
319 __err |= ios_base::eofbit;
324 // _GLIBCXX_RESOLVE_LIB_DEFECTS
325 // 91. Description of operator>> and getline() for string<>
326 // might cause endless loop
327 __in._M_setstate(ios_base::badbit);
331 __err |= ios_base::failbit;
333 __in.setstate(__err);
339 getline(basic_istream<char>& __in, basic_string<char>& __str,
342 typedef basic_istream<char> __istream_type;
343 typedef __istream_type::int_type __int_type;
344 typedef __istream_type::char_type __char_type;
345 typedef __istream_type::traits_type __traits_type;
346 typedef __istream_type::__streambuf_type __streambuf_type;
347 typedef __istream_type::__ctype_type __ctype_type;
348 typedef basic_string<char> __string_type;
349 typedef __string_type::size_type __size_type;
351 __size_type __extracted = 0;
352 const __size_type __n = __str.max_size();
353 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
354 __istream_type::sentry __cerb(__in, true);
360 const __int_type __idelim = __traits_type::to_int_type(__delim);
361 const __int_type __eof = __traits_type::eof();
362 __streambuf_type* __sb = __in.rdbuf();
363 __int_type __c = __sb->sgetc();
365 while (__extracted < __n
366 && !__traits_type::eq_int_type(__c, __eof)
367 && !__traits_type::eq_int_type(__c, __idelim))
369 streamsize __size = std::min(streamsize(__sb->egptr()
371 streamsize(__n - __extracted));
374 const __char_type* __p = __traits_type::find(__sb->gptr(),
378 __size = __p - __sb->gptr();
379 __str.append(__sb->gptr(), __size);
381 __extracted += __size;
386 __str += __traits_type::to_char_type(__c);
388 __c = __sb->snextc();
392 if (__traits_type::eq_int_type(__c, __eof))
393 __err |= ios_base::eofbit;
394 else if (__traits_type::eq_int_type(__c, __idelim))
400 __err |= ios_base::failbit;
404 // _GLIBCXX_RESOLVE_LIB_DEFECTS
405 // 91. Description of operator>> and getline() for string<>
406 // might cause endless loop
407 __in._M_setstate(ios_base::badbit);
411 __err |= ios_base::failbit;
413 __in.setstate(__err);
417 #ifdef _GLIBCXX_USE_WCHAR_T
419 basic_istream<wchar_t>&
420 basic_istream<wchar_t>::
421 getline(char_type* __s, streamsize __n, char_type __delim)
424 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
425 sentry __cerb(*this, true);
430 const int_type __idelim = traits_type::to_int_type(__delim);
431 const int_type __eof = traits_type::eof();
432 __streambuf_type* __sb = this->rdbuf();
433 int_type __c = __sb->sgetc();
435 while (_M_gcount + 1 < __n
436 && !traits_type::eq_int_type(__c, __eof)
437 && !traits_type::eq_int_type(__c, __idelim))
439 streamsize __size = std::min(streamsize(__sb->egptr()
441 streamsize(__n - _M_gcount
445 const char_type* __p = traits_type::find(__sb->gptr(),
449 __size = __p - __sb->gptr();
450 traits_type::copy(__s, __sb->gptr(), __size);
458 *__s++ = traits_type::to_char_type(__c);
460 __c = __sb->snextc();
464 if (traits_type::eq_int_type(__c, __eof))
465 __err |= ios_base::eofbit;
466 else if (traits_type::eq_int_type(__c, __idelim))
472 __err |= ios_base::failbit;
475 { this->_M_setstate(ios_base::badbit); }
477 // _GLIBCXX_RESOLVE_LIB_DEFECTS
478 // 243. get and getline when sentry reports failure.
482 __err |= ios_base::failbit;
484 this->setstate(__err);
489 basic_istream<wchar_t>&
490 basic_istream<wchar_t>::
491 ignore(streamsize __n, int_type __delim)
493 if (traits_type::eq_int_type(__delim, traits_type::eof()))
497 sentry __cerb(*this, true);
498 if (__cerb && __n > 0)
500 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
503 const char_type __cdelim = traits_type::to_char_type(__delim);
504 const int_type __eof = traits_type::eof();
505 __streambuf_type* __sb = this->rdbuf();
506 int_type __c = __sb->sgetc();
508 bool __large_ignore = false;
511 while (_M_gcount < __n
512 && !traits_type::eq_int_type(__c, __eof)
513 && !traits_type::eq_int_type(__c, __delim))
515 streamsize __size = std::min(streamsize(__sb->egptr()
517 streamsize(__n - _M_gcount));
520 const char_type* __p = traits_type::find(__sb->gptr(),
524 __size = __p - __sb->gptr();
532 __c = __sb->snextc();
535 if (__n == numeric_limits<streamsize>::max()
536 && !traits_type::eq_int_type(__c, __eof)
537 && !traits_type::eq_int_type(__c, __delim))
539 _M_gcount = numeric_limits<streamsize>::min();
540 __large_ignore = true;
547 _M_gcount = numeric_limits<streamsize>::max();
549 if (traits_type::eq_int_type(__c, __eof))
550 __err |= ios_base::eofbit;
551 else if (traits_type::eq_int_type(__c, __delim))
553 if (_M_gcount < numeric_limits<streamsize>::max())
559 { this->_M_setstate(ios_base::badbit); }
561 this->setstate(__err);
567 basic_istream<wchar_t>&
568 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
571 typedef basic_istream<wchar_t> __istream_type;
572 typedef __istream_type::int_type __int_type;
573 typedef __istream_type::char_type __char_type;
574 typedef __istream_type::traits_type __traits_type;
575 typedef __istream_type::__streambuf_type __streambuf_type;
576 typedef __istream_type::__ctype_type __ctype_type;
577 typedef basic_string<wchar_t> __string_type;
578 typedef __string_type::size_type __size_type;
580 __size_type __extracted = 0;
581 const __size_type __n = __str.max_size();
582 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
583 __istream_type::sentry __cerb(__in, true);
589 const __int_type __idelim = __traits_type::to_int_type(__delim);
590 const __int_type __eof = __traits_type::eof();
591 __streambuf_type* __sb = __in.rdbuf();
592 __int_type __c = __sb->sgetc();
594 while (__extracted < __n
595 && !__traits_type::eq_int_type(__c, __eof)
596 && !__traits_type::eq_int_type(__c, __idelim))
598 streamsize __size = std::min(streamsize(__sb->egptr()
600 streamsize(__n - __extracted));
603 const __char_type* __p = __traits_type::find(__sb->gptr(),
607 __size = __p - __sb->gptr();
608 __str.append(__sb->gptr(), __size);
610 __extracted += __size;
615 __str += __traits_type::to_char_type(__c);
617 __c = __sb->snextc();
621 if (__traits_type::eq_int_type(__c, __eof))
622 __err |= ios_base::eofbit;
623 else if (__traits_type::eq_int_type(__c, __idelim))
629 __err |= ios_base::failbit;
633 // _GLIBCXX_RESOLVE_LIB_DEFECTS
634 // 91. Description of operator>> and getline() for string<>
635 // might cause endless loop
636 __in._M_setstate(ios_base::badbit);
640 __err |= ios_base::failbit;
642 __in.setstate(__err);
647 _GLIBCXX_END_NAMESPACE