OSDN Git Service

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