197403Sobrien// Stream buffer classes -*- C++ -*-
297403Sobrien
3169691Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
497403Sobrien// Free Software Foundation, Inc.
597403Sobrien//
697403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
797403Sobrien// software; you can redistribute it and/or modify it under the
897403Sobrien// terms of the GNU General Public License as published by the
997403Sobrien// Free Software Foundation; either version 2, or (at your option)
1097403Sobrien// any later version.
1197403Sobrien
1297403Sobrien// This library is distributed in the hope that it will be useful,
1397403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1497403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1597403Sobrien// GNU General Public License for more details.
1697403Sobrien
1797403Sobrien// You should have received a copy of the GNU General Public License along
1897403Sobrien// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
2097403Sobrien// USA.
2197403Sobrien
2297403Sobrien// As a special exception, you may use this file as part of a free software
2397403Sobrien// library without restriction.  Specifically, if other files instantiate
2497403Sobrien// templates or use macros or inline functions from this file, or you compile
2597403Sobrien// this file and link it with other files to produce an executable, this
2697403Sobrien// file does not by itself cause the resulting executable to be covered by
2797403Sobrien// the GNU General Public License.  This exception does not however
2897403Sobrien// invalidate any other reasons why the executable file might be covered by
2997403Sobrien// the GNU General Public License.
3097403Sobrien
31169691Skan/** @file streambuf
32169691Skan *  This is a Standard C++ Library header.
33169691Skan */
34169691Skan
3597403Sobrien//
3697403Sobrien// ISO C++ 14882: 27.5  Stream buffers
3797403Sobrien//
3897403Sobrien
39169691Skan#ifndef _GLIBXX_STREAMBUF
40169691Skan#define _GLIBXX_STREAMBUF 1
4197403Sobrien
4297403Sobrien#pragma GCC system_header
4397403Sobrien
4497403Sobrien#include <bits/c++config.h>
4597403Sobrien#include <iosfwd>
4697403Sobrien#include <bits/localefwd.h>
4797403Sobrien#include <bits/ios_base.h>
48169691Skan#include <bits/cpp_type_traits.h>
49169691Skan#include <ext/type_traits.h>
5097403Sobrien
51169691Skan_GLIBCXX_BEGIN_NAMESPACE(std)
52169691Skan
53117397Skan  /**
54117397Skan   *  @if maint
55117397Skan   *  Does stuff.
56117397Skan   *  @endif
57117397Skan  */
5897403Sobrien  template<typename _CharT, typename _Traits>
5997403Sobrien    streamsize
60169691Skan    __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
61169691Skan			  basic_streambuf<_CharT, _Traits>*, bool&);
62169691Skan
63117397Skan  /**
64117397Skan   *  @brief  The actual work of input and output (interface).
65117397Skan   *
66117397Skan   *  This is a base class.  Derived stream buffers each control a
67117397Skan   *  pair of character sequences:  one for input, and one for output.
68117397Skan   *
69117397Skan   *  Section [27.5.1] of the standard describes the requirements and
70117397Skan   *  behavior of stream buffer classes.  That section (three paragraphs)
71117397Skan   *  is reproduced here, for simplicity and accuracy.
72117397Skan   *
73117397Skan   *  -# Stream buffers can impose various constraints on the sequences
74117397Skan   *     they control.  Some constraints are:
75117397Skan   *     - The controlled input sequence can be not readable.
76117397Skan   *     - The controlled output sequence can be not writable.
77117397Skan   *     - The controlled sequences can be associated with the contents of
78117397Skan   *       other representations for character sequences, such as external
79117397Skan   *       files.
80117397Skan   *     - The controlled sequences can support operations @e directly to or
81117397Skan   *       from associated sequences.
82117397Skan   *     - The controlled sequences can impose limitations on how the
83117397Skan   *       program can read characters from a sequence, write characters to
84117397Skan   *       a sequence, put characters back into an input sequence, or alter
85117397Skan   *       the stream position.
86117397Skan   *     .
87117397Skan   *  -# Each sequence is characterized by three pointers which, if non-null,
88117397Skan   *     all point into the same @c charT array object.  The array object
89117397Skan   *     represents, at any moment, a (sub)sequence of characters from the
90117397Skan   *     sequence.  Operations performed on a sequence alter the values
91117397Skan   *     stored in these pointers, perform reads and writes directly to or
92117397Skan   *     from associated sequences, and alter "the stream position" and
93117397Skan   *     conversion state as needed to maintain this subsequence relationship.
94117397Skan   *     The three pointers are:
95117397Skan   *     - the <em>beginning pointer</em>, or lowest element address in the
96117397Skan   *       array (called @e xbeg here);
97117397Skan   *     - the <em>next pointer</em>, or next element address that is a
98117397Skan   *       current candidate for reading or writing (called @e xnext here);
99117397Skan   *     - the <em>end pointer</em>, or first element address beyond the
100117397Skan   *       end of the array (called @e xend here).
101117397Skan   *     .
102117397Skan   *  -# The following semantic constraints shall always apply for any set
103117397Skan   *     of three pointers for a sequence, using the pointer names given
104117397Skan   *     immediately above:
105117397Skan   *     - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
106117397Skan   *       also be non-null pointers into the same @c charT array, as
107117397Skan   *       described above; otherwise, @e xbeg and @e xend shall also be null.
108117397Skan   *     - If @e xnext is not a null pointer and @e xnext < @e xend for an
109117397Skan   *       output sequence, then a <em>write position</em> is available.
110117397Skan   *       In this case, @e *xnext shall be assignable as the next element
111117397Skan   *       to write (to put, or to store a character value, into the sequence).
112117397Skan   *     - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
113117397Skan   *       input sequence, then a <em>putback position</em> is available.
114117397Skan   *       In this case, @e xnext[-1] shall have a defined value and is the
115117397Skan   *       next (preceding) element to store a character that is put back
116117397Skan   *       into the input sequence.
117117397Skan   *     - If @e xnext is not a null pointer and @e xnext< @e xend for an
118117397Skan   *       input sequence, then a <em>read position</em> is available.
119117397Skan   *       In this case, @e *xnext shall have a defined value and is the
120117397Skan   *       next element to read (to get, or to obtain a character value,
121117397Skan   *       from the sequence).
122117397Skan  */
12397403Sobrien  template<typename _CharT, typename _Traits>
12497403Sobrien    class basic_streambuf
12597403Sobrien    {
12697403Sobrien    public:
127117397Skan      //@{
128117397Skan      /**
129117397Skan       *  These are standard types.  They permit a standardized way of
130117397Skan       *  referring to names of (or names dependant on) the template
131117397Skan       *  parameters, which are specific to the implementation.
132117397Skan      */
13397403Sobrien      typedef _CharT 					char_type;
13497403Sobrien      typedef _Traits 					traits_type;
13597403Sobrien      typedef typename traits_type::int_type 		int_type;
13697403Sobrien      typedef typename traits_type::pos_type 		pos_type;
13797403Sobrien      typedef typename traits_type::off_type 		off_type;
138117397Skan      //@}
13997403Sobrien
140117397Skan      //@{
141117397Skan      /**
142117397Skan       *  @if maint
143132720Skan       *  This is a non-standard type.
144117397Skan       *  @endif
145117397Skan      */
14697403Sobrien      typedef basic_streambuf<char_type, traits_type>  	__streambuf_type;
147117397Skan      //@}
14897403Sobrien
14997403Sobrien      friend class basic_ios<char_type, traits_type>;
15097403Sobrien      friend class basic_istream<char_type, traits_type>;
15197403Sobrien      friend class basic_ostream<char_type, traits_type>;
15297403Sobrien      friend class istreambuf_iterator<char_type, traits_type>;
15397403Sobrien      friend class ostreambuf_iterator<char_type, traits_type>;
15497403Sobrien
15597403Sobrien      friend streamsize
156169691Skan      __copy_streambufs_eof<>(__streambuf_type*, __streambuf_type*, bool&);
157169691Skan
158169691Skan      template<typename _CharT2>
159169691Skan        friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
160169691Skan					       _CharT2*>::__type
161169691Skan        __copy_aux(istreambuf_iterator<_CharT2>,
162169691Skan		   istreambuf_iterator<_CharT2>, _CharT2*);
163169691Skan
164169691Skan      template<typename _CharT2>
165169691Skan        friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
166169691Skan				  istreambuf_iterator<_CharT2> >::__type
167169691Skan        find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
168169691Skan	     const _CharT2&);
169169691Skan
170169691Skan      template<typename _CharT2, typename _Traits2>
171169691Skan        friend basic_istream<_CharT2, _Traits2>&
172169691Skan        operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
173169691Skan
174169691Skan      template<typename _CharT2, typename _Traits2, typename _Alloc>
175169691Skan        friend basic_istream<_CharT2, _Traits2>&
176169691Skan        operator>>(basic_istream<_CharT2, _Traits2>&,
177169691Skan		   basic_string<_CharT2, _Traits2, _Alloc>&);
178169691Skan
179169691Skan      template<typename _CharT2, typename _Traits2, typename _Alloc>
180169691Skan        friend basic_istream<_CharT2, _Traits2>&
181169691Skan        getline(basic_istream<_CharT2, _Traits2>&,
182169691Skan		basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
183169691Skan
18497403Sobrien    protected:
185117397Skan      //@{
186117397Skan      /**
187117397Skan       *  @if maint
188117397Skan       *  This is based on _IO_FILE, just reordered to be more consistent,
189117397Skan       *  and is intended to be the most minimal abstraction for an
190117397Skan       *  internal buffer.
191117397Skan       *  -  get == input == read
192117397Skan       *  -  put == output == write
193117397Skan       *  @endif
194117397Skan      */
195132720Skan      char_type* 		_M_in_beg;     // Start of get area.
196132720Skan      char_type* 		_M_in_cur;     // Current read area.
197132720Skan      char_type* 		_M_in_end;     // End of get area.
198132720Skan      char_type* 		_M_out_beg;    // Start of put area.
199132720Skan      char_type* 		_M_out_cur;    // Current put area.
200132720Skan      char_type* 		_M_out_end;    // End of put area.
20197403Sobrien
202117397Skan      /**
203117397Skan       *  @if maint
204117397Skan       *  Current locale setting.
205117397Skan       *  @endif
206117397Skan      */
20797403Sobrien      locale 			_M_buf_locale;
20897403Sobrien
20997403Sobrien  public:
210117397Skan      /// Destructor deallocates no buffer space.
21197403Sobrien      virtual
21297403Sobrien      ~basic_streambuf()
213132720Skan      { }
21497403Sobrien
215117397Skan      // [27.5.2.2.1] locales
216117397Skan      /**
217117397Skan       *  @brief  Entry point for imbue().
218117397Skan       *  @param  loc  The new locale.
219117397Skan       *  @return  The previous locale.
220117397Skan       *
221117397Skan       *  Calls the derived imbue(loc).
222117397Skan      */
22397403Sobrien      locale
22497403Sobrien      pubimbue(const locale &__loc)
22597403Sobrien      {
22697403Sobrien	locale __tmp(this->getloc());
22797403Sobrien	this->imbue(__loc);
228132720Skan	_M_buf_locale = __loc;
22997403Sobrien	return __tmp;
23097403Sobrien      }
23197403Sobrien
232117397Skan      /**
233117397Skan       *  @brief  Locale access.
234117397Skan       *  @return  The current locale in effect.
235117397Skan       *
236117397Skan       *  If pubimbue(loc) has been called, then the most recent @c loc
237117397Skan       *  is returned.  Otherwise the global locale in effect at the time
238117397Skan       *  of construction is returned.
239117397Skan      */
24097403Sobrien      locale
24197403Sobrien      getloc() const
242110614Skan      { return _M_buf_locale; }
24397403Sobrien
244117397Skan      // [27.5.2.2.2] buffer management and positioning
245117397Skan      //@{
246117397Skan      /**
247117397Skan       *  @brief  Entry points for derived buffer functions.
248117397Skan       *
249117397Skan       *  The public versions of @c pubfoo dispatch to the protected
250117397Skan       *  derived @c foo member functions, passing the arguments (if any)
251117397Skan       *  and returning the result unchanged.
252117397Skan      */
25397403Sobrien      __streambuf_type*
25497403Sobrien      pubsetbuf(char_type* __s, streamsize __n)
25597403Sobrien      { return this->setbuf(__s, __n); }
25697403Sobrien
25797403Sobrien      pos_type
25897403Sobrien      pubseekoff(off_type __off, ios_base::seekdir __way,
25997403Sobrien		 ios_base::openmode __mode = ios_base::in | ios_base::out)
26097403Sobrien      { return this->seekoff(__off, __way, __mode); }
26197403Sobrien
26297403Sobrien      pos_type
26397403Sobrien      pubseekpos(pos_type __sp,
26497403Sobrien		 ios_base::openmode __mode = ios_base::in | ios_base::out)
26597403Sobrien      { return this->seekpos(__sp, __mode); }
26697403Sobrien
26797403Sobrien      int
26897403Sobrien      pubsync() { return this->sync(); }
269117397Skan      //@}
27097403Sobrien
271117397Skan      // [27.5.2.2.3] get area
272117397Skan      /**
273117397Skan       *  @brief  Looking ahead into the stream.
274117397Skan       *  @return  The number of characters available.
275117397Skan       *
276117397Skan       *  If a read position is available, returns the number of characters
277117397Skan       *  available for reading before the buffer must be refilled.
278117397Skan       *  Otherwise returns the derived @c showmanyc().
279117397Skan      */
28097403Sobrien      streamsize
28197403Sobrien      in_avail()
28297403Sobrien      {
283132720Skan	const streamsize __ret = this->egptr() - this->gptr();
284132720Skan	return __ret ? __ret : this->showmanyc();
28597403Sobrien      }
28697403Sobrien
287117397Skan      /**
288117397Skan       *  @brief  Getting the next character.
289117397Skan       *  @return  The next character, or eof.
290117397Skan       *
291117397Skan       *  Calls @c sbumpc(), and if that function returns
292117397Skan       *  @c traits::eof(), so does this function.  Otherwise, @c sgetc().
293117397Skan      */
29497403Sobrien      int_type
29597403Sobrien      snextc()
29697403Sobrien      {
297132720Skan	int_type __ret = traits_type::eof();
298132720Skan	if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
299132720Skan						       __ret), true))
300132720Skan	  __ret = this->sgetc();
301132720Skan	return __ret;
30297403Sobrien      }
30397403Sobrien
304117397Skan      /**
305117397Skan       *  @brief  Getting the next character.
306117397Skan       *  @return  The next character, or eof.
307117397Skan       *
308117397Skan       *  If the input read position is available, returns that character
309117397Skan       *  and increments the read pointer, otherwise calls and returns
310117397Skan       *  @c uflow().
311117397Skan      */
31297403Sobrien      int_type
313132720Skan      sbumpc()
314132720Skan      {
315132720Skan	int_type __ret;
316132720Skan	if (__builtin_expect(this->gptr() < this->egptr(), true))
317132720Skan	  {
318132720Skan	    __ret = traits_type::to_int_type(*this->gptr());
319132720Skan	    this->gbump(1);
320132720Skan	  }
321132720Skan	else
322132720Skan	  __ret = this->uflow();
323132720Skan	return __ret;
324132720Skan      }
32597403Sobrien
326117397Skan      /**
327117397Skan       *  @brief  Getting the next character.
328117397Skan       *  @return  The next character, or eof.
329117397Skan       *
330117397Skan       *  If the input read position is available, returns that character,
331117397Skan       *  otherwise calls and returns @c underflow().  Does not move the
332117397Skan       *  read position after fetching the character.
333117397Skan      */
33497403Sobrien      int_type
33597403Sobrien      sgetc()
33697403Sobrien      {
33797403Sobrien	int_type __ret;
338132720Skan	if (__builtin_expect(this->gptr() < this->egptr(), true))
339132720Skan	  __ret = traits_type::to_int_type(*this->gptr());
34097403Sobrien	else
34197403Sobrien	  __ret = this->underflow();
34297403Sobrien	return __ret;
34397403Sobrien      }
34497403Sobrien
345117397Skan      /**
346117397Skan       *  @brief  Entry point for xsgetn.
347117397Skan       *  @param  s  A buffer area.
348117397Skan       *  @param  n  A count.
349117397Skan       *
350117397Skan       *  Returns xsgetn(s,n).  The effect is to fill @a s[0] through
351117397Skan       *  @a s[n-1] with characters from the input sequence, if possible.
352117397Skan      */
35397403Sobrien      streamsize
35497403Sobrien      sgetn(char_type* __s, streamsize __n)
35597403Sobrien      { return this->xsgetn(__s, __n); }
35697403Sobrien
357117397Skan      // [27.5.2.2.4] putback
358117397Skan      /**
359117397Skan       *  @brief  Pushing characters back into the input stream.
360117397Skan       *  @param  c  The character to push back.
361117397Skan       *  @return  The previous character, if possible.
362117397Skan       *
363117397Skan       *  Similar to sungetc(), but @a c is pushed onto the stream instead
364117397Skan       *  of "the previous character".  If successful, the next character
365117397Skan       *  fetched from the input stream will be @a c.
366117397Skan      */
36797403Sobrien      int_type
368132720Skan      sputbackc(char_type __c)
369132720Skan      {
370132720Skan	int_type __ret;
371132720Skan	const bool __testpos = this->eback() < this->gptr();
372132720Skan	if (__builtin_expect(!__testpos ||
373132720Skan			     !traits_type::eq(__c, this->gptr()[-1]), false))
374132720Skan	  __ret = this->pbackfail(traits_type::to_int_type(__c));
375132720Skan	else
376132720Skan	  {
377132720Skan	    this->gbump(-1);
378132720Skan	    __ret = traits_type::to_int_type(*this->gptr());
379132720Skan	  }
380132720Skan	return __ret;
381132720Skan      }
38297403Sobrien
383117397Skan      /**
384117397Skan       *  @brief  Moving backwards in the input stream.
385117397Skan       *  @return  The previous character, if possible.
386117397Skan       *
387117397Skan       *  If a putback position is available, this function decrements the
388117397Skan       *  input pointer and returns that character.  Otherwise, calls and
389117397Skan       *  returns pbackfail().  The effect is to "unget" the last character
390117397Skan       *  "gotten".
391117397Skan      */
39297403Sobrien      int_type
393132720Skan      sungetc()
394132720Skan      {
395132720Skan	int_type __ret;
396132720Skan	if (__builtin_expect(this->eback() < this->gptr(), true))
397132720Skan	  {
398132720Skan	    this->gbump(-1);
399132720Skan	    __ret = traits_type::to_int_type(*this->gptr());
400132720Skan	  }
401132720Skan	else
402132720Skan	  __ret = this->pbackfail();
403132720Skan	return __ret;
404132720Skan      }
40597403Sobrien
406117397Skan      // [27.5.2.2.5] put area
407117397Skan      /**
408117397Skan       *  @brief  Entry point for all single-character output functions.
409117397Skan       *  @param  c  A character to output.
410117397Skan       *  @return  @a c, if possible.
411117397Skan       *
412117397Skan       *  One of two public output functions.
413117397Skan       *
414117397Skan       *  If a write position is available for the output sequence (i.e.,
415117397Skan       *  the buffer is not full), stores @a c in that position, increments
416117397Skan       *  the position, and returns @c traits::to_int_type(c).  If a write
417117397Skan       *  position is not available, returns @c overflow(c).
418117397Skan      */
41997403Sobrien      int_type
420132720Skan      sputc(char_type __c)
421132720Skan      {
422132720Skan	int_type __ret;
423132720Skan	if (__builtin_expect(this->pptr() < this->epptr(), true))
424132720Skan	  {
425132720Skan	    *this->pptr() = __c;
426132720Skan	    this->pbump(1);
427132720Skan	    __ret = traits_type::to_int_type(__c);
428132720Skan	  }
429132720Skan	else
430132720Skan	  __ret = this->overflow(traits_type::to_int_type(__c));
431132720Skan	return __ret;
432132720Skan      }
43397403Sobrien
434117397Skan      /**
435117397Skan       *  @brief  Entry point for all single-character output functions.
436117397Skan       *  @param  s  A buffer read area.
437117397Skan       *  @param  n  A count.
438117397Skan       *
439117397Skan       *  One of two public output functions.
440117397Skan       *
441117397Skan       *
442117397Skan       *  Returns xsputn(s,n).  The effect is to write @a s[0] through
443117397Skan       *  @a s[n-1] to the output sequence, if possible.
444117397Skan      */
44597403Sobrien      streamsize
44697403Sobrien      sputn(const char_type* __s, streamsize __n)
44797403Sobrien      { return this->xsputn(__s, __n); }
44897403Sobrien
44997403Sobrien    protected:
450117397Skan      /**
451117397Skan       *  @brief  Base constructor.
452117397Skan       *
453117397Skan       *  Only called from derived constructors, and sets up all the
454117397Skan       *  buffer data to zero, including the pointers described in the
455117397Skan       *  basic_streambuf class description.  Note that, as a result,
456117397Skan       *  - the class starts with no read nor write positions available,
457117397Skan       *  - this is not an error
458117397Skan      */
45997403Sobrien      basic_streambuf()
460132720Skan      : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
461132720Skan      _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
462132720Skan      _M_buf_locale(locale())
46397403Sobrien      { }
46497403Sobrien
465117397Skan      // [27.5.2.3.1] get area access
466117397Skan      //@{
467117397Skan      /**
468117397Skan       *  @brief  Access to the get area.
469117397Skan       *
470117397Skan       *  These functions are only available to other protected functions,
471117397Skan       *  including derived classes.
472117397Skan       *
473117397Skan       *  - eback() returns the beginning pointer for the input sequence
474117397Skan       *  - gptr() returns the next pointer for the input sequence
475117397Skan       *  - egptr() returns the end pointer for the input sequence
476117397Skan      */
47797403Sobrien      char_type*
47897403Sobrien      eback() const { return _M_in_beg; }
47997403Sobrien
48097403Sobrien      char_type*
48197403Sobrien      gptr()  const { return _M_in_cur;  }
48297403Sobrien
48397403Sobrien      char_type*
48497403Sobrien      egptr() const { return _M_in_end; }
485117397Skan      //@}
48697403Sobrien
487117397Skan      /**
488117397Skan       *  @brief  Moving the read position.
489117397Skan       *  @param  n  The delta by which to move.
490117397Skan       *
491117397Skan       *  This just advances the read position without returning any data.
492117397Skan      */
49397403Sobrien      void
49497403Sobrien      gbump(int __n) { _M_in_cur += __n; }
49597403Sobrien
496117397Skan      /**
497117397Skan       *  @brief  Setting the three read area pointers.
498117397Skan       *  @param  gbeg  A pointer.
499117397Skan       *  @param  gnext  A pointer.
500117397Skan       *  @param  gend  A pointer.
501117397Skan       *  @post  @a gbeg == @c eback(), @a gnext == @c gptr(), and
502117397Skan       *         @a gend == @c egptr()
503117397Skan      */
50497403Sobrien      void
50597403Sobrien      setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
50697403Sobrien      {
50797403Sobrien	_M_in_beg = __gbeg;
50897403Sobrien	_M_in_cur = __gnext;
50997403Sobrien	_M_in_end = __gend;
51097403Sobrien      }
51197403Sobrien
512117397Skan      // [27.5.2.3.2] put area access
513117397Skan      //@{
514117397Skan      /**
515117397Skan       *  @brief  Access to the put area.
516117397Skan       *
517117397Skan       *  These functions are only available to other protected functions,
518117397Skan       *  including derived classes.
519117397Skan       *
520117397Skan       *  - pbase() returns the beginning pointer for the output sequence
521117397Skan       *  - pptr() returns the next pointer for the output sequence
522117397Skan       *  - epptr() returns the end pointer for the output sequence
523117397Skan      */
52497403Sobrien      char_type*
52597403Sobrien      pbase() const { return _M_out_beg; }
52697403Sobrien
52797403Sobrien      char_type*
52897403Sobrien      pptr() const { return _M_out_cur; }
52997403Sobrien
53097403Sobrien      char_type*
53197403Sobrien      epptr() const { return _M_out_end; }
532117397Skan      //@}
53397403Sobrien
534117397Skan      /**
535117397Skan       *  @brief  Moving the write position.
536117397Skan       *  @param  n  The delta by which to move.
537117397Skan       *
538117397Skan       *  This just advances the write position without returning any data.
539117397Skan      */
54097403Sobrien      void
54197403Sobrien      pbump(int __n) { _M_out_cur += __n; }
54297403Sobrien
543117397Skan      /**
544117397Skan       *  @brief  Setting the three write area pointers.
545117397Skan       *  @param  pbeg  A pointer.
546117397Skan       *  @param  pend  A pointer.
547117397Skan       *  @post  @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
548117397Skan       *         @a pend == @c epptr()
549117397Skan      */
55097403Sobrien      void
55197403Sobrien      setp(char_type* __pbeg, char_type* __pend)
55297403Sobrien      {
55397403Sobrien	_M_out_beg = _M_out_cur = __pbeg;
554132720Skan	_M_out_end = __pend;
55597403Sobrien      }
55697403Sobrien
557117397Skan      // [27.5.2.4] virtual functions
558117397Skan      // [27.5.2.4.1] locales
559117397Skan      /**
560117397Skan       *  @brief  Changes translations.
561117397Skan       *  @param  loc  A new locale.
562117397Skan       *
563117397Skan       *  Translations done during I/O which depend on the current locale
564117397Skan       *  are changed by this call.  The standard adds, "Between invocations
565117397Skan       *  of this function a class derived from streambuf can safely cache
566117397Skan       *  results of calls to locale functions and to members of facets
567132720Skan       *  so obtained."
568132720Skan       *
569132720Skan       *  @note  Base class version does nothing.
570117397Skan      */
57197403Sobrien      virtual void
572132720Skan      imbue(const locale&)
573132720Skan      { }
57497403Sobrien
575117397Skan      // [27.5.2.4.2] buffer management and positioning
576117397Skan      /**
577117397Skan       *  @brief  Maniuplates the buffer.
578117397Skan       *
579117397Skan       *  Each derived class provides its own appropriate behavior.  See
580117397Skan       *  the next-to-last paragraph of
581117397Skan       *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
582117397Skan       *  more on this function.
583117397Skan       *
584117397Skan       *  @note  Base class version does nothing, returns @c this.
585117397Skan      */
58697403Sobrien      virtual basic_streambuf<char_type,_Traits>*
58797403Sobrien      setbuf(char_type*, streamsize)
58897403Sobrien      {	return this; }
58997403Sobrien
590117397Skan      /**
591117397Skan       *  @brief  Alters the stream positions.
592117397Skan       *
593117397Skan       *  Each derived class provides its own appropriate behavior.
594117397Skan       *  @note  Base class version does nothing, returns a @c pos_type
595117397Skan       *         that represents an invalid stream position.
596117397Skan      */
59797403Sobrien      virtual pos_type
59897403Sobrien      seekoff(off_type, ios_base::seekdir,
59997403Sobrien	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
60097403Sobrien      { return pos_type(off_type(-1)); }
60197403Sobrien
602117397Skan      /**
603117397Skan       *  @brief  Alters the stream positions.
604117397Skan       *
605117397Skan       *  Each derived class provides its own appropriate behavior.
606117397Skan       *  @note  Base class version does nothing, returns a @c pos_type
607117397Skan       *         that represents an invalid stream position.
608117397Skan      */
60997403Sobrien      virtual pos_type
61097403Sobrien      seekpos(pos_type,
61197403Sobrien	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
61297403Sobrien      { return pos_type(off_type(-1)); }
61397403Sobrien
614117397Skan      /**
615117397Skan       *  @brief  Synchronizes the buffer arrays with the controlled sequences.
616117397Skan       *  @return  -1 on failure.
617117397Skan       *
618117397Skan       *  Each derived class provides its own appropriate behavior,
619117397Skan       *  including the definition of "failure".
620117397Skan       *  @note  Base class version does nothing, returns zero.
621117397Skan      */
62297403Sobrien      virtual int
62397403Sobrien      sync() { return 0; }
62497403Sobrien
625117397Skan      // [27.5.2.4.3] get area
626117397Skan      /**
627117397Skan       *  @brief  Investigating the data available.
628117397Skan       *  @return  An estimate of the number of characters available in the
629117397Skan       *           input sequence, or -1.
630117397Skan       *
631117397Skan       *  "If it returns a positive value, then successive calls to
632117397Skan       *  @c underflow() will not return @c traits::eof() until at least that
633117397Skan       *  number of characters have been supplied.  If @c showmanyc()
634117397Skan       *  returns -1, then calls to @c underflow() or @c uflow() will fail."
635117397Skan       *  [27.5.2.4.3]/1
636117397Skan       *
637117397Skan       *  @note  Base class version does nothing, returns zero.
638117397Skan       *  @note  The standard adds that "the intention is not only that the
639117397Skan       *         calls [to underflow or uflow] will not return @c eof() but
640117397Skan       *         that they will return "immediately".
641117397Skan       *  @note  The standard adds that "the morphemes of @c showmanyc are
642117397Skan       *         "es-how-many-see", not "show-manic".
643117397Skan      */
64497403Sobrien      virtual streamsize
64597403Sobrien      showmanyc() { return 0; }
64697403Sobrien
647117397Skan      /**
648117397Skan       *  @brief  Multiple character extraction.
649117397Skan       *  @param  s  A buffer area.
650117397Skan       *  @param  n  Maximum number of characters to assign.
651117397Skan       *  @return  The number of characters assigned.
652117397Skan       *
653117397Skan       *  Fills @a s[0] through @a s[n-1] with characters from the input
654117397Skan       *  sequence, as if by @c sbumpc().  Stops when either @a n characters
655117397Skan       *  have been copied, or when @c traits::eof() would be copied.
656117397Skan       *
657117397Skan       *  It is expected that derived classes provide a more efficient
658117397Skan       *  implementation by overriding this definition.
659117397Skan      */
66097403Sobrien      virtual streamsize
66197403Sobrien      xsgetn(char_type* __s, streamsize __n);
66297403Sobrien
663117397Skan      /**
664117397Skan       *  @brief  Fetches more data from the controlled sequence.
665117397Skan       *  @return  The first character from the <em>pending sequence</em>.
666117397Skan       *
667117397Skan       *  Informally, this function is called when the input buffer is
668117397Skan       *  exhausted (or does not exist, as buffering need not actually be
669117397Skan       *  done).  If a buffer exists, it is "refilled".  In either case, the
670117397Skan       *  next available character is returned, or @c traits::eof() to
671117397Skan       *  indicate a null pending sequence.
672117397Skan       *
673117397Skan       *  For a formal definiton of the pending sequence, see a good text
674117397Skan       *  such as Langer & Kreft, or [27.5.2.4.3]/7-14.
675117397Skan       *
676117397Skan       *  A functioning input streambuf can be created by overriding only
677117397Skan       *  this function (no buffer area will be used).  For an example, see
678117397Skan       *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
679117397Skan       *
680117397Skan       *  @note  Base class version does nothing, returns eof().
681117397Skan      */
68297403Sobrien      virtual int_type
68397403Sobrien      underflow()
68497403Sobrien      { return traits_type::eof(); }
68597403Sobrien
686117397Skan      /**
687117397Skan       *  @brief  Fetches more data from the controlled sequence.
688117397Skan       *  @return  The first character from the <em>pending sequence</em>.
689117397Skan       *
690117397Skan       *  Informally, this function does the same thing as @c underflow(),
691117397Skan       *  and in fact is required to call that function.  It also returns
692117397Skan       *  the new character, like @c underflow() does.  However, this
693117397Skan       *  function also moves the read position forward by one.
694117397Skan      */
69597403Sobrien      virtual int_type
69697403Sobrien      uflow()
69797403Sobrien      {
69897403Sobrien	int_type __ret = traits_type::eof();
699132720Skan	const bool __testeof = traits_type::eq_int_type(this->underflow(),
700132720Skan							__ret);
701132720Skan	if (!__testeof)
70297403Sobrien	  {
703132720Skan	    __ret = traits_type::to_int_type(*this->gptr());
704132720Skan	    this->gbump(1);
70597403Sobrien	  }
70697403Sobrien	return __ret;
70797403Sobrien      }
70897403Sobrien
709117397Skan      // [27.5.2.4.4] putback
710117397Skan      /**
711117397Skan       *  @brief  Tries to back up the input sequence.
712117397Skan       *  @param  c  The character to be inserted back into the sequence.
713117397Skan       *  @return  eof() on failure, "some other value" on success
714117397Skan       *  @post  The constraints of @c gptr(), @c eback(), and @c pptr()
715117397Skan       *         are the same as for @c underflow().
716117397Skan       *
717117397Skan       *  @note  Base class version does nothing, returns eof().
718117397Skan      */
71997403Sobrien      virtual int_type
72097403Sobrien      pbackfail(int_type /* __c */  = traits_type::eof())
72197403Sobrien      { return traits_type::eof(); }
72297403Sobrien
72397403Sobrien      // Put area:
724117397Skan      /**
725117397Skan       *  @brief  Multiple character insertion.
726117397Skan       *  @param  s  A buffer area.
727117397Skan       *  @param  n  Maximum number of characters to write.
728117397Skan       *  @return  The number of characters written.
729117397Skan       *
730117397Skan       *  Writes @a s[0] through @a s[n-1] to the output sequence, as if
731117397Skan       *  by @c sputc().  Stops when either @a n characters have been
732117397Skan       *  copied, or when @c sputc() would return @c traits::eof().
733117397Skan       *
734117397Skan       *  It is expected that derived classes provide a more efficient
735117397Skan       *  implementation by overriding this definition.
736117397Skan      */
73797403Sobrien      virtual streamsize
73897403Sobrien      xsputn(const char_type* __s, streamsize __n);
73997403Sobrien
740117397Skan      /**
741117397Skan       *  @brief  Consumes data from the buffer; writes to the
742117397Skan       *          controlled sequence.
743117397Skan       *  @param  c  An additional character to consume.
744117397Skan       *  @return  eof() to indicate failure, something else (usually
745117397Skan       *           @a c, or not_eof())
746117397Skan       *
747117397Skan       *  Informally, this function is called when the output buffer is full
748117397Skan       *  (or does not exist, as buffering need not actually be done).  If a
749117397Skan       *  buffer exists, it is "consumed", with "some effect" on the
750117397Skan       *  controlled sequence.  (Typically, the buffer is written out to the
751117397Skan       *  sequence verbatim.)  In either case, the character @a c is also
752117397Skan       *  written out, if @a c is not @c eof().
753117397Skan       *
754117397Skan       *  For a formal definiton of this function, see a good text
755117397Skan       *  such as Langer & Kreft, or [27.5.2.4.5]/3-7.
756117397Skan       *
757117397Skan       *  A functioning output streambuf can be created by overriding only
758117397Skan       *  this function (no buffer area will be used).
759117397Skan       *
760117397Skan       *  @note  Base class version does nothing, returns eof().
761117397Skan      */
76297403Sobrien      virtual int_type
76397403Sobrien      overflow(int_type /* __c */ = traits_type::eof())
76497403Sobrien      { return traits_type::eof(); }
76597403Sobrien
766132720Skan#ifdef _GLIBCXX_DEPRECATED
767117397Skan    // Annex D.6
76897403Sobrien    public:
769117397Skan      /**
770117397Skan       *  @brief  Tosses a character.
771117397Skan       *
772117397Skan       *  Advances the read pointer, ignoring the character that would have
773117397Skan       *  been read.
774117397Skan       *
775117397Skan       *  See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
776117397Skan       *
777117397Skan       *  @note  This function has been deprecated by the standard.  You
778132720Skan       *         must define @c _GLIBCXX_DEPRECATED to make this visible; see
779117397Skan       *         c++config.h.
780117397Skan      */
78197403Sobrien      void
78297403Sobrien      stossc()
78397403Sobrien      {
784132720Skan	if (this->gptr() < this->egptr())
785132720Skan	  this->gbump(1);
78697403Sobrien	else
78797403Sobrien	  this->uflow();
78897403Sobrien      }
78997403Sobrien#endif
79097403Sobrien
79197403Sobrien    private:
792132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
793132720Skan      // Side effect of DR 50.
794132720Skan      basic_streambuf(const __streambuf_type& __sb)
795132720Skan      : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur),
796132720Skan      _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg),
797132720Skan      _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
798132720Skan      _M_buf_locale(__sb._M_buf_locale)
799132720Skan      { }
80097403Sobrien
80197403Sobrien      __streambuf_type&
80297403Sobrien      operator=(const __streambuf_type&) { return *this; };
80397403Sobrien    };
80497403Sobrien
805169691Skan  // Explicit specialization declarations, defined in src/streambuf.cc.
806169691Skan  template<>
807169691Skan    streamsize
808169691Skan    __copy_streambufs_eof(basic_streambuf<char>* __sbin,
809169691Skan			  basic_streambuf<char>* __sbout, bool& __ineof);
810169691Skan#ifdef _GLIBCXX_USE_WCHAR_T
811169691Skan  template<>
812169691Skan    streamsize
813169691Skan    __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
814169691Skan			  basic_streambuf<wchar_t>* __sbout, bool& __ineof);
815169691Skan#endif
816169691Skan
817169691Skan_GLIBCXX_END_NAMESPACE
818169691Skan
819132720Skan#ifndef _GLIBCXX_EXPORT_TEMPLATE
820132720Skan# include <bits/streambuf.tcc>
82197403Sobrien#endif
82297403Sobrien
823132720Skan#endif /* _GLIBCXX_STREAMBUF */
824