]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/istream.tcc
PR libstdc++/36104 part four
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / istream.tcc
CommitLineData
a32e3c09
BK
1// istream classes -*- C++ -*-
2
226a2e08 3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
7c3e9502 4// 2006, 2007, 2008, 2009, 2010, 2011
a32e3c09 5// Free Software Foundation, Inc.
725dc051
BK
6//
7// This file is part of the GNU ISO C++ Library. This library is free
8// software; you can redistribute it and/or modify it under the
9// terms of the GNU General Public License as published by the
748086b7 10// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
11// any later version.
12
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
748086b7
JJ
18// Under Section 7 of GPL version 3, you are granted additional
19// permissions described in the GCC Runtime Library Exception, version
20// 3.1, as published by the Free Software Foundation.
21
22// You should have received a copy of the GNU General Public License and
23// a copy of the GCC Runtime Library Exception along with this program;
24// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25// <http://www.gnu.org/licenses/>.
725dc051 26
f910786b 27/** @file bits/istream.tcc
0aa06b18 28 * This is an internal header file, included by other library headers.
f910786b 29 * Do not attempt to use it directly. @headername{istream}
0aa06b18
BK
30 */
31
725dc051 32//
479864fe 33// ISO C++ 14882: 27.6.1 Input streams
725dc051
BK
34//
35
3d7c150e
BK
36#ifndef _ISTREAM_TCC
37#define _ISTREAM_TCC 1
38
3b794528
BK
39#pragma GCC system_header
40
7c3e9502 41#include <bits/cxxabi_forced.h>
d05f74f1 42
12ffa228
BK
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 46
725dc051
BK
47 template<typename _CharT, typename _Traits>
48 basic_istream<_CharT, _Traits>::sentry::
26c691a8 49 sentry(basic_istream<_CharT, _Traits>& __in, bool __noskip) : _M_ok(false)
725dc051 50 {
6f0398bb 51 ios_base::iostate __err = ios_base::goodbit;
ed6814f7 52 if (__in.good())
725dc051
BK
53 {
54 if (__in.tie())
55 __in.tie()->flush();
7f786096 56 if (!__noskip && bool(__in.flags() & ios_base::skipws))
ed6814f7 57 {
725dc051 58 const __int_type __eof = traits_type::eof();
39003c99
BK
59 __streambuf_type* __sb = __in.rdbuf();
60 __int_type __c = __sb->sgetc();
40e5b283 61
2803847d 62 const __ctype_type& __ct = __check_facet(__in._M_ctype);
0cd1de6f 63 while (!traits_type::eq_int_type(__c, __eof)
26c691a8 64 && __ct.is(ctype_base::space,
2803847d 65 traits_type::to_char_type(__c)))
12841eb3 66 __c = __sb->snextc();
39003c99 67
f5677b15 68 // _GLIBCXX_RESOLVE_LIB_DEFECTS
e0f05105 69 // 195. Should basic_istream::sentry's constructor ever
4c411e4f 70 // set eofbit?
f13a69ec 71 if (traits_type::eq_int_type(__c, __eof))
48f499cf 72 __err |= ios_base::eofbit;
725dc051
BK
73 }
74 }
13187a45 75
48f499cf 76 if (__in.good() && __err == ios_base::goodbit)
13187a45
BK
77 _M_ok = true;
78 else
79 {
48f499cf 80 __err |= ios_base::failbit;
6b98580b 81 __in.setstate(__err);
13187a45 82 }
725dc051
BK
83 }
84
725dc051 85 template<typename _CharT, typename _Traits>
49d5c016
PC
86 template<typename _ValueT>
87 basic_istream<_CharT, _Traits>&
88 basic_istream<_CharT, _Traits>::
89 _M_extract(_ValueT& __v)
90 {
91 sentry __cerb(*this, false);
880b527f 92 if (__cerb)
49d5c016 93 {
6f0398bb 94 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 95 __try
49d5c016
PC
96 {
97 const __num_get_type& __ng = __check_facet(this->_M_num_get);
98 __ng.get(*this, 0, *this, __err, __v);
99 }
bc2631e0 100 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1 101 {
6f0398bb 102 this->_M_setstate(ios_base::badbit);
d05f74f1
JM
103 __throw_exception_again;
104 }
bc2631e0 105 __catch(...)
49d5c016
PC
106 { this->_M_setstate(ios_base::badbit); }
107 if (__err)
108 this->setstate(__err);
109 }
110 return *this;
111 }
725dc051 112
e7968bd8
PC
113 template<typename _CharT, typename _Traits>
114 basic_istream<_CharT, _Traits>&
115 basic_istream<_CharT, _Traits>::
116 operator>>(short& __n)
117 {
118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
119 // 118. basic_istream uses nonexistent num_get member functions.
6f0398bb 120 sentry __cerb(*this, false);
880b527f 121 if (__cerb)
e7968bd8 122 {
6f0398bb
PC
123 ios_base::iostate __err = ios_base::goodbit;
124 __try
125 {
126 long __l;
127 const __num_get_type& __ng = __check_facet(this->_M_num_get);
128 __ng.get(*this, 0, *this, __err, __l);
129
130 // _GLIBCXX_RESOLVE_LIB_DEFECTS
131 // 696. istream::operator>>(int&) broken.
132 if (__l < __gnu_cxx::__numeric_traits<short>::__min)
133 {
134 __err |= ios_base::failbit;
135 __n = __gnu_cxx::__numeric_traits<short>::__min;
136 }
137 else if (__l > __gnu_cxx::__numeric_traits<short>::__max)
138 {
139 __err |= ios_base::failbit;
140 __n = __gnu_cxx::__numeric_traits<short>::__max;
141 }
142 else
143 __n = short(__l);
144 }
145 __catch(__cxxabiv1::__forced_unwind&)
146 {
147 this->_M_setstate(ios_base::badbit);
148 __throw_exception_again;
149 }
150 __catch(...)
151 { this->_M_setstate(ios_base::badbit); }
152 if (__err)
153 this->setstate(__err);
e7968bd8
PC
154 }
155 return *this;
156 }
6f0398bb 157
e7968bd8
PC
158 template<typename _CharT, typename _Traits>
159 basic_istream<_CharT, _Traits>&
160 basic_istream<_CharT, _Traits>::
161 operator>>(int& __n)
162 {
163 // _GLIBCXX_RESOLVE_LIB_DEFECTS
164 // 118. basic_istream uses nonexistent num_get member functions.
6f0398bb 165 sentry __cerb(*this, false);
880b527f 166 if (__cerb)
e7968bd8 167 {
6f0398bb
PC
168 ios_base::iostate __err = ios_base::goodbit;
169 __try
170 {
171 long __l;
172 const __num_get_type& __ng = __check_facet(this->_M_num_get);
173 __ng.get(*this, 0, *this, __err, __l);
174
175 // _GLIBCXX_RESOLVE_LIB_DEFECTS
176 // 696. istream::operator>>(int&) broken.
177 if (__l < __gnu_cxx::__numeric_traits<int>::__min)
178 {
179 __err |= ios_base::failbit;
180 __n = __gnu_cxx::__numeric_traits<int>::__min;
181 }
182 else if (__l > __gnu_cxx::__numeric_traits<int>::__max)
183 {
184 __err |= ios_base::failbit;
185 __n = __gnu_cxx::__numeric_traits<int>::__max;
186 }
187 else
188 __n = int(__l);
189 }
190 __catch(__cxxabiv1::__forced_unwind&)
191 {
192 this->_M_setstate(ios_base::badbit);
193 __throw_exception_again;
194 }
195 __catch(...)
196 { this->_M_setstate(ios_base::badbit); }
197 if (__err)
198 this->setstate(__err);
e7968bd8
PC
199 }
200 return *this;
201 }
202
725dc051 203 template<typename _CharT, typename _Traits>
ed6814f7 204 basic_istream<_CharT, _Traits>&
725dc051
BK
205 basic_istream<_CharT, _Traits>::
206 operator>>(__streambuf_type* __sbout)
207 {
6f0398bb 208 ios_base::iostate __err = ios_base::goodbit;
12841eb3 209 sentry __cerb(*this, false);
880b527f 210 if (__cerb && __sbout)
12841eb3 211 {
bc2631e0 212 __try
12841eb3 213 {
6f4d3d86
PC
214 bool __ineof;
215 if (!__copy_streambufs_eof(this->rdbuf(), __sbout, __ineof))
12841eb3 216 __err |= ios_base::failbit;
6f4d3d86
PC
217 if (__ineof)
218 __err |= ios_base::eofbit;
12841eb3 219 }
bc2631e0 220 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
221 {
222 this->_M_setstate(ios_base::failbit);
223 __throw_exception_again;
224 }
bc2631e0 225 __catch(...)
12841eb3
BK
226 { this->_M_setstate(ios_base::failbit); }
227 }
228 else if (!__sbout)
229 __err |= ios_base::failbit;
6b98580b
BK
230 if (__err)
231 this->setstate(__err);
12841eb3 232 return *this;
725dc051
BK
233 }
234
235 template<typename _CharT, typename _Traits>
31bfa177 236 typename basic_istream<_CharT, _Traits>::int_type
725dc051
BK
237 basic_istream<_CharT, _Traits>::
238 get(void)
239 {
240 const int_type __eof = traits_type::eof();
241 int_type __c = __eof;
242 _M_gcount = 0;
6f0398bb 243 ios_base::iostate __err = ios_base::goodbit;
725dc051 244 sentry __cerb(*this, true);
880b527f 245 if (__cerb)
725dc051 246 {
bc2631e0 247 __try
bfa1e6b1
BK
248 {
249 __c = this->rdbuf()->sbumpc();
250 // 27.6.1.1 paragraph 3
f13a69ec 251 if (!traits_type::eq_int_type(__c, __eof))
bfa1e6b1
BK
252 _M_gcount = 1;
253 else
12841eb3 254 __err |= ios_base::eofbit;
bfa1e6b1 255 }
bc2631e0 256 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
257 {
258 this->_M_setstate(ios_base::badbit);
259 __throw_exception_again;
260 }
bc2631e0 261 __catch(...)
12841eb3 262 { this->_M_setstate(ios_base::badbit); }
725dc051 263 }
12841eb3
BK
264 if (!_M_gcount)
265 __err |= ios_base::failbit;
6b98580b
BK
266 if (__err)
267 this->setstate(__err);
725dc051
BK
268 return __c;
269 }
270
271 template<typename _CharT, typename _Traits>
272 basic_istream<_CharT, _Traits>&
273 basic_istream<_CharT, _Traits>::
274 get(char_type& __c)
275 {
276 _M_gcount = 0;
6f0398bb 277 ios_base::iostate __err = ios_base::goodbit;
725dc051 278 sentry __cerb(*this, true);
880b527f 279 if (__cerb)
725dc051 280 {
bc2631e0 281 __try
bfa1e6b1 282 {
3adf6cad 283 const int_type __cb = this->rdbuf()->sbumpc();
bfa1e6b1 284 // 27.6.1.1 paragraph 3
12841eb3 285 if (!traits_type::eq_int_type(__cb, traits_type::eof()))
bfa1e6b1
BK
286 {
287 _M_gcount = 1;
12841eb3 288 __c = traits_type::to_char_type(__cb);
bfa1e6b1
BK
289 }
290 else
12841eb3 291 __err |= ios_base::eofbit;
bfa1e6b1 292 }
bc2631e0 293 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
294 {
295 this->_M_setstate(ios_base::badbit);
296 __throw_exception_again;
297 }
bc2631e0 298 __catch(...)
12841eb3 299 { this->_M_setstate(ios_base::badbit); }
725dc051 300 }
12841eb3
BK
301 if (!_M_gcount)
302 __err |= ios_base::failbit;
6b98580b
BK
303 if (__err)
304 this->setstate(__err);
725dc051
BK
305 return *this;
306 }
307
308 template<typename _CharT, typename _Traits>
309 basic_istream<_CharT, _Traits>&
310 basic_istream<_CharT, _Traits>::
311 get(char_type* __s, streamsize __n, char_type __delim)
312 {
313 _M_gcount = 0;
6f0398bb 314 ios_base::iostate __err = ios_base::goodbit;
725dc051 315 sentry __cerb(*this, true);
880b527f 316 if (__cerb)
725dc051 317 {
bc2631e0 318 __try
bfa1e6b1
BK
319 {
320 const int_type __idelim = traits_type::to_int_type(__delim);
321 const int_type __eof = traits_type::eof();
322 __streambuf_type* __sb = this->rdbuf();
ed6814f7
BI
323 int_type __c = __sb->sgetc();
324
325 while (_M_gcount + 1 < __n
f13a69ec
BK
326 && !traits_type::eq_int_type(__c, __eof)
327 && !traits_type::eq_int_type(__c, __idelim))
bfa1e6b1
BK
328 {
329 *__s++ = traits_type::to_char_type(__c);
330 ++_M_gcount;
b1c5b5a0 331 __c = __sb->snextc();
bfa1e6b1 332 }
f13a69ec 333 if (traits_type::eq_int_type(__c, __eof))
12841eb3 334 __err |= ios_base::eofbit;
bfa1e6b1 335 }
bc2631e0 336 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
337 {
338 this->_M_setstate(ios_base::badbit);
339 __throw_exception_again;
340 }
bc2631e0 341 __catch(...)
12841eb3 342 { this->_M_setstate(ios_base::badbit); }
725dc051 343 }
75a25e3f
PC
344 // _GLIBCXX_RESOLVE_LIB_DEFECTS
345 // 243. get and getline when sentry reports failure.
346 if (__n > 0)
347 *__s = char_type();
725dc051 348 if (!_M_gcount)
12841eb3 349 __err |= ios_base::failbit;
6b98580b
BK
350 if (__err)
351 this->setstate(__err);
725dc051
BK
352 return *this;
353 }
354
355 template<typename _CharT, typename _Traits>
356 basic_istream<_CharT, _Traits>&
357 basic_istream<_CharT, _Traits>::
358 get(__streambuf_type& __sb, char_type __delim)
359 {
360 _M_gcount = 0;
6f0398bb 361 ios_base::iostate __err = ios_base::goodbit;
725dc051 362 sentry __cerb(*this, true);
880b527f 363 if (__cerb)
725dc051 364 {
bc2631e0 365 __try
bfa1e6b1
BK
366 {
367 const int_type __idelim = traits_type::to_int_type(__delim);
ed6814f7 368 const int_type __eof = traits_type::eof();
42ca0438
BK
369 __streambuf_type* __this_sb = this->rdbuf();
370 int_type __c = __this_sb->sgetc();
f13a69ec 371 char_type __c2 = traits_type::to_char_type(__c);
ed6814f7
BI
372
373 while (!traits_type::eq_int_type(__c, __eof)
374 && !traits_type::eq_int_type(__c, __idelim)
f13a69ec 375 && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
bfa1e6b1
BK
376 {
377 ++_M_gcount;
42ca0438 378 __c = __this_sb->snextc();
f13a69ec 379 __c2 = traits_type::to_char_type(__c);
bfa1e6b1 380 }
f13a69ec 381 if (traits_type::eq_int_type(__c, __eof))
12841eb3 382 __err |= ios_base::eofbit;
bfa1e6b1 383 }
bc2631e0 384 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
385 {
386 this->_M_setstate(ios_base::badbit);
387 __throw_exception_again;
388 }
bc2631e0 389 __catch(...)
12841eb3 390 { this->_M_setstate(ios_base::badbit); }
725dc051
BK
391 }
392 if (!_M_gcount)
12841eb3 393 __err |= ios_base::failbit;
6b98580b
BK
394 if (__err)
395 this->setstate(__err);
725dc051
BK
396 return *this;
397 }
398
399 template<typename _CharT, typename _Traits>
400 basic_istream<_CharT, _Traits>&
401 basic_istream<_CharT, _Traits>::
402 getline(char_type* __s, streamsize __n, char_type __delim)
403 {
404 _M_gcount = 0;
6f0398bb 405 ios_base::iostate __err = ios_base::goodbit;
725dc051 406 sentry __cerb(*this, true);
880b527f 407 if (__cerb)
adb31ad6 408 {
bc2631e0 409 __try
adb31ad6
PC
410 {
411 const int_type __idelim = traits_type::to_int_type(__delim);
412 const int_type __eof = traits_type::eof();
413 __streambuf_type* __sb = this->rdbuf();
414 int_type __c = __sb->sgetc();
415
416 while (_M_gcount + 1 < __n
417 && !traits_type::eq_int_type(__c, __eof)
418 && !traits_type::eq_int_type(__c, __idelim))
419 {
420 *__s++ = traits_type::to_char_type(__c);
421 __c = __sb->snextc();
422 ++_M_gcount;
423 }
424 if (traits_type::eq_int_type(__c, __eof))
425 __err |= ios_base::eofbit;
426 else
427 {
428 if (traits_type::eq_int_type(__c, __idelim))
429 {
430 __sb->sbumpc();
431 ++_M_gcount;
432 }
433 else
434 __err |= ios_base::failbit;
435 }
436 }
bc2631e0 437 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
438 {
439 this->_M_setstate(ios_base::badbit);
440 __throw_exception_again;
441 }
bc2631e0 442 __catch(...)
adb31ad6
PC
443 { this->_M_setstate(ios_base::badbit); }
444 }
75a25e3f
PC
445 // _GLIBCXX_RESOLVE_LIB_DEFECTS
446 // 243. get and getline when sentry reports failure.
447 if (__n > 0)
448 *__s = char_type();
725dc051 449 if (!_M_gcount)
adb31ad6 450 __err |= ios_base::failbit;
6b98580b 451 if (__err)
adb31ad6 452 this->setstate(__err);
725dc051
BK
453 return *this;
454 }
ed6814f7 455
80dddedc
PC
456 // We provide three overloads, since the first two are much simpler
457 // than the general case. Also, the latter two can thus adopt the
458 // same "batchy" strategy used by getline above.
459 template<typename _CharT, typename _Traits>
460 basic_istream<_CharT, _Traits>&
461 basic_istream<_CharT, _Traits>::
462 ignore(void)
463 {
464 _M_gcount = 0;
465 sentry __cerb(*this, true);
880b527f 466 if (__cerb)
80dddedc 467 {
6f0398bb 468 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 469 __try
80dddedc
PC
470 {
471 const int_type __eof = traits_type::eof();
472 __streambuf_type* __sb = this->rdbuf();
473
474 if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
475 __err |= ios_base::eofbit;
476 else
477 _M_gcount = 1;
478 }
bc2631e0 479 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
480 {
481 this->_M_setstate(ios_base::badbit);
482 __throw_exception_again;
483 }
bc2631e0 484 __catch(...)
80dddedc
PC
485 { this->_M_setstate(ios_base::badbit); }
486 if (__err)
487 this->setstate(__err);
488 }
489 return *this;
490 }
491
492 template<typename _CharT, typename _Traits>
493 basic_istream<_CharT, _Traits>&
494 basic_istream<_CharT, _Traits>::
495 ignore(streamsize __n)
496 {
80dddedc
PC
497 _M_gcount = 0;
498 sentry __cerb(*this, true);
880b527f 499 if (__cerb && __n > 0)
adb31ad6 500 {
6f0398bb 501 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 502 __try
adb31ad6
PC
503 {
504 const int_type __eof = traits_type::eof();
505 __streambuf_type* __sb = this->rdbuf();
bea53dbf
PC
506 int_type __c = __sb->sgetc();
507
1e156882
PC
508 // N.B. On LFS-enabled platforms streamsize is still 32 bits
509 // wide: if we want to implement the standard mandated behavior
510 // for n == max() (see 27.6.1.3/24) we are at risk of signed
511 // integer overflow: thus these contortions. Also note that,
512 // by definition, when more than 2G chars are actually ignored,
513 // _M_gcount (the return value of gcount, that is) cannot be
514 // really correct, being unavoidably too small.
78593d78 515 bool __large_ignore = false;
bea53dbf
PC
516 while (true)
517 {
518 while (_M_gcount < __n
519 && !traits_type::eq_int_type(__c, __eof))
520 {
521 ++_M_gcount;
522 __c = __sb->snextc();
523 }
6725add5 524 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
bea53dbf 525 && !traits_type::eq_int_type(__c, __eof))
78593d78 526 {
6725add5
PC
527 _M_gcount =
528 __gnu_cxx::__numeric_traits<streamsize>::__min;
78593d78
PC
529 __large_ignore = true;
530 }
bea53dbf
PC
531 else
532 break;
533 }
adb31ad6 534
78593d78 535 if (__large_ignore)
6725add5 536 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
78593d78 537
a141a5af 538 if (traits_type::eq_int_type(__c, __eof))
adb31ad6
PC
539 __err |= ios_base::eofbit;
540 }
bc2631e0 541 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
542 {
543 this->_M_setstate(ios_base::badbit);
544 __throw_exception_again;
545 }
bc2631e0 546 __catch(...)
adb31ad6
PC
547 { this->_M_setstate(ios_base::badbit); }
548 if (__err)
549 this->setstate(__err);
550 }
80dddedc
PC
551 return *this;
552 }
553
725dc051
BK
554 template<typename _CharT, typename _Traits>
555 basic_istream<_CharT, _Traits>&
556 basic_istream<_CharT, _Traits>::
557 ignore(streamsize __n, int_type __delim)
558 {
559 _M_gcount = 0;
560 sentry __cerb(*this, true);
880b527f 561 if (__cerb && __n > 0)
adb31ad6 562 {
6f0398bb 563 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 564 __try
adb31ad6
PC
565 {
566 const int_type __eof = traits_type::eof();
567 __streambuf_type* __sb = this->rdbuf();
bea53dbf
PC
568 int_type __c = __sb->sgetc();
569
1e156882 570 // See comment above.
78593d78 571 bool __large_ignore = false;
bea53dbf
PC
572 while (true)
573 {
574 while (_M_gcount < __n
575 && !traits_type::eq_int_type(__c, __eof)
576 && !traits_type::eq_int_type(__c, __delim))
577 {
578 ++_M_gcount;
579 __c = __sb->snextc();
580 }
6725add5 581 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
bea53dbf
PC
582 && !traits_type::eq_int_type(__c, __eof)
583 && !traits_type::eq_int_type(__c, __delim))
78593d78 584 {
6725add5
PC
585 _M_gcount =
586 __gnu_cxx::__numeric_traits<streamsize>::__min;
78593d78
PC
587 __large_ignore = true;
588 }
bea53dbf
PC
589 else
590 break;
591 }
adb31ad6 592
78593d78 593 if (__large_ignore)
6725add5 594 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
78593d78 595
a141a5af 596 if (traits_type::eq_int_type(__c, __eof))
adb31ad6 597 __err |= ios_base::eofbit;
a141a5af 598 else if (traits_type::eq_int_type(__c, __delim))
bea53dbf 599 {
6725add5
PC
600 if (_M_gcount
601 < __gnu_cxx::__numeric_traits<streamsize>::__max)
78593d78 602 ++_M_gcount;
bea53dbf
PC
603 __sb->sbumpc();
604 }
adb31ad6 605 }
bc2631e0 606 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
607 {
608 this->_M_setstate(ios_base::badbit);
609 __throw_exception_again;
610 }
bc2631e0 611 __catch(...)
adb31ad6
PC
612 { this->_M_setstate(ios_base::badbit); }
613 if (__err)
614 this->setstate(__err);
615 }
725dc051
BK
616 return *this;
617 }
ed6814f7 618
725dc051 619 template<typename _CharT, typename _Traits>
31bfa177 620 typename basic_istream<_CharT, _Traits>::int_type
725dc051
BK
621 basic_istream<_CharT, _Traits>::
622 peek(void)
623 {
624 int_type __c = traits_type::eof();
625 _M_gcount = 0;
626 sentry __cerb(*this, true);
880b527f 627 if (__cerb)
725dc051 628 {
6f0398bb 629 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 630 __try
e5012ba5
PC
631 {
632 __c = this->rdbuf()->sgetc();
633 if (traits_type::eq_int_type(__c, traits_type::eof()))
12841eb3 634 __err |= ios_base::eofbit;
e5012ba5 635 }
bc2631e0 636 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
637 {
638 this->_M_setstate(ios_base::badbit);
639 __throw_exception_again;
640 }
bc2631e0 641 __catch(...)
12841eb3 642 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
643 if (__err)
644 this->setstate(__err);
ed6814f7 645 }
725dc051
BK
646 return __c;
647 }
648
649 template<typename _CharT, typename _Traits>
650 basic_istream<_CharT, _Traits>&
651 basic_istream<_CharT, _Traits>::
652 read(char_type* __s, streamsize __n)
653 {
654 _M_gcount = 0;
655 sentry __cerb(*this, true);
880b527f 656 if (__cerb)
725dc051 657 {
6f0398bb 658 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 659 __try
725dc051 660 {
6280bdbe 661 _M_gcount = this->rdbuf()->sgetn(__s, __n);
b2e4f4fd 662 if (_M_gcount != __n)
12841eb3 663 __err |= (ios_base::eofbit | ios_base::failbit);
ed6814f7 664 }
bc2631e0 665 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
666 {
667 this->_M_setstate(ios_base::badbit);
668 __throw_exception_again;
669 }
bc2631e0 670 __catch(...)
12841eb3 671 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
672 if (__err)
673 this->setstate(__err);
725dc051 674 }
725dc051
BK
675 return *this;
676 }
ed6814f7 677
725dc051 678 template<typename _CharT, typename _Traits>
ed6814f7 679 streamsize
725dc051
BK
680 basic_istream<_CharT, _Traits>::
681 readsome(char_type* __s, streamsize __n)
682 {
725dc051
BK
683 _M_gcount = 0;
684 sentry __cerb(*this, true);
880b527f 685 if (__cerb)
725dc051 686 {
6f0398bb 687 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 688 __try
725dc051 689 {
112615e7 690 // Cannot compare int_type with streamsize generically.
32da6208
PC
691 const streamsize __num = this->rdbuf()->in_avail();
692 if (__num > 0)
6280bdbe 693 _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
32da6208 694 else if (__num == -1)
12841eb3 695 __err |= ios_base::eofbit;
36ce7daa 696 }
bc2631e0 697 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
698 {
699 this->_M_setstate(ios_base::badbit);
700 __throw_exception_again;
701 }
bc2631e0 702 __catch(...)
12841eb3 703 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
704 if (__err)
705 this->setstate(__err);
725dc051 706 }
725dc051
BK
707 return _M_gcount;
708 }
ed6814f7 709
725dc051
BK
710 template<typename _CharT, typename _Traits>
711 basic_istream<_CharT, _Traits>&
712 basic_istream<_CharT, _Traits>::
713 putback(char_type __c)
714 {
f5677b15
PC
715 // _GLIBCXX_RESOLVE_LIB_DEFECTS
716 // 60. What is a formatted input function?
23cac885 717 _M_gcount = 0;
2ca2a899
PC
718 // Clear eofbit per N3168.
719 this->clear(this->rdstate() & ~ios_base::eofbit);
725dc051 720 sentry __cerb(*this, true);
880b527f 721 if (__cerb)
725dc051 722 {
6f0398bb 723 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 724 __try
bfa1e6b1
BK
725 {
726 const int_type __eof = traits_type::eof();
727 __streambuf_type* __sb = this->rdbuf();
ed6814f7 728 if (!__sb
f13a69ec 729 || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
12841eb3 730 __err |= ios_base::badbit;
bfa1e6b1 731 }
bc2631e0 732 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
733 {
734 this->_M_setstate(ios_base::badbit);
735 __throw_exception_again;
736 }
bc2631e0 737 __catch(...)
12841eb3 738 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
739 if (__err)
740 this->setstate(__err);
725dc051 741 }
725dc051
BK
742 return *this;
743 }
ed6814f7 744
725dc051
BK
745 template<typename _CharT, typename _Traits>
746 basic_istream<_CharT, _Traits>&
747 basic_istream<_CharT, _Traits>::
748 unget(void)
749 {
f5677b15
PC
750 // _GLIBCXX_RESOLVE_LIB_DEFECTS
751 // 60. What is a formatted input function?
725dc051 752 _M_gcount = 0;
2ca2a899
PC
753 // Clear eofbit per N3168.
754 this->clear(this->rdstate() & ~ios_base::eofbit);
725dc051 755 sentry __cerb(*this, true);
880b527f 756 if (__cerb)
725dc051 757 {
6f0398bb 758 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 759 __try
bfa1e6b1
BK
760 {
761 const int_type __eof = traits_type::eof();
762 __streambuf_type* __sb = this->rdbuf();
ed6814f7 763 if (!__sb
f13a69ec 764 || traits_type::eq_int_type(__sb->sungetc(), __eof))
12841eb3 765 __err |= ios_base::badbit;
bfa1e6b1 766 }
bc2631e0 767 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
768 {
769 this->_M_setstate(ios_base::badbit);
770 __throw_exception_again;
771 }
bc2631e0 772 __catch(...)
12841eb3 773 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
774 if (__err)
775 this->setstate(__err);
725dc051 776 }
725dc051
BK
777 return *this;
778 }
ed6814f7 779
725dc051
BK
780 template<typename _CharT, typename _Traits>
781 int
782 basic_istream<_CharT, _Traits>::
783 sync(void)
784 {
12841eb3 785 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6894ddcf 786 // DR60. Do not change _M_gcount.
f13a69ec 787 int __ret = -1;
725dc051 788 sentry __cerb(*this, true);
880b527f 789 if (__cerb)
725dc051 790 {
6f0398bb 791 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 792 __try
bfa1e6b1
BK
793 {
794 __streambuf_type* __sb = this->rdbuf();
f13a69ec
BK
795 if (__sb)
796 {
797 if (__sb->pubsync() == -1)
12841eb3 798 __err |= ios_base::badbit;
ed6814f7 799 else
f13a69ec
BK
800 __ret = 0;
801 }
bfa1e6b1 802 }
bc2631e0 803 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
804 {
805 this->_M_setstate(ios_base::badbit);
806 __throw_exception_again;
807 }
bc2631e0 808 __catch(...)
12841eb3 809 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
810 if (__err)
811 this->setstate(__err);
725dc051
BK
812 }
813 return __ret;
814 }
ed6814f7 815
725dc051
BK
816 template<typename _CharT, typename _Traits>
817 typename basic_istream<_CharT, _Traits>::pos_type
818 basic_istream<_CharT, _Traits>::
819 tellg(void)
820 {
12841eb3 821 // _GLIBCXX_RESOLVE_LIB_DEFECTS
d8c30611 822 // DR60. Do not change _M_gcount.
725dc051 823 pos_type __ret = pos_type(-1);
2ca2a899
PC
824 sentry __cerb(*this, true);
825 if (__cerb)
d05f74f1 826 {
2ca2a899
PC
827 __try
828 {
829 if (!this->fail())
830 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur,
831 ios_base::in);
832 }
833 __catch(__cxxabiv1::__forced_unwind&)
834 {
835 this->_M_setstate(ios_base::badbit);
836 __throw_exception_again;
837 }
838 __catch(...)
839 { this->_M_setstate(ios_base::badbit); }
d05f74f1 840 }
725dc051
BK
841 return __ret;
842 }
843
725dc051
BK
844 template<typename _CharT, typename _Traits>
845 basic_istream<_CharT, _Traits>&
846 basic_istream<_CharT, _Traits>::
847 seekg(pos_type __pos)
848 {
12841eb3 849 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6894ddcf 850 // DR60. Do not change _M_gcount.
2ca2a899
PC
851 // Clear eofbit per N3168.
852 this->clear(this->rdstate() & ~ios_base::eofbit);
853 sentry __cerb(*this, true);
854 if (__cerb)
725dc051 855 {
2ca2a899
PC
856 ios_base::iostate __err = ios_base::goodbit;
857 __try
12841eb3 858 {
2ca2a899
PC
859 if (!this->fail())
860 {
861 // 136. seekp, seekg setting wrong streams?
862 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
863 ios_base::in);
864
865 // 129. Need error indication from seekp() and seekg()
866 if (__p == pos_type(off_type(-1)))
867 __err |= ios_base::failbit;
868 }
12841eb3 869 }
2ca2a899
PC
870 __catch(__cxxabiv1::__forced_unwind&)
871 {
872 this->_M_setstate(ios_base::badbit);
873 __throw_exception_again;
874 }
875 __catch(...)
876 { this->_M_setstate(ios_base::badbit); }
877 if (__err)
878 this->setstate(__err);
725dc051
BK
879 }
880 return *this;
881 }
882
883 template<typename _CharT, typename _Traits>
884 basic_istream<_CharT, _Traits>&
885 basic_istream<_CharT, _Traits>::
886 seekg(off_type __off, ios_base::seekdir __dir)
887 {
12841eb3 888 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6894ddcf 889 // DR60. Do not change _M_gcount.
2ca2a899
PC
890 // Clear eofbit per N3168.
891 this->clear(this->rdstate() & ~ios_base::eofbit);
892 sentry __cerb(*this, true);
893 if (__cerb)
725dc051 894 {
2ca2a899
PC
895 ios_base::iostate __err = ios_base::goodbit;
896 __try
12841eb3 897 {
2ca2a899
PC
898 if (!this->fail())
899 {
900 // 136. seekp, seekg setting wrong streams?
901 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
902 ios_base::in);
c849c938 903
2ca2a899
PC
904 // 129. Need error indication from seekp() and seekg()
905 if (__p == pos_type(off_type(-1)))
906 __err |= ios_base::failbit;
907 }
12841eb3 908 }
2ca2a899
PC
909 __catch(__cxxabiv1::__forced_unwind&)
910 {
911 this->_M_setstate(ios_base::badbit);
912 __throw_exception_again;
913 }
914 __catch(...)
915 { this->_M_setstate(ios_base::badbit); }
916 if (__err)
917 this->setstate(__err);
725dc051
BK
918 }
919 return *this;
920 }
921
922 // 27.6.1.2.3 Character extraction templates
923 template<typename _CharT, typename _Traits>
924 basic_istream<_CharT, _Traits>&
925 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
926 {
ed6814f7 927 typedef basic_istream<_CharT, _Traits> __istream_type;
3adf6cad
PC
928 typedef typename __istream_type::int_type __int_type;
929
bcd0cabb 930 typename __istream_type::sentry __cerb(__in, false);
880b527f 931 if (__cerb)
725dc051 932 {
6f0398bb 933 ios_base::iostate __err = ios_base::goodbit;
bc2631e0 934 __try
bfa1e6b1 935 {
3adf6cad 936 const __int_type __cb = __in.rdbuf()->sbumpc();
12841eb3
BK
937 if (!_Traits::eq_int_type(__cb, _Traits::eof()))
938 __c = _Traits::to_char_type(__cb);
939 else
940 __err |= (ios_base::eofbit | ios_base::failbit);
bfa1e6b1 941 }
bc2631e0 942 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
943 {
944 __in._M_setstate(ios_base::badbit);
945 __throw_exception_again;
946 }
bc2631e0 947 __catch(...)
12841eb3 948 { __in._M_setstate(ios_base::badbit); }
6b98580b
BK
949 if (__err)
950 __in.setstate(__err);
725dc051 951 }
725dc051
BK
952 return __in;
953 }
954
955 template<typename _CharT, typename _Traits>
956 basic_istream<_CharT, _Traits>&
957 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
958 {
ed6814f7 959 typedef basic_istream<_CharT, _Traits> __istream_type;
11202768 960 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
ed6814f7
BI
961 typedef typename _Traits::int_type int_type;
962 typedef _CharT char_type;
963 typedef ctype<_CharT> __ctype_type;
725dc051 964
12841eb3 965 streamsize __extracted = 0;
6f0398bb 966 ios_base::iostate __err = ios_base::goodbit;
bcd0cabb 967 typename __istream_type::sentry __cerb(__in, false);
880b527f 968 if (__cerb)
725dc051 969 {
bc2631e0 970 __try
bfa1e6b1
BK
971 {
972 // Figure out how many characters to extract.
973 streamsize __num = __in.width();
1e64c2fc 974 if (__num <= 0)
6725add5 975 __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
ed6814f7 976
2803847d
BK
977 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
978
40e5b283 979 const int_type __eof = _Traits::eof();
bfa1e6b1 980 __streambuf_type* __sb = __in.rdbuf();
42ca0438 981 int_type __c = __sb->sgetc();
ed6814f7
BI
982
983 while (__extracted < __num - 1
112615e7 984 && !_Traits::eq_int_type(__c, __eof)
ed6814f7 985 && !__ct.is(ctype_base::space,
2803847d 986 _Traits::to_char_type(__c)))
bfa1e6b1 987 {
1deba98b 988 *__s++ = _Traits::to_char_type(__c);
bfa1e6b1 989 ++__extracted;
42ca0438 990 __c = __sb->snextc();
bfa1e6b1 991 }
112615e7 992 if (_Traits::eq_int_type(__c, __eof))
12841eb3 993 __err |= ios_base::eofbit;
725dc051 994
f5677b15
PC
995 // _GLIBCXX_RESOLVE_LIB_DEFECTS
996 // 68. Extractors for char* should store null at end
bfa1e6b1 997 *__s = char_type();
bfa1e6b1
BK
998 __in.width(0);
999 }
bc2631e0 1000 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1
JM
1001 {
1002 __in._M_setstate(ios_base::badbit);
1003 __throw_exception_again;
1004 }
bc2631e0 1005 __catch(...)
12841eb3 1006 { __in._M_setstate(ios_base::badbit); }
725dc051
BK
1007 }
1008 if (!__extracted)
12841eb3 1009 __err |= ios_base::failbit;
6b98580b
BK
1010 if (__err)
1011 __in.setstate(__err);
725dc051
BK
1012 return __in;
1013 }
1014
1015 // 27.6.1.4 Standard basic_istream manipulators
1016 template<typename _CharT, typename _Traits>
11202768
PC
1017 basic_istream<_CharT, _Traits>&
1018 ws(basic_istream<_CharT, _Traits>& __in)
725dc051 1019 {
ed6814f7 1020 typedef basic_istream<_CharT, _Traits> __istream_type;
11202768 1021 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
ed6814f7 1022 typedef typename __istream_type::int_type __int_type;
11202768 1023 typedef ctype<_CharT> __ctype_type;
725dc051 1024
2803847d 1025 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
ed6814f7 1026 const __int_type __eof = _Traits::eof();
42ca0438
BK
1027 __streambuf_type* __sb = __in.rdbuf();
1028 __int_type __c = __sb->sgetc();
725dc051 1029
ed6814f7 1030 while (!_Traits::eq_int_type(__c, __eof)
2803847d 1031 && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
42ca0438 1032 __c = __sb->snextc();
112615e7
BK
1033
1034 if (_Traits::eq_int_type(__c, __eof))
12841eb3 1035 __in.setstate(ios_base::eofbit);
725dc051
BK
1036 return __in;
1037 }
1038
a32e3c09 1039 // Inhibit implicit instantiations for required instantiations,
ed6814f7 1040 // which are defined via explicit instantiations elsewhere.
a32e3c09 1041 // NB: This syntax is a GNU extension.
3d7c150e 1042#if _GLIBCXX_EXTERN_TEMPLATE
a32e3c09
BK
1043 extern template class basic_istream<char>;
1044 extern template istream& ws(istream&);
1045 extern template istream& operator>>(istream&, char&);
1046 extern template istream& operator>>(istream&, char*);
1047 extern template istream& operator>>(istream&, unsigned char&);
1048 extern template istream& operator>>(istream&, signed char&);
1049 extern template istream& operator>>(istream&, unsigned char*);
1050 extern template istream& operator>>(istream&, signed char*);
1051
f12409e6
PC
1052 extern template istream& istream::_M_extract(unsigned short&);
1053 extern template istream& istream::_M_extract(unsigned int&);
1054 extern template istream& istream::_M_extract(long&);
1055 extern template istream& istream::_M_extract(unsigned long&);
1056 extern template istream& istream::_M_extract(bool&);
1057#ifdef _GLIBCXX_USE_LONG_LONG
1058 extern template istream& istream::_M_extract(long long&);
1059 extern template istream& istream::_M_extract(unsigned long long&);
1060#endif
1061 extern template istream& istream::_M_extract(float&);
1062 extern template istream& istream::_M_extract(double&);
1063 extern template istream& istream::_M_extract(long double&);
1064 extern template istream& istream::_M_extract(void*&);
1065
98599ff8
BK
1066 extern template class basic_iostream<char>;
1067
3d7c150e 1068#ifdef _GLIBCXX_USE_WCHAR_T
a32e3c09
BK
1069 extern template class basic_istream<wchar_t>;
1070 extern template wistream& ws(wistream&);
1071 extern template wistream& operator>>(wistream&, wchar_t&);
1072 extern template wistream& operator>>(wistream&, wchar_t*);
98599ff8 1073
f12409e6
PC
1074 extern template wistream& wistream::_M_extract(unsigned short&);
1075 extern template wistream& wistream::_M_extract(unsigned int&);
1076 extern template wistream& wistream::_M_extract(long&);
1077 extern template wistream& wistream::_M_extract(unsigned long&);
1078 extern template wistream& wistream::_M_extract(bool&);
1079#ifdef _GLIBCXX_USE_LONG_LONG
1080 extern template wistream& wistream::_M_extract(long long&);
1081 extern template wistream& wistream::_M_extract(unsigned long long&);
1082#endif
1083 extern template wistream& wistream::_M_extract(float&);
1084 extern template wistream& wistream::_M_extract(double&);
1085 extern template wistream& wistream::_M_extract(long double&);
1086 extern template wistream& wistream::_M_extract(void*&);
1087
98599ff8 1088 extern template class basic_iostream<wchar_t>;
3522a91b 1089#endif
1bc8b0ad 1090#endif
3cbc7af0 1091
12ffa228
BK
1092_GLIBCXX_END_NAMESPACE_VERSION
1093} // namespace
3d7c150e
BK
1094
1095#endif