e794fe33060cdbb22901c262680f450ed355c5bf
[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 if (_M_check_facet(_M_fnumget))
138 _M_fnumget->get(*this, 0, *this, __err, __n);
139 this->setstate(__err);
140 }
141 catch(exception& __fail)
142 {
143 // 27.6.1.2.1 Common requirements.
144 // Turn this on without causing an ios::failure to be thrown.
145 this->setstate(ios_base::badbit);
146 if ((this->exceptions() & ios_base::badbit) != 0)
147 __throw_exception_again;
148 }
149 }
150 return *this;
151 }
152
153 template<typename _CharT, typename _Traits>
154 basic_istream<_CharT, _Traits>&
155 basic_istream<_CharT, _Traits>::
156 operator>>(unsigned short& __n)
157 {
158 sentry __cerb(*this, false);
159 if (__cerb)
160 {
161 try
162 {
163 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
164 if (_M_check_facet(_M_fnumget))
165 _M_fnumget->get(*this, 0, *this, __err, __n);
166 this->setstate(__err);
167 }
168 catch(exception& __fail)
169 {
170 // 27.6.1.2.1 Common requirements.
171 // Turn this on without causing an ios::failure to be thrown.
172 this->setstate(ios_base::badbit);
173 if ((this->exceptions() & ios_base::badbit) != 0)
174 __throw_exception_again;
175 }
176 }
177 return *this;
178 }
179
180 template<typename _CharT, typename _Traits>
181 basic_istream<_CharT, _Traits>&
182 basic_istream<_CharT, _Traits>::
183 operator>>(int& __n)
184 {
185 sentry __cerb(*this, false);
186 if (__cerb)
187 {
188 try
189 {
190 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
191 if (_M_check_facet(_M_fnumget))
192 _M_fnumget->get(*this, 0, *this, __err, __n);
193 this->setstate(__err);
194 }
195 catch(exception& __fail)
196 {
197 // 27.6.1.2.1 Common requirements.
198 // Turn this on without causing an ios::failure to be thrown.
199 this->setstate(ios_base::badbit);
200 if ((this->exceptions() & ios_base::badbit) != 0)
201 __throw_exception_again;
202 }
203 }
204 return *this;
205 }
206
207 template<typename _CharT, typename _Traits>
208 basic_istream<_CharT, _Traits>&
209 basic_istream<_CharT, _Traits>::
210 operator>>(unsigned int& __n)
211 {
212 sentry __cerb(*this, false);
213 if (__cerb)
214 {
215 try
216 {
217 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
218 if (_M_check_facet(_M_fnumget))
219 _M_fnumget->get(*this, 0, *this, __err, __n);
220 this->setstate(__err);
221 }
222 catch(exception& __fail)
223 {
224 // 27.6.1.2.1 Common requirements.
225 // Turn this on without causing an ios::failure to be thrown.
226 this->setstate(ios_base::badbit);
227 if ((this->exceptions() & ios_base::badbit) != 0)
228 __throw_exception_again;
229 }
230 }
231 return *this;
232 }
233
234 template<typename _CharT, typename _Traits>
235 basic_istream<_CharT, _Traits>&
236 basic_istream<_CharT, _Traits>::
237 operator>>(long& __n)
238 {
239 sentry __cerb(*this, false);
240 if (__cerb)
241 {
242 try
243 {
244 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
245 if (_M_check_facet(_M_fnumget))
246 _M_fnumget->get(*this, 0, *this, __err, __n);
247 this->setstate(__err);
248 }
249 catch(exception& __fail)
250 {
251 // 27.6.1.2.1 Common requirements.
252 // Turn this on without causing an ios::failure to be thrown.
253 this->setstate(ios_base::badbit);
254 if ((this->exceptions() & ios_base::badbit) != 0)
255 __throw_exception_again;
256 }
257 }
258 return *this;
259 }
260
261 template<typename _CharT, typename _Traits>
262 basic_istream<_CharT, _Traits>&
263 basic_istream<_CharT, _Traits>::
264 operator>>(unsigned long& __n)
265 {
266 sentry __cerb(*this, false);
267 if (__cerb)
268 {
269 try
270 {
271 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
272 if (_M_check_facet(_M_fnumget))
273 _M_fnumget->get(*this, 0, *this, __err, __n);
274 this->setstate(__err);
275 }
276 catch(exception& __fail)
277 {
278 // 27.6.1.2.1 Common requirements.
279 // Turn this on without causing an ios::failure to be thrown.
280 this->setstate(ios_base::badbit);
281 if ((this->exceptions() & ios_base::badbit) != 0)
282 __throw_exception_again;
283 }
284 }
285 return *this;
286 }
287
288 #ifdef _GLIBCPP_USE_LONG_LONG
289 template<typename _CharT, typename _Traits>
290 basic_istream<_CharT, _Traits>&
291 basic_istream<_CharT, _Traits>::
292 operator>>(long long& __n)
293 {
294 sentry __cerb(*this, false);
295 if (__cerb)
296 {
297 try
298 {
299 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
300 if (_M_check_facet(_M_fnumget))
301 _M_fnumget->get(*this, 0, *this, __err, __n);
302 this->setstate(__err);
303 }
304 catch(exception& __fail)
305 {
306 // 27.6.1.2.1 Common requirements.
307 // Turn this on without causing an ios::failure to be thrown.
308 this->setstate(ios_base::badbit);
309 if ((this->exceptions() & ios_base::badbit) != 0)
310 __throw_exception_again;
311 }
312 }
313 return *this;
314 }
315
316 template<typename _CharT, typename _Traits>
317 basic_istream<_CharT, _Traits>&
318 basic_istream<_CharT, _Traits>::
319 operator>>(unsigned long long& __n)
320 {
321 sentry __cerb(*this, false);
322 if (__cerb)
323 {
324 try
325 {
326 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
327 if (_M_check_facet(_M_fnumget))
328 _M_fnumget->get(*this, 0, *this, __err, __n);
329 this->setstate(__err);
330 }
331 catch(exception& __fail)
332 {
333 // 27.6.1.2.1 Common requirements.
334 // Turn this on without causing an ios::failure to be thrown.
335 this->setstate(ios_base::badbit);
336 if ((this->exceptions() & ios_base::badbit) != 0)
337 __throw_exception_again;
338 }
339 }
340 return *this;
341 }
342 #endif
343
344 template<typename _CharT, typename _Traits>
345 basic_istream<_CharT, _Traits>&
346 basic_istream<_CharT, _Traits>::
347 operator>>(float& __n)
348 {
349 sentry __cerb(*this, false);
350 if (__cerb)
351 {
352 try
353 {
354 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
355 if (_M_check_facet(_M_fnumget))
356 _M_fnumget->get(*this, 0, *this, __err, __n);
357 this->setstate(__err);
358 }
359 catch(exception& __fail)
360 {
361 // 27.6.1.2.1 Common requirements.
362 // Turn this on without causing an ios::failure to be thrown.
363 this->setstate(ios_base::badbit);
364 if ((this->exceptions() & ios_base::badbit) != 0)
365 __throw_exception_again;
366 }
367 }
368 return *this;
369 }
370
371 template<typename _CharT, typename _Traits>
372 basic_istream<_CharT, _Traits>&
373 basic_istream<_CharT, _Traits>::
374 operator>>(double& __n)
375 {
376 sentry __cerb(*this, false);
377 if (__cerb)
378 {
379 try
380 {
381 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
382 if (_M_check_facet(_M_fnumget))
383 _M_fnumget->get(*this, 0, *this, __err, __n);
384 this->setstate(__err);
385 }
386 catch(exception& __fail)
387 {
388 // 27.6.1.2.1 Common requirements.
389 // Turn this on without causing an ios::failure to be thrown.
390 this->setstate(ios_base::badbit);
391 if ((this->exceptions() & ios_base::badbit) != 0)
392 __throw_exception_again;
393 }
394 }
395 return *this;
396 }
397
398 template<typename _CharT, typename _Traits>
399 basic_istream<_CharT, _Traits>&
400 basic_istream<_CharT, _Traits>::
401 operator>>(long double& __n)
402 {
403 sentry __cerb(*this, false);
404 if (__cerb)
405 {
406 try
407 {
408 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
409 if (_M_check_facet(_M_fnumget))
410 _M_fnumget->get(*this, 0, *this, __err, __n);
411 this->setstate(__err);
412 }
413 catch(exception& __fail)
414 {
415 // 27.6.1.2.1 Common requirements.
416 // Turn this on without causing an ios::failure to be thrown.
417 this->setstate(ios_base::badbit);
418 if ((this->exceptions() & ios_base::badbit) != 0)
419 __throw_exception_again;
420 }
421 }
422 return *this;
423 }
424
425 template<typename _CharT, typename _Traits>
426 basic_istream<_CharT, _Traits>&
427 basic_istream<_CharT, _Traits>::
428 operator>>(void*& __n)
429 {
430 sentry __cerb(*this, false);
431 if (__cerb)
432 {
433 try
434 {
435 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
436 if (_M_check_facet(_M_fnumget))
437 _M_fnumget->get(*this, 0, *this, __err, __n);
438 this->setstate(__err);
439 }
440 catch(exception& __fail)
441 {
442 // 27.6.1.2.1 Common requirements.
443 // Turn this on without causing an ios::failure to be thrown.
444 this->setstate(ios_base::badbit);
445 if ((this->exceptions() & ios_base::badbit) != 0)
446 __throw_exception_again;
447 }
448 }
449 return *this;
450 }
451
452 template<typename _CharT, typename _Traits>
453 basic_istream<_CharT, _Traits>&
454 basic_istream<_CharT, _Traits>::
455 operator>>(__streambuf_type* __sbout)
456 {
457 streamsize __xtrct = 0;
458 __streambuf_type* __sbin = this->rdbuf();
459 sentry __cerb(*this, false);
460 if (__sbout && __cerb)
461 __xtrct = __copy_streambufs(*this, __sbin, __sbout);
462 if (!__sbout || !__xtrct)
463 this->setstate(ios_base::failbit);
464 return *this;
465 }
466
467 template<typename _CharT, typename _Traits>
468 basic_istream<_CharT, _Traits>::int_type
469 basic_istream<_CharT, _Traits>::
470 get(void)
471 {
472 const int_type __eof = traits_type::eof();
473 int_type __c = __eof;
474 _M_gcount = 0;
475 sentry __cerb(*this, true);
476 if (__cerb)
477 {
478 try
479 {
480 __c = this->rdbuf()->sbumpc();
481 // 27.6.1.1 paragraph 3
482 if (__c != __eof)
483 _M_gcount = 1;
484 else
485 this->setstate(ios_base::eofbit | ios_base::failbit);
486 }
487 catch(exception& __fail)
488 {
489 // 27.6.1.3 paragraph 1
490 // Turn this on without causing an ios::failure to be thrown.
491 this->setstate(ios_base::badbit);
492 if ((this->exceptions() & ios_base::badbit) != 0)
493 __throw_exception_again;
494 }
495 }
496 return __c;
497 }
498
499 template<typename _CharT, typename _Traits>
500 basic_istream<_CharT, _Traits>&
501 basic_istream<_CharT, _Traits>::
502 get(char_type& __c)
503 {
504 _M_gcount = 0;
505 sentry __cerb(*this, true);
506 if (__cerb)
507 {
508 try
509 {
510 const int_type __eof = traits_type::eof();
511 int_type __bufval = this->rdbuf()->sbumpc();
512 // 27.6.1.1 paragraph 3
513 if (__bufval != __eof)
514 {
515 _M_gcount = 1;
516 __c = traits_type::to_char_type(__bufval);
517 }
518 else
519 this->setstate(ios_base::eofbit | ios_base::failbit);
520 }
521 catch(exception& __fail)
522 {
523 // 27.6.1.3 paragraph 1
524 // Turn this on without causing an ios::failure to be thrown.
525 this->setstate(ios_base::badbit);
526 if ((this->exceptions() & ios_base::badbit) != 0)
527 __throw_exception_again;
528 }
529 }
530 return *this;
531 }
532
533 template<typename _CharT, typename _Traits>
534 basic_istream<_CharT, _Traits>&
535 basic_istream<_CharT, _Traits>::
536 get(char_type* __s, streamsize __n, char_type __delim)
537 {
538 _M_gcount = 0;
539 sentry __cerb(*this, true);
540 if (__cerb && __n > 1)
541 {
542 try
543 {
544 const int_type __idelim = traits_type::to_int_type(__delim);
545 const int_type __eof = traits_type::eof();
546 __streambuf_type* __sb = this->rdbuf();
547 int_type __c = __sb->sbumpc();
548 bool __testdelim = __c == __idelim;
549 bool __testeof = __c == __eof;
550
551 while (_M_gcount < __n - 1 && !__testeof && !__testdelim)
552 {
553 *__s++ = traits_type::to_char_type(__c);
554 ++_M_gcount;
555 __c = __sb->sbumpc();
556 __testeof = __c == __eof;
557 __testdelim = __c == __idelim;
558 }
559 if (__testdelim || _M_gcount == __n - 1)
560 __sb->sputbackc(__c);
561 if (__testeof)
562 this->setstate(ios_base::eofbit);
563 }
564 catch(exception& __fail)
565 {
566 // 27.6.1.3 paragraph 1
567 // Turn this on without causing an ios::failure to be thrown.
568 this->setstate(ios_base::badbit);
569 if ((this->exceptions() & ios_base::badbit) != 0)
570 __throw_exception_again;
571 }
572 }
573 *__s = char_type(NULL);
574 if (!_M_gcount)
575 this->setstate(ios_base::failbit);
576 return *this;
577 }
578
579 template<typename _CharT, typename _Traits>
580 basic_istream<_CharT, _Traits>&
581 basic_istream<_CharT, _Traits>::
582 get(__streambuf_type& __sb, char_type __delim)
583 {
584 _M_gcount = 0;
585 sentry __cerb(*this, true);
586 if (__cerb)
587 {
588 int_type __c;
589 __streambuf_type* __this_sb = this->rdbuf();
590 try
591 {
592 const int_type __idelim = traits_type::to_int_type(__delim);
593 const int_type __eof = traits_type::eof();
594 __c = __this_sb->sbumpc();
595 bool __testdelim = __c == __idelim;
596 bool __testeof = __c == __eof;
597 bool __testput = true;
598
599 while (!__testeof && !__testdelim
600 && (__testput = __sb.sputc(traits_type::to_char_type(__c))
601 != __eof))
602 {
603 ++_M_gcount;
604 __c = __this_sb->sbumpc();
605 __testeof = __c == __eof;
606 __testdelim = __c == __idelim;
607 }
608 if (__testdelim || !__testput)
609 __this_sb->sputbackc(traits_type::to_char_type(__c));
610 if (__testeof)
611 this->setstate(ios_base::eofbit);
612 }
613 catch(exception& __fail)
614 {
615 // Exception may result from sputc->overflow.
616 __this_sb->sputbackc(traits_type::to_char_type(__c));
617 }
618 }
619 if (!_M_gcount)
620 this->setstate(ios_base::failbit);
621 return *this;
622 }
623
624 template<typename _CharT, typename _Traits>
625 basic_istream<_CharT, _Traits>&
626 basic_istream<_CharT, _Traits>::
627 getline(char_type* __s, streamsize __n, char_type __delim)
628 {
629 _M_gcount = 0;
630 sentry __cerb(*this, true);
631 if (__cerb)
632 {
633 try
634 {
635 __streambuf_type* __sb = this->rdbuf();
636 int_type __c = __sb->sbumpc();
637 ++_M_gcount;
638 const int_type __idelim = traits_type::to_int_type(__delim);
639 const int_type __eof = traits_type::eof();
640 bool __testdelim = __c == __idelim;
641 bool __testeof = __c == __eof;
642
643 while (_M_gcount < __n && !__testeof && !__testdelim)
644 {
645 *__s++ = traits_type::to_char_type(__c);
646 __c = __sb->sbumpc();
647 ++_M_gcount;
648 __testeof = __c == __eof;
649 __testdelim = __c == __idelim;
650 }
651
652 if (__testeof)
653 {
654 --_M_gcount;
655 this->setstate(ios_base::eofbit);
656 }
657 else if (!__testdelim)
658 {
659 --_M_gcount;
660 __sb->sputbackc(traits_type::to_char_type(__c));
661 this->setstate(ios_base::failbit);
662 }
663 }
664 catch(exception& __fail)
665 {
666 // 27.6.1.3 paragraph 1
667 // Turn this on without causing an ios::failure to be thrown.
668 this->setstate(ios_base::badbit);
669 if ((this->exceptions() & ios_base::badbit) != 0)
670 __throw_exception_again;
671 }
672 }
673 *__s = char_type(NULL);
674 if (!_M_gcount)
675 this->setstate(ios_base::failbit);
676 return *this;
677 }
678
679 template<typename _CharT, typename _Traits>
680 basic_istream<_CharT, _Traits>&
681 basic_istream<_CharT, _Traits>::
682 ignore(streamsize __n, int_type __delim)
683 {
684 _M_gcount = 0;
685 sentry __cerb(*this, true);
686 if (__cerb && __n > 0)
687 {
688 try
689 {
690 const int_type __idelim = traits_type::to_int_type(__delim);
691 const int_type __eof = traits_type::eof();
692 __streambuf_type* __sb = this->rdbuf();
693 int_type __c = __sb->sbumpc();
694 bool __testdelim = __c == __idelim;
695 bool __testeof = __c == __eof;
696
697 __n = min(__n, numeric_limits<streamsize>::max());
698 while (_M_gcount < __n - 1 && !__testeof && !__testdelim)
699 {
700 ++_M_gcount;
701 __c = __sb->sbumpc();
702 __testeof = __c == __eof;
703 __testdelim = __c == __idelim;
704 }
705 if ((_M_gcount == __n - 1 && !__testeof) || __testdelim)
706 ++_M_gcount;
707 if (__testeof)
708 this->setstate(ios_base::eofbit);
709 }
710 catch(exception& __fail)
711 {
712 // 27.6.1.3 paragraph 1
713 // Turn this on without causing an ios::failure to be thrown.
714 this->setstate(ios_base::badbit);
715 if ((this->exceptions() & ios_base::badbit) != 0)
716 __throw_exception_again;
717 }
718 }
719 return *this;
720 }
721
722 template<typename _CharT, typename _Traits>
723 basic_istream<_CharT, _Traits>::int_type
724 basic_istream<_CharT, _Traits>::
725 peek(void)
726 {
727 int_type __c = traits_type::eof();
728 _M_gcount = 0;
729 sentry __cerb(*this, true);
730 if (__cerb)
731 {
732 try
733 { __c = this->rdbuf()->sgetc(); }
734 catch(exception& __fail)
735 {
736 // 27.6.1.3 paragraph 1
737 // Turn this on without causing an ios::failure to be thrown.
738 this->setstate(ios_base::badbit);
739 if ((this->exceptions() & ios_base::badbit) != 0)
740 __throw_exception_again;
741 }
742 }
743 return __c;
744 }
745
746 template<typename _CharT, typename _Traits>
747 basic_istream<_CharT, _Traits>&
748 basic_istream<_CharT, _Traits>::
749 read(char_type* __s, streamsize __n)
750 {
751 _M_gcount = 0;
752 sentry __cerb(*this, true);
753 if (__cerb)
754 {
755 if (__n > 0)
756 {
757 try
758 {
759 const int_type __eof = traits_type::eof();
760 __streambuf_type* __sb = this->rdbuf();
761 int_type __c = __sb->sbumpc();
762 bool __testeof = __c == __eof;
763
764 while (_M_gcount < __n - 1 && !__testeof)
765 {
766 *__s++ = traits_type::to_char_type(__c);
767 ++_M_gcount;
768 __c = __sb->sbumpc();
769 __testeof = __c == __eof;
770 }
771 if (__testeof)
772 this->setstate(ios_base::eofbit | ios_base::failbit);
773 else
774 {
775 // _M_gcount == __n - 1
776 *__s++ = traits_type::to_char_type(__c);
777 ++_M_gcount;
778 }
779 }
780 catch(exception& __fail)
781 {
782 // 27.6.1.3 paragraph 1
783 // Turn this on without causing an ios::failure to be thrown.
784 this->setstate(ios_base::badbit);
785 if ((this->exceptions() & ios_base::badbit) != 0)
786 __throw_exception_again;
787 }
788 }
789 }
790 else
791 this->setstate(ios_base::failbit);
792 return *this;
793 }
794
795 template<typename _CharT, typename _Traits>
796 streamsize
797 basic_istream<_CharT, _Traits>::
798 readsome(char_type* __s, streamsize __n)
799 {
800 const int_type __eof = traits_type::eof();
801 _M_gcount = 0;
802 sentry __cerb(*this, true);
803 if (__cerb)
804 {
805 if (__n > 0)
806 {
807 try
808 {
809 streamsize __num = this->rdbuf()->in_avail();
810 if (__num != static_cast<streamsize>(__eof))
811 {
812 __num = min(__num, __n);
813 _M_gcount = this->rdbuf()->sgetn(__s, __num);
814 }
815 else
816 this->setstate(ios_base::eofbit);
817 }
818 catch(exception& __fail)
819 {
820 // 27.6.1.3 paragraph 1
821 // Turn this on without causing an ios::failure to be thrown.
822 this->setstate(ios_base::badbit);
823 if ((this->exceptions() & ios_base::badbit) != 0)
824 __throw_exception_again;
825 }
826 }
827 }
828 else
829 this->setstate(ios_base::failbit);
830 return _M_gcount;
831 }
832
833 template<typename _CharT, typename _Traits>
834 basic_istream<_CharT, _Traits>&
835 basic_istream<_CharT, _Traits>::
836 putback(char_type __c)
837 {
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 || __sb->sputbackc(__c) == __eof)
846 this->setstate(ios_base::badbit);
847 }
848 catch(exception& __fail)
849 {
850 // 27.6.1.3 paragraph 1
851 // Turn this on without causing an ios::failure to be thrown.
852 this->setstate(ios_base::badbit);
853 if ((this->exceptions() & ios_base::badbit) != 0)
854 __throw_exception_again;
855 }
856 }
857 else
858 this->setstate(ios_base::failbit);
859 return *this;
860 }
861
862 template<typename _CharT, typename _Traits>
863 basic_istream<_CharT, _Traits>&
864 basic_istream<_CharT, _Traits>::
865 unget(void)
866 {
867 _M_gcount = 0;
868 sentry __cerb(*this, true);
869 if (__cerb)
870 {
871 try
872 {
873 const int_type __eof = traits_type::eof();
874 __streambuf_type* __sb = this->rdbuf();
875 if (!__sb || __eof == __sb->sungetc())
876 this->setstate(ios_base::badbit);
877 }
878 catch(exception& __fail)
879 {
880 // 27.6.1.3 paragraph 1
881 // Turn this on without causing an ios::failure to be thrown.
882 this->setstate(ios_base::badbit);
883 if ((this->exceptions() & ios_base::badbit) != 0)
884 __throw_exception_again;
885 }
886 }
887 else
888 this->setstate(ios_base::failbit);
889 return *this;
890 }
891
892 template<typename _CharT, typename _Traits>
893 int
894 basic_istream<_CharT, _Traits>::
895 sync(void)
896 {
897 int __ret = traits_type::eof();
898 _M_gcount = 0;
899 sentry __cerb(*this, true);
900 if (__cerb)
901 {
902 try
903 {
904 __streambuf_type* __sb = this->rdbuf();
905 if (!__sb || __ret == __sb->pubsync())
906 this->setstate(ios_base::badbit);
907 else
908 __ret = 0;
909 }
910 catch(exception& __fail)
911 {
912 // 27.6.1.3 paragraph 1
913 // Turn this on without causing an ios::failure to be thrown.
914 this->setstate(ios_base::badbit);
915 if ((this->exceptions() & ios_base::badbit) != 0)
916 __throw_exception_again;
917 }
918 }
919 return __ret;
920 }
921
922 template<typename _CharT, typename _Traits>
923 typename basic_istream<_CharT, _Traits>::pos_type
924 basic_istream<_CharT, _Traits>::
925 tellg(void)
926 {
927 pos_type __ret = pos_type(-1);
928 _M_gcount = 0;
929 sentry __cerb(*this, true);
930 if (__cerb)
931 {
932 try
933 {
934 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
935 }
936 catch(exception& __fail)
937 {
938 // 27.6.1.3 paragraph 1
939 // Turn this on without causing an ios::failure to be thrown.
940 this->setstate(ios_base::badbit);
941 if ((this->exceptions() & ios_base::badbit) != 0)
942 __throw_exception_again;
943 }
944 }
945 return __ret;
946 }
947
948
949 template<typename _CharT, typename _Traits>
950 basic_istream<_CharT, _Traits>&
951 basic_istream<_CharT, _Traits>::
952 seekg(pos_type __pos)
953 {
954 _M_gcount = 0;
955 sentry __cerb(*this, true);
956 if (__cerb)
957 {
958 try
959 {
960 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
961 // 136. seekp, seekg setting wrong streams?
962 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::in);
963
964 // 129. Need error indication from seekp() and seekg()
965 if (__err == pos_type(off_type(-1)))
966 this->setstate(failbit);
967 #endif
968 }
969 catch(exception& __fail)
970 {
971 // 27.6.1.3 paragraph 1
972 // Turn this on without causing an ios::failure to be thrown.
973 this->setstate(ios_base::badbit);
974 if ((this->exceptions() & ios_base::badbit) != 0)
975 __throw_exception_again;
976 }
977 }
978 return *this;
979 }
980
981 template<typename _CharT, typename _Traits>
982 basic_istream<_CharT, _Traits>&
983 basic_istream<_CharT, _Traits>::
984 seekg(off_type __off, ios_base::seekdir __dir)
985 {
986 _M_gcount = 0;
987 sentry __cerb(*this, true);
988 if (__cerb)
989 {
990 try
991 {
992 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
993 // 136. seekp, seekg setting wrong streams?
994 pos_type __err = this->rdbuf()->pubseekoff(__off, __dir,
995 ios_base::in);
996
997 // 129. Need error indication from seekp() and seekg()
998 if (__err == pos_type(off_type(-1)))
999 this->setstate(failbit);
1000 #endif
1001 }
1002 catch(exception& __fail)
1003 {
1004 // 27.6.1.3 paragraph 1
1005 // Turn this on without causing an ios::failure to be thrown.
1006 this->setstate(ios_base::badbit);
1007 if ((this->exceptions() & ios_base::badbit) != 0)
1008 __throw_exception_again;
1009 }
1010 }
1011 return *this;
1012 }
1013
1014 // 27.6.1.2.3 Character extraction templates
1015 template<typename _CharT, typename _Traits>
1016 basic_istream<_CharT, _Traits>&
1017 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
1018 {
1019 typedef basic_istream<_CharT, _Traits> __istream_type;
1020 typename __istream_type::sentry __cerb(__in, false);
1021 if (__cerb)
1022 {
1023 try
1024 { __in.get(__c); }
1025 catch(exception& __fail)
1026 {
1027 // 27.6.1.2.1 Common requirements.
1028 // Turn this on without causing an ios::failure to be thrown.
1029 __in.setstate(ios_base::badbit);
1030 if ((__in.exceptions() & ios_base::badbit) != 0)
1031 __throw_exception_again;
1032 }
1033 }
1034 else
1035 __in.setstate(ios_base::failbit);
1036 return __in;
1037 }
1038
1039 template<typename _CharT, typename _Traits>
1040 basic_istream<_CharT, _Traits>&
1041 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
1042 {
1043 typedef basic_istream<_CharT, _Traits> __istream_type;
1044 typedef typename __istream_type::__streambuf_type __streambuf_type;
1045 typedef typename _Traits::int_type int_type;
1046 typedef _CharT char_type;
1047 typedef ctype<_CharT> __ctype_type;
1048 streamsize __extracted = 0;
1049
1050 typename __istream_type::sentry __cerb(__in, false);
1051 if (__cerb)
1052 {
1053 try
1054 {
1055 // Figure out how many characters to extract.
1056 streamsize __num = __in.width();
1057 if (__num == 0)
1058 __num = numeric_limits<streamsize>::max();
1059
1060 __streambuf_type* __sb = __in.rdbuf();
1061 const __ctype_type* __ctype = __in._M_get_fctype_ios();
1062 int_type __c = __sb->sbumpc();
1063 const int_type __eof = _Traits::eof();
1064 bool __testsp = __ctype->is(ctype_base::space, __c);
1065 bool __testeof = __c == __eof;
1066
1067 while (__extracted < __num - 1 && !__testeof && !__testsp)
1068 {
1069 *__s++ = __c;
1070 ++__extracted;
1071 __c = __sb->sbumpc();
1072 __testeof = __c == __eof;
1073 __testsp = __ctype->is(ctype_base::space, __c);
1074 }
1075
1076 if (!__testeof)
1077 __sb->sputbackc(__c);
1078 else
1079 __in.setstate(ios_base::eofbit);
1080
1081 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1082 //68. Extractors for char* should store null at end
1083 *__s = char_type();
1084 #endif
1085 __in.width(0);
1086 }
1087 catch(exception& __fail)
1088 {
1089 // 27.6.1.2.1 Common requirements.
1090 // Turn this on without causing an ios::failure to be thrown.
1091 __in.setstate(ios_base::badbit);
1092 if ((__in.exceptions() & ios_base::badbit) != 0)
1093 __throw_exception_again;
1094 }
1095 }
1096 if (!__extracted)
1097 __in.setstate(ios_base::failbit);
1098 return __in;
1099 }
1100
1101 // 27.6.1.4 Standard basic_istream manipulators
1102 template<typename _CharT, typename _Traits>
1103 basic_istream<_CharT,_Traits>&
1104 ws(basic_istream<_CharT,_Traits>& __in)
1105 {
1106 typedef basic_istream<_CharT, _Traits> __istream_type;
1107 typedef typename __istream_type::__streambuf_type __streambuf_type;
1108 typedef typename __istream_type::__ctype_type __ctype_type;
1109 typedef typename __istream_type::int_type __int_type;
1110 typedef typename __istream_type::char_type __char_type;
1111
1112 __streambuf_type* __sb = __in.rdbuf();
1113 const __ctype_type* __ctype = __in._M_get_fctype_ios();
1114 const __int_type __eof = _Traits::eof();
1115 __int_type __c;
1116 bool __testeof;
1117 bool __testsp;
1118
1119 do
1120 {
1121 __c = __sb->sbumpc();
1122 __testeof = __c == __eof;
1123 __testsp = __ctype->is(ctype_base::space, __c);
1124 }
1125 while (!__testeof && __testsp);
1126
1127 if (!__testeof && !__testsp)
1128 __sb->sputbackc(__c);
1129 else
1130 __in.setstate(ios_base::eofbit);
1131
1132 return __in;
1133 }
1134
1135 // 21.3.7.9 basic_string::getline and operators
1136 template<typename _CharT, typename _Traits, typename _Alloc>
1137 basic_istream<_CharT, _Traits>&
1138 operator>>(basic_istream<_CharT, _Traits>& __in,
1139 basic_string<_CharT, _Traits, _Alloc>& __str)
1140 {
1141 typedef basic_istream<_CharT, _Traits> __istream_type;
1142 typedef typename __istream_type::int_type __int_type;
1143 typedef typename __istream_type::__streambuf_type __streambuf_type;
1144 typedef typename __istream_type::__ctype_type __ctype_type;
1145 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1146 typedef typename __string_type::size_type __size_type;
1147 __size_type __extracted = 0;
1148
1149 typename __istream_type::sentry __cerb(__in, false);
1150 if (__cerb)
1151 {
1152 __str.erase();
1153 streamsize __w = __in.width();
1154 __size_type __n;
1155 __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size();
1156
1157 __streambuf_type* __sb = __in.rdbuf();
1158 const __ctype_type* __ctype = __in._M_get_fctype_ios();
1159 __int_type __c = __sb->sbumpc();
1160 const __int_type __eof = _Traits::eof();
1161 bool __testsp = __ctype->is(ctype_base::space, __c);
1162 bool __testeof = __c == __eof;
1163
1164 while (__extracted < __n && !__testeof && !__testsp)
1165 {
1166 __str += _Traits::to_char_type(__c);
1167 ++__extracted;
1168 __c = __sb->sbumpc();
1169 __testeof = __c == __eof;
1170 __testsp = __ctype->is(ctype_base::space, __c);
1171 }
1172 if (!__testeof)
1173 __sb->sputbackc(__c);
1174 else
1175 __in.setstate(ios_base::eofbit);
1176 __in.width(0);
1177 }
1178 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
1179 // 2000-02-01 Number to be determined
1180 if (!__extracted)
1181 __in.setstate (ios_base::failbit);
1182 #endif
1183 return __in;
1184 }
1185
1186 template<typename _CharT, typename _Traits, typename _Alloc>
1187 basic_istream<_CharT, _Traits>&
1188 getline(basic_istream<_CharT, _Traits>& __in,
1189 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
1190 {
1191 typedef basic_istream<_CharT, _Traits> __istream_type;
1192 typedef typename __istream_type::int_type __int_type;
1193 typedef typename __istream_type::__streambuf_type __streambuf_type;
1194 typedef typename __istream_type::__ctype_type __ctype_type;
1195 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
1196 typedef typename __string_type::size_type __size_type;
1197
1198 __size_type __extracted = 0;
1199 bool __testdelim = false;
1200 typename __istream_type::sentry __cerb(__in, true);
1201 if (__cerb)
1202 {
1203 __str.erase();
1204 __size_type __n = __str.max_size();
1205
1206 __int_type __idelim = _Traits::to_int_type(__delim);
1207 __streambuf_type* __sb = __in.rdbuf();
1208 __int_type __c = __sb->sbumpc();
1209 const __int_type __eof = _Traits::eof();
1210 __testdelim = __c == __idelim;
1211 bool __testeof = __c == __eof;
1212
1213 while (__extracted <= __n && !__testeof && !__testdelim)
1214 {
1215 __str += _Traits::to_char_type(__c);
1216 ++__extracted;
1217 __c = __sb->sbumpc();
1218 __testeof = __c == __eof;
1219 __testdelim = __c == __idelim;
1220 }
1221 if (__testeof)
1222 __in.setstate(ios_base::eofbit);
1223 }
1224 if (!__extracted && !__testdelim)
1225 __in.setstate(ios_base::failbit);
1226 return __in;
1227 }
1228
1229 template<class _CharT, class _Traits, class _Alloc>
1230 inline basic_istream<_CharT,_Traits>&
1231 getline(basic_istream<_CharT, _Traits>& __in,
1232 basic_string<_CharT,_Traits,_Alloc>& __str)
1233 { return getline(__in, __str, __in.widen('\n')); }
1234 } // namespace std
1235
1236 // Local Variables:
1237 // mode:C++
1238 // End:
1239