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