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