Home | History | Annotate | Line # | Download | only in std
      1   1.1  mrg // Stream buffer classes -*- C++ -*-
      2   1.1  mrg 
      3  1.12  mrg // Copyright (C) 1997-2022 Free Software Foundation, Inc.
      4   1.1  mrg //
      5   1.1  mrg // This file is part of the GNU ISO C++ Library.  This library is free
      6   1.1  mrg // software; you can redistribute it and/or modify it under the
      7   1.1  mrg // terms of the GNU General Public License as published by the
      8   1.1  mrg // Free Software Foundation; either version 3, or (at your option)
      9   1.1  mrg // any later version.
     10   1.1  mrg 
     11   1.1  mrg // This library is distributed in the hope that it will be useful,
     12   1.1  mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   1.1  mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14   1.1  mrg // GNU General Public License for more details.
     15   1.1  mrg 
     16   1.1  mrg // Under Section 7 of GPL version 3, you are granted additional
     17   1.1  mrg // permissions described in the GCC Runtime Library Exception, version
     18   1.1  mrg // 3.1, as published by the Free Software Foundation.
     19   1.1  mrg 
     20   1.1  mrg // You should have received a copy of the GNU General Public License and
     21   1.1  mrg // a copy of the GCC Runtime Library Exception along with this program;
     22   1.1  mrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23   1.1  mrg // <http://www.gnu.org/licenses/>.
     24   1.1  mrg 
     25   1.3  mrg /** @file include/streambuf
     26   1.1  mrg  *  This is a Standard C++ Library header.
     27   1.1  mrg  */
     28   1.1  mrg 
     29   1.1  mrg //
     30   1.1  mrg // ISO C++ 14882: 27.5  Stream buffers
     31   1.1  mrg //
     32   1.1  mrg 
     33   1.1  mrg #ifndef _GLIBXX_STREAMBUF
     34   1.1  mrg #define _GLIBXX_STREAMBUF 1
     35   1.1  mrg 
     36   1.1  mrg #pragma GCC system_header
     37   1.1  mrg 
     38   1.1  mrg #include <bits/c++config.h>
     39   1.1  mrg #include <iosfwd>
     40   1.1  mrg #include <bits/localefwd.h>
     41   1.1  mrg #include <bits/ios_base.h>
     42   1.1  mrg #include <bits/cpp_type_traits.h>
     43   1.1  mrg #include <ext/type_traits.h>
     44   1.1  mrg 
     45   1.3  mrg namespace std _GLIBCXX_VISIBILITY(default)
     46   1.3  mrg {
     47   1.3  mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
     48   1.1  mrg 
     49   1.9  mrg #define _IsUnused __attribute__ ((__unused__))
     50   1.9  mrg 
     51   1.1  mrg   template<typename _CharT, typename _Traits>
     52   1.1  mrg     streamsize
     53   1.1  mrg     __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
     54   1.1  mrg 			  basic_streambuf<_CharT, _Traits>*, bool&);
     55   1.1  mrg 
     56   1.1  mrg   /**
     57   1.1  mrg    *  @brief  The actual work of input and output (interface).
     58   1.1  mrg    *  @ingroup io
     59   1.1  mrg    *
     60   1.3  mrg    *  @tparam _CharT  Type of character stream.
     61   1.3  mrg    *  @tparam _Traits  Traits for character type, defaults to
     62   1.3  mrg    *                   char_traits<_CharT>.
     63   1.3  mrg    *
     64   1.1  mrg    *  This is a base class.  Derived stream buffers each control a
     65   1.1  mrg    *  pair of character sequences:  one for input, and one for output.
     66   1.1  mrg    *
     67   1.1  mrg    *  Section [27.5.1] of the standard describes the requirements and
     68   1.1  mrg    *  behavior of stream buffer classes.  That section (three paragraphs)
     69   1.1  mrg    *  is reproduced here, for simplicity and accuracy.
     70   1.1  mrg    *
     71   1.1  mrg    *  -# Stream buffers can impose various constraints on the sequences
     72   1.1  mrg    *     they control.  Some constraints are:
     73   1.1  mrg    *     - The controlled input sequence can be not readable.
     74   1.1  mrg    *     - The controlled output sequence can be not writable.
     75   1.1  mrg    *     - The controlled sequences can be associated with the contents of
     76   1.1  mrg    *       other representations for character sequences, such as external
     77   1.1  mrg    *       files.
     78   1.1  mrg    *     - The controlled sequences can support operations @e directly to or
     79   1.1  mrg    *       from associated sequences.
     80   1.1  mrg    *     - The controlled sequences can impose limitations on how the
     81   1.1  mrg    *       program can read characters from a sequence, write characters to
     82   1.1  mrg    *       a sequence, put characters back into an input sequence, or alter
     83   1.1  mrg    *       the stream position.
     84   1.1  mrg    *     .
     85   1.1  mrg    *  -# Each sequence is characterized by three pointers which, if non-null,
     86   1.1  mrg    *     all point into the same @c charT array object.  The array object
     87   1.1  mrg    *     represents, at any moment, a (sub)sequence of characters from the
     88   1.1  mrg    *     sequence.  Operations performed on a sequence alter the values
     89   1.1  mrg    *     stored in these pointers, perform reads and writes directly to or
     90   1.1  mrg    *     from associated sequences, and alter <em>the stream position</em> and
     91   1.1  mrg    *     conversion state as needed to maintain this subsequence relationship.
     92   1.1  mrg    *     The three pointers are:
     93   1.1  mrg    *     - the <em>beginning pointer</em>, or lowest element address in the
     94   1.1  mrg    *       array (called @e xbeg here);
     95   1.1  mrg    *     - the <em>next pointer</em>, or next element address that is a
     96   1.1  mrg    *       current candidate for reading or writing (called @e xnext here);
     97   1.1  mrg    *     - the <em>end pointer</em>, or first element address beyond the
     98   1.1  mrg    *       end of the array (called @e xend here).
     99   1.1  mrg    *     .
    100   1.1  mrg    *  -# The following semantic constraints shall always apply for any set
    101   1.1  mrg    *     of three pointers for a sequence, using the pointer names given
    102   1.1  mrg    *     immediately above:
    103   1.1  mrg    *     - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
    104   1.1  mrg    *       also be non-null pointers into the same @c charT array, as
    105   1.1  mrg    *       described above; otherwise, @e xbeg and @e xend shall also be null.
    106   1.1  mrg    *     - If @e xnext is not a null pointer and @e xnext < @e xend for an
    107   1.1  mrg    *       output sequence, then a <em>write position</em> is available.
    108   1.1  mrg    *       In this case, @e *xnext shall be assignable as the next element
    109   1.1  mrg    *       to write (to put, or to store a character value, into the sequence).
    110   1.1  mrg    *     - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
    111   1.1  mrg    *       input sequence, then a <em>putback position</em> is available.
    112   1.1  mrg    *       In this case, @e xnext[-1] shall have a defined value and is the
    113   1.1  mrg    *       next (preceding) element to store a character that is put back
    114   1.1  mrg    *       into the input sequence.
    115   1.1  mrg    *     - If @e xnext is not a null pointer and @e xnext< @e xend for an
    116   1.1  mrg    *       input sequence, then a <em>read position</em> is available.
    117   1.1  mrg    *       In this case, @e *xnext shall have a defined value and is the
    118   1.1  mrg    *       next element to read (to get, or to obtain a character value,
    119   1.1  mrg    *       from the sequence).
    120   1.1  mrg   */
    121   1.1  mrg   template<typename _CharT, typename _Traits>
    122   1.5  mrg     class basic_streambuf
    123   1.1  mrg     {
    124   1.1  mrg     public:
    125  1.11  mrg       ///@{
    126   1.1  mrg       /**
    127   1.1  mrg        *  These are standard types.  They permit a standardized way of
    128   1.3  mrg        *  referring to names of (or names dependent on) the template
    129   1.1  mrg        *  parameters, which are specific to the implementation.
    130   1.1  mrg       */
    131   1.1  mrg       typedef _CharT 					char_type;
    132   1.1  mrg       typedef _Traits 					traits_type;
    133   1.1  mrg       typedef typename traits_type::int_type 		int_type;
    134   1.1  mrg       typedef typename traits_type::pos_type 		pos_type;
    135   1.1  mrg       typedef typename traits_type::off_type 		off_type;
    136  1.11  mrg       ///@}
    137   1.1  mrg 
    138  1.11  mrg       ///@{
    139   1.1  mrg       /// This is a non-standard type.
    140   1.1  mrg       typedef basic_streambuf<char_type, traits_type>  	__streambuf_type;
    141  1.11  mrg       ///@}
    142   1.5  mrg 
    143   1.1  mrg       friend class basic_ios<char_type, traits_type>;
    144   1.1  mrg       friend class basic_istream<char_type, traits_type>;
    145   1.1  mrg       friend class basic_ostream<char_type, traits_type>;
    146   1.1  mrg       friend class istreambuf_iterator<char_type, traits_type>;
    147   1.1  mrg       friend class ostreambuf_iterator<char_type, traits_type>;
    148   1.1  mrg 
    149   1.1  mrg       friend streamsize
    150   1.3  mrg       __copy_streambufs_eof<>(basic_streambuf*, basic_streambuf*, bool&);
    151   1.1  mrg 
    152   1.1  mrg       template<bool _IsMove, typename _CharT2>
    153   1.5  mrg         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
    154   1.1  mrg 					       _CharT2*>::__type
    155   1.1  mrg         __copy_move_a2(istreambuf_iterator<_CharT2>,
    156   1.1  mrg 		       istreambuf_iterator<_CharT2>, _CharT2*);
    157   1.1  mrg 
    158   1.1  mrg       template<typename _CharT2>
    159   1.1  mrg         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
    160   1.1  mrg 				  istreambuf_iterator<_CharT2> >::__type
    161   1.1  mrg         find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
    162   1.1  mrg 	     const _CharT2&);
    163   1.1  mrg 
    164   1.9  mrg       template<typename _CharT2, typename _Distance>
    165   1.9  mrg         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
    166   1.9  mrg 					       void>::__type
    167   1.9  mrg         advance(istreambuf_iterator<_CharT2>&, _Distance);
    168   1.9  mrg 
    169  1.12  mrg       friend void __istream_extract(istream&, char*, streamsize);
    170   1.1  mrg 
    171   1.1  mrg       template<typename _CharT2, typename _Traits2, typename _Alloc>
    172   1.1  mrg         friend basic_istream<_CharT2, _Traits2>&
    173   1.1  mrg         operator>>(basic_istream<_CharT2, _Traits2>&,
    174   1.1  mrg 		   basic_string<_CharT2, _Traits2, _Alloc>&);
    175   1.1  mrg 
    176   1.1  mrg       template<typename _CharT2, typename _Traits2, typename _Alloc>
    177   1.1  mrg         friend basic_istream<_CharT2, _Traits2>&
    178   1.1  mrg         getline(basic_istream<_CharT2, _Traits2>&,
    179   1.1  mrg 		basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
    180   1.1  mrg 
    181   1.1  mrg     protected:
    182   1.3  mrg       /*
    183   1.1  mrg        *  This is based on _IO_FILE, just reordered to be more consistent,
    184   1.1  mrg        *  and is intended to be the most minimal abstraction for an
    185   1.1  mrg        *  internal buffer.
    186   1.1  mrg        *  -  get == input == read
    187   1.1  mrg        *  -  put == output == write
    188   1.1  mrg       */
    189   1.3  mrg       char_type* 		_M_in_beg;     ///< Start of get area.
    190   1.3  mrg       char_type* 		_M_in_cur;     ///< Current read area.
    191   1.3  mrg       char_type* 		_M_in_end;     ///< End of get area.
    192   1.3  mrg       char_type* 		_M_out_beg;    ///< Start of put area.
    193   1.3  mrg       char_type* 		_M_out_cur;    ///< Current put area.
    194   1.3  mrg       char_type* 		_M_out_end;    ///< End of put area.
    195   1.1  mrg 
    196   1.1  mrg       /// Current locale setting.
    197   1.5  mrg       locale 			_M_buf_locale;
    198   1.1  mrg 
    199   1.1  mrg   public:
    200   1.1  mrg       /// Destructor deallocates no buffer space.
    201   1.5  mrg       virtual
    202   1.5  mrg       ~basic_streambuf()
    203   1.1  mrg       { }
    204   1.1  mrg 
    205   1.1  mrg       // [27.5.2.2.1] locales
    206   1.1  mrg       /**
    207   1.1  mrg        *  @brief  Entry point for imbue().
    208   1.3  mrg        *  @param  __loc  The new locale.
    209   1.1  mrg        *  @return  The previous locale.
    210   1.1  mrg        *
    211   1.3  mrg        *  Calls the derived imbue(__loc).
    212   1.1  mrg       */
    213   1.5  mrg       locale
    214   1.3  mrg       pubimbue(const locale& __loc)
    215   1.1  mrg       {
    216   1.1  mrg 	locale __tmp(this->getloc());
    217   1.1  mrg 	this->imbue(__loc);
    218   1.1  mrg 	_M_buf_locale = __loc;
    219   1.1  mrg 	return __tmp;
    220   1.1  mrg       }
    221   1.1  mrg 
    222   1.1  mrg       /**
    223   1.1  mrg        *  @brief  Locale access.
    224   1.1  mrg        *  @return  The current locale in effect.
    225   1.1  mrg        *
    226   1.1  mrg        *  If pubimbue(loc) has been called, then the most recent @c loc
    227   1.1  mrg        *  is returned.  Otherwise the global locale in effect at the time
    228   1.1  mrg        *  of construction is returned.
    229   1.1  mrg       */
    230   1.5  mrg       locale
    231   1.1  mrg       getloc() const
    232   1.5  mrg       { return _M_buf_locale; }
    233   1.1  mrg 
    234   1.1  mrg       // [27.5.2.2.2] buffer management and positioning
    235  1.11  mrg       ///@{
    236   1.1  mrg       /**
    237   1.1  mrg        *  @brief  Entry points for derived buffer functions.
    238   1.1  mrg        *
    239   1.1  mrg        *  The public versions of @c pubfoo dispatch to the protected
    240   1.1  mrg        *  derived @c foo member functions, passing the arguments (if any)
    241   1.1  mrg        *  and returning the result unchanged.
    242   1.1  mrg       */
    243   1.3  mrg       basic_streambuf*
    244   1.5  mrg       pubsetbuf(char_type* __s, streamsize __n)
    245   1.1  mrg       { return this->setbuf(__s, __n); }
    246   1.1  mrg 
    247   1.3  mrg       /**
    248   1.3  mrg        *  @brief  Alters the stream position.
    249   1.3  mrg        *  @param  __off  Offset.
    250   1.3  mrg        *  @param  __way  Value for ios_base::seekdir.
    251   1.3  mrg        *  @param  __mode Value for ios_base::openmode.
    252   1.3  mrg        *
    253   1.3  mrg        *  Calls virtual seekoff function.
    254   1.3  mrg       */
    255   1.5  mrg       pos_type
    256   1.5  mrg       pubseekoff(off_type __off, ios_base::seekdir __way,
    257   1.1  mrg 		 ios_base::openmode __mode = ios_base::in | ios_base::out)
    258   1.1  mrg       { return this->seekoff(__off, __way, __mode); }
    259   1.1  mrg 
    260   1.3  mrg       /**
    261   1.3  mrg        *  @brief  Alters the stream position.
    262   1.3  mrg        *  @param  __sp  Position
    263   1.3  mrg        *  @param  __mode Value for ios_base::openmode.
    264   1.3  mrg        *
    265   1.3  mrg        *  Calls virtual seekpos function.
    266   1.3  mrg       */
    267   1.5  mrg       pos_type
    268   1.1  mrg       pubseekpos(pos_type __sp,
    269   1.1  mrg 		 ios_base::openmode __mode = ios_base::in | ios_base::out)
    270   1.1  mrg       { return this->seekpos(__sp, __mode); }
    271   1.1  mrg 
    272   1.3  mrg       /**
    273   1.3  mrg        *  @brief  Calls virtual sync function.
    274   1.3  mrg       */
    275   1.5  mrg       int
    276   1.1  mrg       pubsync() { return this->sync(); }
    277  1.11  mrg       ///@}
    278   1.1  mrg 
    279   1.1  mrg       // [27.5.2.2.3] get area
    280   1.1  mrg       /**
    281   1.1  mrg        *  @brief  Looking ahead into the stream.
    282   1.1  mrg        *  @return  The number of characters available.
    283   1.1  mrg        *
    284   1.1  mrg        *  If a read position is available, returns the number of characters
    285   1.1  mrg        *  available for reading before the buffer must be refilled.
    286   1.1  mrg        *  Otherwise returns the derived @c showmanyc().
    287   1.1  mrg       */
    288   1.5  mrg       streamsize
    289   1.5  mrg       in_avail()
    290   1.5  mrg       {
    291   1.1  mrg 	const streamsize __ret = this->egptr() - this->gptr();
    292   1.1  mrg 	return __ret ? __ret : this->showmanyc();
    293   1.1  mrg       }
    294   1.1  mrg 
    295   1.1  mrg       /**
    296   1.1  mrg        *  @brief  Getting the next character.
    297   1.1  mrg        *  @return  The next character, or eof.
    298   1.1  mrg        *
    299   1.1  mrg        *  Calls @c sbumpc(), and if that function returns
    300   1.1  mrg        *  @c traits::eof(), so does this function.  Otherwise, @c sgetc().
    301   1.1  mrg       */
    302   1.5  mrg       int_type
    303   1.1  mrg       snextc()
    304   1.1  mrg       {
    305   1.1  mrg 	int_type __ret = traits_type::eof();
    306   1.5  mrg 	if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
    307   1.1  mrg 						       __ret), true))
    308   1.1  mrg 	  __ret = this->sgetc();
    309   1.1  mrg 	return __ret;
    310   1.1  mrg       }
    311   1.1  mrg 
    312   1.1  mrg       /**
    313   1.1  mrg        *  @brief  Getting the next character.
    314   1.1  mrg        *  @return  The next character, or eof.
    315   1.1  mrg        *
    316   1.1  mrg        *  If the input read position is available, returns that character
    317   1.1  mrg        *  and increments the read pointer, otherwise calls and returns
    318   1.1  mrg        *  @c uflow().
    319   1.1  mrg       */
    320   1.5  mrg       int_type
    321   1.1  mrg       sbumpc()
    322   1.1  mrg       {
    323   1.1  mrg 	int_type __ret;
    324   1.1  mrg 	if (__builtin_expect(this->gptr() < this->egptr(), true))
    325   1.1  mrg 	  {
    326   1.1  mrg 	    __ret = traits_type::to_int_type(*this->gptr());
    327   1.1  mrg 	    this->gbump(1);
    328   1.1  mrg 	  }
    329   1.5  mrg 	else
    330   1.1  mrg 	  __ret = this->uflow();
    331   1.1  mrg 	return __ret;
    332   1.1  mrg       }
    333   1.1  mrg 
    334   1.1  mrg       /**
    335   1.1  mrg        *  @brief  Getting the next character.
    336   1.1  mrg        *  @return  The next character, or eof.
    337   1.1  mrg        *
    338   1.1  mrg        *  If the input read position is available, returns that character,
    339   1.5  mrg        *  otherwise calls and returns @c underflow().  Does not move the
    340   1.1  mrg        *  read position after fetching the character.
    341   1.1  mrg       */
    342   1.5  mrg       int_type
    343   1.1  mrg       sgetc()
    344   1.1  mrg       {
    345   1.1  mrg 	int_type __ret;
    346   1.1  mrg 	if (__builtin_expect(this->gptr() < this->egptr(), true))
    347   1.1  mrg 	  __ret = traits_type::to_int_type(*this->gptr());
    348   1.5  mrg 	else
    349   1.1  mrg 	  __ret = this->underflow();
    350   1.1  mrg 	return __ret;
    351   1.1  mrg       }
    352   1.1  mrg 
    353   1.1  mrg       /**
    354   1.1  mrg        *  @brief  Entry point for xsgetn.
    355   1.3  mrg        *  @param  __s  A buffer area.
    356   1.3  mrg        *  @param  __n  A count.
    357   1.1  mrg        *
    358   1.3  mrg        *  Returns xsgetn(__s,__n).  The effect is to fill @a __s[0] through
    359   1.3  mrg        *  @a __s[__n-1] with characters from the input sequence, if possible.
    360   1.1  mrg       */
    361   1.5  mrg       streamsize
    362   1.1  mrg       sgetn(char_type* __s, streamsize __n)
    363   1.1  mrg       { return this->xsgetn(__s, __n); }
    364   1.1  mrg 
    365   1.1  mrg       // [27.5.2.2.4] putback
    366   1.1  mrg       /**
    367   1.1  mrg        *  @brief  Pushing characters back into the input stream.
    368   1.3  mrg        *  @param  __c  The character to push back.
    369   1.1  mrg        *  @return  The previous character, if possible.
    370   1.1  mrg        *
    371   1.3  mrg        *  Similar to sungetc(), but @a __c is pushed onto the stream
    372   1.1  mrg        *  instead of <em>the previous character.</em> If successful,
    373   1.1  mrg        *  the next character fetched from the input stream will be @a
    374   1.3  mrg        *  __c.
    375   1.1  mrg       */
    376   1.5  mrg       int_type
    377   1.1  mrg       sputbackc(char_type __c)
    378   1.1  mrg       {
    379   1.1  mrg 	int_type __ret;
    380   1.1  mrg 	const bool __testpos = this->eback() < this->gptr();
    381   1.5  mrg 	if (__builtin_expect(!__testpos ||
    382   1.1  mrg 			     !traits_type::eq(__c, this->gptr()[-1]), false))
    383   1.1  mrg 	  __ret = this->pbackfail(traits_type::to_int_type(__c));
    384   1.5  mrg 	else
    385   1.1  mrg 	  {
    386   1.1  mrg 	    this->gbump(-1);
    387   1.1  mrg 	    __ret = traits_type::to_int_type(*this->gptr());
    388   1.1  mrg 	  }
    389   1.1  mrg 	return __ret;
    390   1.1  mrg       }
    391   1.1  mrg 
    392   1.1  mrg       /**
    393   1.1  mrg        *  @brief  Moving backwards in the input stream.
    394   1.1  mrg        *  @return  The previous character, if possible.
    395   1.1  mrg        *
    396   1.1  mrg        *  If a putback position is available, this function decrements
    397   1.1  mrg        *  the input pointer and returns that character.  Otherwise,
    398   1.1  mrg        *  calls and returns pbackfail().  The effect is to @a unget
    399   1.1  mrg        *  the last character @a gotten.
    400   1.1  mrg       */
    401   1.5  mrg       int_type
    402   1.1  mrg       sungetc()
    403   1.1  mrg       {
    404   1.1  mrg 	int_type __ret;
    405   1.1  mrg 	if (__builtin_expect(this->eback() < this->gptr(), true))
    406   1.1  mrg 	  {
    407   1.1  mrg 	    this->gbump(-1);
    408   1.1  mrg 	    __ret = traits_type::to_int_type(*this->gptr());
    409   1.1  mrg 	  }
    410   1.5  mrg 	else
    411   1.1  mrg 	  __ret = this->pbackfail();
    412   1.1  mrg 	return __ret;
    413   1.1  mrg       }
    414   1.1  mrg 
    415   1.1  mrg       // [27.5.2.2.5] put area
    416   1.1  mrg       /**
    417   1.1  mrg        *  @brief  Entry point for all single-character output functions.
    418   1.3  mrg        *  @param  __c  A character to output.
    419   1.3  mrg        *  @return  @a __c, if possible.
    420   1.1  mrg        *
    421   1.1  mrg        *  One of two public output functions.
    422   1.1  mrg        *
    423   1.1  mrg        *  If a write position is available for the output sequence (i.e.,
    424   1.3  mrg        *  the buffer is not full), stores @a __c in that position, increments
    425   1.3  mrg        *  the position, and returns @c traits::to_int_type(__c).  If a write
    426   1.3  mrg        *  position is not available, returns @c overflow(__c).
    427   1.1  mrg       */
    428   1.5  mrg       int_type
    429   1.1  mrg       sputc(char_type __c)
    430   1.1  mrg       {
    431   1.1  mrg 	int_type __ret;
    432   1.1  mrg 	if (__builtin_expect(this->pptr() < this->epptr(), true))
    433   1.1  mrg 	  {
    434   1.1  mrg 	    *this->pptr() = __c;
    435   1.1  mrg 	    this->pbump(1);
    436   1.1  mrg 	    __ret = traits_type::to_int_type(__c);
    437   1.1  mrg 	  }
    438   1.1  mrg 	else
    439   1.1  mrg 	  __ret = this->overflow(traits_type::to_int_type(__c));
    440   1.1  mrg 	return __ret;
    441   1.1  mrg       }
    442   1.1  mrg 
    443   1.1  mrg       /**
    444   1.1  mrg        *  @brief  Entry point for all single-character output functions.
    445   1.3  mrg        *  @param  __s  A buffer read area.
    446   1.3  mrg        *  @param  __n  A count.
    447   1.1  mrg        *
    448   1.1  mrg        *  One of two public output functions.
    449   1.1  mrg        *
    450   1.1  mrg        *
    451   1.3  mrg        *  Returns xsputn(__s,__n).  The effect is to write @a __s[0] through
    452   1.3  mrg        *  @a __s[__n-1] to the output sequence, if possible.
    453   1.1  mrg       */
    454   1.5  mrg       streamsize
    455   1.1  mrg       sputn(const char_type* __s, streamsize __n)
    456   1.1  mrg       { return this->xsputn(__s, __n); }
    457   1.1  mrg 
    458   1.1  mrg     protected:
    459   1.1  mrg       /**
    460   1.1  mrg        *  @brief  Base constructor.
    461   1.1  mrg        *
    462   1.1  mrg        *  Only called from derived constructors, and sets up all the
    463   1.1  mrg        *  buffer data to zero, including the pointers described in the
    464   1.1  mrg        *  basic_streambuf class description.  Note that, as a result,
    465   1.1  mrg        *  - the class starts with no read nor write positions available,
    466   1.1  mrg        *  - this is not an error
    467   1.1  mrg       */
    468   1.1  mrg       basic_streambuf()
    469   1.5  mrg       : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
    470   1.1  mrg       _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
    471   1.5  mrg       _M_buf_locale(locale())
    472   1.1  mrg       { }
    473   1.1  mrg 
    474   1.1  mrg       // [27.5.2.3.1] get area access
    475  1.11  mrg       ///@{
    476   1.1  mrg       /**
    477   1.1  mrg        *  @brief  Access to the get area.
    478   1.1  mrg        *
    479   1.1  mrg        *  These functions are only available to other protected functions,
    480   1.1  mrg        *  including derived classes.
    481   1.1  mrg        *
    482   1.1  mrg        *  - eback() returns the beginning pointer for the input sequence
    483   1.1  mrg        *  - gptr() returns the next pointer for the input sequence
    484   1.1  mrg        *  - egptr() returns the end pointer for the input sequence
    485   1.1  mrg       */
    486   1.5  mrg       char_type*
    487   1.1  mrg       eback() const { return _M_in_beg; }
    488   1.1  mrg 
    489   1.5  mrg       char_type*
    490   1.1  mrg       gptr()  const { return _M_in_cur;  }
    491   1.1  mrg 
    492   1.5  mrg       char_type*
    493   1.1  mrg       egptr() const { return _M_in_end; }
    494  1.11  mrg       ///@}
    495   1.1  mrg 
    496   1.1  mrg       /**
    497   1.1  mrg        *  @brief  Moving the read position.
    498   1.3  mrg        *  @param  __n  The delta by which to move.
    499   1.1  mrg        *
    500   1.1  mrg        *  This just advances the read position without returning any data.
    501   1.1  mrg       */
    502   1.5  mrg       void
    503   1.1  mrg       gbump(int __n) { _M_in_cur += __n; }
    504   1.1  mrg 
    505   1.1  mrg       /**
    506   1.1  mrg        *  @brief  Setting the three read area pointers.
    507   1.3  mrg        *  @param  __gbeg  A pointer.
    508   1.3  mrg        *  @param  __gnext  A pointer.
    509   1.3  mrg        *  @param  __gend  A pointer.
    510   1.3  mrg        *  @post  @a __gbeg == @c eback(), @a __gnext == @c gptr(), and
    511   1.3  mrg        *         @a __gend == @c egptr()
    512   1.1  mrg       */
    513   1.5  mrg       void
    514   1.1  mrg       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
    515   1.1  mrg       {
    516   1.1  mrg 	_M_in_beg = __gbeg;
    517   1.1  mrg 	_M_in_cur = __gnext;
    518   1.1  mrg 	_M_in_end = __gend;
    519   1.1  mrg       }
    520   1.1  mrg 
    521   1.1  mrg       // [27.5.2.3.2] put area access
    522  1.11  mrg       ///@{
    523   1.1  mrg       /**
    524   1.1  mrg        *  @brief  Access to the put area.
    525   1.1  mrg        *
    526   1.1  mrg        *  These functions are only available to other protected functions,
    527   1.1  mrg        *  including derived classes.
    528   1.1  mrg        *
    529   1.1  mrg        *  - pbase() returns the beginning pointer for the output sequence
    530   1.1  mrg        *  - pptr() returns the next pointer for the output sequence
    531   1.1  mrg        *  - epptr() returns the end pointer for the output sequence
    532   1.1  mrg       */
    533   1.5  mrg       char_type*
    534   1.1  mrg       pbase() const { return _M_out_beg; }
    535   1.1  mrg 
    536   1.5  mrg       char_type*
    537   1.1  mrg       pptr() const { return _M_out_cur; }
    538   1.1  mrg 
    539   1.5  mrg       char_type*
    540   1.1  mrg       epptr() const { return _M_out_end; }
    541  1.11  mrg       ///@}
    542   1.1  mrg 
    543   1.1  mrg       /**
    544   1.1  mrg        *  @brief  Moving the write position.
    545   1.3  mrg        *  @param  __n  The delta by which to move.
    546   1.1  mrg        *
    547   1.1  mrg        *  This just advances the write position without returning any data.
    548   1.1  mrg       */
    549   1.5  mrg       void
    550   1.1  mrg       pbump(int __n) { _M_out_cur += __n; }
    551   1.1  mrg 
    552   1.1  mrg       /**
    553   1.1  mrg        *  @brief  Setting the three write area pointers.
    554   1.3  mrg        *  @param  __pbeg  A pointer.
    555   1.3  mrg        *  @param  __pend  A pointer.
    556   1.3  mrg        *  @post  @a __pbeg == @c pbase(), @a __pbeg == @c pptr(), and
    557   1.3  mrg        *         @a __pend == @c epptr()
    558   1.1  mrg       */
    559   1.5  mrg       void
    560   1.1  mrg       setp(char_type* __pbeg, char_type* __pend)
    561   1.5  mrg       {
    562   1.5  mrg 	_M_out_beg = _M_out_cur = __pbeg;
    563   1.1  mrg 	_M_out_end = __pend;
    564   1.1  mrg       }
    565   1.1  mrg 
    566   1.1  mrg       // [27.5.2.4] virtual functions
    567   1.1  mrg       // [27.5.2.4.1] locales
    568   1.1  mrg       /**
    569   1.1  mrg        *  @brief  Changes translations.
    570   1.3  mrg        *  @param  __loc  A new locale.
    571   1.1  mrg        *
    572   1.1  mrg        *  Translations done during I/O which depend on the current
    573   1.1  mrg        *  locale are changed by this call.  The standard adds,
    574   1.1  mrg        *  <em>Between invocations of this function a class derived
    575   1.1  mrg        *  from streambuf can safely cache results of calls to locale
    576   1.1  mrg        *  functions and to members of facets so obtained.</em>
    577   1.1  mrg        *
    578   1.1  mrg        *  @note  Base class version does nothing.
    579   1.1  mrg       */
    580   1.5  mrg       virtual void
    581   1.9  mrg       imbue(const locale& __loc _IsUnused)
    582   1.1  mrg       { }
    583   1.1  mrg 
    584   1.1  mrg       // [27.5.2.4.2] buffer management and positioning
    585   1.1  mrg       /**
    586   1.1  mrg        *  @brief  Manipulates the buffer.
    587   1.1  mrg        *
    588   1.1  mrg        *  Each derived class provides its own appropriate behavior.  See
    589   1.5  mrg        *  the next-to-last paragraph of
    590   1.5  mrg        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
    591   1.1  mrg        *  for more on this function.
    592   1.1  mrg        *
    593   1.1  mrg        *  @note  Base class version does nothing, returns @c this.
    594   1.1  mrg       */
    595   1.5  mrg       virtual basic_streambuf<char_type,_Traits>*
    596   1.1  mrg       setbuf(char_type*, streamsize)
    597   1.1  mrg       {	return this; }
    598   1.5  mrg 
    599   1.1  mrg       /**
    600   1.1  mrg        *  @brief  Alters the stream positions.
    601   1.1  mrg        *
    602   1.1  mrg        *  Each derived class provides its own appropriate behavior.
    603   1.1  mrg        *  @note  Base class version does nothing, returns a @c pos_type
    604   1.1  mrg        *         that represents an invalid stream position.
    605   1.1  mrg       */
    606   1.5  mrg       virtual pos_type
    607   1.1  mrg       seekoff(off_type, ios_base::seekdir,
    608   1.1  mrg 	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
    609   1.5  mrg       { return pos_type(off_type(-1)); }
    610   1.1  mrg 
    611   1.1  mrg       /**
    612   1.1  mrg        *  @brief  Alters the stream positions.
    613   1.1  mrg        *
    614   1.1  mrg        *  Each derived class provides its own appropriate behavior.
    615   1.1  mrg        *  @note  Base class version does nothing, returns a @c pos_type
    616   1.1  mrg        *         that represents an invalid stream position.
    617   1.1  mrg       */
    618   1.5  mrg       virtual pos_type
    619   1.5  mrg       seekpos(pos_type,
    620   1.1  mrg 	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
    621   1.5  mrg       { return pos_type(off_type(-1)); }
    622   1.1  mrg 
    623   1.1  mrg       /**
    624   1.1  mrg        *  @brief  Synchronizes the buffer arrays with the controlled sequences.
    625   1.1  mrg        *  @return  -1 on failure.
    626   1.1  mrg        *
    627   1.1  mrg        *  Each derived class provides its own appropriate behavior,
    628   1.1  mrg        *  including the definition of @a failure.
    629   1.1  mrg        *  @note  Base class version does nothing, returns zero.
    630   1.1  mrg       */
    631   1.5  mrg       virtual int
    632   1.1  mrg       sync() { return 0; }
    633   1.1  mrg 
    634   1.1  mrg       // [27.5.2.4.3] get area
    635   1.1  mrg       /**
    636   1.1  mrg        *  @brief  Investigating the data available.
    637   1.1  mrg        *  @return  An estimate of the number of characters available in the
    638   1.1  mrg        *           input sequence, or -1.
    639   1.1  mrg        *
    640   1.1  mrg        *  <em>If it returns a positive value, then successive calls to
    641   1.1  mrg        *  @c underflow() will not return @c traits::eof() until at
    642   1.1  mrg        *  least that number of characters have been supplied.  If @c
    643   1.1  mrg        *  showmanyc() returns -1, then calls to @c underflow() or @c
    644   1.1  mrg        *  uflow() will fail.</em> [27.5.2.4.3]/1
    645   1.1  mrg        *
    646   1.1  mrg        *  @note  Base class version does nothing, returns zero.
    647   1.1  mrg        *  @note  The standard adds that <em>the intention is not only that the
    648   1.1  mrg        *         calls [to underflow or uflow] will not return @c eof() but
    649   1.1  mrg        *         that they will return immediately.</em>
    650   1.1  mrg        *  @note  The standard adds that <em>the morphemes of @c showmanyc are
    651   1.1  mrg        *         @b es-how-many-see, not @b show-manic.</em>
    652   1.1  mrg       */
    653   1.5  mrg       virtual streamsize
    654   1.1  mrg       showmanyc() { return 0; }
    655   1.1  mrg 
    656   1.1  mrg       /**
    657   1.1  mrg        *  @brief  Multiple character extraction.
    658   1.3  mrg        *  @param  __s  A buffer area.
    659   1.3  mrg        *  @param  __n  Maximum number of characters to assign.
    660   1.1  mrg        *  @return  The number of characters assigned.
    661   1.1  mrg        *
    662   1.3  mrg        *  Fills @a __s[0] through @a __s[__n-1] with characters from the input
    663   1.3  mrg        *  sequence, as if by @c sbumpc().  Stops when either @a __n characters
    664   1.1  mrg        *  have been copied, or when @c traits::eof() would be copied.
    665   1.1  mrg        *
    666   1.1  mrg        *  It is expected that derived classes provide a more efficient
    667   1.1  mrg        *  implementation by overriding this definition.
    668   1.1  mrg       */
    669   1.5  mrg       virtual streamsize
    670   1.1  mrg       xsgetn(char_type* __s, streamsize __n);
    671   1.1  mrg 
    672   1.1  mrg       /**
    673   1.1  mrg        *  @brief  Fetches more data from the controlled sequence.
    674   1.1  mrg        *  @return  The first character from the <em>pending sequence</em>.
    675   1.1  mrg        *
    676   1.1  mrg        *  Informally, this function is called when the input buffer is
    677   1.1  mrg        *  exhausted (or does not exist, as buffering need not actually be
    678   1.1  mrg        *  done).  If a buffer exists, it is @a refilled.  In either case, the
    679   1.1  mrg        *  next available character is returned, or @c traits::eof() to
    680   1.1  mrg        *  indicate a null pending sequence.
    681   1.1  mrg        *
    682   1.1  mrg        *  For a formal definition of the pending sequence, see a good text
    683   1.1  mrg        *  such as Langer & Kreft, or [27.5.2.4.3]/7-14.
    684   1.1  mrg        *
    685   1.1  mrg        *  A functioning input streambuf can be created by overriding only
    686   1.1  mrg        *  this function (no buffer area will be used).  For an example, see
    687   1.5  mrg        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html
    688   1.1  mrg        *
    689   1.1  mrg        *  @note  Base class version does nothing, returns eof().
    690   1.1  mrg       */
    691   1.5  mrg       virtual int_type
    692   1.1  mrg       underflow()
    693   1.1  mrg       { return traits_type::eof(); }
    694   1.1  mrg 
    695   1.1  mrg       /**
    696   1.1  mrg        *  @brief  Fetches more data from the controlled sequence.
    697   1.1  mrg        *  @return  The first character from the <em>pending sequence</em>.
    698   1.1  mrg        *
    699   1.1  mrg        *  Informally, this function does the same thing as @c underflow(),
    700   1.1  mrg        *  and in fact is required to call that function.  It also returns
    701   1.1  mrg        *  the new character, like @c underflow() does.  However, this
    702   1.1  mrg        *  function also moves the read position forward by one.
    703   1.1  mrg       */
    704   1.5  mrg       virtual int_type
    705   1.5  mrg       uflow()
    706   1.1  mrg       {
    707   1.1  mrg 	int_type __ret = traits_type::eof();
    708   1.5  mrg 	const bool __testeof = traits_type::eq_int_type(this->underflow(),
    709   1.1  mrg 							__ret);
    710   1.1  mrg 	if (!__testeof)
    711   1.1  mrg 	  {
    712   1.1  mrg 	    __ret = traits_type::to_int_type(*this->gptr());
    713   1.1  mrg 	    this->gbump(1);
    714   1.1  mrg 	  }
    715   1.5  mrg 	return __ret;
    716   1.1  mrg       }
    717   1.1  mrg 
    718   1.1  mrg       // [27.5.2.4.4] putback
    719   1.1  mrg       /**
    720   1.1  mrg        *  @brief  Tries to back up the input sequence.
    721   1.3  mrg        *  @param  __c  The character to be inserted back into the sequence.
    722   1.1  mrg        *  @return  eof() on failure, <em>some other value</em> on success
    723   1.1  mrg        *  @post  The constraints of @c gptr(), @c eback(), and @c pptr()
    724   1.1  mrg        *         are the same as for @c underflow().
    725   1.1  mrg        *
    726   1.1  mrg        *  @note  Base class version does nothing, returns eof().
    727   1.1  mrg       */
    728   1.5  mrg       virtual int_type
    729   1.9  mrg       pbackfail(int_type __c _IsUnused  = traits_type::eof())
    730   1.1  mrg       { return traits_type::eof(); }
    731   1.1  mrg 
    732   1.1  mrg       // Put area:
    733   1.1  mrg       /**
    734   1.1  mrg        *  @brief  Multiple character insertion.
    735   1.3  mrg        *  @param  __s  A buffer area.
    736   1.3  mrg        *  @param  __n  Maximum number of characters to write.
    737   1.1  mrg        *  @return  The number of characters written.
    738   1.1  mrg        *
    739   1.3  mrg        *  Writes @a __s[0] through @a __s[__n-1] to the output sequence, as if
    740   1.1  mrg        *  by @c sputc().  Stops when either @a n characters have been
    741   1.1  mrg        *  copied, or when @c sputc() would return @c traits::eof().
    742   1.1  mrg        *
    743   1.1  mrg        *  It is expected that derived classes provide a more efficient
    744   1.1  mrg        *  implementation by overriding this definition.
    745   1.1  mrg       */
    746   1.5  mrg       virtual streamsize
    747   1.1  mrg       xsputn(const char_type* __s, streamsize __n);
    748   1.1  mrg 
    749   1.1  mrg       /**
    750   1.1  mrg        *  @brief  Consumes data from the buffer; writes to the
    751   1.1  mrg        *          controlled sequence.
    752   1.3  mrg        *  @param  __c  An additional character to consume.
    753   1.1  mrg        *  @return  eof() to indicate failure, something else (usually
    754   1.3  mrg        *           @a __c, or not_eof())
    755   1.1  mrg        *
    756   1.1  mrg        *  Informally, this function is called when the output buffer
    757   1.1  mrg        *  is full (or does not exist, as buffering need not actually
    758   1.1  mrg        *  be done).  If a buffer exists, it is @a consumed, with
    759   1.1  mrg        *  <em>some effect</em> on the controlled sequence.
    760   1.1  mrg        *  (Typically, the buffer is written out to the sequence
    761   1.1  mrg        *  verbatim.)  In either case, the character @a c is also
    762   1.3  mrg        *  written out, if @a __c is not @c eof().
    763   1.1  mrg        *
    764   1.1  mrg        *  For a formal definition of this function, see a good text
    765   1.1  mrg        *  such as Langer & Kreft, or [27.5.2.4.5]/3-7.
    766   1.1  mrg        *
    767   1.1  mrg        *  A functioning output streambuf can be created by overriding only
    768   1.1  mrg        *  this function (no buffer area will be used).
    769   1.1  mrg        *
    770   1.1  mrg        *  @note  Base class version does nothing, returns eof().
    771   1.1  mrg       */
    772   1.5  mrg       virtual int_type
    773   1.9  mrg       overflow(int_type __c _IsUnused  = traits_type::eof())
    774   1.1  mrg       { return traits_type::eof(); }
    775   1.1  mrg 
    776   1.9  mrg #if _GLIBCXX_USE_DEPRECATED && __cplusplus <= 201402L
    777   1.9  mrg     // Annex D.6 (removed in C++17)
    778   1.1  mrg     public:
    779   1.1  mrg       /**
    780   1.1  mrg        *  @brief  Tosses a character.
    781   1.1  mrg        *
    782   1.1  mrg        *  Advances the read pointer, ignoring the character that would have
    783   1.1  mrg        *  been read.
    784   1.1  mrg        *
    785   1.1  mrg        *  See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
    786   1.1  mrg        */
    787  1.11  mrg       _GLIBCXX_DEPRECATED_SUGGEST("std::basic_streambuf::sbumpc")
    788   1.5  mrg       void
    789   1.5  mrg       stossc()
    790   1.1  mrg       {
    791   1.5  mrg 	if (this->gptr() < this->egptr())
    792   1.1  mrg 	  this->gbump(1);
    793   1.5  mrg 	else
    794   1.1  mrg 	  this->uflow();
    795   1.1  mrg       }
    796   1.1  mrg #endif
    797   1.1  mrg 
    798   1.3  mrg       // Also used by specializations for char and wchar_t in src.
    799   1.5  mrg       void
    800   1.3  mrg       __safe_gbump(streamsize __n) { _M_in_cur += __n; }
    801   1.3  mrg 
    802   1.3  mrg       void
    803   1.3  mrg       __safe_pbump(streamsize __n) { _M_out_cur += __n; }
    804   1.3  mrg 
    805   1.5  mrg #if __cplusplus < 201103L
    806   1.1  mrg     private:
    807   1.5  mrg #else
    808   1.5  mrg     protected:
    809   1.5  mrg #endif
    810   1.5  mrg       basic_streambuf(const basic_streambuf&);
    811   1.1  mrg 
    812   1.3  mrg       basic_streambuf&
    813   1.5  mrg       operator=(const basic_streambuf&);
    814   1.5  mrg 
    815   1.5  mrg #if __cplusplus >= 201103L
    816   1.5  mrg       void
    817   1.5  mrg       swap(basic_streambuf& __sb)
    818   1.5  mrg       {
    819   1.5  mrg 	std::swap(_M_in_beg, __sb._M_in_beg);
    820   1.5  mrg 	std::swap(_M_in_cur, __sb._M_in_cur);
    821   1.5  mrg 	std::swap(_M_in_end, __sb._M_in_end);
    822   1.5  mrg 	std::swap(_M_out_beg, __sb._M_out_beg);
    823   1.5  mrg 	std::swap(_M_out_cur, __sb._M_out_cur);
    824   1.5  mrg 	std::swap(_M_out_end, __sb._M_out_end);
    825   1.5  mrg 	std::swap(_M_buf_locale, __sb._M_buf_locale);
    826   1.5  mrg       }
    827   1.5  mrg #endif
    828   1.1  mrg     };
    829   1.1  mrg 
    830   1.5  mrg #if __cplusplus >= 201103L
    831   1.5  mrg   template<typename _CharT, typename _Traits>
    832   1.5  mrg     std::basic_streambuf<_CharT, _Traits>::
    833   1.5  mrg     basic_streambuf(const basic_streambuf&) = default;
    834   1.5  mrg 
    835   1.5  mrg   template<typename _CharT, typename _Traits>
    836   1.5  mrg     std::basic_streambuf<_CharT, _Traits>&
    837   1.5  mrg     std::basic_streambuf<_CharT, _Traits>::
    838   1.5  mrg     operator=(const basic_streambuf&) = default;
    839   1.5  mrg #endif
    840   1.5  mrg 
    841   1.1  mrg   // Explicit specialization declarations, defined in src/streambuf.cc.
    842   1.1  mrg   template<>
    843   1.1  mrg     streamsize
    844   1.1  mrg     __copy_streambufs_eof(basic_streambuf<char>* __sbin,
    845   1.1  mrg 			  basic_streambuf<char>* __sbout, bool& __ineof);
    846   1.1  mrg #ifdef _GLIBCXX_USE_WCHAR_T
    847   1.1  mrg   template<>
    848   1.1  mrg     streamsize
    849   1.1  mrg     __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
    850   1.1  mrg 			  basic_streambuf<wchar_t>* __sbout, bool& __ineof);
    851   1.1  mrg #endif
    852   1.1  mrg 
    853   1.9  mrg #undef _IsUnused
    854   1.9  mrg 
    855   1.3  mrg _GLIBCXX_END_NAMESPACE_VERSION
    856   1.3  mrg } // namespace
    857   1.1  mrg 
    858   1.3  mrg #include <bits/streambuf.tcc>
    859   1.1  mrg 
    860   1.1  mrg #endif /* _GLIBCXX_STREAMBUF */
    861