OSDN Git Service

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