home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _istream.c < prev    next >
C/C++ Source or Header  |  2002-02-02  |  44KB  |  1,390 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18. #ifndef _STLP_ISTREAM_C
  19. #define _STLP_ISTREAM_C
  20.  
  21. #ifndef _STLP_INTERNAL_ISTREAM_H
  22. # include <stl/_istream.h>
  23. #endif
  24.  
  25. # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION)
  26.  
  27. #ifndef _STLP_LIMITS_H
  28. # include <stl/_limits.h>
  29. #endif
  30.  
  31. #ifndef _STLP_INTERNAL_NUM_GET_H
  32. # include <stl/_num_get.h>
  33. #endif
  34.  
  35. # if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
  36. // no wchar_t is supported for this mode
  37. # define __BIS_int_type__ int
  38. # define __BIS_pos_type__ streampos
  39. # define __BIS_off_type__ streamoff
  40. # else
  41. # define __BIS_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::int_type
  42. # define __BIS_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::pos_type
  43. # define __BIS_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::off_type
  44. # endif
  45.  
  46. _STLP_BEGIN_NAMESPACE
  47.  
  48. //----------------------------------------------------------------------
  49. // Function object structs used by some member functions.
  50.  
  51. template <class _Traits>
  52. struct _Is_not_wspace {
  53.   typedef typename _Traits::char_type argument_type;
  54.   typedef bool                        result_type;
  55.  
  56.   const ctype<argument_type>* _M_ctype;
  57.  
  58.   _Is_not_wspace(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {}
  59.   bool operator()(argument_type __c) const
  60.     { return !_M_ctype->is(ctype_base::space, __c); }
  61. };
  62.  
  63. template <class _Traits>
  64. struct _Is_wspace_null {
  65.   typedef typename _Traits::char_type argument_type;
  66.   typedef bool                        result_type;
  67.  
  68.   const ctype<argument_type>* _M_ctype;
  69.  
  70.   _Is_wspace_null(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {}
  71.   bool operator()(argument_type __c) const {
  72.     return _Traits::eq(__c, argument_type()) || 
  73.            _M_ctype->is(ctype_base::space, __c);
  74.   }
  75. };
  76.  
  77. template <class _Traits>
  78. struct _Scan_for_wspace {
  79.   typedef typename _Traits::char_type  char_type;
  80.   typedef char_type*                   first_argument_type;
  81.   typedef char_type*                   second_argument_type;
  82.   typedef char_type*                   result_type;
  83.  
  84.   const ctype<char_type>* _M_ctype;
  85.  
  86.   _Scan_for_wspace(const ctype<char_type>* __ctype) : _M_ctype(__ctype) {}
  87.   const char_type*
  88.   operator()(const char_type* __first, const char_type* __last) const {
  89.     return _M_ctype->scan_is(ctype_base::space, __first, __last);
  90.   }
  91. };
  92.  
  93. template <class _Traits>
  94. struct _Scan_wspace_null {
  95.   typedef typename _Traits::char_type  char_type;
  96.   typedef char_type*                   first_argument_type;
  97.   typedef char_type*                   second_argument_type;
  98.   typedef char_type*                   result_type;
  99.  
  100.   const ctype<char_type>* _M_ctype;
  101.  
  102.   _Scan_wspace_null(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {}
  103.   const char_type*
  104.   operator()(const char_type* __first, const char_type* __last) const {
  105.     __last = find_if(__first, __last,
  106.                      _Eq_char_bound<_Traits>(char_type()));
  107.     return _M_ctype->scan_is(ctype_base::space, __first, __last);
  108.   }
  109. };
  110.  
  111. template <class _Traits>
  112. struct _Scan_for_not_wspace {
  113.   typedef typename _Traits::char_type  char_type;
  114.   typedef char_type*                   first_argument_type;
  115.   typedef char_type*                   second_argument_type;
  116.   typedef char_type*                   result_type;
  117.  
  118.   const ctype<char_type>* _M_ctype;
  119.  
  120.   _Scan_for_not_wspace(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {}
  121.   const char_type*
  122.   operator()(const char_type* __first, const char_type* __last) const {
  123.     return _M_ctype->scan_not(ctype_base::space, __first, __last);
  124.   }
  125. };
  126.  
  127. template <class _Traits>
  128. struct _Scan_for_char_val
  129. {
  130.   typedef typename _Traits::char_type char_type;
  131.   typedef char_type*                  first_argument_type;
  132.   typedef char_type*                  second_argument_type;
  133.   typedef char_type*                  result_type;
  134.  
  135.   char_type _M_val;
  136.  
  137.   _Scan_for_char_val(char_type __val) : _M_val(__val) {}
  138.  
  139.   const char_type*
  140.   operator()(const char_type* __first, const char_type* __last) const {
  141.     return find_if(__first, __last, _Eq_char_bound<_Traits>(_M_val)); 
  142.   }
  143. };
  144.  
  145. template <class _Traits>
  146. struct _Scan_for_int_val
  147. {
  148.   typedef typename _Traits::char_type char_type;
  149.   typedef typename _Traits::int_type  int_type;
  150.   typedef char_type*                  first_argument_type;
  151.   typedef char_type*                  second_argument_type;
  152.   typedef char_type*                  result_type;
  153.  
  154.   int_type _M_val;
  155.  
  156.   _Scan_for_int_val(int_type __val) : _M_val(__val) {}
  157.  
  158.   const char_type*
  159.   operator()(const char_type* __first, const char_type* __last) const {
  160.     return find_if(__first, __last,
  161.                    _Eq_int_bound<_Traits>(_M_val));
  162.   }
  163. };
  164.  
  165. // Helper function: try to push back a character to a streambuf, 
  166. // return true if the pushback succeeded.  Does not throw.
  167.  
  168. template <class _CharT, class _Traits> 
  169. bool _STLP_CALL
  170. __pushback(basic_streambuf<_CharT, _Traits>* __buf, _CharT __c)
  171. {
  172.   bool ret;
  173.   _STLP_TRY {
  174.     const typename _Traits::int_type __eof = _Traits::eof();
  175.     ret = !_Traits::eq_int_type(__buf->sputbackc(__c), __eof);
  176.   }
  177.   _STLP_CATCH_ALL {
  178.     ret = false;
  179.   }
  180.   return ret;
  181. }
  182.  
  183. template <class _CharT, class _Traits>
  184. basic_istream<_CharT, _Traits>& _STLP_CALL
  185. ws(basic_istream<_CharT, _Traits>& __is)
  186. {
  187.   typedef typename basic_istream<_CharT, _Traits>::sentry      _Sentry;
  188.   _Sentry __sentry(__is, _No_Skip_WS()); // Don't skip whitespace.
  189.   if (__sentry)
  190.     __is._M_skip_whitespace(false);
  191.   return __is;
  192. }
  193.  
  194. // Helper functions for istream<>::sentry constructor.
  195. template <class _CharT, class _Traits>
  196. bool
  197. _M_init_skip(basic_istream<_CharT, _Traits>& __is) {
  198.   if (__is.good()) {
  199.     if (__is.tie())
  200.       __is.tie()->flush();
  201.     
  202.     __is._M_skip_whitespace(true);
  203.   }
  204.   
  205.   if (!__is.good()) {
  206.     __is.setstate(ios_base::failbit);
  207.     return false;
  208.   } else
  209.     return true;
  210. }
  211.  
  212. template <class _CharT, class _Traits>
  213. bool
  214. _M_init_noskip(basic_istream<_CharT, _Traits>& __is){
  215.   if (__is.good()) {
  216.     if (__is.tie())
  217.       __is.tie()->flush();
  218.     
  219.     if (!__is.rdbuf())
  220.       __is.setstate(ios_base::badbit);
  221.   }
  222.   else
  223.     __is.setstate(ios_base::failbit);
  224.   return __is.good();
  225. }
  226.  
  227. //----------------------------------------------------------------------
  228. // Definitions of basic_istream<>'s noninline member functions.
  229.  
  230. // Helper function for formatted input of numbers.
  231. template <class _CharT, class _Traits, class _Number> 
  232. ios_base::iostate _STLP_CALL 
  233. _M_get_num(basic_istream<_CharT, _Traits>& __that, _Number& __val)
  234. {
  235.   typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
  236.   ios_base::iostate __err = 0;
  237.   _Sentry __sentry( __that );     // Skip whitespace.
  238.   if (__sentry) {
  239.     typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> > _Num_get;
  240.     _STLP_TRY {
  241.       ((const _Num_get&)use_facet<_Num_get>(__that.getloc())).get(istreambuf_iterator<_CharT, _Traits>(__that.rdbuf()),
  242.                     0, __that, __err, __val);
  243.     }
  244.     _STLP_CATCH_ALL {
  245.       __that._M_handle_exception(ios_base::badbit);
  246.     }
  247.     if (__err) __that.setstate(__err);
  248.   }
  249.   return __err;
  250. }
  251.  
  252.  
  253. // Unformatted input
  254.  
  255. template <class _CharT, class _Traits>
  256. __BIS_int_type__
  257. basic_istream<_CharT, _Traits>::peek()
  258. {
  259.   typename _Traits::int_type __tmp = _Traits::eof();
  260.  
  261.   this->_M_gcount = 0;
  262.   sentry __sentry(*this, _No_Skip_WS());
  263.  
  264.   if (__sentry) {
  265.     _STLP_TRY {
  266.       __tmp = this->rdbuf()->sgetc();
  267.     }
  268.     _STLP_CATCH_ALL {
  269.       this->_M_handle_exception(ios_base::badbit);
  270.     }
  271.     if (this->_S_eof(__tmp))
  272.       this->setstate(ios_base::eofbit);
  273.   }
  274.  
  275.   return __tmp;
  276. }
  277.  
  278.  
  279. template <class _CharT, class _Traits>
  280. __BIS_int_type__
  281. basic_istream<_CharT, _Traits>::get()
  282. {
  283.   typename _Traits::int_type __tmp = _Traits::eof();
  284.   sentry __sentry(*this, _No_Skip_WS());
  285.   this->_M_gcount = 0;
  286.  
  287.   if (__sentry) {
  288.     _STLP_TRY {
  289.       __tmp = this->rdbuf()->sbumpc();
  290.     }
  291.     _STLP_CATCH_ALL {
  292.       this->_M_handle_exception(ios_base::badbit);
  293.     }
  294.  
  295.     if (!this->_S_eof(__tmp))
  296.       this->_M_gcount = 1;
  297.   }
  298.  
  299.   if (_M_gcount == 0)
  300.     this->setstate(ios_base::eofbit | ios_base::failbit);
  301.  
  302.   return __tmp;
  303. }
  304.  
  305. template <class _CharT, class _Traits>
  306. basic_istream<_CharT, _Traits>& 
  307. basic_istream<_CharT, _Traits>::get(_CharT& __c)
  308. {
  309.   sentry __sentry(*this, _No_Skip_WS());
  310.   this->_M_gcount = 0;
  311.  
  312.   if (__sentry) {
  313.     typename _Traits::int_type __tmp = _Traits::eof();
  314.     _STLP_TRY {
  315.       __tmp = this->rdbuf()->sbumpc();
  316.     }
  317.     _STLP_CATCH_ALL {
  318.       this->_M_handle_exception(ios_base::badbit);
  319.     }
  320.  
  321.     if (!this->_S_eof(__tmp)) {
  322.       this->_M_gcount = 1;
  323.       __c = _Traits::to_char_type(__tmp);
  324.     }
  325.   }
  326.  
  327.   if (this->_M_gcount == 0)
  328.     this->setstate(ios_base::eofbit | ios_base::failbit);
  329.  
  330.   return *this;
  331. }
  332.  
  333.  
  334.  
  335. // Read characters and discard them.  The standard specifies a single
  336. // function with two arguments, each with a default.  We instead use
  337. // three overloded functions, because it's possible to implement the
  338. // first two more efficiently than the fully general third version.
  339. template <class _CharT, class _Traits>
  340. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore()
  341. {
  342.   sentry __sentry(*this, _No_Skip_WS());
  343.   this->_M_gcount = 0;
  344.  
  345.   if (__sentry) {
  346.     int_type __c;
  347.     _STLP_TRY {
  348.       __c = this->rdbuf()->sbumpc();
  349.     }
  350.     _STLP_CATCH_ALL {
  351.       this->_M_handle_exception(ios_base::badbit);
  352.       return *this;
  353.     }
  354.  
  355.     if (!this->_S_eof(__c))
  356.       this->_M_gcount = 1;
  357.     else
  358.       this->setstate(ios_base::eofbit);
  359.   }
  360.  
  361.   return *this;
  362. }
  363.  
  364. // Putback
  365.  
  366. template <class _CharT, class _Traits>
  367. basic_istream<_CharT, _Traits>& 
  368. basic_istream<_CharT, _Traits>::putback(_CharT __c) {
  369.   this->_M_gcount = 0;
  370.   sentry __sentry(*this, _No_Skip_WS());
  371.  
  372.   if (__sentry) {
  373.     typename _Traits::int_type __tmp = _Traits::eof();
  374.     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  375. //    if (!__buf || this->_S_eof(__buf->sputbackc(__c)))
  376.     if (__buf) {
  377.       _STLP_TRY {
  378.         __tmp = __buf->sputbackc(__c);
  379.       }
  380.       _STLP_CATCH_ALL {
  381.         this->_M_handle_exception(ios_base::badbit);
  382.       }
  383.     }
  384.     if (this->_S_eof(__tmp))
  385.       this->setstate(ios_base::badbit);
  386.   }
  387.   else
  388.     this->setstate(ios_base::failbit);
  389.  
  390.   return *this;
  391. }
  392.  
  393. template <class _CharT, class _Traits>
  394. basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
  395.   this->_M_gcount = 0;
  396.  
  397.   sentry __sentry(*this, _No_Skip_WS());
  398.  
  399.   if (__sentry) {
  400.     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  401.     //     if (!__buf || _Traits::eq_int_type(__buf->sungetc(), _Traits::eof()))
  402.     if (__buf) {
  403.       _STLP_TRY {
  404.         _CharT __tmp;
  405.         __tmp = __buf->sungetc();
  406.         if (this->_S_eof(__tmp))
  407.           this->setstate(ios_base::badbit);
  408.       }
  409.       _STLP_CATCH_ALL {
  410.         this->_M_handle_exception(ios_base::badbit);
  411.       }
  412.     } else
  413.       this->setstate(ios_base::badbit);    
  414.   }
  415.   else
  416.     this->setstate(ios_base::failbit);
  417.  
  418.   return *this;
  419. }
  420.  
  421. // Positioning and buffer control.
  422.  
  423. template <class _CharT, class _Traits>
  424. int basic_istream<_CharT, _Traits>::sync() {
  425.   sentry __sentry(*this, _No_Skip_WS());
  426.  
  427.   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  428.   if (__buf) {
  429.     if (__buf->pubsync() == -1) {
  430.       this->setstate(ios_base::badbit);
  431.       return -1;
  432.     }
  433.     else
  434.       return 0;
  435.   }
  436.   else
  437.     return -1;
  438. }
  439.  
  440. template <class _CharT, class _Traits>
  441. __BIS_pos_type__
  442. basic_istream<_CharT, _Traits>::tellg() {
  443.   //   sentry __sentry(*this, _No_Skip_WS());
  444.   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  445.   return (__buf && !this->fail()) ? __buf->pubseekoff(0, ios_base::cur, ios_base::in)
  446.     : pos_type(-1);
  447. }
  448.  
  449. template <class _CharT, class _Traits>
  450. basic_istream<_CharT, _Traits>&
  451. basic_istream<_CharT, _Traits>::seekg(pos_type __pos) {
  452.   sentry __sentry(*this, _No_Skip_WS());
  453.  
  454.   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  455.   if (!this->fail() && __buf)
  456.     __buf->pubseekpos(__pos, ios_base::in);
  457.   return *this;
  458. }
  459.  
  460. template <class _CharT, class _Traits>
  461. basic_istream<_CharT, _Traits>&
  462. basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
  463. {
  464.   sentry __sentry(*this, _No_Skip_WS());
  465.  
  466.   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  467.   if (!this->fail() && __buf)
  468.     __buf->pubseekoff(__off, __dir, ios_base::in);
  469.   return *this;
  470. }
  471.  
  472. // Formatted input of characters and character arrays.
  473.  
  474. template <class _CharT, class _Traits>
  475. void basic_istream<_CharT, _Traits>::_M_formatted_get(_CharT& __c)
  476. {
  477. //  typename _Traits::int_type __tmp = _Traits::eof();
  478.  
  479.   sentry __sentry(*this); // Skip whitespace.
  480.  
  481.   if (__sentry) {
  482.     typename _Traits::int_type __tmp = _Traits::eof();
  483.     
  484.     _STLP_TRY {
  485.       __tmp = this->rdbuf()->sbumpc();
  486.     }
  487.     _STLP_CATCH_ALL {
  488.       this->_M_handle_exception(ios_base::badbit);
  489.       return;
  490.     }
  491.  
  492.     if (!this->_S_eof(__tmp))
  493.       __c = _Traits::to_char_type(__tmp);
  494.     else 
  495.       this->setstate(ios_base::eofbit | ios_base::failbit);
  496.   }
  497. }
  498.  
  499.  
  500. //---------------------------------------------------------------------------
  501. // istream's helper functions.
  502.  
  503. // A generic function for unbuffered input.  We stop when we reach EOF,
  504. // or when we have extracted _Num characters, or when the function object
  505. // __is_delim return true.  In the last case, it extracts the character
  506. // for which __is_delim is true, if and only if __extract_delim is true. 
  507. // It appends a null character to the end of the string; this means that
  508. // it may store up to _Num + 1 characters.  
  509. //
  510. // __is_getline governs two corner cases: reading _Num characters without
  511. // encountering delim or eof (in which case failbit is set if __is_getline
  512. // is true); and reading _Num characters where the _Num+1'st character is
  513. // eof (in which case eofbit is set if __is_getline is true).
  514. //
  515. // It is assumed that __is_delim never throws.
  516. //
  517. // Return value is the number of characters extracted, including the
  518. // delimiter if it is extracted.  Note that the number of characaters
  519. // extracted isn't necessarily the same as the number stored.
  520.  
  521. template < class _CharT, class _Traits, class _Is_Delim>
  522. streamsize _STLP_CALL 
  523. _M_read_unbuffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __buf,
  524.                        streamsize _Num, _CharT* __s,
  525.                        _Is_Delim __is_delim,
  526.                        bool __extract_delim, bool __append_null,
  527.                        bool __is_getline)
  528. {
  529.   streamsize __n = 0;
  530.   ios_base::iostate __status = 0;
  531.   
  532.   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  533.   // The operations that can potentially throw are sbumpc, snextc, and sgetc.
  534.   _STLP_TRY {
  535.     int_type __c = __buf->sgetc();
  536.     while (true) {
  537.       if (__that->_S_eof(__c)) {
  538.         if (__n < _Num || __is_getline)
  539.           __status |= ios_base::eofbit;
  540.         break;
  541.       }
  542.  
  543.       else if (__is_delim(__c)) {
  544.         if (__extract_delim) {  // Extract and discard current character.
  545.           __buf->sbumpc();
  546.           ++__n;
  547.         }
  548.         break;
  549.       }
  550.  
  551.       else if (__n == _Num) {
  552.         if (__is_getline)
  553.           __status |= ios_base::failbit;
  554.         break;
  555.       }
  556.         
  557.       *__s++ = _Traits::to_char_type(__c);
  558.       ++__n;
  559.       __c = __buf->snextc();
  560.     }
  561.   }
  562.   _STLP_CATCH_ALL {
  563.     __that->_M_handle_exception(ios_base::badbit);
  564.     *__s = _STLP_DEFAULT_CONSTRUCTED(_CharT);
  565.     return __n;
  566.   }
  567.  
  568.   if (__append_null)
  569.     *__s =  _STLP_DEFAULT_CONSTRUCTED(_CharT);
  570.   if (__status)
  571.     __that->setstate(__status);    // This might throw.
  572.   return __n;
  573. }
  574.  
  575. // Much like _M_read_unbuffered, but with one additional function object:
  576. // __scan_delim(first, last) returns the first pointer p in [first, last)
  577. // such that __is_delim(p) is true.  
  578.  
  579. template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
  580. streamsize _STLP_CALL 
  581. _M_read_buffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __buf,
  582.                      streamsize _Num, _CharT* __s,
  583.                      _Is_Delim __is_delim, _Scan_Delim __scan_delim,
  584.                      bool __extract_delim, bool __append_null,
  585.                      bool __is_getline)
  586. {
  587.   streamsize __n = 0;
  588.   ios_base::iostate __status = 0;
  589.   bool __done    = false;
  590.  
  591.   _STLP_TRY {
  592.     while (__buf->_M_egptr() != __buf->_M_gptr() && !__done) {
  593.       const _CharT* __first = __buf->_M_gptr();
  594.       const _CharT* __last  = __buf->_M_egptr();
  595.       ptrdiff_t __request = _Num - __n;
  596.  
  597.       const _CharT* __p  = __scan_delim(__first, __last);
  598.       ptrdiff_t __chunk = (min) (ptrdiff_t(__p - __first), __request);
  599.       _Traits::copy(__s, __first, __chunk);
  600.       __s += __chunk;
  601.       __n += __chunk;
  602.       __buf->_M_gbump((int)__chunk);
  603.  
  604.       // We terminated by finding delim.
  605.       if (__p != __last && __p - __first <= __request) {
  606.         if (__extract_delim) {
  607.           __n += 1;
  608.           __buf->_M_gbump(1);
  609.         }
  610.         __done = true;
  611.       }
  612.  
  613.       // We terminated by reading all the characters we were asked for.
  614.       else if(__n == _Num) {
  615.  
  616.         // Find out if we have reached eof.  This matters for getline.
  617.         if (__is_getline) {
  618.           if (__chunk == __last - __first) {
  619.             if (__that->_S_eof(__buf->sgetc()))
  620.               __status |= ios_base::eofbit;            
  621.           }
  622.           else
  623.             __status |= ios_base::failbit;
  624.         }
  625.         __done   = true;
  626.       }
  627.  
  628.       // The buffer contained fewer than _Num - __n characters.  Either we're
  629.       // at eof, or we should refill the buffer and try again.
  630.       else {
  631.     if (__that->_S_eof(__buf->sgetc())) {
  632.           __status |= ios_base::eofbit;
  633.           __done = true;
  634.         }
  635.       }
  636.     } // Close the while loop.
  637.   }
  638.   _STLP_CATCH_ALL {
  639.     __that->_M_handle_exception(ios_base::badbit);
  640.     __done = true;
  641.   }
  642.  
  643.   if (__done) {
  644.     if (__append_null)
  645.         *__s =  _STLP_DEFAULT_CONSTRUCTED(_CharT);
  646.     if (__status != 0)
  647.       __that->setstate(__status);   // This might throw.
  648.     return __n;
  649.   }
  650.  
  651.   // If execution has reached this point, then we have an empty buffer but
  652.   // we have not reached eof.  What that means is that the streambuf has
  653.   // decided to switch from buffered to unbuffered input.  We switch to
  654.   // to _M_read_unbuffered.
  655.  
  656.   return __n + _M_read_unbuffered(__that,  __buf, _Num - __n, __s, __is_delim,
  657.                                   __extract_delim,__append_null,__is_getline);
  658. }
  659.  
  660.  
  661.  
  662.  
  663. template <class _CharT, class _Traits>
  664. basic_istream<_CharT, _Traits>&
  665. basic_istream<_CharT, _Traits>::get(_CharT* __s, streamsize __n,
  666.                                     _CharT __delim) {
  667.   sentry __sentry(*this, _No_Skip_WS());
  668.   this->_M_gcount = 0;
  669.  
  670.   if (__sentry) {
  671.     if (__n > 0) {
  672.       basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  673.  
  674.       if (__buf->egptr() != __buf->gptr())
  675.         this->_M_gcount =
  676.           _M_read_buffered(this,  __buf, __n - 1, __s,
  677.                            _Eq_char_bound<_Traits>(__delim),
  678.                            _Scan_for_char_val<_Traits>(__delim),
  679.                            false, true, false);
  680.       else
  681.         this->_M_gcount =
  682.           _M_read_unbuffered(this,  __buf, __n - 1, __s,
  683.                              _Eq_char_bound<_Traits>(__delim),
  684.                              false, true, false);
  685.     }
  686.   }
  687.  
  688.   if (this->_M_gcount == 0)
  689.     this->setstate(ios_base::failbit);
  690.  
  691.   return *this;
  692. }
  693.  
  694. // Getline is essentially identical to get, except that it extracts
  695. // the delimiter.
  696. template <class _CharT, class _Traits>
  697. basic_istream<_CharT, _Traits>&
  698. basic_istream<_CharT, _Traits>::getline(_CharT* __s, streamsize __n,
  699.                                         _CharT __delim) {
  700.   sentry __sentry(*this, _No_Skip_WS());
  701.   this->_M_gcount = 0;
  702.  
  703.   if (__sentry) {
  704.     if (__n > 0) {
  705.       basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  706.       this->_M_gcount = __buf->egptr() != __buf->gptr()
  707.         ? _M_read_buffered(this,  __buf, __n - 1, __s,
  708.                            _Eq_char_bound<_Traits>(__delim),
  709.                            _Scan_for_char_val<_Traits>(__delim),
  710.                            true, true, true)
  711.         : _M_read_unbuffered(this,  __buf, __n - 1, __s,
  712.                              _Eq_char_bound<_Traits>(__delim),
  713.                              true, true, true);
  714.     }
  715.   }
  716.  
  717.   if (this->_M_gcount == 0)
  718.     this->setstate(ios_base::failbit);
  719.  
  720.   return *this;
  721. }
  722.  
  723. // Read n characters.  We don't look for any delimiter, and we don't
  724. // put in a terminating null character.
  725. template <class _CharT, class _Traits>
  726. basic_istream<_CharT, _Traits>&
  727. basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
  728. {
  729.   sentry __sentry(*this, _No_Skip_WS());
  730.   this->_M_gcount = 0;
  731.  
  732.   if (__sentry && !this->eof()) {
  733.     basic_streambuf<_CharT, _Traits>*__buf = this->rdbuf();
  734.     if (__buf->gptr() != __buf->egptr()) 
  735.       _M_gcount
  736.         = _M_read_buffered(this,  __buf, __n, __s,
  737.                            _Constant_unary_fun<bool, int_type>(false),
  738.                            _Project2nd<const _CharT*, const _CharT*>(),
  739.                            false, false, false);
  740.     else
  741.       _M_gcount
  742.         = _M_read_unbuffered(this,  __buf, __n, __s,
  743.                              _Constant_unary_fun<bool, int_type>(false),
  744.                              false, false, false);
  745.   }
  746.   else
  747.     this->setstate(ios_base::failbit);
  748.  
  749.   if (this->eof())
  750.     this->setstate(ios_base::eofbit | ios_base::failbit);
  751.  
  752.   return *this;
  753. }
  754.  
  755.  
  756. // Read n or fewer characters.  We don't look for any delimiter, and
  757. // we don't put in a terminating null character.
  758. template <class _CharT, class _Traits>
  759. streamsize
  760. basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __nmax)
  761. {
  762.   sentry __sentry(*this, _No_Skip_WS());
  763.   this->_M_gcount = 0;
  764.  
  765.   if (__sentry && !this->eof() && __nmax >= 0) {
  766.  
  767.     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  768.     streamsize __avail = __buf->in_avail();
  769.  
  770.     // fbp : isn't full-blown setstate required here ?
  771.     if (__avail == -1)
  772.       this->_M_setstate_nothrow(ios_base::eofbit);
  773.  
  774.     else if (__avail != 0) {
  775.  
  776.       if (__buf->gptr() != __buf->egptr()) 
  777.         _M_gcount
  778.           = _M_read_buffered(this,  __buf, (min) (__avail, __nmax), __s,
  779.                              _Constant_unary_fun<bool, int_type>(false),
  780.                              _Project2nd<const _CharT*, const _CharT*>(),
  781.                              false, false, false);
  782.       else
  783.         _M_gcount
  784.           = _M_read_unbuffered(this,  __buf, (min) (__avail, __nmax), __s,
  785.                                _Constant_unary_fun<bool, int_type>(false),
  786.                                false, false, false);
  787.     }
  788.   }
  789.   else {
  790.     // fbp : changed so that failbit is set only there, to pass Dietmar's test
  791.     if (this->eof())
  792.       this->setstate(ios_base::eofbit | ios_base::failbit);
  793.     else
  794.       this->setstate(ios_base::failbit);
  795.   }
  796.  
  797.   //  if (this->eof())
  798.   //    this->setstate(ios_base::eofbit | ios_base::failbit);
  799.   
  800.   return _M_gcount;
  801. }
  802.  
  803. template <class _CharT, class _Traits>
  804. void basic_istream<_CharT, _Traits>::_M_formatted_get(_CharT* __s)
  805. {
  806.   sentry __sentry(*this); // Skip whitespace.
  807.  
  808.   if (__sentry) {
  809.     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  810.     streamsize __nmax = this->width() > 0
  811.       ? this->width() - 1
  812.       : (numeric_limits<streamsize>::max)() / sizeof(_CharT) - 1;
  813.  
  814.     streamsize __n = __buf->gptr() != __buf->egptr()
  815.       ? _M_read_buffered(this,  __buf, __nmax, __s,
  816.                          _Is_wspace_null<_Traits>((const ctype<_CharT>*)this->_M_ctype_facet()),
  817.                          _Scan_wspace_null<_Traits>((const ctype<_CharT>*)this->_M_ctype_facet()),
  818.              false, true, false)
  819.       : _M_read_unbuffered(this,  __buf, __nmax, __s,
  820.                            _Is_wspace_null<_Traits>((const ctype<_CharT>*)this->_M_ctype_facet()),
  821.                false, true, false);
  822.     if (__n == 0)
  823.       this->setstate(ios_base::failbit);
  824.   }
  825.   this->width(0);
  826. }
  827.  
  828. // A generic unbuffered function for ignoring characters.  We stop
  829. // when we reach EOF, or when the function object __is_delim returns
  830. // true.  In the last case, it extracts the character for which
  831. // __is_delim is true, if and only if __extract_delim is true.
  832.  
  833. template < class _CharT, class _Traits, class _Is_Delim>
  834. void _STLP_CALL 
  835. _M_ignore_unbuffered(basic_istream<_CharT, _Traits>* __that, 
  836.              basic_streambuf<_CharT, _Traits>* __buf,
  837.              _Is_Delim __is_delim,
  838.              bool __extract_delim, bool __set_failbit)
  839. {
  840.   bool __done = false;
  841.   ios_base::iostate __status = 0;
  842.   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  843.  
  844.   _STLP_TRY {
  845.     while (!__done) {
  846.       int_type __c = __buf->sbumpc();
  847.  
  848.       if (__that->_S_eof(__c)) {
  849.         __done = true;
  850.         __status |= __set_failbit ? ios_base::eofbit | ios_base::failbit
  851.                                   : ios_base::eofbit;
  852.       }
  853.  
  854.       else if (__is_delim(__c)) {
  855.         __done = true;
  856.         if (!__extract_delim)
  857.           if (__that->_S_eof(__buf->sputbackc(_Traits::to_char_type(__c))))
  858.             __status |= ios_base::failbit;
  859.       }
  860.     }
  861.   }
  862.   _STLP_CATCH_ALL {
  863.     __that->_M_handle_exception(ios_base::badbit);
  864.   }
  865.  
  866.   __that->setstate(__status);
  867. }
  868.  
  869. // A generic buffered function for ignoring characters.  Much like
  870. // _M_ignore_unbuffered, but with one additional function object:
  871. // __scan_delim(first, last) returns the first pointer p in [first,
  872. // last) such that __is_delim(p) is true.
  873.  
  874. template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
  875. void _STLP_CALL 
  876. _M_ignore_buffered(basic_istream<_CharT, _Traits>* __that, 
  877.            basic_streambuf<_CharT, _Traits>* __buf,
  878.            _Is_Delim __is_delim, _Scan_Delim __scan_delim,
  879.            bool __extract_delim, bool __set_failbit)
  880. {
  881.   bool __at_eof      = false;
  882.   bool __found_delim = false;
  883.  
  884.   _STLP_TRY {
  885.     while (__buf->_M_egptr() != __buf->_M_gptr() && !__at_eof && !__found_delim) {
  886.       const _CharT* __p = __scan_delim(__buf->_M_gptr(), __buf->_M_egptr());
  887.       __buf->_M_gbump((int)(__p - __buf->_M_gptr()));
  888.     
  889.       if (__p != __buf->_M_egptr()) { // We found delim, so we're done.
  890.         if (__extract_delim)
  891.           __buf->_M_gbump(1);
  892.         __found_delim = true;
  893.       }
  894.  
  895.       else                         // No delim.  Try to refil the buffer.
  896.         __at_eof = __that->_S_eof(__buf->sgetc());
  897.     }                              // Close the while loop.
  898.   }
  899.   _STLP_CATCH_ALL {
  900.     __that->_M_handle_exception(ios_base::badbit);
  901.     return;
  902.   }
  903.  
  904.   if (__at_eof) {
  905.     __that->setstate(__set_failbit ? ios_base::eofbit | ios_base::failbit
  906.                                    : ios_base::eofbit);
  907.     return;
  908.   }
  909.   if (__found_delim)
  910.     return;
  911.  
  912.   // If execution has reached this point, then we have an empty buffer but
  913.   // we have not reached eof.  What that means is that the streambuf has
  914.   // decided to switch from a buffered to an unbuffered mode.  We switch 
  915.   // to _M_ignore_unbuffered.
  916.   _M_ignore_unbuffered(__that,  __buf, __is_delim, __extract_delim, __set_failbit);
  917. }
  918.  
  919. // Overloaded versions of _M_ignore_unbuffered and _M_ignore_unbuffered 
  920. // with an explicit count _Num.  Return value is the number of
  921. // characters extracted.
  922. //
  923. // The function object __max_chars takes two arguments, _Num and __n
  924. // (the latter being the number of characters we have already read), 
  925. // and returns the maximum number of characters to read from the buffer.
  926. // We parameterize _M_ignore_buffered so that we can use it for both
  927. // bounded and unbounded input; for the former the function object should
  928. // be minus<>, and for the latter it should return a constant maximum value.
  929.  
  930. template < class _CharT, class _Traits, class _Max_Chars, class _Is_Delim>
  931. streamsize _STLP_CALL 
  932. _M_ignore_unbuffered(basic_istream<_CharT, _Traits>* __that,
  933.              basic_streambuf<_CharT, _Traits>* __buf,
  934.              streamsize _Num, _Max_Chars __max_chars,
  935.              _Is_Delim __is_delim,
  936.              bool __extract_delim, bool __set_failbit)
  937. {
  938.   streamsize __n = 0;
  939.   ios_base::iostate __status = 0;
  940.   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  941.   
  942.   _STLP_TRY {
  943.     while (__max_chars(_Num, __n) > 0) {
  944.       int_type __c = __buf->sbumpc();
  945.       
  946.       if (__that->_S_eof(__c)) {
  947.         __status |= __set_failbit ? ios_base::eofbit | ios_base::failbit
  948.                                   : ios_base::eofbit;
  949.         break;
  950.       }
  951.  
  952.       else if (__is_delim(__c)) {
  953.         if (__extract_delim)
  954.           ++__n;
  955.         else if (__that->_S_eof(__buf->sputbackc(_Traits::to_char_type(__c))))
  956.           __status |= ios_base::failbit;
  957.  
  958.         break;
  959.       }
  960.       // fbp : added counter increment to pass Dietmar's test
  961.       ++__n;
  962.     }
  963.   }
  964.   _STLP_CATCH_ALL {
  965.     __that->_M_handle_exception(ios_base::badbit);
  966.   }
  967.  
  968.   if (__status)
  969.     __that->setstate(__status);   // This might throw.
  970.   return __n;
  971. }
  972.  
  973. template < class _CharT, class _Traits, class _Max_Chars, class _Is_Delim, class _Scan_Delim>
  974. streamsize _STLP_CALL 
  975. _M_ignore_buffered(basic_istream<_CharT, _Traits>* __that,
  976.            basic_streambuf<_CharT, _Traits>* __buf,
  977.            streamsize _Num,
  978.            _Max_Chars __max_chars,
  979.            _Is_Delim __is_delim, _Scan_Delim __scan_delim,
  980.            bool __extract_delim, bool __set_failbit)
  981. {
  982.   streamsize __n = 0;
  983.   bool __at_eof = false;
  984.   bool __done   = false;
  985.  
  986.   _STLP_TRY {
  987.     while (__buf->_M_egptr() != __buf->_M_gptr() && !__done) {
  988.       ptrdiff_t __avail = __buf->_M_egptr() - __buf->_M_gptr();
  989.       streamsize __m = __max_chars(_Num, __n);
  990.     
  991.       if (__avail >= __m) {       // We have more characters than we need.
  992.         const _CharT* __last = __buf->_M_gptr() + __m;
  993.         const _CharT* __p = __scan_delim(__buf->_M_gptr(), __last);
  994.         ptrdiff_t __chunk = __p - __buf->_M_gptr();
  995.         __n += __chunk;
  996.         __buf->_M_gbump((int)__chunk);
  997.  
  998.         if (__extract_delim && __p != __last) {
  999.           __n += 1;
  1000.           __buf->_M_gbump(1);
  1001.         }
  1002.  
  1003.         __done = true;
  1004.       }
  1005.  
  1006.       else {
  1007.         const _CharT* __p = __scan_delim(__buf->_M_gptr(), __buf->_M_egptr());
  1008.         ptrdiff_t __chunk = __p - __buf->_M_gptr();
  1009.         __n += __chunk;
  1010.         __buf->_M_gbump((int)__chunk);
  1011.  
  1012.         if (__p != __buf->_M_egptr()) { // We found delim.
  1013.           if (__extract_delim) {
  1014.             __n += 1;
  1015.             __buf->_M_gbump(1);
  1016.           }
  1017.  
  1018.           __done = true;
  1019.         }
  1020.  
  1021.         // We didn't find delim.  Try to refill the buffer.
  1022.         else if (__that->_S_eof(__buf->sgetc())) {
  1023.           __done   = true;
  1024.           __at_eof = true;
  1025.         } 
  1026.       }
  1027.     } // Close the while loop.
  1028.   }
  1029.   _STLP_CATCH_ALL {
  1030.     __that->_M_handle_exception(ios_base::badbit);
  1031.     return __n;
  1032.   }
  1033.  
  1034.   if (__at_eof)
  1035.     __that->setstate(__set_failbit ? ios_base::eofbit | ios_base::failbit
  1036.                                    : ios_base::eofbit);
  1037.  
  1038.   if (__done)
  1039.     return __n;
  1040.  
  1041.   // If execution has reached this point, then we have an empty buffer but
  1042.   // we have not reached eof.  What that means is that the streambuf has
  1043.   // decided to switch from buffered to unbuffered input.  We switch to
  1044.   // to _M_ignore_unbuffered.
  1045.  
  1046.   return __n + _M_ignore_unbuffered( __that,  __buf, _Num, __max_chars,
  1047.                                     __is_delim, __extract_delim, __set_failbit);
  1048. }
  1049.  
  1050.  
  1051. template <class _CharT, class _Traits>
  1052. basic_istream<_CharT, _Traits>&
  1053. basic_istream<_CharT, _Traits>::ignore(streamsize __n)
  1054. {
  1055.   sentry __sentry(*this, _No_Skip_WS());
  1056.   this->_M_gcount = 0;
  1057.  
  1058.   if (__sentry) {
  1059.     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  1060.     typedef _Constant_unary_fun<bool, int_type> _Const_bool;
  1061.     typedef _Constant_binary_fun<streamsize, streamsize, streamsize>
  1062.       _Const_streamsize;
  1063.     const streamsize __maxss = (numeric_limits<streamsize>::max)();
  1064.  
  1065.     if (__n == (numeric_limits<int>::max)()) {
  1066.       if (__buf->gptr() != __buf->egptr())
  1067.         _M_gcount
  1068.           = _M_ignore_buffered(this,  __buf,
  1069.                                __maxss, _Const_streamsize(__maxss),
  1070.                                _Const_bool(false),
  1071.                                _Project2nd<const _CharT*, const _CharT*>(),
  1072.                                false, false);
  1073.       else
  1074.         _M_gcount = _M_ignore_unbuffered(this,  __buf,
  1075.                                          __maxss, _Const_streamsize(__maxss),
  1076.                                          _Const_bool(false), false, false);
  1077.     }
  1078.     else {
  1079.       if (__buf->gptr() != __buf->egptr())
  1080.         _M_gcount
  1081.           = _M_ignore_buffered(this,  __buf,
  1082.                                __n, minus<streamsize>(),
  1083.                                _Const_bool(false),
  1084.                                _Project2nd<const _CharT*, const _CharT*>(),
  1085.                                false, false);
  1086.       else
  1087.         _M_gcount = _M_ignore_unbuffered(this,  __buf, __n, minus<streamsize>(),
  1088.                                          _Const_bool(false), false, false);
  1089.     }
  1090.   }
  1091.  
  1092.   return *this;
  1093. }
  1094.  
  1095. template <class _CharT, class _Traits>
  1096. basic_istream<_CharT, _Traits>&
  1097. basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __delim)
  1098. {
  1099.   sentry __sentry(*this, _No_Skip_WS());
  1100.   this->_M_gcount = 0;
  1101.  
  1102.   if (__sentry) {
  1103.     basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  1104.     typedef _Constant_unary_fun<bool, int_type> _Const_bool;
  1105.     typedef _Constant_binary_fun<streamsize, streamsize, streamsize>
  1106.       _Const_streamsize;
  1107.     const streamsize __maxss = (numeric_limits<streamsize>::max)();
  1108.  
  1109.     if (__n == (numeric_limits<int>::max)()) {
  1110.       if (__buf->gptr() != __buf->egptr())
  1111.         _M_gcount = _M_ignore_buffered(this,  __buf,
  1112.                                        __maxss, _Const_streamsize(__maxss),
  1113.                                        _Eq_int_bound<_Traits>(__delim),
  1114.                                        _Scan_for_int_val<_Traits>(__delim),
  1115.                                        true, false);
  1116.       else
  1117.         _M_gcount = _M_ignore_unbuffered(this,  __buf,
  1118.                                          __maxss, _Const_streamsize(__maxss),
  1119.                                          _Eq_int_bound<_Traits>(__delim),
  1120.                                          true, false);
  1121.     }
  1122.     else {
  1123.       if (__buf->gptr() != __buf->egptr())
  1124.         _M_gcount = _M_ignore_buffered(this,  __buf,
  1125.                                        __n, minus<streamsize>(),
  1126.                                        _Eq_int_bound<_Traits>(
  1127.                                                __delim),
  1128.                                        _Scan_for_int_val<_Traits>(__delim),
  1129.                                        true, false);
  1130.       else
  1131.         _M_gcount = _M_ignore_unbuffered(this,  __buf, __n, minus<streamsize>(),
  1132.                                          _Eq_int_bound<_Traits>(__delim),
  1133.                                          true, false);
  1134.     }
  1135.   }
  1136.  
  1137.   return *this;
  1138. }
  1139.  
  1140. // This member function does not construct a sentry object, because
  1141. // it is called from sentry's constructor.  
  1142. template <class _CharT, class _Traits>
  1143. void basic_istream<_CharT, _Traits>::_M_skip_whitespace(bool __set_failbit)
  1144. {
  1145.   basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
  1146.   if (!__buf)
  1147.     this->setstate(ios_base::badbit);
  1148.   else if (__buf->gptr() != __buf->egptr())
  1149.     _M_ignore_buffered(this,  __buf,
  1150.                        _Is_not_wspace<_Traits>((const ctype<_CharT>*)this->_M_ctype_facet()),
  1151.                        _Scan_for_not_wspace<_Traits>((const ctype<_CharT>*)this->_M_ctype_facet()),
  1152.                        false, __set_failbit);
  1153.   else
  1154.     _M_ignore_unbuffered(this,  __buf,
  1155.                          _Is_not_wspace<_Traits>((const ctype<_CharT>*)this->_M_ctype_facet()),
  1156.                          false, __set_failbit);
  1157. }
  1158.  
  1159.  
  1160. // This is a very simple loop that reads characters from __src and puts
  1161. // them into __dest.  It looks complicated because of the (standard-
  1162. // mandated) exception handling policy.
  1163. //
  1164. // We stop when we get an exception, when we fail to insert into the 
  1165. // output streambuf, or when __is_delim is true.
  1166.  
  1167. template < class _CharT, class _Traits, class _Is_Delim>
  1168. streamsize _STLP_CALL 
  1169. _M_copy_unbuffered( basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __src,
  1170.             basic_streambuf<_CharT, _Traits>* __dest,
  1171.             _Is_Delim __is_delim,
  1172.             bool __extract_delim, bool __rethrow)
  1173. {
  1174.   streamsize __extracted = 0;
  1175.   ios_base::iostate __status = 0;
  1176.   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  1177.   int_type __c;
  1178.  
  1179.   _STLP_TRY {
  1180.   
  1181.     while (true) {
  1182.   
  1183.       // Get a character. If there's an exception, catch and (maybe) rethrow it.
  1184.       __c = __src->sbumpc();
  1185.       
  1186.       // If we failed to get a character, then quit.
  1187.       if (__that->_S_eof(__c)) {
  1188.         __status |= ios_base::eofbit;
  1189.         break;
  1190.       }  
  1191.       // If it's the delimiter, then quit.
  1192.       else if (__is_delim(__c)) {
  1193.         if (!__extract_delim && !__pushback(__src, _Traits::to_char_type(__c)))
  1194.           __status |= ios_base::failbit;
  1195.         break;
  1196.       }
  1197.       
  1198.       else {
  1199.       
  1200.         // Try to put the character in the output streambuf.
  1201.         bool __failed = false;
  1202.         _STLP_TRY {
  1203.           if (!__that->_S_eof(__dest->sputc(__c)))
  1204.             ++__extracted;
  1205.           else
  1206.             __failed = true;
  1207.         }
  1208.         _STLP_CATCH_ALL {
  1209.           __failed = true;
  1210.         }
  1211.         
  1212.         // If we failed to put the character in the output streambuf, then
  1213.         // try to push it back to the input streambuf.
  1214.         if (__failed && !__pushback(__src, _Traits::to_char_type(__c)))
  1215.           __status |= ios_base::failbit;
  1216.  
  1217.         // fbp : avoiding infinite loop in io-27-6-1-2-3.exp 
  1218.         if (__failed)
  1219.           break;
  1220.       }
  1221.  
  1222.     } /* while (true) */
  1223.     
  1224.   }
  1225.   // fbp : this try/catch moved here in reasonable assumption
  1226.   // __is_delim never throw (__pushback is guaranteed not to)
  1227.   _STLP_CATCH_ALL {
  1228.     // See 27.6.1.2.3, paragraph 13.
  1229.     if (__rethrow && __extracted == 0)
  1230.       __that->_M_handle_exception(ios_base::failbit);
  1231.   }
  1232.   __that->setstate(__status);
  1233.   return __extracted;
  1234. }
  1235.       
  1236. // Buffered copying from one streambuf to another.  We copy the characters
  1237. // in chunks, rather than one at a time.  We still have to worry about all
  1238. // of the error conditions we checked in _M_copy_unbuffered, plus one more:
  1239. // the streambuf might decide to switch from a buffered to an unbuffered mode.
  1240.  
  1241. template < class _CharT, class _Traits, class _Is_Delim, class _Scan_Delim>
  1242. streamsize _STLP_CALL 
  1243. _M_copy_buffered(basic_istream<_CharT, _Traits>* __that, basic_streambuf<_CharT, _Traits>* __src,
  1244.                      basic_streambuf<_CharT, _Traits>* __dest,
  1245.                      _Scan_Delim __scan_delim, _Is_Delim __is_delim,
  1246.                      bool __extract_delim, bool __rethrow)
  1247. {
  1248.   streamsize __extracted = 0;
  1249.   ios_base::iostate __status = 0;
  1250.   typedef typename basic_istream<_CharT, _Traits>::int_type int_type;
  1251.   int_type __c = _Traits::eof();
  1252.   _CharT* __first = __src->_M_gptr();
  1253.   ptrdiff_t __avail = __src->_M_egptr() - __first;
  1254.   // fbp : introduced to move catch/try blocks out of the loop
  1255.   bool __do_handle_exceptions;
  1256.  
  1257.   _STLP_TRY {
  1258.     while (true) {
  1259.       __do_handle_exceptions = false ;
  1260.       const _CharT* __last = __scan_delim(__first, __src->_M_egptr());
  1261.       
  1262.       // Try to copy the entire input buffer to the output buffer.
  1263.       streamsize __n = __dest->sputn(__first, __extract_delim && __last != __src->_M_egptr()
  1264.                                      ? (__last - __first) + 1
  1265.                                      : (__last - __first));
  1266.       __src->_M_gbump((int)__n);
  1267.       __extracted += __n;
  1268.       
  1269.       // from this on, catch() will call _M_handle_exceptions()
  1270.       __do_handle_exceptions = true;
  1271.       
  1272.       if (__n < __avail)          // We found the delimiter, or else failed to
  1273.         break;                    // copy some characters.
  1274.       
  1275.       __c = __src->sgetc();
  1276.  
  1277.       // Three possibilities: we succeeded in refilling the buffer, or 
  1278.       // we got EOF, or the streambuf has switched to unbuffered mode.
  1279.       __first = __src->_M_gptr();
  1280.       __avail = __src->_M_egptr() - __first;
  1281.       
  1282.       if (__avail > 0)
  1283.         {}    // dwa 1/16/00 -- suppress a Metrowerks warning
  1284.       else if (__that->_S_eof(__c)) {
  1285.         __status |= ios_base::eofbit;
  1286.         break;
  1287.       }
  1288.       else 
  1289.         return __extracted + _M_copy_unbuffered(__that,  __src, __dest, __is_delim,
  1290.                                                 __extract_delim, __rethrow);
  1291.     } /* while */
  1292.   }
  1293.   
  1294.   _STLP_CATCH_ALL {
  1295.     // See 27.6.1.2.3, paragraph 13.
  1296.     if (__rethrow && __do_handle_exceptions &&  __extracted == 0)
  1297.       __that->_M_handle_exception(ios_base::failbit);
  1298.   }
  1299.   
  1300.   if (__status)
  1301.     __that->setstate(__status);   // This might throw.
  1302.   return __extracted;
  1303.  
  1304.  
  1305.  
  1306. template <class _CharT, class _Traits>
  1307. basic_istream<_CharT, _Traits>& 
  1308. basic_istream<_CharT, _Traits>
  1309.   ::get(basic_streambuf<_CharT, _Traits>& __dest, _CharT __delim)
  1310. {
  1311.   sentry __sentry(*this, _No_Skip_WS());
  1312.   this->_M_gcount = 0;
  1313.  
  1314.   if (__sentry) {
  1315.     basic_streambuf<_CharT, _Traits>* __src = this->rdbuf();
  1316.  
  1317.     if (__src)
  1318.       this->_M_gcount = __src->egptr() != __src->gptr()
  1319.         ? _M_copy_buffered(this,  __src, &__dest,
  1320.                            _Scan_for_char_val<_Traits>(__delim),
  1321.                            _Eq_char_bound<_Traits>(__delim),
  1322.                            false, false)
  1323.         : _M_copy_unbuffered(this,  __src, &__dest,
  1324.                              _Eq_char_bound<_Traits>(__delim),
  1325.                              false, false);
  1326.   }
  1327.  
  1328.   if (this->_M_gcount == 0)
  1329.     this->setstate(ios_base::failbit);
  1330.  
  1331.   return *this;
  1332. }
  1333.  
  1334. // Copying characters into a streambuf.
  1335. template <class _CharT, class _Traits> 
  1336. basic_istream<_CharT, _Traits>& 
  1337. basic_istream<_CharT, _Traits>
  1338.   ::operator>>(basic_streambuf<_CharT, _Traits>* __dest)
  1339. {
  1340.   streamsize __n = 0;
  1341.   typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
  1342.   _Sentry __sentry(*this);
  1343.   if (__sentry) {
  1344.     basic_streambuf<_CharT, _Traits>* __src = this->rdbuf();
  1345.     if (__src && __dest)
  1346.       __n = __src->egptr() != __src->gptr()
  1347.         ? _M_copy_buffered(this,  __src, __dest,
  1348.                            _Project2nd<const _CharT*, const _CharT*>(),
  1349.                            _Constant_unary_fun<bool, int_type>(false),
  1350.                            false, true)
  1351.         : _M_copy_unbuffered(this,  __src, __dest,
  1352.                              _Constant_unary_fun<bool, int_type>(false),
  1353.                              false, true);
  1354.   }
  1355.  
  1356.   if (__n == 0)
  1357.     this->setstate(ios_base::failbit);
  1358.   
  1359.   return *this;
  1360. }
  1361.  
  1362. // ----------------------------------------------------------------
  1363. // basic_iostream<> class 
  1364. // ----------------------------------------------------------------
  1365.  
  1366. template <class _CharT, class _Traits>
  1367. basic_iostream<_CharT, _Traits>
  1368.   ::basic_iostream(basic_streambuf<_CharT, _Traits>* __buf)
  1369.     : basic_ios<_CharT, _Traits>(),
  1370.       basic_istream<_CharT, _Traits>(__buf),
  1371.       basic_ostream<_CharT, _Traits>(__buf)
  1372. {
  1373.   this->init(__buf);
  1374. }
  1375.  
  1376. template <class _CharT, class _Traits>
  1377. basic_iostream<_CharT, _Traits>::~basic_iostream()
  1378. {}
  1379.  
  1380. _STLP_END_NAMESPACE
  1381.  
  1382. # undef __BIS_int_type__
  1383. # undef __BIS_pos_type__
  1384. # undef __BIS_off_type__
  1385.  
  1386. # endif /* defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) */
  1387.  
  1388. #endif /* _STLP_ISTREAM_C */
  1389.