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