libstdc++: istream.tcc Source File

libstdc++

istream.tcc
Go to the documentation of this file.
1// istream classes -*- C++ -*-
2
3// Copyright (C) 1997-2026 Free Software Foundation, Inc.
4//
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 3, or (at your option)
9// any later version.
10
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.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/istream.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{istream}
28 */
29
30//
31// ISO C++ 14882: 27.6.1 Input streams
32//
33
34#ifndef _ISTREAM_TCC
35#define _ISTREAM_TCC 1
36
37#ifdef _GLIBCXX_SYSHDR
38#pragma GCC system_header
39#endif
40
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
43
44#include <bits/cxxabi_forced.h>
45
46namespace std _GLIBCXX_VISIBILITY(default)
47{
48_GLIBCXX_BEGIN_NAMESPACE_VERSION
49
50 template<typename _CharT, typename _Traits>
51 basic_istream<_CharT, _Traits>::sentry::
52 sentry(basic_istream<_CharT, _Traits>& __in, bool __noskip) : _M_ok(false)
53 {
54 ios_base::iostate __err = ios_base::goodbit;
55 if (__in.good())
56 {
57 __try
58 {
59 if (__in.tie())
60 __in.tie()->flush();
61 if (!__noskip && bool(__in.flags() & ios_base::skipws))
62 {
63 const __int_type __eof = traits_type::eof();
64 __streambuf_type* __sb = __in.rdbuf();
65 __int_type __c = __sb->sgetc();
66
67 const __ctype_type& __ct = __check_facet(__in._M_ctype);
68 while (!traits_type::eq_int_type(__c, __eof)
69 && __ct.is(ctype_base::space,
70 traits_type::to_char_type(__c)))
71 __c = __sb->snextc();
72
73 // _GLIBCXX_RESOLVE_LIB_DEFECTS
74 // 195. Should basic_istream::sentry's constructor ever
75 // set eofbit?
76 if (traits_type::eq_int_type(__c, __eof))
77 __err |= ios_base::eofbit;
78 }
79 }
80 __catch(__cxxabiv1::__forced_unwind&)
81 {
82 __in._M_setstate(ios_base::badbit);
83 __throw_exception_again;
84 }
85 __catch(...)
86 { __in._M_setstate(ios_base::badbit); }
87 }
88
89 if (__in.good() && __err == ios_base::goodbit)
90 _M_ok = true;
91 else
92 {
93 __err |= ios_base::failbit;
94 __in.setstate(__err);
95 }
96 }
97
98 template<typename _CharT, typename _Traits>
99 template<typename _ValueT>
100 basic_istream<_CharT, _Traits>&
101 basic_istream<_CharT, _Traits>::
102 _M_extract(_ValueT& __v)
103 {
104 sentry __cerb(*this, false);
105 if (__cerb)
106 {
107 ios_base::iostate __err = ios_base::goodbit;
108 __try
109 {
110#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
111 const __num_get_type& __ng = __check_facet(this->_M_num_get);
112#else
113 const __num_get_type& __ng
114 = use_facet<__num_get_type>(this->_M_ios_locale);
115#endif
116 __ng.get(*this, 0, *this, __err, __v);
117 }
118 __catch(__cxxabiv1::__forced_unwind&)
119 {
120 this->_M_setstate(ios_base::badbit);
121 __throw_exception_again;
122 }
123 __catch(...)
124 { this->_M_setstate(ios_base::badbit); }
125 if (__err)
126 this->setstate(__err);
127 }
128 return *this;
129 }
130
131 template<typename _CharT, typename _Traits>
132 basic_istream<_CharT, _Traits>&
133 basic_istream<_CharT, _Traits>::
134 operator>>(short& __n)
135 {
136 // _GLIBCXX_RESOLVE_LIB_DEFECTS
137 // 118. basic_istream uses nonexistent num_get member functions.
138 sentry __cerb(*this, false);
139 if (__cerb)
140 {
141 ios_base::iostate __err = ios_base::goodbit;
142 __try
143 {
144 long __l;
145#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
146 const __num_get_type& __ng = __check_facet(this->_M_num_get);
147#else
148 const __num_get_type& __ng
149 = use_facet<__num_get_type>(this->_M_ios_locale);
150#endif
151 __ng.get(*this, 0, *this, __err, __l);
152
153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
154 // 696. istream::operator>>(int&) broken.
155 if (__l < __gnu_cxx::__numeric_traits<short>::__min)
156 {
157 __err |= ios_base::failbit;
158 __n = __gnu_cxx::__numeric_traits<short>::__min;
159 }
160 else if (__l > __gnu_cxx::__numeric_traits<short>::__max)
161 {
162 __err |= ios_base::failbit;
163 __n = __gnu_cxx::__numeric_traits<short>::__max;
164 }
165 else
166 __n = short(__l);
167 }
168 __catch(__cxxabiv1::__forced_unwind&)
169 {
170 this->_M_setstate(ios_base::badbit);
171 __throw_exception_again;
172 }
173 __catch(...)
174 { this->_M_setstate(ios_base::badbit); }
175 if (__err)
176 this->setstate(__err);
177 }
178 return *this;
179 }
180
181 template<typename _CharT, typename _Traits>
182 basic_istream<_CharT, _Traits>&
183 basic_istream<_CharT, _Traits>::
184 operator>>(int& __n)
185 {
186 // _GLIBCXX_RESOLVE_LIB_DEFECTS
187 // 118. basic_istream uses nonexistent num_get member functions.
188 sentry __cerb(*this, false);
189 if (__cerb)
190 {
191 ios_base::iostate __err = ios_base::goodbit;
192 __try
193 {
194 long __l;
195#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
196 const __num_get_type& __ng = __check_facet(this->_M_num_get);
197#else
198 const __num_get_type& __ng
199 = use_facet<__num_get_type>(this->_M_ios_locale);
200#endif
201 __ng.get(*this, 0, *this, __err, __l);
202
203 // _GLIBCXX_RESOLVE_LIB_DEFECTS
204 // 696. istream::operator>>(int&) broken.
205 if (__l < __gnu_cxx::__numeric_traits<int>::__min)
206 {
207 __err |= ios_base::failbit;
208 __n = __gnu_cxx::__numeric_traits<int>::__min;
209 }
210 else if (__l > __gnu_cxx::__numeric_traits<int>::__max)
211 {
212 __err |= ios_base::failbit;
213 __n = __gnu_cxx::__numeric_traits<int>::__max;
214 }
215 else
216 __n = int(__l);
217 }
218 __catch(__cxxabiv1::__forced_unwind&)
219 {
220 this->_M_setstate(ios_base::badbit);
221 __throw_exception_again;
222 }
223 __catch(...)
224 { this->_M_setstate(ios_base::badbit); }
225 if (__err)
226 this->setstate(__err);
227 }
228 return *this;
229 }
230
231 template<typename _CharT, typename _Traits>
232 basic_istream<_CharT, _Traits>&
233 basic_istream<_CharT, _Traits>::
234 operator>>(__streambuf_type* __sbout)
235 {
236 ios_base::iostate __err = ios_base::goodbit;
237 sentry __cerb(*this, false);
238 if (__cerb && __sbout)
239 {
240 __try
241 {
242 bool __ineof;
243 if (!__copy_streambufs_eof(this->rdbuf(), __sbout, __ineof))
244 __err |= ios_base::failbit;
245 if (__ineof)
246 __err |= ios_base::eofbit;
247 }
248 __catch(__cxxabiv1::__forced_unwind&)
249 {
250 this->_M_setstate(ios_base::failbit);
251 __throw_exception_again;
252 }
253 __catch(...)
254 { this->_M_setstate(ios_base::failbit); }
255 }
256 else if (!__sbout)
257 __err |= ios_base::failbit;
258 if (__err)
259 this->setstate(__err);
260 return *this;
261 }
262
263 template<typename _CharT, typename _Traits>
264 typename basic_istream<_CharT, _Traits>::int_type
265 basic_istream<_CharT, _Traits>::
266 get(void)
267 {
268 const int_type __eof = traits_type::eof();
269 int_type __c = __eof;
270 _M_gcount = 0;
271 ios_base::iostate __err = ios_base::goodbit;
272 sentry __cerb(*this, true);
273 if (__cerb)
274 {
275 __try
276 {
277 __c = this->rdbuf()->sbumpc();
278 // 27.6.1.1 paragraph 3
279 if (!traits_type::eq_int_type(__c, __eof))
280 _M_gcount = 1;
281 else
282 __err |= ios_base::eofbit;
283 }
284 __catch(__cxxabiv1::__forced_unwind&)
285 {
286 this->_M_setstate(ios_base::badbit);
287 __throw_exception_again;
288 }
289 __catch(...)
290 { this->_M_setstate(ios_base::badbit); }
291 }
292 if (!_M_gcount)
293 __err |= ios_base::failbit;
294 if (__err)
295 this->setstate(__err);
296 return __c;
297 }
298
299 template<typename _CharT, typename _Traits>
300 basic_istream<_CharT, _Traits>&
301 basic_istream<_CharT, _Traits>::
302 get(char_type& __c)
303 {
304 _M_gcount = 0;
305 ios_base::iostate __err = ios_base::goodbit;
306 sentry __cerb(*this, true);
307 if (__cerb)
308 {
309 __try
310 {
311 const int_type __cb = this->rdbuf()->sbumpc();
312 // 27.6.1.1 paragraph 3
313 if (!traits_type::eq_int_type(__cb, traits_type::eof()))
314 {
315 _M_gcount = 1;
316 __c = traits_type::to_char_type(__cb);
317 }
318 else
319 __err |= ios_base::eofbit;
320 }
321 __catch(__cxxabiv1::__forced_unwind&)
322 {
323 this->_M_setstate(ios_base::badbit);
324 __throw_exception_again;
325 }
326 __catch(...)
327 { this->_M_setstate(ios_base::badbit); }
328 }
329 if (!_M_gcount)
330 __err |= ios_base::failbit;
331 if (__err)
332 this->setstate(__err);
333 return *this;
334 }
335
336 template<typename _CharT, typename _Traits>
337 basic_istream<_CharT, _Traits>&
338 basic_istream<_CharT, _Traits>::
339 get(char_type* __s, streamsize __n, char_type __delim)
340 {
341 _M_gcount = 0;
342 ios_base::iostate __err = ios_base::goodbit;
343 sentry __cerb(*this, true);
344 if (__cerb)
345 {
346 __try
347 {
348 const int_type __idelim = traits_type::to_int_type(__delim);
349 const int_type __eof = traits_type::eof();
350 __streambuf_type* __sb = this->rdbuf();
351 int_type __c = __sb->sgetc();
352
353 while (_M_gcount + 1 < __n
354 && !traits_type::eq_int_type(__c, __eof)
355 && !traits_type::eq_int_type(__c, __idelim))
356 {
357 *__s++ = traits_type::to_char_type(__c);
358 ++_M_gcount;
359 __c = __sb->snextc();
360 }
361 if (traits_type::eq_int_type(__c, __eof))
362 __err |= ios_base::eofbit;
363 }
364 __catch(__cxxabiv1::__forced_unwind&)
365 {
366 this->_M_setstate(ios_base::badbit);
367 __throw_exception_again;
368 }
369 __catch(...)
370 { this->_M_setstate(ios_base::badbit); }
371 }
372 // _GLIBCXX_RESOLVE_LIB_DEFECTS
373 // 243. get and getline when sentry reports failure.
374 if (__n > 0)
375 *__s = char_type();
376 if (!_M_gcount)
377 __err |= ios_base::failbit;
378 if (__err)
379 this->setstate(__err);
380 return *this;
381 }
382
383 template<typename _CharT, typename _Traits>
384 basic_istream<_CharT, _Traits>&
385 basic_istream<_CharT, _Traits>::
386 get(__streambuf_type& __sb, char_type __delim)
387 {
388 _M_gcount = 0;
389 ios_base::iostate __err = ios_base::goodbit;
390 sentry __cerb(*this, true);
391 if (__cerb)
392 {
393 __try
394 {
395 const int_type __idelim = traits_type::to_int_type(__delim);
396 const int_type __eof = traits_type::eof();
397 __streambuf_type* __this_sb = this->rdbuf();
398 int_type __c = __this_sb->sgetc();
399 char_type __c2 = traits_type::to_char_type(__c);
400#pragma GCC diagnostic push
401#pragma GCC diagnostic ignored "-Wlong-long"
402 unsigned long long __gcount = 0;
403#pragma GCC diagnostic pop
404
405 while (!traits_type::eq_int_type(__c, __eof)
406 && !traits_type::eq_int_type(__c, __idelim)
407 && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
408 {
409 ++__gcount;
410 __c = __this_sb->snextc();
411 __c2 = traits_type::to_char_type(__c);
412 }
413 if (traits_type::eq_int_type(__c, __eof))
414 __err |= ios_base::eofbit;
415 // _GLIBCXX_RESOLVE_LIB_DEFECTS
416 // 3464. istream::gcount() can overflow
417 if (__gcount <= __gnu_cxx::__numeric_traits<streamsize>::__max)
418 _M_gcount = __gcount;
419 else
420 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
421 }
422 __catch(__cxxabiv1::__forced_unwind&)
423 {
424 this->_M_setstate(ios_base::badbit);
425 __throw_exception_again;
426 }
427 __catch(...)
428 { this->_M_setstate(ios_base::badbit); }
429 }
430 if (!_M_gcount)
431 __err |= ios_base::failbit;
432 if (__err)
433 this->setstate(__err);
434 return *this;
435 }
436
437 template<typename _CharT, typename _Traits>
438 basic_istream<_CharT, _Traits>&
439 basic_istream<_CharT, _Traits>::
440 getline(char_type* __s, streamsize __n, char_type __delim)
441 {
442 _M_gcount = 0;
443 ios_base::iostate __err = ios_base::goodbit;
444 sentry __cerb(*this, true);
445 if (__cerb)
446 {
447 __try
448 {
449 const int_type __idelim = traits_type::to_int_type(__delim);
450 const int_type __eof = traits_type::eof();
451 __streambuf_type* __sb = this->rdbuf();
452 int_type __c = __sb->sgetc();
453
454 while (_M_gcount + 1 < __n
455 && !traits_type::eq_int_type(__c, __eof)
456 && !traits_type::eq_int_type(__c, __idelim))
457 {
458 *__s++ = traits_type::to_char_type(__c);
459 __c = __sb->snextc();
460 ++_M_gcount;
461 }
462 if (traits_type::eq_int_type(__c, __eof))
463 __err |= ios_base::eofbit;
464 else
465 {
466 if (traits_type::eq_int_type(__c, __idelim))
467 {
468 __sb->sbumpc();
469 ++_M_gcount;
470 }
471 else
472 __err |= ios_base::failbit;
473 }
474 }
475 __catch(__cxxabiv1::__forced_unwind&)
476 {
477 this->_M_setstate(ios_base::badbit);
478 __throw_exception_again;
479 }
480 __catch(...)
481 { this->_M_setstate(ios_base::badbit); }
482 }
483 // _GLIBCXX_RESOLVE_LIB_DEFECTS
484 // 243. get and getline when sentry reports failure.
485 if (__n > 0)
486 *__s = char_type();
487 if (!_M_gcount)
488 __err |= ios_base::failbit;
489 if (__err)
490 this->setstate(__err);
491 return *this;
492 }
493
494 // We provide three overloads, since the first two are much simpler
495 // than the general case. Also, the latter two can thus adopt the
496 // same "batchy" strategy used by getline above.
497 template<typename _CharT, typename _Traits>
498 basic_istream<_CharT, _Traits>&
499 basic_istream<_CharT, _Traits>::
500 ignore(void)
501 {
502 _M_gcount = 0;
503 sentry __cerb(*this, true);
504 if (__cerb)
505 {
506 ios_base::iostate __err = ios_base::goodbit;
507 __try
508 {
509 const int_type __eof = traits_type::eof();
510 __streambuf_type* __sb = this->rdbuf();
511
512 if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
513 __err |= ios_base::eofbit;
514 else
515 _M_gcount = 1;
516 }
517 __catch(__cxxabiv1::__forced_unwind&)
518 {
519 this->_M_setstate(ios_base::badbit);
520 __throw_exception_again;
521 }
522 __catch(...)
523 { this->_M_setstate(ios_base::badbit); }
524 if (__err)
525 this->setstate(__err);
526 }
527 return *this;
528 }
529
530 template<typename _CharT, typename _Traits>
531 basic_istream<_CharT, _Traits>&
532 basic_istream<_CharT, _Traits>::
533 ignore(streamsize __n)
534 {
535 _M_gcount = 0;
536 sentry __cerb(*this, true);
537 if (__cerb && __n > 0)
538 {
539 ios_base::iostate __err = ios_base::goodbit;
540 __try
541 {
542 const int_type __eof = traits_type::eof();
543 __streambuf_type* __sb = this->rdbuf();
544 int_type __c = __sb->sgetc();
545
546 // N.B. On LFS-enabled platforms streamsize is still 32 bits
547 // wide: if we want to implement the standard mandated behavior
548 // for n == max() (see 27.6.1.3/24) we are at risk of signed
549 // integer overflow: thus these contortions. Also note that,
550 // by definition, when more than 2G chars are actually ignored,
551 // _M_gcount (the return value of gcount, that is) cannot be
552 // really correct, being unavoidably too small.
553 bool __large_ignore = false;
554 while (true)
555 {
556 while (_M_gcount < __n
557 && !traits_type::eq_int_type(__c, __eof))
558 {
559 ++_M_gcount;
560 __c = __sb->snextc();
561 }
562 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
563 && !traits_type::eq_int_type(__c, __eof))
564 {
565 _M_gcount =
566 __gnu_cxx::__numeric_traits<streamsize>::__min;
567 __large_ignore = true;
568 }
569 else
570 break;
571 }
572
573 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max)
574 {
575 if (__large_ignore)
576 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
577
578 if (traits_type::eq_int_type(__c, __eof))
579 __err |= ios_base::eofbit;
580 }
581 else if (_M_gcount < __n)
582 {
583 if (traits_type::eq_int_type(__c, __eof))
584 __err |= ios_base::eofbit;
585 }
586 }
587 __catch(__cxxabiv1::__forced_unwind&)
588 {
589 this->_M_setstate(ios_base::badbit);
590 __throw_exception_again;
591 }
592 __catch(...)
593 { this->_M_setstate(ios_base::badbit); }
594 if (__err)
595 this->setstate(__err);
596 }
597 return *this;
598 }
599
600 template<typename _CharT, typename _Traits>
601 basic_istream<_CharT, _Traits>&
602 basic_istream<_CharT, _Traits>::
603 ignore(streamsize __n, int_type __delim)
604 {
605 _M_gcount = 0;
606 sentry __cerb(*this, true);
607 if (__cerb && __n > 0)
608 {
609 ios_base::iostate __err = ios_base::goodbit;
610 __try
611 {
612 const int_type __eof = traits_type::eof();
613 __streambuf_type* __sb = this->rdbuf();
614 int_type __c = __sb->sgetc();
615
616 // See comment above.
617 bool __large_ignore = false;
618 while (true)
619 {
620 while (_M_gcount < __n
621 && !traits_type::eq_int_type(__c, __eof)
622 && !traits_type::eq_int_type(__c, __delim))
623 {
624 ++_M_gcount;
625 __c = __sb->snextc();
626 }
627 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
628 && !traits_type::eq_int_type(__c, __eof)
629 && !traits_type::eq_int_type(__c, __delim))
630 {
631 _M_gcount =
632 __gnu_cxx::__numeric_traits<streamsize>::__min;
633 __large_ignore = true;
634 }
635 else
636 break;
637 }
638
639 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max)
640 {
641 if (__large_ignore)
642 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
643
644 if (traits_type::eq_int_type(__c, __eof))
645 __err |= ios_base::eofbit;
646 else
647 {
648 if (_M_gcount != __n)
649 ++_M_gcount;
650 __sb->sbumpc();
651 }
652 }
653 else if (_M_gcount < __n) // implies __c == __delim or EOF
654 {
655 if (traits_type::eq_int_type(__c, __eof))
656 __err |= ios_base::eofbit;
657 else
658 {
659 ++_M_gcount;
660 __sb->sbumpc();
661 }
662 }
663 }
664 __catch(__cxxabiv1::__forced_unwind&)
665 {
666 this->_M_setstate(ios_base::badbit);
667 __throw_exception_again;
668 }
669 __catch(...)
670 { this->_M_setstate(ios_base::badbit); }
671 if (__err)
672 this->setstate(__err);
673 }
674 return *this;
675 }
676
677 template<typename _CharT, typename _Traits>
678 typename basic_istream<_CharT, _Traits>::int_type
679 basic_istream<_CharT, _Traits>::
680 peek(void)
681 {
682 int_type __c = traits_type::eof();
683 _M_gcount = 0;
684 sentry __cerb(*this, true);
685 if (__cerb)
686 {
687 ios_base::iostate __err = ios_base::goodbit;
688 __try
689 {
690 __c = this->rdbuf()->sgetc();
691 if (traits_type::eq_int_type(__c, traits_type::eof()))
692 __err |= ios_base::eofbit;
693 }
694 __catch(__cxxabiv1::__forced_unwind&)
695 {
696 this->_M_setstate(ios_base::badbit);
697 __throw_exception_again;
698 }
699 __catch(...)
700 { this->_M_setstate(ios_base::badbit); }
701 if (__err)
702 this->setstate(__err);
703 }
704 return __c;
705 }
706
707 template<typename _CharT, typename _Traits>
708 basic_istream<_CharT, _Traits>&
709 basic_istream<_CharT, _Traits>::
710 read(char_type* __s, streamsize __n)
711 {
712 _M_gcount = 0;
713 sentry __cerb(*this, true);
714 if (__cerb)
715 {
716 ios_base::iostate __err = ios_base::goodbit;
717 __try
718 {
719 _M_gcount = this->rdbuf()->sgetn(__s, __n);
720 if (_M_gcount != __n)
721 __err |= (ios_base::eofbit | ios_base::failbit);
722 }
723 __catch(__cxxabiv1::__forced_unwind&)
724 {
725 this->_M_setstate(ios_base::badbit);
726 __throw_exception_again;
727 }
728 __catch(...)
729 { this->_M_setstate(ios_base::badbit); }
730 if (__err)
731 this->setstate(__err);
732 }
733 return *this;
734 }
735
736 template<typename _CharT, typename _Traits>
737 streamsize
738 basic_istream<_CharT, _Traits>::
739 readsome(char_type* __s, streamsize __n)
740 {
741 _M_gcount = 0;
742 sentry __cerb(*this, true);
743 if (__cerb)
744 {
745 ios_base::iostate __err = ios_base::goodbit;
746 __try
747 {
748 // Cannot compare int_type with streamsize generically.
749 const streamsize __num = this->rdbuf()->in_avail();
750 if (__num > 0)
751 _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
752 else if (__num == -1)
753 __err |= ios_base::eofbit;
754 }
755 __catch(__cxxabiv1::__forced_unwind&)
756 {
757 this->_M_setstate(ios_base::badbit);
758 __throw_exception_again;
759 }
760 __catch(...)
761 { this->_M_setstate(ios_base::badbit); }
762 if (__err)
763 this->setstate(__err);
764 }
765 return _M_gcount;
766 }
767
768 template<typename _CharT, typename _Traits>
769 basic_istream<_CharT, _Traits>&
770 basic_istream<_CharT, _Traits>::
771 putback(char_type __c)
772 {
773 // _GLIBCXX_RESOLVE_LIB_DEFECTS
774 // 60. What is a formatted input function?
775 _M_gcount = 0;
776 // Clear eofbit per N3168.
777 this->clear(this->rdstate() & ~ios_base::eofbit);
778 sentry __cerb(*this, true);
779 if (__cerb)
780 {
781 ios_base::iostate __err = ios_base::goodbit;
782 __try
783 {
784 const int_type __eof = traits_type::eof();
785 __streambuf_type* __sb = this->rdbuf();
786 if (!__sb
787 || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
788 __err |= ios_base::badbit;
789 }
790 __catch(__cxxabiv1::__forced_unwind&)
791 {
792 this->_M_setstate(ios_base::badbit);
793 __throw_exception_again;
794 }
795 __catch(...)
796 { this->_M_setstate(ios_base::badbit); }
797 if (__err)
798 this->setstate(__err);
799 }
800 return *this;
801 }
802
803 template<typename _CharT, typename _Traits>
804 basic_istream<_CharT, _Traits>&
805 basic_istream<_CharT, _Traits>::
806 unget(void)
807 {
808 // _GLIBCXX_RESOLVE_LIB_DEFECTS
809 // 60. What is a formatted input function?
810 _M_gcount = 0;
811 // Clear eofbit per N3168.
812 this->clear(this->rdstate() & ~ios_base::eofbit);
813 sentry __cerb(*this, true);
814 if (__cerb)
815 {
816 ios_base::iostate __err = ios_base::goodbit;
817 __try
818 {
819 const int_type __eof = traits_type::eof();
820 __streambuf_type* __sb = this->rdbuf();
821 if (!__sb
822 || traits_type::eq_int_type(__sb->sungetc(), __eof))
823 __err |= ios_base::badbit;
824 }
825 __catch(__cxxabiv1::__forced_unwind&)
826 {
827 this->_M_setstate(ios_base::badbit);
828 __throw_exception_again;
829 }
830 __catch(...)
831 { this->_M_setstate(ios_base::badbit); }
832 if (__err)
833 this->setstate(__err);
834 }
835 return *this;
836 }
837
838 template<typename _CharT, typename _Traits>
839 int
840 basic_istream<_CharT, _Traits>::
841 sync(void)
842 {
843 // _GLIBCXX_RESOLVE_LIB_DEFECTS
844 // DR60. Do not change _M_gcount.
845 int __ret = -1;
846 sentry __cerb(*this, true);
847 if (__cerb)
848 {
849 ios_base::iostate __err = ios_base::goodbit;
850 __try
851 {
852 __streambuf_type* __sb = this->rdbuf();
853 if (__sb)
854 {
855 if (__sb->pubsync() == -1)
856 __err |= ios_base::badbit;
857 else
858 __ret = 0;
859 }
860 }
861 __catch(__cxxabiv1::__forced_unwind&)
862 {
863 this->_M_setstate(ios_base::badbit);
864 __throw_exception_again;
865 }
866 __catch(...)
867 { this->_M_setstate(ios_base::badbit); }
868 if (__err)
869 this->setstate(__err);
870 }
871 return __ret;
872 }
873
874 template<typename _CharT, typename _Traits>
875 typename basic_istream<_CharT, _Traits>::pos_type
876 basic_istream<_CharT, _Traits>::
877 tellg(void)
878 {
879 // _GLIBCXX_RESOLVE_LIB_DEFECTS
880 // DR60. Do not change _M_gcount.
881 pos_type __ret = pos_type(-1);
882 sentry __cerb(*this, true);
883 if (__cerb)
884 {
885 __try
886 {
887 if (!this->fail())
888 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur,
889 ios_base::in);
890 }
891 __catch(__cxxabiv1::__forced_unwind&)
892 {
893 this->_M_setstate(ios_base::badbit);
894 __throw_exception_again;
895 }
896 __catch(...)
897 { this->_M_setstate(ios_base::badbit); }
898 }
899 return __ret;
900 }
901
902 template<typename _CharT, typename _Traits>
903 basic_istream<_CharT, _Traits>&
904 basic_istream<_CharT, _Traits>::
905 seekg(pos_type __pos)
906 {
907 // _GLIBCXX_RESOLVE_LIB_DEFECTS
908 // DR60. Do not change _M_gcount.
909 // Clear eofbit per N3168.
910 this->clear(this->rdstate() & ~ios_base::eofbit);
911 sentry __cerb(*this, true);
912 if (__cerb)
913 {
914 ios_base::iostate __err = ios_base::goodbit;
915 __try
916 {
917 if (!this->fail())
918 {
919 // 136. seekp, seekg setting wrong streams?
920 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
921 ios_base::in);
922
923 // 129. Need error indication from seekp() and seekg()
924 if (__p == pos_type(off_type(-1)))
925 __err |= ios_base::failbit;
926 }
927 }
928 __catch(__cxxabiv1::__forced_unwind&)
929 {
930 this->_M_setstate(ios_base::badbit);
931 __throw_exception_again;
932 }
933 __catch(...)
934 { this->_M_setstate(ios_base::badbit); }
935 if (__err)
936 this->setstate(__err);
937 }
938 return *this;
939 }
940
941 template<typename _CharT, typename _Traits>
942 basic_istream<_CharT, _Traits>&
943 basic_istream<_CharT, _Traits>::
944 seekg(off_type __off, ios_base::seekdir __dir)
945 {
946 // _GLIBCXX_RESOLVE_LIB_DEFECTS
947 // DR60. Do not change _M_gcount.
948 // Clear eofbit per N3168.
949 this->clear(this->rdstate() & ~ios_base::eofbit);
950 sentry __cerb(*this, true);
951 if (__cerb)
952 {
953 ios_base::iostate __err = ios_base::goodbit;
954 __try
955 {
956 if (!this->fail())
957 {
958 // 136. seekp, seekg setting wrong streams?
959 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
960 ios_base::in);
961
962 // 129. Need error indication from seekp() and seekg()
963 if (__p == pos_type(off_type(-1)))
964 __err |= ios_base::failbit;
965 }
966 }
967 __catch(__cxxabiv1::__forced_unwind&)
968 {
969 this->_M_setstate(ios_base::badbit);
970 __throw_exception_again;
971 }
972 __catch(...)
973 { this->_M_setstate(ios_base::badbit); }
974 if (__err)
975 this->setstate(__err);
976 }
977 return *this;
978 }
979
980 // 27.6.1.2.3 Character extraction templates
981 template<typename _CharT, typename _Traits>
982 basic_istream<_CharT, _Traits>&
983 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
984 {
985 typedef basic_istream<_CharT, _Traits> __istream_type;
986 typedef typename __istream_type::int_type __int_type;
987
988 typename __istream_type::sentry __cerb(__in, false);
989 if (__cerb)
990 {
991 ios_base::iostate __err = ios_base::goodbit;
992 __try
993 {
994 const __int_type __cb = __in.rdbuf()->sbumpc();
995 if (!_Traits::eq_int_type(__cb, _Traits::eof()))
996 __c = _Traits::to_char_type(__cb);
997 else
998 __err |= (ios_base::eofbit | ios_base::failbit);
999 }
1000 __catch(__cxxabiv1::__forced_unwind&)
1001 {
1002 __in._M_setstate(ios_base::badbit);
1003 __throw_exception_again;
1004 }
1005 __catch(...)
1006 { __in._M_setstate(ios_base::badbit); }
1007 if (__err)
1008 __in.setstate(__err);
1009 }
1010 return __in;
1011 }
1012
1013 template<typename _CharT, typename _Traits>
1014 void
1015 __istream_extract(basic_istream<_CharT, _Traits>& __in, _CharT* __s,
1016 streamsize __num)
1017 {
1018 typedef basic_istream<_CharT, _Traits> __istream_type;
1019 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
1020 typedef typename _Traits::int_type int_type;
1021 typedef _CharT char_type;
1022 typedef ctype<_CharT> __ctype_type;
1023
1024 streamsize __extracted = 0;
1025 ios_base::iostate __err = ios_base::goodbit;
1026 typename __istream_type::sentry __cerb(__in, false);
1027 if (__cerb)
1028 {
1029 __try
1030 {
1031 // Figure out how many characters to extract.
1032 streamsize __width = __in.width();
1033 if (0 < __width && __width < __num)
1034 __num = __width;
1035
1036 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1037
1038 const int_type __eof = _Traits::eof();
1039 __streambuf_type* __sb = __in.rdbuf();
1040 int_type __c = __sb->sgetc();
1041
1042 while (__extracted < __num - 1
1043 && !_Traits::eq_int_type(__c, __eof)
1044 && !__ct.is(ctype_base::space,
1045 _Traits::to_char_type(__c)))
1046 {
1047 *__s++ = _Traits::to_char_type(__c);
1048 ++__extracted;
1049 __c = __sb->snextc();
1050 }
1051
1052 if (__extracted < __num - 1
1053 && _Traits::eq_int_type(__c, __eof))
1054 __err |= ios_base::eofbit;
1055
1056 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1057 // 68. Extractors for char* should store null at end
1058 *__s = char_type();
1059 __in.width(0);
1060 }
1061 __catch(__cxxabiv1::__forced_unwind&)
1062 {
1063 __in._M_setstate(ios_base::badbit);
1064 __throw_exception_again;
1065 }
1066 __catch(...)
1067 { __in._M_setstate(ios_base::badbit); }
1068 }
1069 if (!__extracted)
1070 __err |= ios_base::failbit;
1071 if (__err)
1072 __in.setstate(__err);
1073 }
1074
1075 // 27.6.1.4 Standard basic_istream manipulators
1076 template<typename _CharT, typename _Traits>
1077 basic_istream<_CharT, _Traits>&
1078 ws(basic_istream<_CharT, _Traits>& __in)
1079 {
1080 typedef basic_istream<_CharT, _Traits> __istream_type;
1081 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
1082 typedef typename __istream_type::int_type __int_type;
1083 typedef ctype<_CharT> __ctype_type;
1084
1085 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1086 // 451. behavior of std::ws
1087 typename __istream_type::sentry __cerb(__in, true);
1088 if (__cerb)
1089 {
1090 ios_base::iostate __err = ios_base::goodbit;
1091 __try
1092 {
1093 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
1094 const __int_type __eof = _Traits::eof();
1095 __streambuf_type* __sb = __in.rdbuf();
1096 __int_type __c = __sb->sgetc();
1097
1098 while (true)
1099 {
1100 if (_Traits::eq_int_type(__c, __eof))
1101 {
1102 __err = ios_base::eofbit;
1103 break;
1104 }
1105 if (!__ct.is(ctype_base::space, _Traits::to_char_type(__c)))
1106 break;
1107 __c = __sb->snextc();
1108 }
1109 }
1110 __catch (const __cxxabiv1::__forced_unwind&)
1111 {
1112 __in._M_setstate(ios_base::badbit);
1113 __throw_exception_again;
1114 }
1115 __catch (...)
1116 {
1117 __in._M_setstate(ios_base::badbit);
1118 }
1119 if (__err)
1120 __in.setstate(__err);
1121 }
1122 return __in;
1123 }
1124
1125 // Inhibit implicit instantiations for required instantiations,
1126 // which are defined via explicit instantiations elsewhere.
1127#if _GLIBCXX_EXTERN_TEMPLATE
1128#pragma GCC diagnostic push
1129#pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
1130#pragma GCC diagnostic ignored "-Wlong-long"
1131 extern template class basic_istream<char>;
1132 extern template istream& ws(istream&);
1133 extern template istream& operator>>(istream&, char&);
1134 extern template istream& operator>>(istream&, unsigned char&);
1135 extern template istream& operator>>(istream&, signed char&);
1136
1137 extern template istream& istream::_M_extract(unsigned short&);
1138 extern template istream& istream::_M_extract(unsigned int&);
1139 extern template istream& istream::_M_extract(long&);
1140 extern template istream& istream::_M_extract(unsigned long&);
1141 extern template istream& istream::_M_extract(bool&);
1142#ifdef _GLIBCXX_USE_LONG_LONG
1143#pragma GCC diagnostic push
1144#pragma GCC diagnostic ignored "-Wlong-long"
1145 extern template istream& istream::_M_extract(long long&);
1146 extern template istream& istream::_M_extract(unsigned long long&);
1147#pragma GCC diagnostic pop
1148#endif
1149 extern template istream& istream::_M_extract(float&);
1150 extern template istream& istream::_M_extract(double&);
1151 extern template istream& istream::_M_extract(long double&);
1152 extern template istream& istream::_M_extract(void*&);
1153
1154 extern template class basic_iostream<char>;
1155
1156#ifdef _GLIBCXX_USE_WCHAR_T
1157 extern template class basic_istream<wchar_t>;
1158 extern template wistream& ws(wistream&);
1159 extern template wistream& operator>>(wistream&, wchar_t&);
1160 extern template void __istream_extract(wistream&, wchar_t*, streamsize);
1161
1162 extern template wistream& wistream::_M_extract(unsigned short&);
1163 extern template wistream& wistream::_M_extract(unsigned int&);
1164 extern template wistream& wistream::_M_extract(long&);
1165 extern template wistream& wistream::_M_extract(unsigned long&);
1166 extern template wistream& wistream::_M_extract(bool&);
1167#ifdef _GLIBCXX_USE_LONG_LONG
1168 extern template wistream& wistream::_M_extract(long long&);
1169 extern template wistream& wistream::_M_extract(unsigned long long&);
1170#endif
1171 extern template wistream& wistream::_M_extract(float&);
1172 extern template wistream& wistream::_M_extract(double&);
1173 extern template wistream& wistream::_M_extract(long double&);
1174 extern template wistream& wistream::_M_extract(void*&);
1175
1176 extern template class basic_iostream<wchar_t>;
1177#endif
1178#pragma GCC diagnostic pop
1179#endif
1180
1181_GLIBCXX_END_NAMESPACE_VERSION
1182} // namespace std
1183
1184#pragma GCC diagnostic pop
1185#endif
cxxabi_forced.h
std::istream
basic_istream< char > istream
Base class for char input streams.
Definition iosfwd:142
std::wistream
basic_istream< wchar_t > wistream
Base class for wchar_t input streams.
Definition iosfwd:182
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:232
std::use_facet
const _Facet & use_facet(const locale &__loc)
Return a facet.
Definition locale_classes.tcc:220
std
ISO C++ entities toplevel namespace is std.
std::streamsize
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
std::operator>>
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1658
std::ws
basic_istream< _CharT, _Traits > & ws(basic_istream< _CharT, _Traits > &__is)
Quick and easy way to eat whitespace.
Definition istream.tcc:1078
std::basic_ios::clear
void clear(iostate __state=goodbit)
[Re]sets the error state.
Definition basic_ios.tcc:46
std::basic_ios::setstate
void setstate(iostate __state)
Sets additional flags in the error state.
Definition basic_ios.h:167
std::basic_ios::rdstate
iostate rdstate() const
Returns the error state of the stream buffer.
Definition basic_ios.h:147
std::basic_ios::good
bool good() const
Fast error checking.
Definition basic_ios.h:191
std::basic_ios::fail
bool fail() const
Fast error checking.
Definition basic_ios.h:214
std::basic_ios::rdbuf
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Definition basic_ios.h:338
std::basic_streambuf
The actual work of input and output (interface).
Definition streambuf:127
std::basic_streambuf::snextc
int_type snextc()
Getting the next character.
Definition streambuf:307
std::basic_streambuf::sbumpc
int_type sbumpc()
Getting the next character.
Definition streambuf:325
std::basic_streambuf::sungetc
int_type sungetc()
Moving backwards in the input stream.
Definition streambuf:406
std::basic_streambuf::sgetc
int_type sgetc()
Getting the next character.
Definition streambuf:347
std::basic_streambuf::pubsync
int pubsync()
Calls virtual sync function.
Definition streambuf:280
std::basic_streambuf::sputbackc
int_type sputbackc(char_type __c)
Pushing characters back into the input stream.
Definition streambuf:381
std::basic_streambuf::sputc
int_type sputc(char_type __c)
Entry point for all single-character output functions.
Definition streambuf:433
std::basic_istream
Template class basic_istream.
Definition istream:67
std::basic_istream::seekg
__istream_type & seekg(pos_type)
Changing the current read position.
Definition istream.tcc:905
std::basic_istream::get
int_type get()
Simple extraction.
Definition istream.tcc:266
std::basic_istream::readsome
streamsize readsome(char_type *__s, streamsize __n)
Extraction until the buffer is exhausted, but no more.
Definition istream.tcc:739
std::basic_istream::peek
int_type peek()
Looking ahead in the stream.
Definition istream.tcc:680
std::basic_istream::unget
__istream_type & unget()
Unextracting the previous character.
Definition istream.tcc:806
std::basic_istream::tellg
pos_type tellg()
Getting the current read position.
Definition istream.tcc:877
std::basic_istream::_M_gcount
streamsize _M_gcount
Definition istream:90
std::basic_istream::ignore
__istream_type & ignore()
Simple extraction.
Definition istream.tcc:500
std::basic_istream::read
__istream_type & read(char_type *__s, streamsize __n)
Extraction without delimiters.
Definition istream.tcc:710
std::basic_istream::putback
__istream_type & putback(char_type __c)
Unextracting a single character.
Definition istream.tcc:771
std::basic_istream::basic_istream
basic_istream(__streambuf_type *__sb)
Base constructor.
Definition istream:101
std::basic_istream::getline
__istream_type & getline(char_type *__s, streamsize __n, char_type __delim)
String extraction.
Definition istream.tcc:440
std::basic_istream::operator>>
__istream_type & operator>>(__istream_type &(*__pf)(__istream_type &))
Interface for manipulators.
Definition istream:128
std::basic_istream::sync
int sync()
Synchronizing the stream buffer.
Definition istream.tcc:841
std::basic_istream::sentry
Performs setup work for input streams.
Definition istream:793
std::basic_istream::sentry::sentry
sentry(basic_istream< _CharT, _Traits > &__is, bool __noskipws=false)
The constructor performs all the work.
Definition istream.tcc:52
__cxxabiv1::__forced_unwind
Thrown as part of forced unwinding.
Definition cxxabi_forced.h:51
std::ios_base::iostate
_Ios_Iostate iostate
This is a bitmask type.
Definition ios_base.h:453
std::ios_base::cur
static const seekdir cur
Request a seek relative to the current position within the sequence.
Definition ios_base.h:529
std::ios_base::in
static const openmode in
Open for input. Default for ifstream and fstream.
Definition ios_base.h:498
std::ios_base::eofbit
static const iostate eofbit
Indicates that an input operation reached the end of an input sequence.
Definition ios_base.h:460
std::ios_base::goodbit
static const iostate goodbit
Indicates all is well.
Definition ios_base.h:468
std::ios_base::badbit
static const iostate badbit
Indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition ios_base.h:457
std::ios_base::seekdir
_Ios_Seekdir seekdir
This is an enumerated type.
Definition ios_base.h:523
std::ios_base::getloc
locale getloc() const
Locale access.
Definition ios_base.h:841
std::ios_base::failbit
static const iostate failbit
Indicates that an input operation failed to read the expected characters, or that an output operation...
Definition ios_base.h:465
std::ctype
Primary class template ctype facet.
Definition locale_facets.h:618
std::num_get::get
iter_type get(iter_type __in, iter_type __end, ios_base &__io, ios_base::iostate &__err, bool &__v) const
Numeric parsing.
Definition locale_facets.h:2009