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