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
== __gnu_cxx::__numeric_traits
<streamsize
>::__max
156 && !traits_type::eq_int_type(__c
, __eof
)
157 && !traits_type::eq_int_type(__c
, __delim
))
160 __gnu_cxx::__numeric_traits
<streamsize
>::__min
;
161 __large_ignore
= true;
168 _M_gcount
= __gnu_cxx::__numeric_traits
<streamsize
>::__max
;
170 if (traits_type::eq_int_type(__c
, __eof
))
171 __err
|= ios_base::eofbit
;
172 else if (traits_type::eq_int_type(__c
, __delim
))
175 < __gnu_cxx::__numeric_traits
<streamsize
>::__max
)
181 { this->_M_setstate(ios_base::badbit
); }
183 this->setstate(__err
);
190 operator>>(basic_istream
<char>& __in
, char* __s
)
192 typedef basic_istream
<char> __istream_type
;
193 typedef __istream_type::int_type __int_type
;
194 typedef __istream_type::char_type __char_type
;
195 typedef __istream_type::traits_type __traits_type
;
196 typedef __istream_type::__streambuf_type __streambuf_type
;
197 typedef __istream_type::__ctype_type __ctype_type
;
199 streamsize __extracted
= 0;
200 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
201 __istream_type::sentry
__cerb(__in
, false);
206 // Figure out how many characters to extract.
207 streamsize __num
= __in
.width();
209 __num
= __gnu_cxx::__numeric_traits
<streamsize
>::__max
;
211 const __ctype_type
& __ct
= use_facet
<__ctype_type
>(__in
.getloc());
213 const __int_type __eof
= __traits_type::eof();
214 __streambuf_type
* __sb
= __in
.rdbuf();
215 __int_type __c
= __sb
->sgetc();
217 while (__extracted
< __num
- 1
218 && !__traits_type::eq_int_type(__c
, __eof
)
219 && !__ct
.is(ctype_base::space
,
220 __traits_type::to_char_type(__c
)))
222 streamsize __size
= std::min(streamsize(__sb
->egptr()
224 streamsize(__num
- __extracted
228 __size
= (__ct
.scan_is(ctype_base::space
,
230 __sb
->gptr() + __size
)
232 __traits_type::copy(__s
, __sb
->gptr(), __size
);
235 __extracted
+= __size
;
240 *__s
++ = __traits_type::to_char_type(__c
);
242 __c
= __sb
->snextc();
246 if (__traits_type::eq_int_type(__c
, __eof
))
247 __err
|= ios_base::eofbit
;
249 // _GLIBCXX_RESOLVE_LIB_DEFECTS
250 // 68. Extractors for char* should store null at end
251 *__s
= __char_type();
255 { __in
._M_setstate(ios_base::badbit
); }
258 __err
|= ios_base::failbit
;
260 __in
.setstate(__err
);
266 operator>>(basic_istream
<char>& __in
, basic_string
<char>& __str
)
268 typedef basic_istream
<char> __istream_type
;
269 typedef __istream_type::int_type __int_type
;
270 typedef __istream_type::char_type __char_type
;
271 typedef __istream_type::traits_type __traits_type
;
272 typedef __istream_type::__streambuf_type __streambuf_type
;
273 typedef __istream_type::__ctype_type __ctype_type
;
274 typedef basic_string
<char> __string_type
;
275 typedef __string_type::size_type __size_type
;
277 __size_type __extracted
= 0;
278 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
279 __istream_type::sentry
__cerb(__in
, false);
285 const streamsize __w
= __in
.width();
286 const __size_type __n
= __w
> 0 ? static_cast<__size_type
>(__w
)
288 const __ctype_type
& __ct
= use_facet
<__ctype_type
>(__in
.getloc());
289 const __int_type __eof
= __traits_type::eof();
290 __streambuf_type
* __sb
= __in
.rdbuf();
291 __int_type __c
= __sb
->sgetc();
293 while (__extracted
< __n
294 && !__traits_type::eq_int_type(__c
, __eof
)
295 && !__ct
.is(ctype_base::space
,
296 __traits_type::to_char_type(__c
)))
298 streamsize __size
= std::min(streamsize(__sb
->egptr()
300 streamsize(__n
- __extracted
));
303 __size
= (__ct
.scan_is(ctype_base::space
,
305 __sb
->gptr() + __size
)
307 __str
.append(__sb
->gptr(), __size
);
309 __extracted
+= __size
;
314 __str
+= __traits_type::to_char_type(__c
);
316 __c
= __sb
->snextc();
320 if (__traits_type::eq_int_type(__c
, __eof
))
321 __err
|= ios_base::eofbit
;
326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
327 // 91. Description of operator>> and getline() for string<>
328 // might cause endless loop
329 __in
._M_setstate(ios_base::badbit
);
333 __err
|= ios_base::failbit
;
335 __in
.setstate(__err
);
341 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
344 typedef basic_istream
<char> __istream_type
;
345 typedef __istream_type::int_type __int_type
;
346 typedef __istream_type::char_type __char_type
;
347 typedef __istream_type::traits_type __traits_type
;
348 typedef __istream_type::__streambuf_type __streambuf_type
;
349 typedef __istream_type::__ctype_type __ctype_type
;
350 typedef basic_string
<char> __string_type
;
351 typedef __string_type::size_type __size_type
;
353 __size_type __extracted
= 0;
354 const __size_type __n
= __str
.max_size();
355 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
356 __istream_type::sentry
__cerb(__in
, true);
362 const __int_type __idelim
= __traits_type::to_int_type(__delim
);
363 const __int_type __eof
= __traits_type::eof();
364 __streambuf_type
* __sb
= __in
.rdbuf();
365 __int_type __c
= __sb
->sgetc();
367 while (__extracted
< __n
368 && !__traits_type::eq_int_type(__c
, __eof
)
369 && !__traits_type::eq_int_type(__c
, __idelim
))
371 streamsize __size
= std::min(streamsize(__sb
->egptr()
373 streamsize(__n
- __extracted
));
376 const __char_type
* __p
= __traits_type::find(__sb
->gptr(),
380 __size
= __p
- __sb
->gptr();
381 __str
.append(__sb
->gptr(), __size
);
383 __extracted
+= __size
;
388 __str
+= __traits_type::to_char_type(__c
);
390 __c
= __sb
->snextc();
394 if (__traits_type::eq_int_type(__c
, __eof
))
395 __err
|= ios_base::eofbit
;
396 else if (__traits_type::eq_int_type(__c
, __idelim
))
402 __err
|= ios_base::failbit
;
406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
407 // 91. Description of operator>> and getline() for string<>
408 // might cause endless loop
409 __in
._M_setstate(ios_base::badbit
);
413 __err
|= ios_base::failbit
;
415 __in
.setstate(__err
);
419 #ifdef _GLIBCXX_USE_WCHAR_T
421 basic_istream
<wchar_t>&
422 basic_istream
<wchar_t>::
423 getline(char_type
* __s
, streamsize __n
, char_type __delim
)
426 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
427 sentry
__cerb(*this, true);
432 const int_type __idelim
= traits_type::to_int_type(__delim
);
433 const int_type __eof
= traits_type::eof();
434 __streambuf_type
* __sb
= this->rdbuf();
435 int_type __c
= __sb
->sgetc();
437 while (_M_gcount
+ 1 < __n
438 && !traits_type::eq_int_type(__c
, __eof
)
439 && !traits_type::eq_int_type(__c
, __idelim
))
441 streamsize __size
= std::min(streamsize(__sb
->egptr()
443 streamsize(__n
- _M_gcount
447 const char_type
* __p
= traits_type::find(__sb
->gptr(),
451 __size
= __p
- __sb
->gptr();
452 traits_type::copy(__s
, __sb
->gptr(), __size
);
460 *__s
++ = traits_type::to_char_type(__c
);
462 __c
= __sb
->snextc();
466 if (traits_type::eq_int_type(__c
, __eof
))
467 __err
|= ios_base::eofbit
;
468 else if (traits_type::eq_int_type(__c
, __idelim
))
474 __err
|= ios_base::failbit
;
477 { this->_M_setstate(ios_base::badbit
); }
479 // _GLIBCXX_RESOLVE_LIB_DEFECTS
480 // 243. get and getline when sentry reports failure.
484 __err
|= ios_base::failbit
;
486 this->setstate(__err
);
491 basic_istream
<wchar_t>&
492 basic_istream
<wchar_t>::
493 ignore(streamsize __n
, int_type __delim
)
495 if (traits_type::eq_int_type(__delim
, traits_type::eof()))
499 sentry
__cerb(*this, true);
500 if (__cerb
&& __n
> 0)
502 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
505 const char_type __cdelim
= traits_type::to_char_type(__delim
);
506 const int_type __eof
= traits_type::eof();
507 __streambuf_type
* __sb
= this->rdbuf();
508 int_type __c
= __sb
->sgetc();
510 bool __large_ignore
= false;
513 while (_M_gcount
< __n
514 && !traits_type::eq_int_type(__c
, __eof
)
515 && !traits_type::eq_int_type(__c
, __delim
))
517 streamsize __size
= std::min(streamsize(__sb
->egptr()
519 streamsize(__n
- _M_gcount
));
522 const char_type
* __p
= traits_type::find(__sb
->gptr(),
526 __size
= __p
- __sb
->gptr();
534 __c
= __sb
->snextc();
537 if (__n
== __gnu_cxx::__numeric_traits
<streamsize
>::__max
538 && !traits_type::eq_int_type(__c
, __eof
)
539 && !traits_type::eq_int_type(__c
, __delim
))
542 __gnu_cxx::__numeric_traits
<streamsize
>::__min
;
543 __large_ignore
= true;
550 _M_gcount
= __gnu_cxx::__numeric_traits
<streamsize
>::__max
;
552 if (traits_type::eq_int_type(__c
, __eof
))
553 __err
|= ios_base::eofbit
;
554 else if (traits_type::eq_int_type(__c
, __delim
))
557 < __gnu_cxx::__numeric_traits
<streamsize
>::__max
)
563 { this->_M_setstate(ios_base::badbit
); }
565 this->setstate(__err
);
571 basic_istream
<wchar_t>&
572 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
575 typedef basic_istream
<wchar_t> __istream_type
;
576 typedef __istream_type::int_type __int_type
;
577 typedef __istream_type::char_type __char_type
;
578 typedef __istream_type::traits_type __traits_type
;
579 typedef __istream_type::__streambuf_type __streambuf_type
;
580 typedef __istream_type::__ctype_type __ctype_type
;
581 typedef basic_string
<wchar_t> __string_type
;
582 typedef __string_type::size_type __size_type
;
584 __size_type __extracted
= 0;
585 const __size_type __n
= __str
.max_size();
586 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
587 __istream_type::sentry
__cerb(__in
, true);
593 const __int_type __idelim
= __traits_type::to_int_type(__delim
);
594 const __int_type __eof
= __traits_type::eof();
595 __streambuf_type
* __sb
= __in
.rdbuf();
596 __int_type __c
= __sb
->sgetc();
598 while (__extracted
< __n
599 && !__traits_type::eq_int_type(__c
, __eof
)
600 && !__traits_type::eq_int_type(__c
, __idelim
))
602 streamsize __size
= std::min(streamsize(__sb
->egptr()
604 streamsize(__n
- __extracted
));
607 const __char_type
* __p
= __traits_type::find(__sb
->gptr(),
611 __size
= __p
- __sb
->gptr();
612 __str
.append(__sb
->gptr(), __size
);
614 __extracted
+= __size
;
619 __str
+= __traits_type::to_char_type(__c
);
621 __c
= __sb
->snextc();
625 if (__traits_type::eq_int_type(__c
, __eof
))
626 __err
|= ios_base::eofbit
;
627 else if (__traits_type::eq_int_type(__c
, __idelim
))
633 __err
|= ios_base::failbit
;
637 // _GLIBCXX_RESOLVE_LIB_DEFECTS
638 // 91. Description of operator>> and getline() for string<>
639 // might cause endless loop
640 __in
._M_setstate(ios_base::badbit
);
644 __err
|= ios_base::failbit
;
646 __in
.setstate(__err
);
651 _GLIBCXX_END_NAMESPACE