1 // Input streams -*- C++ -*-
3 // Copyright (C) 2004 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 const bool __bound
= __n
!= numeric_limits
<streamsize
>::max();
130 while (_M_gcount
<= __n
131 && !traits_type::eq_int_type(__c
, __eof
))
133 streamsize __size
= __sb
->egptr() - __sb
->gptr();
135 __size
= std::min(__size
, streamsize(__n
- _M_gcount
+ 1));
146 __c
= __sb
->snextc();
149 if (traits_type::eq_int_type(__c
, __eof
))
150 __err
|= ios_base::eofbit
;
153 { this->_M_setstate(ios_base::badbit
); }
155 this->setstate(__err
);
162 basic_istream
<char>::
163 ignore(streamsize __n
, int_type __delim
)
165 if (traits_type::eq_int_type(__delim
, traits_type::eof()))
169 sentry
__cerb(*this, true);
170 if (__cerb
&& __n
> 0)
172 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
175 const char_type __cdelim
= traits_type::to_char_type(__delim
);
176 const int_type __eof
= traits_type::eof();
177 __streambuf_type
* __sb
= this->rdbuf();
178 int_type __c
= __sb
->sgetc();
180 const bool __bound
= __n
!= numeric_limits
<streamsize
>::max();
183 while (_M_gcount
<= __n
184 && !traits_type::eq_int_type(__c
, __eof
)
185 && !traits_type::eq_int_type(__c
, __delim
))
187 streamsize __size
= __sb
->egptr() - __sb
->gptr();
189 __size
= std::min(__size
, streamsize(__n
- _M_gcount
+ 1));
193 const char_type
* __p
= traits_type::find(__sb
->gptr(),
197 __size
= __p
- __sb
->gptr();
205 __c
= __sb
->snextc();
208 if (traits_type::eq_int_type(__c
, __eof
))
209 __err
|= ios_base::eofbit
;
210 else if (traits_type::eq_int_type(__c
, __delim
))
217 { this->_M_setstate(ios_base::badbit
); }
219 this->setstate(__err
);
226 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
229 typedef basic_istream
<char> __istream_type
;
230 typedef __istream_type::int_type __int_type
;
231 typedef __istream_type::char_type __char_type
;
232 typedef __istream_type::traits_type __traits_type
;
233 typedef __istream_type::__streambuf_type __streambuf_type
;
234 typedef __istream_type::__ctype_type __ctype_type
;
235 typedef basic_string
<char> __string_type
;
236 typedef __string_type::size_type __size_type
;
238 __size_type __extracted
= 0;
239 const __size_type __n
= __str
.max_size();
240 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
241 __istream_type::sentry
__cerb(__in
, true);
247 const __int_type __idelim
= __traits_type::to_int_type(__delim
);
248 const __int_type __eof
= __traits_type::eof();
249 __streambuf_type
* __sb
= __in
.rdbuf();
250 __int_type __c
= __sb
->sgetc();
252 while (__extracted
< __n
253 && !__traits_type::eq_int_type(__c
, __eof
)
254 && !__traits_type::eq_int_type(__c
, __idelim
))
256 streamsize __size
= std::min(streamsize(__sb
->egptr()
258 streamsize(__n
- __extracted
));
261 const __char_type
* __p
= __traits_type::find(__sb
->gptr(),
265 __size
= __p
- __sb
->gptr();
266 __str
.append(__sb
->gptr(), __size
);
268 __extracted
+= __size
;
273 __str
+= __traits_type::to_char_type(__c
);
275 __c
= __sb
->snextc();
279 if (__traits_type::eq_int_type(__c
, __eof
))
280 __err
|= ios_base::eofbit
;
281 else if (__traits_type::eq_int_type(__c
, __idelim
))
287 __err
|= ios_base::failbit
;
291 // _GLIBCXX_RESOLVE_LIB_DEFECTS
292 // 91. Description of operator>> and getline() for string<>
293 // might cause endless loop
294 __in
._M_setstate(ios_base::badbit
);
298 __err
|= ios_base::failbit
;
300 __in
.setstate(__err
);
304 #ifdef _GLIBCXX_USE_WCHAR_T
306 basic_istream
<wchar_t>&
307 basic_istream
<wchar_t>::
308 getline(char_type
* __s
, streamsize __n
, char_type __delim
)
311 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
312 sentry
__cerb(*this, true);
317 const int_type __idelim
= traits_type::to_int_type(__delim
);
318 const int_type __eof
= traits_type::eof();
319 __streambuf_type
* __sb
= this->rdbuf();
320 int_type __c
= __sb
->sgetc();
322 while (_M_gcount
+ 1 < __n
323 && !traits_type::eq_int_type(__c
, __eof
)
324 && !traits_type::eq_int_type(__c
, __idelim
))
326 streamsize __size
= std::min(streamsize(__sb
->egptr()
328 streamsize(__n
- _M_gcount
332 const char_type
* __p
= traits_type::find(__sb
->gptr(),
336 __size
= __p
- __sb
->gptr();
337 traits_type::copy(__s
, __sb
->gptr(), __size
);
345 *__s
++ = traits_type::to_char_type(__c
);
347 __c
= __sb
->snextc();
351 if (traits_type::eq_int_type(__c
, __eof
))
352 __err
|= ios_base::eofbit
;
353 else if (traits_type::eq_int_type(__c
, __idelim
))
359 __err
|= ios_base::failbit
;
362 { this->_M_setstate(ios_base::badbit
); }
364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
365 // 243. get and getline when sentry reports failure.
369 __err
|= ios_base::failbit
;
371 this->setstate(__err
);
376 basic_istream
<wchar_t>&
377 basic_istream
<wchar_t>::
378 ignore(streamsize __n
)
384 sentry
__cerb(*this, true);
385 if (__cerb
&& __n
> 0)
387 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
390 const int_type __eof
= traits_type::eof();
391 __streambuf_type
* __sb
= this->rdbuf();
392 int_type __c
= __sb
->sgetc();
394 const bool __bound
= __n
!= numeric_limits
<streamsize
>::max();
397 while (_M_gcount
<= __n
398 && !traits_type::eq_int_type(__c
, __eof
))
400 streamsize __size
= __sb
->egptr() - __sb
->gptr();
402 __size
= std::min(__size
, streamsize(__n
- _M_gcount
+ 1));
413 __c
= __sb
->snextc();
416 if (traits_type::eq_int_type(__c
, __eof
))
417 __err
|= ios_base::eofbit
;
420 { this->_M_setstate(ios_base::badbit
); }
422 this->setstate(__err
);
428 basic_istream
<wchar_t>&
429 basic_istream
<wchar_t>::
430 ignore(streamsize __n
, int_type __delim
)
432 if (traits_type::eq_int_type(__delim
, traits_type::eof()))
436 sentry
__cerb(*this, true);
437 if (__cerb
&& __n
> 0)
439 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
442 const char_type __cdelim
= traits_type::to_char_type(__delim
);
443 const int_type __eof
= traits_type::eof();
444 __streambuf_type
* __sb
= this->rdbuf();
445 int_type __c
= __sb
->sgetc();
447 const bool __bound
= __n
!= numeric_limits
<streamsize
>::max();
450 while (_M_gcount
<= __n
451 && !traits_type::eq_int_type(__c
, __eof
)
452 && !traits_type::eq_int_type(__c
, __delim
))
454 streamsize __size
= __sb
->egptr() - __sb
->gptr();
456 __size
= std::min(__size
, streamsize(__n
- _M_gcount
+ 1));
460 const char_type
* __p
= traits_type::find(__sb
->gptr(),
464 __size
= __p
- __sb
->gptr();
472 __c
= __sb
->snextc();
475 if (traits_type::eq_int_type(__c
, __eof
))
476 __err
|= ios_base::eofbit
;
477 else if (traits_type::eq_int_type(__c
, __delim
))
484 { this->_M_setstate(ios_base::badbit
); }
486 this->setstate(__err
);
492 basic_istream
<wchar_t>&
493 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
496 typedef basic_istream
<wchar_t> __istream_type
;
497 typedef __istream_type::int_type __int_type
;
498 typedef __istream_type::char_type __char_type
;
499 typedef __istream_type::traits_type __traits_type
;
500 typedef __istream_type::__streambuf_type __streambuf_type
;
501 typedef __istream_type::__ctype_type __ctype_type
;
502 typedef basic_string
<wchar_t> __string_type
;
503 typedef __string_type::size_type __size_type
;
505 __size_type __extracted
= 0;
506 const __size_type __n
= __str
.max_size();
507 ios_base::iostate __err
= ios_base::iostate(ios_base::goodbit
);
508 __istream_type::sentry
__cerb(__in
, true);
514 const __int_type __idelim
= __traits_type::to_int_type(__delim
);
515 const __int_type __eof
= __traits_type::eof();
516 __streambuf_type
* __sb
= __in
.rdbuf();
517 __int_type __c
= __sb
->sgetc();
519 while (__extracted
< __n
520 && !__traits_type::eq_int_type(__c
, __eof
)
521 && !__traits_type::eq_int_type(__c
, __idelim
))
523 streamsize __size
= std::min(streamsize(__sb
->egptr()
525 streamsize(__n
- __extracted
));
528 const __char_type
* __p
= __traits_type::find(__sb
->gptr(),
532 __size
= __p
- __sb
->gptr();
533 __str
.append(__sb
->gptr(), __size
);
535 __extracted
+= __size
;
540 __str
+= __traits_type::to_char_type(__c
);
542 __c
= __sb
->snextc();
546 if (__traits_type::eq_int_type(__c
, __eof
))
547 __err
|= ios_base::eofbit
;
548 else if (__traits_type::eq_int_type(__c
, __idelim
))
554 __err
|= ios_base::failbit
;
558 // _GLIBCXX_RESOLVE_LIB_DEFECTS
559 // 91. Description of operator>> and getline() for string<>
560 // might cause endless loop
561 __in
._M_setstate(ios_base::badbit
);
565 __err
|= ios_base::failbit
;
567 __in
.setstate(__err
);