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