libstdc++
streambuf
Go to the documentation of this file.
00001 // Stream buffer classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00004 // 2006, 2007, 2008, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file include/streambuf
00027  *  This is a Standard C++ Library header.
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 27.5  Stream buffers
00032 //
00033 
00034 #ifndef _GLIBXX_STREAMBUF
00035 #define _GLIBXX_STREAMBUF 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <bits/c++config.h>
00040 #include <iosfwd>
00041 #include <bits/localefwd.h>
00042 #include <bits/ios_base.h>
00043 #include <bits/cpp_type_traits.h>
00044 #include <ext/type_traits.h>
00045 
00046 namespace std _GLIBCXX_VISIBILITY(default)
00047 {
00048 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00049 
00050   template<typename _CharT, typename _Traits>
00051     streamsize
00052     __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
00053               basic_streambuf<_CharT, _Traits>*, bool&);
00054 
00055   /**
00056    *  @brief  The actual work of input and output (interface).
00057    *  @ingroup io
00058    *
00059    *  This is a base class.  Derived stream buffers each control a
00060    *  pair of character sequences:  one for input, and one for output.
00061    *
00062    *  Section [27.5.1] of the standard describes the requirements and
00063    *  behavior of stream buffer classes.  That section (three paragraphs)
00064    *  is reproduced here, for simplicity and accuracy.
00065    *
00066    *  -# Stream buffers can impose various constraints on the sequences
00067    *     they control.  Some constraints are:
00068    *     - The controlled input sequence can be not readable.
00069    *     - The controlled output sequence can be not writable.
00070    *     - The controlled sequences can be associated with the contents of
00071    *       other representations for character sequences, such as external
00072    *       files.
00073    *     - The controlled sequences can support operations @e directly to or
00074    *       from associated sequences.
00075    *     - The controlled sequences can impose limitations on how the
00076    *       program can read characters from a sequence, write characters to
00077    *       a sequence, put characters back into an input sequence, or alter
00078    *       the stream position.
00079    *     .
00080    *  -# Each sequence is characterized by three pointers which, if non-null,
00081    *     all point into the same @c charT array object.  The array object
00082    *     represents, at any moment, a (sub)sequence of characters from the
00083    *     sequence.  Operations performed on a sequence alter the values
00084    *     stored in these pointers, perform reads and writes directly to or
00085    *     from associated sequences, and alter <em>the stream position</em> and
00086    *     conversion state as needed to maintain this subsequence relationship.
00087    *     The three pointers are:
00088    *     - the <em>beginning pointer</em>, or lowest element address in the
00089    *       array (called @e xbeg here);
00090    *     - the <em>next pointer</em>, or next element address that is a
00091    *       current candidate for reading or writing (called @e xnext here);
00092    *     - the <em>end pointer</em>, or first element address beyond the
00093    *       end of the array (called @e xend here).
00094    *     .
00095    *  -# The following semantic constraints shall always apply for any set
00096    *     of three pointers for a sequence, using the pointer names given
00097    *     immediately above:
00098    *     - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
00099    *       also be non-null pointers into the same @c charT array, as
00100    *       described above; otherwise, @e xbeg and @e xend shall also be null.
00101    *     - If @e xnext is not a null pointer and @e xnext < @e xend for an
00102    *       output sequence, then a <em>write position</em> is available.
00103    *       In this case, @e *xnext shall be assignable as the next element
00104    *       to write (to put, or to store a character value, into the sequence).
00105    *     - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
00106    *       input sequence, then a <em>putback position</em> is available.
00107    *       In this case, @e xnext[-1] shall have a defined value and is the
00108    *       next (preceding) element to store a character that is put back
00109    *       into the input sequence.
00110    *     - If @e xnext is not a null pointer and @e xnext< @e xend for an
00111    *       input sequence, then a <em>read position</em> is available.
00112    *       In this case, @e *xnext shall have a defined value and is the
00113    *       next element to read (to get, or to obtain a character value,
00114    *       from the sequence).
00115   */
00116   template<typename _CharT, typename _Traits>
00117     class basic_streambuf 
00118     {
00119     public:
00120       //@{
00121       /**
00122        *  These are standard types.  They permit a standardized way of
00123        *  referring to names of (or names dependant on) the template
00124        *  parameters, which are specific to the implementation.
00125       */
00126       typedef _CharT                    char_type;
00127       typedef _Traits                   traits_type;
00128       typedef typename traits_type::int_type        int_type;
00129       typedef typename traits_type::pos_type        pos_type;
00130       typedef typename traits_type::off_type        off_type;
00131       //@}
00132 
00133       //@{
00134       /// This is a non-standard type.
00135       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00136       //@}
00137       
00138       friend class basic_ios<char_type, traits_type>;
00139       friend class basic_istream<char_type, traits_type>;
00140       friend class basic_ostream<char_type, traits_type>;
00141       friend class istreambuf_iterator<char_type, traits_type>;
00142       friend class ostreambuf_iterator<char_type, traits_type>;
00143 
00144       friend streamsize
00145       __copy_streambufs_eof<>(__streambuf_type*, __streambuf_type*, bool&);
00146 
00147       template<bool _IsMove, typename _CharT2>
00148         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, 
00149                            _CharT2*>::__type
00150         __copy_move_a2(istreambuf_iterator<_CharT2>,
00151                istreambuf_iterator<_CharT2>, _CharT2*);
00152 
00153       template<typename _CharT2>
00154         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
00155                   istreambuf_iterator<_CharT2> >::__type
00156         find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
00157          const _CharT2&);
00158 
00159       template<typename _CharT2, typename _Traits2>
00160         friend basic_istream<_CharT2, _Traits2>&
00161         operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
00162 
00163       template<typename _CharT2, typename _Traits2, typename _Alloc>
00164         friend basic_istream<_CharT2, _Traits2>&
00165         operator>>(basic_istream<_CharT2, _Traits2>&,
00166            basic_string<_CharT2, _Traits2, _Alloc>&);
00167 
00168       template<typename _CharT2, typename _Traits2, typename _Alloc>
00169         friend basic_istream<_CharT2, _Traits2>&
00170         getline(basic_istream<_CharT2, _Traits2>&,
00171         basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
00172 
00173     protected:
00174       /*
00175        *  This is based on _IO_FILE, just reordered to be more consistent,
00176        *  and is intended to be the most minimal abstraction for an
00177        *  internal buffer.
00178        *  -  get == input == read
00179        *  -  put == output == write
00180       */
00181       char_type*        _M_in_beg;     //< Start of get area.
00182       char_type*        _M_in_cur;     //< Current read area.
00183       char_type*        _M_in_end;     //< End of get area.
00184       char_type*        _M_out_beg;    //< Start of put area.
00185       char_type*        _M_out_cur;    //< Current put area.
00186       char_type*        _M_out_end;    //< End of put area.
00187 
00188       /// Current locale setting.
00189       locale            _M_buf_locale;  
00190 
00191   public:
00192       /// Destructor deallocates no buffer space.
00193       virtual 
00194       ~basic_streambuf() 
00195       { }
00196 
00197       // [27.5.2.2.1] locales
00198       /**
00199        *  @brief  Entry point for imbue().
00200        *  @param  loc  The new locale.
00201        *  @return  The previous locale.
00202        *
00203        *  Calls the derived imbue(loc).
00204       */
00205       locale 
00206       pubimbue(const locale &__loc)
00207       {
00208     locale __tmp(this->getloc());
00209     this->imbue(__loc);
00210     _M_buf_locale = __loc;
00211     return __tmp;
00212       }
00213 
00214       /**
00215        *  @brief  Locale access.
00216        *  @return  The current locale in effect.
00217        *
00218        *  If pubimbue(loc) has been called, then the most recent @c loc
00219        *  is returned.  Otherwise the global locale in effect at the time
00220        *  of construction is returned.
00221       */
00222       locale   
00223       getloc() const
00224       { return _M_buf_locale; } 
00225 
00226       // [27.5.2.2.2] buffer management and positioning
00227       //@{
00228       /**
00229        *  @brief  Entry points for derived buffer functions.
00230        *
00231        *  The public versions of @c pubfoo dispatch to the protected
00232        *  derived @c foo member functions, passing the arguments (if any)
00233        *  and returning the result unchanged.
00234       */
00235       __streambuf_type* 
00236       pubsetbuf(char_type* __s, streamsize __n) 
00237       { return this->setbuf(__s, __n); }
00238 
00239       pos_type 
00240       pubseekoff(off_type __off, ios_base::seekdir __way, 
00241          ios_base::openmode __mode = ios_base::in | ios_base::out)
00242       { return this->seekoff(__off, __way, __mode); }
00243 
00244       pos_type 
00245       pubseekpos(pos_type __sp,
00246          ios_base::openmode __mode = ios_base::in | ios_base::out)
00247       { return this->seekpos(__sp, __mode); }
00248 
00249       int 
00250       pubsync() { return this->sync(); }
00251       //@}
00252 
00253       // [27.5.2.2.3] get area
00254       /**
00255        *  @brief  Looking ahead into the stream.
00256        *  @return  The number of characters available.
00257        *
00258        *  If a read position is available, returns the number of characters
00259        *  available for reading before the buffer must be refilled.
00260        *  Otherwise returns the derived @c showmanyc().
00261       */
00262       streamsize 
00263       in_avail() 
00264       { 
00265     const streamsize __ret = this->egptr() - this->gptr();
00266     return __ret ? __ret : this->showmanyc();
00267       }
00268 
00269       /**
00270        *  @brief  Getting the next character.
00271        *  @return  The next character, or eof.
00272        *
00273        *  Calls @c sbumpc(), and if that function returns
00274        *  @c traits::eof(), so does this function.  Otherwise, @c sgetc().
00275       */
00276       int_type 
00277       snextc()
00278       {
00279     int_type __ret = traits_type::eof();
00280     if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(), 
00281                                __ret), true))
00282       __ret = this->sgetc();
00283     return __ret;
00284       }
00285 
00286       /**
00287        *  @brief  Getting the next character.
00288        *  @return  The next character, or eof.
00289        *
00290        *  If the input read position is available, returns that character
00291        *  and increments the read pointer, otherwise calls and returns
00292        *  @c uflow().
00293       */
00294       int_type 
00295       sbumpc()
00296       {
00297     int_type __ret;
00298     if (__builtin_expect(this->gptr() < this->egptr(), true))
00299       {
00300         __ret = traits_type::to_int_type(*this->gptr());
00301         this->gbump(1);
00302       }
00303     else 
00304       __ret = this->uflow();
00305     return __ret;
00306       }
00307 
00308       /**
00309        *  @brief  Getting the next character.
00310        *  @return  The next character, or eof.
00311        *
00312        *  If the input read position is available, returns that character,
00313        *  otherwise calls and returns @c underflow().  Does not move the 
00314        *  read position after fetching the character.
00315       */
00316       int_type 
00317       sgetc()
00318       {
00319     int_type __ret;
00320     if (__builtin_expect(this->gptr() < this->egptr(), true))
00321       __ret = traits_type::to_int_type(*this->gptr());
00322     else 
00323       __ret = this->underflow();
00324     return __ret;
00325       }
00326 
00327       /**
00328        *  @brief  Entry point for xsgetn.
00329        *  @param  s  A buffer area.
00330        *  @param  n  A count.
00331        *
00332        *  Returns xsgetn(s,n).  The effect is to fill @a s[0] through
00333        *  @a s[n-1] with characters from the input sequence, if possible.
00334       */
00335       streamsize 
00336       sgetn(char_type* __s, streamsize __n)
00337       { return this->xsgetn(__s, __n); }
00338 
00339       // [27.5.2.2.4] putback
00340       /**
00341        *  @brief  Pushing characters back into the input stream.
00342        *  @param  c  The character to push back.
00343        *  @return  The previous character, if possible.
00344        *
00345        *  Similar to sungetc(), but @a c is pushed onto the stream
00346        *  instead of <em>the previous character.</em> If successful,
00347        *  the next character fetched from the input stream will be @a
00348        *  c.
00349       */
00350       int_type 
00351       sputbackc(char_type __c)
00352       {
00353     int_type __ret;
00354     const bool __testpos = this->eback() < this->gptr();
00355     if (__builtin_expect(!__testpos || 
00356                  !traits_type::eq(__c, this->gptr()[-1]), false))
00357       __ret = this->pbackfail(traits_type::to_int_type(__c));
00358     else 
00359       {
00360         this->gbump(-1);
00361         __ret = traits_type::to_int_type(*this->gptr());
00362       }
00363     return __ret;
00364       }
00365 
00366       /**
00367        *  @brief  Moving backwards in the input stream.
00368        *  @return  The previous character, if possible.
00369        *
00370        *  If a putback position is available, this function decrements
00371        *  the input pointer and returns that character.  Otherwise,
00372        *  calls and returns pbackfail().  The effect is to @a unget
00373        *  the last character @a gotten.
00374       */
00375       int_type 
00376       sungetc()
00377       {
00378     int_type __ret;
00379     if (__builtin_expect(this->eback() < this->gptr(), true))
00380       {
00381         this->gbump(-1);
00382         __ret = traits_type::to_int_type(*this->gptr());
00383       }
00384     else 
00385       __ret = this->pbackfail();
00386     return __ret;
00387       }
00388 
00389       // [27.5.2.2.5] put area
00390       /**
00391        *  @brief  Entry point for all single-character output functions.
00392        *  @param  c  A character to output.
00393        *  @return  @a c, if possible.
00394        *
00395        *  One of two public output functions.
00396        *
00397        *  If a write position is available for the output sequence (i.e.,
00398        *  the buffer is not full), stores @a c in that position, increments
00399        *  the position, and returns @c traits::to_int_type(c).  If a write
00400        *  position is not available, returns @c overflow(c).
00401       */
00402       int_type 
00403       sputc(char_type __c)
00404       {
00405     int_type __ret;
00406     if (__builtin_expect(this->pptr() < this->epptr(), true))
00407       {
00408         *this->pptr() = __c;
00409         this->pbump(1);
00410         __ret = traits_type::to_int_type(__c);
00411       }
00412     else
00413       __ret = this->overflow(traits_type::to_int_type(__c));
00414     return __ret;
00415       }
00416 
00417       /**
00418        *  @brief  Entry point for all single-character output functions.
00419        *  @param  s  A buffer read area.
00420        *  @param  n  A count.
00421        *
00422        *  One of two public output functions.
00423        *
00424        *
00425        *  Returns xsputn(s,n).  The effect is to write @a s[0] through
00426        *  @a s[n-1] to the output sequence, if possible.
00427       */
00428       streamsize 
00429       sputn(const char_type* __s, streamsize __n)
00430       { return this->xsputn(__s, __n); }
00431 
00432     protected:
00433       /**
00434        *  @brief  Base constructor.
00435        *
00436        *  Only called from derived constructors, and sets up all the
00437        *  buffer data to zero, including the pointers described in the
00438        *  basic_streambuf class description.  Note that, as a result,
00439        *  - the class starts with no read nor write positions available,
00440        *  - this is not an error
00441       */
00442       basic_streambuf()
00443       : _M_in_beg(0), _M_in_cur(0), _M_in_end(0), 
00444       _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
00445       _M_buf_locale(locale()) 
00446       { }
00447 
00448       // [27.5.2.3.1] get area access
00449       //@{
00450       /**
00451        *  @brief  Access to the get area.
00452        *
00453        *  These functions are only available to other protected functions,
00454        *  including derived classes.
00455        *
00456        *  - eback() returns the beginning pointer for the input sequence
00457        *  - gptr() returns the next pointer for the input sequence
00458        *  - egptr() returns the end pointer for the input sequence
00459       */
00460       char_type* 
00461       eback() const { return _M_in_beg; }
00462 
00463       char_type* 
00464       gptr()  const { return _M_in_cur;  }
00465 
00466       char_type* 
00467       egptr() const { return _M_in_end; }
00468       //@}
00469 
00470       /**
00471        *  @brief  Moving the read position.
00472        *  @param  n  The delta by which to move.
00473        *
00474        *  This just advances the read position without returning any data.
00475       */
00476       void 
00477       gbump(int __n) { _M_in_cur += __n; }
00478 
00479       /**
00480        *  @brief  Setting the three read area pointers.
00481        *  @param  gbeg  A pointer.
00482        *  @param  gnext  A pointer.
00483        *  @param  gend  A pointer.
00484        *  @post  @a gbeg == @c eback(), @a gnext == @c gptr(), and
00485        *         @a gend == @c egptr()
00486       */
00487       void 
00488       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
00489       {
00490     _M_in_beg = __gbeg;
00491     _M_in_cur = __gnext;
00492     _M_in_end = __gend;
00493       }
00494 
00495       // [27.5.2.3.2] put area access
00496       //@{
00497       /**
00498        *  @brief  Access to the put area.
00499        *
00500        *  These functions are only available to other protected functions,
00501        *  including derived classes.
00502        *
00503        *  - pbase() returns the beginning pointer for the output sequence
00504        *  - pptr() returns the next pointer for the output sequence
00505        *  - epptr() returns the end pointer for the output sequence
00506       */
00507       char_type* 
00508       pbase() const { return _M_out_beg; }
00509 
00510       char_type* 
00511       pptr() const { return _M_out_cur; }
00512 
00513       char_type* 
00514       epptr() const { return _M_out_end; }
00515       //@}
00516 
00517       /**
00518        *  @brief  Moving the write position.
00519        *  @param  n  The delta by which to move.
00520        *
00521        *  This just advances the write position without returning any data.
00522       */
00523       void 
00524       pbump(int __n) { _M_out_cur += __n; }
00525 
00526       /**
00527        *  @brief  Setting the three write area pointers.
00528        *  @param  pbeg  A pointer.
00529        *  @param  pend  A pointer.
00530        *  @post  @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
00531        *         @a pend == @c epptr()
00532       */
00533       void 
00534       setp(char_type* __pbeg, char_type* __pend)
00535       { 
00536     _M_out_beg = _M_out_cur = __pbeg; 
00537     _M_out_end = __pend;
00538       }
00539 
00540       // [27.5.2.4] virtual functions
00541       // [27.5.2.4.1] locales
00542       /**
00543        *  @brief  Changes translations.
00544        *  @param  loc  A new locale.
00545        *
00546        *  Translations done during I/O which depend on the current
00547        *  locale are changed by this call.  The standard adds,
00548        *  <em>Between invocations of this function a class derived
00549        *  from streambuf can safely cache results of calls to locale
00550        *  functions and to members of facets so obtained.</em>
00551        *
00552        *  @note  Base class version does nothing.
00553       */
00554       virtual void 
00555       imbue(const locale&) 
00556       { }
00557 
00558       // [27.5.2.4.2] buffer management and positioning
00559       /**
00560        *  @brief  Manipulates the buffer.
00561        *
00562        *  Each derived class provides its own appropriate behavior.  See
00563        *  the next-to-last paragraph of 
00564        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
00565        *  for more on this function.
00566        *
00567        *  @note  Base class version does nothing, returns @c this.
00568       */
00569       virtual basic_streambuf<char_type,_Traits>* 
00570       setbuf(char_type*, streamsize)
00571       { return this; }
00572       
00573       /**
00574        *  @brief  Alters the stream positions.
00575        *
00576        *  Each derived class provides its own appropriate behavior.
00577        *  @note  Base class version does nothing, returns a @c pos_type
00578        *         that represents an invalid stream position.
00579       */
00580       virtual pos_type 
00581       seekoff(off_type, ios_base::seekdir,
00582           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00583       { return pos_type(off_type(-1)); } 
00584 
00585       /**
00586        *  @brief  Alters the stream positions.
00587        *
00588        *  Each derived class provides its own appropriate behavior.
00589        *  @note  Base class version does nothing, returns a @c pos_type
00590        *         that represents an invalid stream position.
00591       */
00592       virtual pos_type 
00593       seekpos(pos_type, 
00594           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00595       { return pos_type(off_type(-1)); } 
00596 
00597       /**
00598        *  @brief  Synchronizes the buffer arrays with the controlled sequences.
00599        *  @return  -1 on failure.
00600        *
00601        *  Each derived class provides its own appropriate behavior,
00602        *  including the definition of @a failure.
00603        *  @note  Base class version does nothing, returns zero.
00604       */
00605       virtual int 
00606       sync() { return 0; }
00607 
00608       // [27.5.2.4.3] get area
00609       /**
00610        *  @brief  Investigating the data available.
00611        *  @return  An estimate of the number of characters available in the
00612        *           input sequence, or -1.
00613        *
00614        *  <em>If it returns a positive value, then successive calls to
00615        *  @c underflow() will not return @c traits::eof() until at
00616        *  least that number of characters have been supplied.  If @c
00617        *  showmanyc() returns -1, then calls to @c underflow() or @c
00618        *  uflow() will fail.</em> [27.5.2.4.3]/1
00619        *
00620        *  @note  Base class version does nothing, returns zero.
00621        *  @note  The standard adds that <em>the intention is not only that the
00622        *         calls [to underflow or uflow] will not return @c eof() but
00623        *         that they will return immediately.</em>
00624        *  @note  The standard adds that <em>the morphemes of @c showmanyc are
00625        *         @b es-how-many-see, not @b show-manic.</em>
00626       */
00627       virtual streamsize 
00628       showmanyc() { return 0; }
00629 
00630       /**
00631        *  @brief  Multiple character extraction.
00632        *  @param  s  A buffer area.
00633        *  @param  n  Maximum number of characters to assign.
00634        *  @return  The number of characters assigned.
00635        *
00636        *  Fills @a s[0] through @a s[n-1] with characters from the input
00637        *  sequence, as if by @c sbumpc().  Stops when either @a n characters
00638        *  have been copied, or when @c traits::eof() would be copied.
00639        *
00640        *  It is expected that derived classes provide a more efficient
00641        *  implementation by overriding this definition.
00642       */
00643       virtual streamsize 
00644       xsgetn(char_type* __s, streamsize __n);
00645 
00646       /**
00647        *  @brief  Fetches more data from the controlled sequence.
00648        *  @return  The first character from the <em>pending sequence</em>.
00649        *
00650        *  Informally, this function is called when the input buffer is
00651        *  exhausted (or does not exist, as buffering need not actually be
00652        *  done).  If a buffer exists, it is @a refilled.  In either case, the
00653        *  next available character is returned, or @c traits::eof() to
00654        *  indicate a null pending sequence.
00655        *
00656        *  For a formal definition of the pending sequence, see a good text
00657        *  such as Langer & Kreft, or [27.5.2.4.3]/7-14.
00658        *
00659        *  A functioning input streambuf can be created by overriding only
00660        *  this function (no buffer area will be used).  For an example, see
00661        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25.html
00662        *
00663        *  @note  Base class version does nothing, returns eof().
00664       */
00665       virtual int_type 
00666       underflow()
00667       { return traits_type::eof(); }
00668 
00669       /**
00670        *  @brief  Fetches more data from the controlled sequence.
00671        *  @return  The first character from the <em>pending sequence</em>.
00672        *
00673        *  Informally, this function does the same thing as @c underflow(),
00674        *  and in fact is required to call that function.  It also returns
00675        *  the new character, like @c underflow() does.  However, this
00676        *  function also moves the read position forward by one.
00677       */
00678       virtual int_type 
00679       uflow() 
00680       {
00681     int_type __ret = traits_type::eof();
00682     const bool __testeof = traits_type::eq_int_type(this->underflow(), 
00683                             __ret);
00684     if (!__testeof)
00685       {
00686         __ret = traits_type::to_int_type(*this->gptr());
00687         this->gbump(1);
00688       }
00689     return __ret;    
00690       }
00691 
00692       // [27.5.2.4.4] putback
00693       /**
00694        *  @brief  Tries to back up the input sequence.
00695        *  @param  c  The character to be inserted back into the sequence.
00696        *  @return  eof() on failure, <em>some other value</em> on success
00697        *  @post  The constraints of @c gptr(), @c eback(), and @c pptr()
00698        *         are the same as for @c underflow().
00699        *
00700        *  @note  Base class version does nothing, returns eof().
00701       */
00702       virtual int_type 
00703       pbackfail(int_type /* __c */  = traits_type::eof())
00704       { return traits_type::eof(); }
00705 
00706       // Put area:
00707       /**
00708        *  @brief  Multiple character insertion.
00709        *  @param  s  A buffer area.
00710        *  @param  n  Maximum number of characters to write.
00711        *  @return  The number of characters written.
00712        *
00713        *  Writes @a s[0] through @a s[n-1] to the output sequence, as if
00714        *  by @c sputc().  Stops when either @a n characters have been
00715        *  copied, or when @c sputc() would return @c traits::eof().
00716        *
00717        *  It is expected that derived classes provide a more efficient
00718        *  implementation by overriding this definition.
00719       */
00720       virtual streamsize 
00721       xsputn(const char_type* __s, streamsize __n);
00722 
00723       /**
00724        *  @brief  Consumes data from the buffer; writes to the
00725        *          controlled sequence.
00726        *  @param  c  An additional character to consume.
00727        *  @return  eof() to indicate failure, something else (usually
00728        *           @a c, or not_eof())
00729        *
00730        *  Informally, this function is called when the output buffer
00731        *  is full (or does not exist, as buffering need not actually
00732        *  be done).  If a buffer exists, it is @a consumed, with
00733        *  <em>some effect</em> on the controlled sequence.
00734        *  (Typically, the buffer is written out to the sequence
00735        *  verbatim.)  In either case, the character @a c is also
00736        *  written out, if @a c is not @c eof().
00737        *
00738        *  For a formal definition of this function, see a good text
00739        *  such as Langer & Kreft, or [27.5.2.4.5]/3-7.
00740        *
00741        *  A functioning output streambuf can be created by overriding only
00742        *  this function (no buffer area will be used).
00743        *
00744        *  @note  Base class version does nothing, returns eof().
00745       */
00746       virtual int_type 
00747       overflow(int_type /* __c */ = traits_type::eof())
00748       { return traits_type::eof(); }
00749 
00750 #if _GLIBCXX_USE_DEPRECATED
00751     // Annex D.6
00752     public:
00753       /**
00754        *  @brief  Tosses a character.
00755        *
00756        *  Advances the read pointer, ignoring the character that would have
00757        *  been read.
00758        *
00759        *  See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
00760        */
00761       void 
00762       stossc() 
00763       {
00764     if (this->gptr() < this->egptr()) 
00765       this->gbump(1);
00766     else 
00767       this->uflow();
00768       }
00769 #endif
00770 
00771       // Also used by specializations for char and wchar_t in src.
00772       void 
00773       __safe_gbump(streamsize __n) { _M_in_cur += __n; }
00774 
00775       void
00776       __safe_pbump(streamsize __n) { _M_out_cur += __n; }
00777 
00778     private:
00779       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00780       // Side effect of DR 50. 
00781       basic_streambuf(const __streambuf_type& __sb)
00782       : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur), 
00783       _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg), 
00784       _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
00785       _M_buf_locale(__sb._M_buf_locale) 
00786       { }
00787 
00788       __streambuf_type& 
00789       operator=(const __streambuf_type&) { return *this; };
00790     };
00791 
00792   // Explicit specialization declarations, defined in src/streambuf.cc.
00793   template<>
00794     streamsize
00795     __copy_streambufs_eof(basic_streambuf<char>* __sbin,
00796               basic_streambuf<char>* __sbout, bool& __ineof);
00797 #ifdef _GLIBCXX_USE_WCHAR_T
00798   template<>
00799     streamsize
00800     __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
00801               basic_streambuf<wchar_t>* __sbout, bool& __ineof);
00802 #endif
00803 
00804 _GLIBCXX_END_NAMESPACE_VERSION
00805 } // namespace
00806 
00807 #include <bits/streambuf.tcc>
00808 
00809 #endif /* _GLIBCXX_STREAMBUF */