/home/ntakagi/work/STLport-5.1.5/stlport/stl/_istream.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 #ifndef _STLP_ISTREAM_C
00019 #define _STLP_ISTREAM_C
00020 
00021 #ifndef _STLP_INTERNAL_ISTREAM
00022 #  include <stl/_istream.h>
00023 #endif
00024 
00025 #ifndef _STLP_INTERNAL_LIMITS
00026 #  include <stl/_limits.h>
00027 #endif
00028 
00029 #ifndef _STLP_INTERNAL_NUM_GET_H
00030 #  include <stl/_num_get.h>
00031 #endif
00032 
00033 #if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
00034 // no wchar_t is supported for this mode
00035 #  define __BIS_int_type__ int
00036 #  define __BIS_pos_type__ streampos
00037 #  define __BIS_off_type__ streamoff
00038 #else
00039 #  define __BIS_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::int_type
00040 #  define __BIS_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::pos_type
00041 #  define __BIS_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::off_type
00042 #endif
00043 
00044 _STLP_BEGIN_NAMESPACE
00045 
00046 //----------------------------------------------------------------------
00047 // Function object structs used by some member functions.
00048 
00049 _STLP_MOVE_TO_PRIV_NAMESPACE
00050 
00051 template <class _Traits>
00052 struct _Is_not_wspace {
00053   typedef typename _Traits::char_type argument_type;
00054   typedef bool                        result_type;
00055 
00056   const ctype<argument_type>* _M_ctype;
00057 
00058   _Is_not_wspace(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {}
00059   bool operator()(argument_type __c) const
00060     { return !_M_ctype->is(ctype_base::space, __c); }
00061 };
00062 
00063 template <class _Traits>
00064 struct _Is_wspace_null {
00065   typedef typename _Traits::char_type argument_type;
00066   typedef bool                        result_type;
00067 
00068   const ctype<argument_type>* _M_ctype;
00069 
00070   _Is_wspace_null(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {}
00071   bool operator()(argument_type __c) const {
00072     return _Traits::eq(__c, argument_type()) ||
00073            _M_ctype->is(ctype_base::space, __c);
00074   }
00075 };
00076 
00077 template <class _Traits>
00078 struct _Scan_for_wspace {
00079   typedef typename _Traits::char_type  char_type;
00080   typedef char_type*                   first_argument_type;
00081   typedef char_type*                   second_argument_type;
00082   typedef char_type*                   result_type;
00083 
00084   const ctype<char_type>* _M_ctype;
00085 
00086   _Scan_for_wspace(const ctype<char_type>* __ctype) : _M_ctype(__ctype) {}
00087   const char_type*
00088   operator()(const char_type* __first, const char_type* __last) const {
00089     return _M_ctype->scan_is(ctype_base::space, __first, __last);
00090   }
00091 };
00092 
00093 template <class _Traits>
00094 struct _Scan_wspace_null {
00095   typedef typename _Traits::char_type  char_type;
00096   typedef char_type*                   first_argument_type;
00097   typedef char_type*                   second_argument_type;
00098   typedef char_type*                   result_type;
00099 
00100   const ctype<char_type>* _M_ctype;
00101 
00102   _Scan_wspace_null(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {}
00103   const char_type*
00104   operator()(const char_type* __first, const char_type* __last) const {
00105     __last = find_if(__first, __last,
00106                      _Eq_char_bound<_Traits>(char_type()));
00107     return _M_ctype->scan_is(ctype_base::space, __first, __last);
00108   }
00109 };
00110 
00111 template <class _Traits>
00112 struct _Scan_for_not_wspace {
00113   typedef typename _Traits::char_type  char_type;
00114   typedef char_type*                   first_argument_type;
00115   typedef char_type*                   second_argument_type;
00116   typedef char_type*                   result_type;
00117 
00118   const ctype<char_type>* _M_ctype;
00119 
00120   _Scan_for_not_wspace(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {}
00121   const char_type*
00122   operator()(const char_type* __first, const char_type* __last) const {
00123     return _M_ctype->scan_not(ctype_base::space, __first, __last);
00124   }
00125 };
00126 
00127 template <class _Traits>
00128 struct _Scan_for_char_val {
00129   typedef typename _Traits::char_type char_type;
00130   typedef char_type*                  first_argument_type;
00131   typedef char_type*                  second_argument_type;
00132   typedef char_type*                  result_type;
00133 
00134   char_type _M_val;
00135 
00136   _Scan_for_char_val(char_type __val) : _M_val(__val) {}
00137 
00138   const char_type*
00139   operator()(const char_type* __first, const char_type* __last) const {
00140     return find_if(__first, __last, _Eq_char_bound<_Traits>(_M_val));
00141   }
00142 };
00143 
00144 template <class _Traits>
00145 struct _Scan_for_int_val {
00146   typedef typename _Traits::char_type char_type;
00147   typedef typename _Traits::int_type  int_type;
00148   typedef char_type*                  first_argument_type;
00149   typedef char_type*                  second_argument_type;
00150   typedef char_type*                  result_type;
00151 
00152   int_type _M_val;
00153 
00154   _Scan_for_int_val(int_type __val) : _M_val(__val) {}
00155 
00156   const char_type*
00157   operator()(const char_type* __first, const char_type* __last) const {
00158     return find_if(__first, __last,
00159                    _Eq_int_bound<_Traits>(_M_val));
00160   }
00161 };
00162 
00163 // Helper function: try to push back a character to a streambuf,
00164 // return true if the pushback succeeded.  Does not throw.
00165 
00166 template <class _CharT, class _Traits>
00167 bool _STLP_CALL
00168 __pushback(basic_streambuf<_CharT, _Traits>* __buf, _CharT __c) {
00169   bool ret;
00170   _STLP_TRY {
00171     const typename _Traits::int_type __eof = _Traits::eof();
00172     ret = !_Traits::eq_int_type(__buf->sputbackc(__c), __eof);
00173   }
00174   _STLP_CATCH_ALL {
00175     ret = false;
00176   }
00177   return ret;
00178 }
00179 
00180 //----------------------------------------------------------------------
00181 // Definitions of basic_istream<>'s noninline member functions.
00182 
00183 // Helper function for formatted input of numbers.
00184 template <class _CharT, class _Traits, class _Number>
00185 ios_base::iostate _STLP_CALL
00186 __get_num(basic_istream<_CharT, _Traits>& __that, _Number& __val) {
00187   typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
00188   ios_base::iostate __err = 0;
00189   _Sentry __sentry( __that );     // Skip whitespace.
00190   if (__sentry) {
00191     typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> > _Num_get;
00192     _STLP_TRY {
00193       ((const _Num_get&)use_facet<_Num_get>(__that.getloc())).get(istreambuf_iterator<_CharT, _Traits>(__that.rdbuf()),
00194                                                                   0, __that, __err, __val);
00195     }
00196     _STLP_CATCH_ALL {
00197       __that._M_handle_exception(ios_base::badbit);
00198     }
00199     if (__err) __that.setstate(__err);
00200   }
00201   return __err;
00202 }
00203 
00204 _STLP_MOVE_TO_STD_NAMESPACE
00205 
00206 template <class _CharT, class _Traits>
00207 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (short& __val) {
00208   long __lval;
00209   _STLP_PRIV __get_num(*this, __lval);
00210   if ( this->fail() ) {
00211     return *this;
00212   }
00213   short __tmp = __STATIC_CAST(short, __lval);
00214   unsigned short __uval = __STATIC_CAST(unsigned short, __lval);
00215   // check if we lose digits
00216   //    if ((__val != __lval) && ((unsigned short)__val != __lval))
00217   if ((__tmp != __lval) && ((long)__uval != __lval))
00218     this->setstate(ios_base::failbit);
00219   else
00220     __val = __tmp;
00221   return *this;
00222 }
00223 
00224 template <class _CharT, class _Traits>
00225 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (int& __val) {
00226   long __lval;
00227   _STLP_PRIV __get_num(*this, __lval);
00228   if ( this->fail() ) {
00229     return *this;
00230   }
00231   int __tmp = __lval;
00232   unsigned int __uval = __lval;
00233   // check if we lose digits
00234   //    if ((__val != __lval) && ((unsigned int)__val != __lval))
00235   if ((__tmp != __lval) && ((long)__uval != __lval))
00236     this->setstate(ios_base::failbit);
00237   else
00238     __val = __tmp;
00239   return *this;
00240 }
00241 
00242 template <class _CharT, class _Traits>
00243 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned short& __val) {
00244   _STLP_PRIV __get_num(*this, __val);
00245   return *this;
00246 }
00247 
00248 template <class _CharT, class _Traits>
00249 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned int& __val) {
00250   _STLP_PRIV __get_num(*this, __val);
00251   return *this;
00252 }
00253 
00254 template <class _CharT, class _Traits>
00255 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (long& __val) {
00256   _STLP_PRIV __get_num(*this, __val);
00257   return *this;
00258 }
00259 
00260 template <class _CharT, class _Traits>
00261 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned long& __val) {
00262   _STLP_PRIV __get_num(*this, __val);
00263   return *this;
00264 }
00265 
00266 #if defined (_STLP_LONG_LONG)
00267 template <class _CharT, class _Traits>
00268 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (_STLP_LONG_LONG& __val) {
00269   _STLP_PRIV __get_num(*this, __val);
00270   return *this;
00271 }
00272 
00273 template <class _CharT, class _Traits>
00274 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned _STLP_LONG_LONG& __val) {
00275   _STLP_PRIV __get_num(*this, __val);
00276   return *this;
00277 }
00278 #endif
00279 template <class _CharT, class _Traits>
00280 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (float& __val) {
00281   _STLP_PRIV __get_num(*this, __val);
00282   return *this;
00283 }
00284 template <class _CharT, class _Traits>
00285 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (double& __val) {
00286   _STLP_PRIV __get_num(*this, __val);
00287   return *this;
00288 }
00289 #if !defined (_STLP_NO_LONG_DOUBLE)
00290 template <class _CharT, class _Traits>
00291 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (long double& __val) {
00292   _STLP_PRIV __get_num(*this, __val);
00293   return *this;
00294 }
00295 #endif
00296 #if !defined (_STLP_NO_BOOL)
00297 template <class _CharT, class _Traits>
00298 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (bool& __val) {
00299   _STLP_PRIV __get_num(*this, __val);
00300   return *this;
00301 }
00302 #endif
00303 
00304 template <class _CharT, class _Traits>
00305 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (void*& __val) {
00306   _STLP_PRIV __get_num(*this, __val);
00307   return *this;
00308 }
00309 
00310 // Unformatted input
00311 
00312 template <class _CharT, class _Traits>
00313 __BIS_int_type__
00314 basic_istream<_CharT, _Traits>::peek() {
00315   typename _Traits::int_type __tmp = _Traits::eof();
00316 
00317   this->_M_gcount = 0;
00318   sentry __sentry(*this, _No_Skip_WS());
00319 
00320   if (__sentry) {
00321     _STLP_TRY {
00322       __tmp = this->rdbuf()->sgetc();
00323     }
00324     _STLP_CATCH_ALL {
00325       this->_M_handle_exception(ios_base::badbit);
00326     }
00327     if (this->_S_eof(__tmp))
00328       this->setstate(ios_base::eofbit);
00329   }
00330 
00331   return __tmp;
00332 }
00333 
00334 
00335 template <class _CharT, class _Traits>
00336 __BIS_int_type__
00337 basic_istream<_CharT, _Traits>::get() {
00338   typename _Traits::int_type __tmp = _Traits::eof();
00339   sentry __sentry(*this, _No_Skip_WS());
00340   this->_M_gcount = 0;
00341 
00342   if (__sentry) {
00343     _STLP_TRY {
00344       __tmp = this->rdbuf()->sbumpc();
00345     }
00346     _STLP_CATCH_ALL {
00347       this->_M_handle_exception(ios_base::badbit);
00348     }
00349 
00350     if (!this->_S_eof(__tmp))
00351       this->_M_gcount = 1;
00352   }
00353 
00354   if (_M_gcount == 0)
00355     this->setstate(ios_base::eofbit | ios_base::failbit);
00356 
00357   return __tmp;
00358 }
00359 
00360 template <class _CharT, class _Traits>
00361 basic_istream<_CharT, _Traits>&
00362 basic_istream<_CharT, _Traits>::get(_CharT& __c) {
00363   sentry __sentry(*this, _No_Skip_WS());
00364   this->_M_gcount = 0;
00365 
00366   if (__sentry) {
00367     typename _Traits::int_type __tmp = _Traits::eof();
00368     _STLP_TRY {
00369       __tmp = this->rdbuf()->sbumpc();
00370     }
00371     _STLP_CATCH_ALL {
00372       this->_M_handle_exception(ios_base::badbit);
00373     }
00374 
00375     if (!this->_S_eof(__tmp)) {
00376       this->_M_gcount = 1;
00377       __c = _Traits::to_char_type(__tmp);
00378     }
00379   }
00380 
00381   if (this->_M_gcount == 0)
00382     this->setstate(ios_base::eofbit | ios_base::failbit);
00383 
00384   return *this;
00385 }
00386 
00387 
00388 // Read characters and discard them.  The standard specifies a single
00389 // function with two arguments, each with a default.  We instead use
00390 // three overloded functions, because it's possible to implement the
00391 // first two more efficiently than the fully general third version.
00392 template <class _CharT, class _Traits>
00393 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore() {
00394   sentry __sentry(*this, _No_Skip_WS());
00395   this->_M_gcount = 0;
00396 
00397   if (__sentry) {
00398     int_type __c;
00399     _STLP_TRY {
00400       __c = this->rdbuf()->sbumpc();
00401     }
00402     _STLP_CATCH_ALL {
00403       this->_M_handle_exception(ios_base::badbit);
00404       return *this;
00405     }
00406 
00407     if (!this->_S_eof(__c))
00408       this->_M_gcount = 1;
00409     else
00410       this->setstate(ios_base::eofbit);
00411   }
00412 
00413   return *this;
00414 }
00415 
00416 // Putback
00417 
00418 template <class _CharT, class _Traits>
00419 basic_istream<_CharT, _Traits>&
00420 basic_istream<_CharT, _Traits>::putback(_CharT __c) {
00421   this->_M_gcount = 0;
00422   sentry __sentry(*this, _No_Skip_WS());
00423 
00424   if (__sentry) {
00425     typename _Traits::int_type __tmp = _Traits::eof();
00426     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00427 //    if (!__buf || this->_S_eof(__buf->sputbackc(__c)))
00428     if (__buf) {
00429       _STLP_TRY {
00430         __tmp = __buf->sputbackc(__c);
00431       }
00432       _STLP_CATCH_ALL {
00433         this->_M_handle_exception(ios_base::badbit);
00434       }
00435     }
00436     if (this->_S_eof(__tmp))
00437       this->setstate(ios_base::badbit);
00438   }
00439   else
00440     this->setstate(ios_base::failbit);
00441 
00442   return *this;
00443 }
00444 
00445 template <class _CharT, class _Traits>
00446 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
00447   this->_M_gcount = 0;
00448 
00449   sentry __sentry(*this, _No_Skip_WS());
00450 
00451   if (__sentry) {
00452     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00453     //     if (!__buf || _Traits::eq_int_type(__buf->sungetc(), _Traits::eof()))
00454     if (__buf) {
00455       _STLP_TRY {
00456         if (this->_S_eof(__buf->sungetc()))
00457           this->setstate(ios_base::badbit);
00458       }
00459       _STLP_CATCH_ALL {
00460         this->_M_handle_exception(ios_base::badbit);
00461       }
00462     } else
00463       this->setstate(ios_base::badbit);
00464   }
00465   else
00466     this->setstate(ios_base::failbit);
00467 
00468   return *this;
00469 }
00470 
00471 // Positioning and buffer control.
00472 
00473 template <class _CharT, class _Traits>
00474 int basic_istream<_CharT, _Traits>::sync() {
00475   sentry __sentry(*this, _No_Skip_WS());
00476 
00477   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00478   if (__buf) {
00479     if (__buf->pubsync() == -1) {
00480       this->setstate(ios_base::badbit);
00481       return -1;
00482     }
00483     else
00484       return 0;
00485   }
00486   else
00487     return -1;
00488 }
00489 
00490 template <class _CharT, class _Traits>
00491 __BIS_pos_type__
00492 basic_istream<_CharT, _Traits>::tellg() {
00493   sentry __sentry(*this, _No_Skip_WS());
00494 
00495   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00496   return (__buf && !this->fail()) ? __buf->pubseekoff(0, ios_base::cur, ios_base::in)
00497     : pos_type(-1);
00498 }
00499 
00500 template <class _CharT, class _Traits>
00501 basic_istream<_CharT, _Traits>&
00502 basic_istream<_CharT, _Traits>::seekg(pos_type __pos) {
00503   sentry __sentry(*this, _No_Skip_WS());
00504 
00505   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00506   if (!this->fail() && __buf) {
00507     if (__buf->pubseekpos(__pos) == pos_type(-1)) {
00508       this->setstate(ios_base::failbit);
00509     }
00510   }
00511   return *this;
00512 }
00513 
00514 template <class _CharT, class _Traits>
00515 basic_istream<_CharT, _Traits>&
00516 basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) {
00517   sentry __sentry(*this, _No_Skip_WS());
00518 
00519   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00520   if (!this->fail() && __buf)
00521     __buf->pubseekoff(__off, __dir);
00522   return *this;
00523 }
00524 
00525 // Formatted input of characters and character arrays.
00526 
00527 template <class _CharT, class _Traits>
00528 void basic_istream<_CharT, _Traits>::_M_formatted_get(_CharT& __c) {
00529 //  typename _Traits::int_type __tmp = _Traits::eof();
00530 
00531   sentry __sentry(*this); // Skip whitespace.
00532 
00533   if (__sentry) {
00534     typename _Traits::int_type __tmp;// = _Traits::eof();
00535 
00536     _STLP_TRY {
00537       __tmp = this->rdbuf()->sbumpc();
00538     }
00539     _STLP_CATCH_ALL {
00540       this->_M_handle_exception(ios_base::badbit);
00541       return;
00542     }
00543 
00544     if (!this->_S_eof(__tmp))
00545       __c = _Traits::to_char_type(__tmp);
00546     else
00547       this->setstate(ios_base::eofbit | ios_base::failbit);
00548   }
00549 }
00550 
00551 
00552 //---------------------------------------------------------------------------
00553 // istream's helper functions.
00554 
00555 // A generic function for unbuffered input.  We stop when we reach EOF,
00556 // or when we have extracted _Num characters, or when the function object
00557 // __is_delim return true.  In the last case, it extracts the character
00558 // for which __is_delim is true, if and only if __extract_delim is true.
00559 // It appends a null character to the end of the string; this means that
00560 // it may store up to _Num + 1 characters.
00561 //
00562 // __is_getline governs two corner cases: reading _Num characters without
00563 // encountering delim or eof (in which case failbit is set if __is_getline
00564 // is true); and reading _Num characters where the _Num+1'st character is
00565 // eof (in which case eofbit is set if __is_getline is true).
00566 //
00567 // It is assumed that __is_delim never throws.
00568 //
00569 // Return value is the number of characters extracted, including the
00570 // delimiter if it is extracted.  Note that the number of characaters
00571 // extracted isn't necessarily the same as the number stored.
00572 
00573 _STLP_MOVE_TO_PRIV_NAMESPACE
00574 
00575 template < class _CharT, class _Traits, class _Is_Delim>
00576 streamsize _STLP_CALL
00577 __read_unbuffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __buf,
00578                   streamsize _Num, _CharT* __s,
00579                   _Is_Delim __is_delim,
00580                   bool __extract_delim, bool __append_null,
00581                   bool __is_getline)
00582 {
00583   streamsize __n = 0;
00584   ios_base::iostate __status = 0;
00585 
00586   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
00587   // The operations that can potentially throw are sbumpc, snextc, and sgetc.
00588   _STLP_TRY {
00589     for (;;) {
00590       if (__n == _Num) {
00591         if (__is_getline) // didn't find delimiter as one of the _Num chars
00592           __status |= ios_base::failbit;
00593         break;
00594       }
00595 
00596       int_type __c = __buf->sbumpc(); // sschwarz
00597 
00598       if (__that->_S_eof(__c)) {
00599         if (__n < _Num || __is_getline)
00600           __status |= ios_base::eofbit;
00601         break;
00602       } else if (__is_delim(_Traits::to_char_type(__c))) {
00603         if (__extract_delim) { // Extract and discard current character.
00604           ++__n;
00605         } else if ( !__pushback(__buf, _Traits::to_char_type(__c)) ) { // leave delimiter
00606           __status |= ios_base::failbit;
00607         }
00608         break;
00609       }
00610       // regular character
00611       *__s++ = _Traits::to_char_type(__c);
00612       ++__n;
00613     }
00614   }
00615   _STLP_CATCH_ALL {
00616     __that->_M_handle_exception(ios_base::badbit);
00617     *__s = _STLP_DEFAULT_CONSTRUCTED(_CharT);
00618     return __n;
00619   }
00620 
00621   if (__append_null)
00622     *__s =  _STLP_DEFAULT_CONSTRUCTED(_CharT);
00623   if (__status)
00624     __that->setstate(__status);    // This might throw.
00625   return __n;
00626 }
00627 
00628 // Much like __read_unbuffered, but with one additional function object:
00629 // __scan_delim(first, last) returns the first pointer p in [first, last)
00630 // such that __is_delim(p) is true.
00631 
00632 template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
00633 streamsize _STLP_CALL
00634 __read_buffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __buf,
00635                  streamsize _Num, _CharT* __s,
00636                  _Is_Delim __is_delim, _Scan_Delim __scan_delim,
00637                  bool __extract_delim, bool __append_null,
00638                  bool __is_getline) {
00639   streamsize __n = 0;
00640   ios_base::iostate __status = 0;
00641   bool __done    = false;
00642 
00643   _STLP_TRY {
00644     while (__buf->_M_egptr() != __buf->_M_gptr() && !__done) {
00645       const _CharT* __first = __buf->_M_gptr();
00646       const _CharT* __last  = __buf->_M_egptr();
00647       //casting numeric_limits<ptrdiff_t>::max to streamsize only works is ptrdiff_t is signed or streamsize representation
00648       //is larger than ptrdiff_t one.
00649       _STLP_STATIC_ASSERT((sizeof(streamsize) > sizeof(ptrdiff_t)) ||
00650                           (sizeof(streamsize) == sizeof(ptrdiff_t)) && numeric_limits<ptrdiff_t>::is_signed)
00651       ptrdiff_t __request = __STATIC_CAST(ptrdiff_t, (min) (__STATIC_CAST(streamsize, (numeric_limits<ptrdiff_t>::max)()), _Num - __n));
00652 
00653       const _CharT* __p  = __scan_delim(__first, __last);
00654       ptrdiff_t __chunk = (min) (ptrdiff_t(__p - __first), __request);
00655       _Traits::copy(__s, __first, __chunk);
00656       __s += __chunk;
00657       __n += __chunk;
00658       __buf->_M_gbump((int)__chunk);
00659 
00660       // We terminated by finding delim.
00661       if (__p != __last && __p - __first <= __request) {
00662         if (__extract_delim) {
00663           __n += 1;
00664           __buf->_M_gbump(1);
00665         }
00666         __done = true;
00667       }
00668 
00669       // We terminated by reading all the characters we were asked for.
00670       else if (__n == _Num) {
00671 
00672         // Find out if we have reached eof.  This matters for getline.
00673         if (__is_getline) {
00674           if (__chunk == __last - __first) {
00675             if (__that->_S_eof(__buf->sgetc()))
00676               __status |= ios_base::eofbit;
00677           }
00678           else
00679             __status |= ios_base::failbit;
00680         }
00681         __done   = true;
00682       }
00683 
00684       // The buffer contained fewer than _Num - __n characters.  Either we're
00685       // at eof, or we should refill the buffer and try again.
00686       else {
00687         if (__that->_S_eof(__buf->sgetc())) {
00688           __status |= ios_base::eofbit;
00689           __done = true;
00690         }
00691       }
00692     } // Close the while loop.
00693   }
00694   _STLP_CATCH_ALL {
00695     __that->_M_handle_exception(ios_base::badbit);
00696     __done = true;
00697   }
00698 
00699   if (__done) {
00700     if (__append_null)
00701         *__s =  _STLP_DEFAULT_CONSTRUCTED(_CharT);
00702     if (__status != 0)
00703       __that->setstate(__status);   // This might throw.
00704     return __n;
00705   }
00706 
00707   // If execution has reached this point, then we have an empty buffer but
00708   // we have not reached eof.  What that means is that the streambuf has
00709   // decided to switch from buffered to unbuffered input.  We switch to
00710   // to __read_unbuffered.
00711 
00712   return __n + __read_unbuffered(__that,  __buf, _Num - __n, __s, __is_delim,
00713                                  __extract_delim,__append_null,__is_getline);
00714 }
00715 
00716 _STLP_MOVE_TO_STD_NAMESPACE
00717 
00718 template <class _CharT, class _Traits>
00719 basic_istream<_CharT, _Traits>&
00720 basic_istream<_CharT, _Traits>::get(_CharT* __s, streamsize __n,
00721                                     _CharT __delim) {
00722   sentry __sentry(*this, _No_Skip_WS());
00723   this->_M_gcount = 0;
00724 
00725   if (__sentry) {
00726     if (__n > 0) {
00727       basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00728 
00729       if (__buf->egptr() != __buf->gptr())
00730         this->_M_gcount =
00731           _STLP_PRIV __read_buffered(this,  __buf, __n - 1, __s,
00732                                      _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
00733                                      _STLP_PRIV _Scan_for_char_val<_Traits>(__delim),
00734                                      false, true, false);
00735       else
00736         this->_M_gcount =
00737           _STLP_PRIV __read_unbuffered(this,  __buf, __n - 1, __s,
00738                                        _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
00739                                        false, true, false);
00740     }
00741   }
00742 
00743   if (this->_M_gcount == 0)
00744     this->setstate(ios_base::failbit);
00745 
00746   return *this;
00747 }
00748 
00749 // Getline is essentially identical to get, except that it extracts
00750 // the delimiter.
00751 template <class _CharT, class _Traits>
00752 basic_istream<_CharT, _Traits>&
00753 basic_istream<_CharT, _Traits>::getline(_CharT* __s, streamsize __n,
00754                                         _CharT __delim) {
00755   sentry __sentry(*this, _No_Skip_WS());
00756   this->_M_gcount = 0;
00757 
00758   if (__sentry) {
00759     if (__n > 0) {
00760       basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00761       this->_M_gcount = __buf->egptr() != __buf->gptr()
00762         ? _STLP_PRIV __read_buffered(this,  __buf, __n - 1, __s,
00763                                      _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
00764                                      _STLP_PRIV _Scan_for_char_val<_Traits>(__delim),
00765                                      true, true, true)
00766         : _STLP_PRIV __read_unbuffered(this,  __buf, __n - 1, __s,
00767                                        _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
00768                                        true, true, true);
00769     }
00770   }
00771 
00772   if (this->_M_gcount == 0)
00773     this->setstate(ios_base::failbit);
00774 
00775   return *this;
00776 }
00777 
00778 // Read n characters.  We don't look for any delimiter, and we don't
00779 // put in a terminating null character.
00780 template <class _CharT, class _Traits>
00781 basic_istream<_CharT, _Traits>&
00782 basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n) {
00783   sentry __sentry(*this, _No_Skip_WS());
00784   this->_M_gcount = 0;
00785 
00786   if (__sentry && !this->eof()) {
00787     basic_streambuf<_CharT, _Traits>*__buf = this->rdbuf();
00788     if (__buf->gptr() != __buf->egptr())
00789       _M_gcount
00790         = _STLP_PRIV __read_buffered(this,  __buf, __n, __s,
00791                                      _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
00792                                      _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
00793                                      false, false, false);
00794     else
00795       _M_gcount
00796         = _STLP_PRIV __read_unbuffered(this,  __buf, __n, __s,
00797                                        _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
00798                                        false, false, false);
00799   }
00800   else
00801     this->setstate(ios_base::failbit);
00802 
00803   if (this->eof())
00804     this->setstate(ios_base::eofbit | ios_base::failbit);
00805 
00806   return *this;
00807 }
00808 
00809 
00810 // Read n or fewer characters.  We don't look for any delimiter, and
00811 // we don't put in a terminating null character.
00812 template <class _CharT, class _Traits>
00813 streamsize
00814 basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __nmax) {
00815   sentry __sentry(*this, _No_Skip_WS());
00816   this->_M_gcount = 0;
00817 
00818   if (__sentry && !this->eof() && __nmax >= 0) {
00819 
00820     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00821     streamsize __avail = __buf->in_avail();
00822 
00823     // fbp : isn't full-blown setstate required here ?
00824     if (__avail == -1)
00825       this->_M_setstate_nothrow(ios_base::eofbit);
00826 
00827     else if (__avail != 0) {
00828 
00829       if (__buf->gptr() != __buf->egptr())
00830         _M_gcount
00831           = _STLP_PRIV __read_buffered(this,  __buf, (min) (__avail, __nmax), __s,
00832                                        _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
00833                                        _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
00834                                        false, false, false);
00835       else
00836         _M_gcount
00837           = _STLP_PRIV __read_unbuffered(this,  __buf, (min) (__avail, __nmax), __s,
00838                                          _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
00839                                          false, false, false);
00840     }
00841   }
00842   else {
00843     // fbp : changed so that failbit is set only there, to pass Dietmar's test
00844     if (this->eof())
00845       this->setstate(ios_base::eofbit | ios_base::failbit);
00846     else
00847       this->setstate(ios_base::failbit);
00848   }
00849 
00850   //  if (this->eof())
00851   //    this->setstate(ios_base::eofbit | ios_base::failbit);
00852 
00853   return _M_gcount;
00854 }
00855 
00856 template <class _CharT, class _Traits>
00857 void basic_istream<_CharT, _Traits>::_M_formatted_get(_CharT* __s) {
00858   sentry __sentry(*this); // Skip whitespace.
00859 
00860   if (__sentry) {
00861     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
00862     streamsize __nmax = this->width() > 0
00863       ? this->width() - 1
00864       : ((numeric_limits<streamsize>::max)() / sizeof(_CharT)) - 1;
00865 
00866     streamsize __n = __buf->gptr() != __buf->egptr()
00867       ? _STLP_PRIV __read_buffered(this,  __buf, __nmax, __s,
00868                                    _STLP_PRIV _Is_wspace_null<_Traits>(__STATIC_CAST(const ctype<_CharT>*, this->_M_ctype_facet())),
00869                                    _STLP_PRIV _Scan_wspace_null<_Traits>(__STATIC_CAST(const ctype<_CharT>*, this->_M_ctype_facet())),
00870                                    false, true, false)
00871       : _STLP_PRIV __read_unbuffered(this,  __buf, __nmax, __s,
00872                                      _STLP_PRIV _Is_wspace_null<_Traits>(__STATIC_CAST(const ctype<_CharT>*, this->_M_ctype_facet())),
00873                                      false, true, false);
00874     if (__n == 0)
00875       this->setstate(ios_base::failbit);
00876   }
00877   this->width(0);
00878 }
00879 
00880 // A generic unbuffered function for ignoring characters.  We stop
00881 // when we reach EOF, or when the function object __is_delim returns
00882 // true.  In the last case, it extracts the character for which
00883 // __is_delim is true, if and only if __extract_delim is true.
00884 
00885 template < class _CharT, class _Traits, class _Is_Delim>
00886 void _STLP_CALL
00887 _M_ignore_unbuffered(basic_istream<_CharT, _Traits>* __that,
00888                      basic_streambuf<_CharT, _Traits>* __buf,
00889                      _Is_Delim __is_delim,
00890                      bool __extract_delim, bool __set_failbit) {
00891   bool __done = false;
00892   ios_base::iostate __status = 0;
00893   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
00894 
00895   _STLP_TRY {
00896     while (!__done) {
00897       int_type __c = __buf->sbumpc();
00898 
00899       if (__that->_S_eof(__c)) {
00900         __done = true;
00901         __status |= __set_failbit ? ios_base::eofbit | ios_base::failbit
00902                                   : ios_base::eofbit;
00903       }
00904 
00905       else if (__is_delim(_Traits::to_char_type(__c))) {
00906         __done = true;
00907         if (!__extract_delim)
00908           if (__that->_S_eof(__buf->sputbackc(_Traits::to_char_type(__c))))
00909             __status |= ios_base::failbit;
00910       }
00911     }
00912   }
00913   _STLP_CATCH_ALL {
00914     __that->_M_handle_exception(ios_base::badbit);
00915   }
00916 
00917   __that->setstate(__status);
00918 }
00919 
00920 // A generic buffered function for ignoring characters.  Much like
00921 // _M_ignore_unbuffered, but with one additional function object:
00922 // __scan_delim(first, last) returns the first pointer p in [first,
00923 // last) such that __is_delim(p) is true.
00924 
00925 template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
00926 void _STLP_CALL
00927 _M_ignore_buffered(basic_istream<_CharT, _Traits>* __that,
00928                    basic_streambuf<_CharT, _Traits>* __buf,
00929                    _Is_Delim __is_delim, _Scan_Delim __scan_delim,
00930                    bool __extract_delim, bool __set_failbit) {
00931   bool __at_eof      = false;
00932   bool __found_delim = false;
00933 
00934   _STLP_TRY {
00935     while (__buf->_M_egptr() != __buf->_M_gptr() && !__at_eof && !__found_delim) {
00936       const _CharT* __p = __scan_delim(__buf->_M_gptr(), __buf->_M_egptr());
00937       __buf->_M_gbump((int)(__p - __buf->_M_gptr()));
00938 
00939       if (__p != __buf->_M_egptr()) { // We found delim, so we're done.
00940         if (__extract_delim)
00941           __buf->_M_gbump(1);
00942         __found_delim = true;
00943       }
00944 
00945       else                         // No delim.  Try to refil the buffer.
00946         __at_eof = __that->_S_eof(__buf->sgetc());
00947     }                              // Close the while loop.
00948   }
00949   _STLP_CATCH_ALL {
00950     __that->_M_handle_exception(ios_base::badbit);
00951     return;
00952   }
00953 
00954   if (__at_eof) {
00955     __that->setstate(__set_failbit ? ios_base::eofbit | ios_base::failbit
00956                                    : ios_base::eofbit);
00957     return;
00958   }
00959   if (__found_delim)
00960     return;
00961 
00962   // If execution has reached this point, then we have an empty buffer but
00963   // we have not reached eof.  What that means is that the streambuf has
00964   // decided to switch from a buffered to an unbuffered mode.  We switch
00965   // to _M_ignore_unbuffered.
00966   _M_ignore_unbuffered(__that,  __buf, __is_delim, __extract_delim, __set_failbit);
00967 }
00968 
00969 // Overloaded versions of _M_ignore_unbuffered and _M_ignore_unbuffered
00970 // with an explicit count _Num.  Return value is the number of
00971 // characters extracted.
00972 //
00973 // The function object __max_chars takes two arguments, _Num and __n
00974 // (the latter being the number of characters we have already read),
00975 // and returns the maximum number of characters to read from the buffer.
00976 // We parameterize _M_ignore_buffered so that we can use it for both
00977 // bounded and unbounded input; for the former the function object should
00978 // be minus<>, and for the latter it should return a constant maximum value.
00979 
00980 template < class _CharT, class _Traits, class _Max_Chars, class _Is_Delim>
00981 streamsize _STLP_CALL
00982 _M_ignore_unbuffered(basic_istream<_CharT, _Traits>* __that,
00983                      basic_streambuf<_CharT, _Traits>* __buf,
00984                      streamsize _Num, _Max_Chars __max_chars,
00985                      _Is_Delim __is_delim,
00986                      bool __extract_delim, bool __set_failbit) {
00987   streamsize __n = 0;
00988   ios_base::iostate __status = 0;
00989   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
00990 
00991   _STLP_TRY {
00992     while (__max_chars(_Num, __n) > 0) {
00993       int_type __c = __buf->sbumpc();
00994 
00995       if (__that->_S_eof(__c)) {
00996         __status |= __set_failbit ? ios_base::eofbit | ios_base::failbit
00997                                   : ios_base::eofbit;
00998         break;
00999       }
01000 
01001       else if (__is_delim(_Traits::to_char_type(__c))) {
01002         if (__extract_delim)
01003           ++__n;
01004         else if (__that->_S_eof(__buf->sputbackc(_Traits::to_char_type(__c))))
01005           __status |= ios_base::failbit;
01006 
01007         break;
01008       }
01009       // fbp : added counter increment to pass Dietmar's test
01010       ++__n;
01011     }
01012   }
01013   _STLP_CATCH_ALL {
01014     __that->_M_handle_exception(ios_base::badbit);
01015   }
01016 
01017   if (__status)
01018     __that->setstate(__status);   // This might throw.
01019   return __n;
01020 }
01021 
01022 template < class _CharT, class _Traits, class _Max_Chars, class _Is_Delim, class _Scan_Delim>
01023 streamsize _STLP_CALL
01024 _M_ignore_buffered(basic_istream<_CharT, _Traits>* __that,
01025                    basic_streambuf<_CharT, _Traits>* __buf,
01026                    streamsize _Num,
01027                    _Max_Chars __max_chars,
01028                    _Is_Delim __is_delim, _Scan_Delim __scan_delim,
01029                    bool __extract_delim, bool __set_failbit) {
01030   streamsize __n = 0;
01031   bool __at_eof = false;
01032   bool __done   = false;
01033 
01034   _STLP_TRY {
01035     while (__buf->_M_egptr() != __buf->_M_gptr() && !__done) {
01036       ptrdiff_t __avail = __buf->_M_egptr() - __buf->_M_gptr();
01037       streamsize __m = __max_chars(_Num, __n);
01038 
01039       if (__avail >= __m) {       // We have more characters than we need.
01040         const _CharT* __last = __buf->_M_gptr() + __STATIC_CAST(ptrdiff_t, __m);
01041         const _CharT* __p = __scan_delim(__buf->_M_gptr(), __last);
01042         ptrdiff_t __chunk = __p - __buf->_M_gptr();
01043         __n += __chunk;
01044         __buf->_M_gbump((int)__chunk);
01045 
01046         if (__extract_delim && __p != __last) {
01047           __n += 1;
01048           __buf->_M_gbump(1);
01049         }
01050 
01051         __done = true;
01052       }
01053 
01054       else {
01055         const _CharT* __p = __scan_delim(__buf->_M_gptr(), __buf->_M_egptr());
01056         ptrdiff_t __chunk = __p - __buf->_M_gptr();
01057         __n += __chunk;
01058         __buf->_M_gbump((int)__chunk);
01059 
01060         if (__p != __buf->_M_egptr()) { // We found delim.
01061           if (__extract_delim) {
01062             __n += 1;
01063             __buf->_M_gbump(1);
01064           }
01065 
01066           __done = true;
01067         }
01068 
01069         // We didn't find delim.  Try to refill the buffer.
01070         else if (__that->_S_eof(__buf->sgetc())) {
01071           __done   = true;
01072           __at_eof = true;
01073         }
01074       }
01075     } // Close the while loop.
01076   }
01077   _STLP_CATCH_ALL {
01078     __that->_M_handle_exception(ios_base::badbit);
01079     return __n;
01080   }
01081 
01082   if (__at_eof)
01083     __that->setstate(__set_failbit ? ios_base::eofbit | ios_base::failbit
01084                                    : ios_base::eofbit);
01085 
01086   if (__done)
01087     return __n;
01088 
01089   // If execution has reached this point, then we have an empty buffer but
01090   // we have not reached eof.  What that means is that the streambuf has
01091   // decided to switch from buffered to unbuffered input.  We switch to
01092   // to _M_ignore_unbuffered.
01093 
01094   return __n + _M_ignore_unbuffered(__that,  __buf, _Num, __max_chars,
01095                                     __is_delim, __extract_delim, __set_failbit);
01096 }
01097 
01098 
01099 template <class _CharT, class _Traits>
01100 basic_istream<_CharT, _Traits>&
01101 basic_istream<_CharT, _Traits>::ignore(streamsize __n) {
01102   sentry __sentry(*this, _No_Skip_WS());
01103   this->_M_gcount = 0;
01104 
01105   if (__sentry) {
01106     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
01107     typedef _STLP_PRIV _Constant_unary_fun<bool, int_type> _Const_bool;
01108     typedef _STLP_PRIV _Constant_binary_fun<streamsize, streamsize, streamsize> _Const_streamsize;
01109     const streamsize __maxss = (numeric_limits<streamsize>::max)();
01110 
01111     if (__n == (numeric_limits<int>::max)()) {
01112       if (__buf->gptr() != __buf->egptr())
01113         _M_gcount = _M_ignore_buffered(this,  __buf,
01114                                        __maxss, _Const_streamsize(__maxss),
01115                                        _Const_bool(false),
01116                                        _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
01117                                        false, false);
01118       else
01119         _M_gcount = _M_ignore_unbuffered(this,  __buf,
01120                                          __maxss, _Const_streamsize(__maxss),
01121                                          _Const_bool(false), false, false);
01122     }
01123     else {
01124       if (__buf->gptr() != __buf->egptr())
01125         _M_gcount = _M_ignore_buffered(this,  __buf,
01126                                        __n, minus<streamsize>(),
01127                                        _Const_bool(false),
01128                                        _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
01129                                        false, false);
01130       else
01131         _M_gcount = _M_ignore_unbuffered(this,  __buf, __n, minus<streamsize>(),
01132                                          _Const_bool(false), false, false);
01133     }
01134   }
01135 
01136   return *this;
01137 }
01138 
01139 template <class _CharT, class _Traits>
01140 basic_istream<_CharT, _Traits>&
01141 basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __delim) {
01142   sentry __sentry(*this, _No_Skip_WS());
01143   this->_M_gcount = 0;
01144 
01145   if (__sentry) {
01146     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
01147     typedef _STLP_PRIV _Constant_unary_fun<bool, int_type> _Const_bool;
01148     typedef _STLP_PRIV _Constant_binary_fun<streamsize, streamsize, streamsize>
01149       _Const_streamsize;
01150     const streamsize __maxss = (numeric_limits<streamsize>::max)();
01151 
01152     if (__n == (numeric_limits<int>::max)()) {
01153       if (__buf->gptr() != __buf->egptr())
01154         _M_gcount = _M_ignore_buffered(this,  __buf,
01155                                        __maxss, _Const_streamsize(__maxss),
01156                                        _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
01157                                        _STLP_PRIV _Scan_for_int_val<_Traits>(__delim),
01158                                        true, false);
01159       else
01160         _M_gcount = _M_ignore_unbuffered(this,  __buf,
01161                                          __maxss, _Const_streamsize(__maxss),
01162                                          _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
01163                                          true, false);
01164     }
01165     else {
01166       if (__buf->gptr() != __buf->egptr())
01167         _M_gcount = _M_ignore_buffered(this,  __buf,
01168                                        __n, minus<streamsize>(),
01169                                        _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
01170                                        _STLP_PRIV _Scan_for_int_val<_Traits>(__delim),
01171                                        true, false);
01172       else
01173         _M_gcount = _M_ignore_unbuffered(this,  __buf, __n, minus<streamsize>(),
01174                                          _STLP_PRIV _Eq_int_bound<_Traits>(__delim),
01175                                          true, false);
01176     }
01177   }
01178 
01179   return *this;
01180 }
01181 
01182 // This member function does not construct a sentry object, because
01183 // it is called from sentry's constructor.
01184 template <class _CharT, class _Traits>
01185 void basic_istream<_CharT, _Traits>::_M_skip_whitespace(bool __set_failbit) {
01186   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
01187   if (!__buf)
01188     this->setstate(ios_base::badbit);
01189   else if (__buf->gptr() != __buf->egptr())
01190     _M_ignore_buffered(this,  __buf,
01191                        _STLP_PRIV _Is_not_wspace<_Traits>(__STATIC_CAST(const ctype<_CharT>*, this->_M_ctype_facet())),
01192                        _STLP_PRIV _Scan_for_not_wspace<_Traits>(__STATIC_CAST(const ctype<_CharT>*, this->_M_ctype_facet())),
01193                        false, __set_failbit);
01194   else
01195     _M_ignore_unbuffered(this,  __buf,
01196                          _STLP_PRIV _Is_not_wspace<_Traits>(__STATIC_CAST(const ctype<_CharT>*, this->_M_ctype_facet())),
01197                          false, __set_failbit);
01198 }
01199 
01200 
01201 // This is a very simple loop that reads characters from __src and puts
01202 // them into __dest.  It looks complicated because of the (standard-
01203 // mandated) exception handling policy.
01204 //
01205 // We stop when we get an exception, when we fail to insert into the
01206 // output streambuf, or when __is_delim is true.
01207 
01208 _STLP_MOVE_TO_PRIV_NAMESPACE
01209 
01210 template < class _CharT, class _Traits, class _Is_Delim>
01211 streamsize _STLP_CALL
01212 __copy_unbuffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __src,
01213                   basic_streambuf<_CharT, _Traits>* __dest,
01214                   _Is_Delim __is_delim,
01215                   bool __extract_delim, bool __rethrow) {
01216   streamsize __extracted = 0;
01217   ios_base::iostate __status = 0;
01218   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
01219   int_type __c;
01220 
01221   _STLP_TRY {
01222     for (;;) {
01223       // Get a character. If there's an exception, catch and (maybe) rethrow it.
01224       __c = __src->sbumpc();
01225 
01226       // If we failed to get a character, then quit.
01227       if (__that->_S_eof(__c)) {
01228         __status |= ios_base::eofbit;
01229         break;
01230       }
01231       // If it's the delimiter, then quit.
01232       else if (__is_delim(_Traits::to_char_type(__c))) {
01233         if (!__extract_delim && !__pushback(__src, _Traits::to_char_type(__c)))
01234           __status |= ios_base::failbit;
01235         break;
01236       }
01237       else {
01238         // Try to put the character in the output streambuf.
01239         bool __failed = false;
01240         _STLP_TRY {
01241           if (!__that->_S_eof(__dest->sputc(_Traits::to_char_type(__c))))
01242             ++__extracted;
01243           else
01244             __failed = true;
01245         }
01246         _STLP_CATCH_ALL {
01247           __failed = true;
01248         }
01249 
01250         // If we failed to put the character in the output streambuf, then
01251         // try to push it back to the input streambuf.
01252         if (__failed && !__pushback(__src, _Traits::to_char_type(__c)))
01253           __status |= ios_base::failbit;
01254 
01255         // fbp : avoiding infinite loop in io-27-6-1-2-3.exp
01256         if (__failed)
01257           break;
01258       }
01259 
01260     } /* for (;;) */
01261 
01262   }
01263   // fbp : this try/catch moved here in reasonable assumption
01264   // __is_delim never throw (__pushback is guaranteed not to)
01265   _STLP_CATCH_ALL {
01266     // See 27.6.1.2.3, paragraph 13.
01267     if (__rethrow && __extracted == 0)
01268       __that->_M_handle_exception(ios_base::failbit);
01269   }
01270   __that->setstate(__status);
01271   return __extracted;
01272 }
01273 
01274 // Buffered copying from one streambuf to another.  We copy the characters
01275 // in chunks, rather than one at a time.  We still have to worry about all
01276 // of the error conditions we checked in __copy_unbuffered, plus one more:
01277 // the streambuf might decide to switch from a buffered to an unbuffered mode.
01278 
01279 template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
01280 streamsize _STLP_CALL
01281 __copy_buffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __src,
01282                 basic_streambuf<_CharT, _Traits>* __dest,
01283                 _Scan_Delim __scan_delim, _Is_Delim __is_delim,
01284                 bool __extract_delim, bool __rethrow) {
01285   streamsize __extracted = 0;
01286   ios_base::iostate __status = 0;
01287   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
01288   //Borland compiler generates a warning if assignment because value is never used:
01289   int_type __c /*= _Traits::eof()*/;
01290   _CharT* __first = __src->_M_gptr();
01291   ptrdiff_t __avail = __src->_M_egptr() - __first;
01292   // fbp : introduced to move catch/try blocks out of the loop
01293   bool __do_handle_exceptions = false;
01294 
01295   _STLP_TRY {
01296     for (;;) {
01297       const _CharT* __last = __scan_delim(__first, __src->_M_egptr());
01298 
01299       // Try to copy the entire input buffer to the output buffer.
01300       streamsize __n = __dest->sputn(__first, __extract_delim && __last != __src->_M_egptr()
01301                                      ? (__last - __first) + 1
01302                                      : (__last - __first));
01303       __src->_M_gbump((int)__n);
01304       __extracted += __n;
01305 
01306       // from this on, catch() will call _M_handle_exceptions()
01307       __do_handle_exceptions = true;
01308 
01309       if (__n < __avail)          // We found the delimiter, or else failed to
01310         break;                    // copy some characters.
01311 
01312       __c = __src->sgetc();
01313 
01314       // Three possibilities: we succeeded in refilling the buffer, or
01315       // we got EOF, or the streambuf has switched to unbuffered mode.
01316       __first = __src->_M_gptr();
01317       __avail = __src->_M_egptr() - __first;
01318 
01319       if (__avail > 0)
01320         {}  // dwa 1/16/00 -- suppress a Metrowerks warning
01321       else if (__that->_S_eof(__c)) {
01322         __status |= ios_base::eofbit;
01323         break;
01324       }
01325       else {
01326         return __extracted + __copy_unbuffered(__that,  __src, __dest, __is_delim,
01327                                                 __extract_delim, __rethrow);
01328       }
01329 
01330       __do_handle_exceptions = false;
01331     }
01332   }
01333 
01334   _STLP_CATCH_ALL {
01335     // See 27.6.1.2.3, paragraph 13.
01336     if (__rethrow && __do_handle_exceptions &&  __extracted == 0)
01337       __that->_M_handle_exception(ios_base::failbit);
01338   }
01339 
01340   if (__status)
01341     __that->setstate(__status);   // This might throw.
01342   return __extracted;
01343 }
01344 
01345 _STLP_MOVE_TO_STD_NAMESPACE
01346 
01347 template <class _CharT, class _Traits>
01348 basic_istream<_CharT, _Traits>&
01349 basic_istream<_CharT, _Traits>
01350   ::get(basic_streambuf<_CharT, _Traits>& __dest, _CharT __delim) {
01351   sentry __sentry(*this, _No_Skip_WS());
01352   this->_M_gcount = 0;
01353 
01354   if (__sentry) {
01355     basic_streambuf<_CharT, _Traits>* __src = this->rdbuf();
01356 
01357     if (__src)
01358       this->_M_gcount = __src->egptr() != __src->gptr()
01359         ? _STLP_PRIV __copy_buffered(this,  __src, &__dest,
01360                                      _STLP_PRIV _Scan_for_char_val<_Traits>(__delim),
01361                                      _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
01362                                      false, false)
01363         : _STLP_PRIV __copy_unbuffered(this,  __src, &__dest,
01364                                        _STLP_PRIV _Eq_char_bound<_Traits>(__delim),
01365                                        false, false);
01366   }
01367 
01368   if (this->_M_gcount == 0)
01369     this->setstate(ios_base::failbit);
01370 
01371   return *this;
01372 }
01373 
01374 // Copying characters into a streambuf.
01375 template <class _CharT, class _Traits>
01376 basic_istream<_CharT, _Traits>&
01377 basic_istream<_CharT, _Traits>
01378   ::operator>>(basic_streambuf<_CharT, _Traits>* __dest) {
01379   streamsize __n = 0;
01380   typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
01381   _Sentry __sentry(*this);
01382   if (__sentry) {
01383     basic_streambuf<_CharT, _Traits>* __src = this->rdbuf();
01384     if (__src && __dest)
01385       __n = __src->egptr() != __src->gptr()
01386         ? _STLP_PRIV __copy_buffered(this,  __src, __dest,
01387                                      _STLP_PRIV _Project2nd<const _CharT*, const _CharT*>(),
01388                                      _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
01389                                      false, true)
01390         : _STLP_PRIV __copy_unbuffered(this,  __src, __dest,
01391                                        _STLP_PRIV _Constant_unary_fun<bool, int_type>(false),
01392                                        false, true);
01393   }
01394 
01395   if (__n == 0)
01396     this->setstate(ios_base::failbit);
01397 
01398   return *this;
01399 }
01400 
01401 // ----------------------------------------------------------------
01402 // basic_iostream<> class
01403 // ----------------------------------------------------------------
01404 
01405 template <class _CharT, class _Traits>
01406 basic_iostream<_CharT, _Traits>
01407   ::basic_iostream(basic_streambuf<_CharT, _Traits>* __buf)
01408     : basic_ios<_CharT, _Traits>(),
01409       basic_istream<_CharT, _Traits>(__buf),
01410       basic_ostream<_CharT, _Traits>(__buf) {
01411   this->init(__buf);
01412 }
01413 
01414 template <class _CharT, class _Traits>
01415 basic_iostream<_CharT, _Traits>::~basic_iostream()
01416 {}
01417 
01418 _STLP_END_NAMESPACE
01419 
01420 #undef __BIS_int_type__
01421 #undef __BIS_pos_type__
01422 #undef __BIS_off_type__
01423 
01424 #endif /* _STLP_ISTREAM_C */
01425 
01426 // Local Variables:
01427 // mode:C++
01428 // End:



Generated on Mon Mar 10 15:32:25 2008 by  doxygen 1.5.1