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