1 // Stream buffer classes -*- C++ -*-
2 
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 //
32 // ISO C++ 14882: 27.5  Stream buffers
33 //
34 
35 /** @file streambuf
36  *  This is a Standard C++ Library header.  You should @c #include this header
37  *  in your programs, rather than any of the "st[dl]_*.h" implementation files.
38  */
39 
40 #ifndef _CPP_STREAMBUF
41 #define _CPP_STREAMBUF	1
42 
43 #pragma GCC system_header
44 
45 #include <bits/c++config.h>
46 #include <iosfwd>
47 #include <cstdio> 	// For SEEK_SET, SEEK_CUR, SEEK_END
48 #include <bits/localefwd.h>
49 #include <bits/ios_base.h>
50 
51 namespace std
52 {
53   /**
54    *  @if maint
55    *  Does stuff.
56    *  @endif
57   */
58   template<typename _CharT, typename _Traits>
59     streamsize
60     __copy_streambufs(basic_ios<_CharT, _Traits>& _ios,
61 		      basic_streambuf<_CharT, _Traits>* __sbin,
62 		      basic_streambuf<_CharT, _Traits>* __sbout);
63 
64   /**
65    *  @brief  The actual work of input and output (interface).
66    *
67    *  This is a base class.  Derived stream buffers each control a
68    *  pair of character sequences:  one for input, and one for output.
69    *
70    *  Section [27.5.1] of the standard describes the requirements and
71    *  behavior of stream buffer classes.  That section (three paragraphs)
72    *  is reproduced here, for simplicity and accuracy.
73    *
74    *  -# Stream buffers can impose various constraints on the sequences
75    *     they control.  Some constraints are:
76    *     - The controlled input sequence can be not readable.
77    *     - The controlled output sequence can be not writable.
78    *     - The controlled sequences can be associated with the contents of
79    *       other representations for character sequences, such as external
80    *       files.
81    *     - The controlled sequences can support operations @e directly to or
82    *       from associated sequences.
83    *     - The controlled sequences can impose limitations on how the
84    *       program can read characters from a sequence, write characters to
85    *       a sequence, put characters back into an input sequence, or alter
86    *       the stream position.
87    *     .
88    *  -# Each sequence is characterized by three pointers which, if non-null,
89    *     all point into the same @c charT array object.  The array object
90    *     represents, at any moment, a (sub)sequence of characters from the
91    *     sequence.  Operations performed on a sequence alter the values
92    *     stored in these pointers, perform reads and writes directly to or
93    *     from associated sequences, and alter "the stream position" and
94    *     conversion state as needed to maintain this subsequence relationship.
95    *     The three pointers are:
96    *     - the <em>beginning pointer</em>, or lowest element address in the
97    *       array (called @e xbeg here);
98    *     - the <em>next pointer</em>, or next element address that is a
99    *       current candidate for reading or writing (called @e xnext here);
100    *     - the <em>end pointer</em>, or first element address beyond the
101    *       end of the array (called @e xend here).
102    *     .
103    *  -# The following semantic constraints shall always apply for any set
104    *     of three pointers for a sequence, using the pointer names given
105    *     immediately above:
106    *     - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
107    *       also be non-null pointers into the same @c charT array, as
108    *       described above; otherwise, @e xbeg and @e xend shall also be null.
109    *     - If @e xnext is not a null pointer and @e xnext < @e xend for an
110    *       output sequence, then a <em>write position</em> is available.
111    *       In this case, @e *xnext shall be assignable as the next element
112    *       to write (to put, or to store a character value, into the sequence).
113    *     - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
114    *       input sequence, then a <em>putback position</em> is available.
115    *       In this case, @e xnext[-1] shall have a defined value and is the
116    *       next (preceding) element to store a character that is put back
117    *       into the input sequence.
118    *     - If @e xnext is not a null pointer and @e xnext< @e xend for an
119    *       input sequence, then a <em>read position</em> is available.
120    *       In this case, @e *xnext shall have a defined value and is the
121    *       next element to read (to get, or to obtain a character value,
122    *       from the sequence).
123   */
124   template<typename _CharT, typename _Traits>
125     class basic_streambuf
126     {
127     public:
128       //@{
129       /**
130        *  These are standard types.  They permit a standardized way of
131        *  referring to names of (or names dependant on) the template
132        *  parameters, which are specific to the implementation.
133       */
134       typedef _CharT 					char_type;
135       typedef _Traits 					traits_type;
136       typedef typename traits_type::int_type 		int_type;
137       typedef typename traits_type::pos_type 		pos_type;
138       typedef typename traits_type::off_type 		off_type;
139       //@}
140 
141       //@{
142       /**
143        *  @if maint
144        *  These are non-standard types.
145        *  @endif
146       */
147       typedef ctype<char_type>           		__ctype_type;
148       typedef basic_streambuf<char_type, traits_type>  	__streambuf_type;
149       typedef typename traits_type::state_type 		__state_type;
150       //@}
151 
152       friend class basic_ios<char_type, traits_type>;
153       friend class basic_istream<char_type, traits_type>;
154       friend class basic_ostream<char_type, traits_type>;
155       friend class istreambuf_iterator<char_type, traits_type>;
156       friend class ostreambuf_iterator<char_type, traits_type>;
157 
158       friend streamsize
159       __copy_streambufs<>(basic_ios<char_type, traits_type>& __ios,
160 			  __streambuf_type* __sbin,__streambuf_type* __sbout);
161 
162     protected:
163       /**
164        *  @if maint
165        *  Pointer to the beginning of internally-allocated space.  Filebuf
166        *  manually allocates/deallocates this, whereas stringstreams attempt
167        *  to use the built-in intelligence of the string class.  If you are
168        *  managing memory, set this.  If not, leave it NULL.
169        *  @endif
170       */
171       char_type*		_M_buf;
172 
173       /**
174        *  @if maint
175        *  Actual size of allocated internal buffer, in bytes.
176        *  @endif
177       */
178       size_t			_M_buf_size;
179 
180       /**
181        *  @if maint
182        *  Optimal or preferred size of internal buffer, in bytes.
183        *  @endif
184       */
185       size_t			_M_buf_size_opt;
186 
187       /**
188        *  @if maint
189        *  True iff _M_in_* and _M_out_* buffers should always point to
190        *  the same place.  True for fstreams, false for sstreams.
191        *  @endif
192       */
193       bool 			_M_buf_unified;
194 
195       //@{
196       /**
197        *  @if maint
198        *  This is based on _IO_FILE, just reordered to be more consistent,
199        *  and is intended to be the most minimal abstraction for an
200        *  internal buffer.
201        *  -  get == input == read
202        *  -  put == output == write
203        *  @endif
204       */
205       char_type* 		_M_in_beg;  	// Start of get area.
206       char_type* 		_M_in_cur;	// Current read area.
207       char_type* 		_M_in_end;	// End of get area.
208       char_type* 		_M_out_beg; 	// Start of put area.
209       char_type* 		_M_out_cur;  	// Current put area.
210       char_type* 		_M_out_end;  	// End of put area.
211       //@}
212 
213       /**
214        *  @if maint
215        *  Place to stash in || out || in | out settings for current streambuf.
216        *  @endif
217       */
218       ios_base::openmode 	_M_mode;
219 
220       /**
221        *  @if maint
222        *  Current locale setting.
223        *  @endif
224       */
225       locale 			_M_buf_locale;
226 
227       /**
228        *  @if maint
229        *  True iff locale is initialized.
230        *  @endif
231       */
232       bool 			_M_buf_locale_init;
233 
234       //@{
235       /**
236        *  @if maint
237        *  Necessary bits for putback buffer management. Only used in
238        *  the basic_filebuf class, as necessary for the standard
239        *  requirements. The only basic_streambuf member function that
240        *  needs access to these data members is in_avail...
241        *
242        *  @note pbacks of over one character are not currently supported.
243        *  @endif
244       */
245       static const size_t   	_S_pback_size = 1;
246       char_type			_M_pback[_S_pback_size];
247       char_type*		_M_pback_cur_save;
248       char_type*		_M_pback_end_save;
249       bool			_M_pback_init;
250       //@}
251 
252       /**
253        *  @if maint
254        *  Yet unused.
255        *  @endif
256       */
257       fpos<__state_type>	_M_pos;
258 
259       // Initializes pback buffers, and moves normal buffers to safety.
260       // Assumptions:
261       // _M_in_cur has already been moved back
262       void
263       _M_pback_create()
264       {
265 	if (!_M_pback_init)
266 	  {
267 	    size_t __dist = _M_in_end - _M_in_cur;
268 	    size_t __len = min(_S_pback_size, __dist);
269 	    traits_type::copy(_M_pback, _M_in_cur, __len);
270 	    _M_pback_cur_save = _M_in_cur;
271 	    _M_pback_end_save = _M_in_end;
272 	    this->setg(_M_pback, _M_pback, _M_pback + __len);
273 	    _M_pback_init = true;
274 	  }
275       }
276 
277       // Deactivates pback buffer contents, and restores normal buffer.
278       // Assumptions:
279       // The pback buffer has only moved forward.
280       void
281       _M_pback_destroy() throw()
282       {
283 	if (_M_pback_init)
284 	  {
285 	    // Length _M_in_cur moved in the pback buffer.
286 	    size_t __off_cur = _M_in_cur - _M_pback;
287 
288 	    // For in | out buffers, the end can be pushed back...
289 	    size_t __off_end = 0;
290 	    size_t __pback_len = _M_in_end - _M_pback;
291 	    size_t __save_len = _M_pback_end_save - _M_buf;
292 	    if (__pback_len > __save_len)
293 	      __off_end = __pback_len - __save_len;
294 
295 	    this->setg(_M_buf, _M_pback_cur_save + __off_cur,
296 		       _M_pback_end_save + __off_end);
297 	    _M_pback_cur_save = NULL;
298 	    _M_pback_end_save = NULL;
299 	    _M_pback_init = false;
300 	  }
301       }
302 
303       // Correctly sets the _M_in_cur pointer, and bumps the
304       // _M_out_cur pointer as well if necessary.
305       void
306       _M_in_cur_move(off_type __n) // argument needs to be +-
307       {
308 	bool __testout = _M_out_cur;
309 	_M_in_cur += __n;
310 	if (__testout && _M_buf_unified)
311 	  _M_out_cur += __n;
312       }
313 
314       // Correctly sets the _M_out_cur pointer, and bumps the
315       // appropriate _M_*_end pointers as well. Necessary for the
316       // un-tied stringbufs, in in|out mode.
317       // Invariant:
318       // __n + _M_out_[cur, end] <= _M_buf + _M_buf_size
319       // Assuming all _M_*_[beg, cur, end] pointers are operating on
320       // the same range:
321       // _M_buf <= _M_*_ <= _M_buf + _M_buf_size
322       void
323       _M_out_cur_move(off_type __n) // argument needs to be +-
324       {
325 	bool __testin = _M_in_cur;
326 
327 	_M_out_cur += __n;
328 	if (__testin && _M_buf_unified)
329 	  _M_in_cur += __n;
330 	if (_M_out_cur > _M_out_end)
331 	  {
332 	    _M_out_end = _M_out_cur;
333 	    // NB: in | out buffers drag the _M_in_end pointer along...
334 	    if (__testin)
335 	      _M_in_end += __n;
336 	  }
337       }
338 
339       // Return the size of the output buffer.  This depends on the
340       // buffer in use: allocated buffers have a stored size in
341       // _M_buf_size and setbuf() buffers don't.
342       off_type
343       _M_out_buf_size()
344       {
345 	off_type __ret = 0;
346 	if (_M_out_cur)
347 	  {
348 	    // Using allocated buffer.
349 	    if (_M_out_beg == _M_buf)
350 	      __ret = _M_out_beg + _M_buf_size - _M_out_cur;
351 	    // Using non-allocated buffer.
352 	    else
353 	      __ret = _M_out_end - _M_out_cur;
354 	  }
355 	return __ret;
356       }
357 
358   public:
359       /// Destructor deallocates no buffer space.
360       virtual
361       ~basic_streambuf()
362       {
363 	_M_buf_unified = false;
364 	_M_buf_size = 0;
365 	_M_buf_size_opt = 0;
366 	_M_mode = ios_base::openmode(0);
367       }
368 
369       // [27.5.2.2.1] locales
370       /**
371        *  @brief  Entry point for imbue().
372        *  @param  loc  The new locale.
373        *  @return  The previous locale.
374        *
375        *  Calls the derived imbue(loc).
376       */
377       locale
378       pubimbue(const locale &__loc)
379       {
380 	locale __tmp(this->getloc());
381 	this->imbue(__loc);
382 	return __tmp;
383       }
384 
385       /**
386        *  @brief  Locale access.
387        *  @return  The current locale in effect.
388        *
389        *  If pubimbue(loc) has been called, then the most recent @c loc
390        *  is returned.  Otherwise the global locale in effect at the time
391        *  of construction is returned.
392       */
393       locale
394       getloc() const
395       { return _M_buf_locale; }
396 
397       // [27.5.2.2.2] buffer management and positioning
398       //@{
399       /**
400        *  @brief  Entry points for derived buffer functions.
401        *
402        *  The public versions of @c pubfoo dispatch to the protected
403        *  derived @c foo member functions, passing the arguments (if any)
404        *  and returning the result unchanged.
405       */
406       __streambuf_type*
407       pubsetbuf(char_type* __s, streamsize __n)
408       { return this->setbuf(__s, __n); }
409 
410       pos_type
411       pubseekoff(off_type __off, ios_base::seekdir __way,
412 		 ios_base::openmode __mode = ios_base::in | ios_base::out)
413       { return this->seekoff(__off, __way, __mode); }
414 
415       pos_type
416       pubseekpos(pos_type __sp,
417 		 ios_base::openmode __mode = ios_base::in | ios_base::out)
418       { return this->seekpos(__sp, __mode); }
419 
420       int
421       pubsync() { return this->sync(); }
422       //@}
423 
424       // [27.5.2.2.3] get area
425       /**
426        *  @brief  Looking ahead into the stream.
427        *  @return  The number of characters available.
428        *
429        *  If a read position is available, returns the number of characters
430        *  available for reading before the buffer must be refilled.
431        *  Otherwise returns the derived @c showmanyc().
432       */
433       streamsize
434       in_avail()
435       {
436 	streamsize __ret;
437 	if (_M_in_cur && _M_in_cur < _M_in_end)
438 	  {
439 	    if (_M_pback_init)
440 	      {
441 		size_t __save_len =  _M_pback_end_save - _M_pback_cur_save;
442 		size_t __pback_len = _M_in_cur - _M_pback;
443 		__ret = __save_len - __pback_len;
444 	      }
445 	    else
446 	      __ret = this->egptr() - this->gptr();
447 	  }
448 	else
449 	  __ret = this->showmanyc();
450 	return __ret;
451       }
452 
453       /**
454        *  @brief  Getting the next character.
455        *  @return  The next character, or eof.
456        *
457        *  Calls @c sbumpc(), and if that function returns
458        *  @c traits::eof(), so does this function.  Otherwise, @c sgetc().
459       */
460       int_type
461       snextc()
462       {
463 	int_type __eof = traits_type::eof();
464 	return (traits_type::eq_int_type(this->sbumpc(), __eof)
465 		? __eof : this->sgetc());
466       }
467 
468       /**
469        *  @brief  Getting the next character.
470        *  @return  The next character, or eof.
471        *
472        *  If the input read position is available, returns that character
473        *  and increments the read pointer, otherwise calls and returns
474        *  @c uflow().
475       */
476       int_type
477       sbumpc();
478 
479       /**
480        *  @brief  Getting the next character.
481        *  @return  The next character, or eof.
482        *
483        *  If the input read position is available, returns that character,
484        *  otherwise calls and returns @c underflow().  Does not move the
485        *  read position after fetching the character.
486       */
487       int_type
488       sgetc()
489       {
490 	int_type __ret;
491 	if (_M_in_cur && _M_in_cur < _M_in_end)
492 	  __ret = traits_type::to_int_type(*(this->gptr()));
493 	else
494 	  __ret = this->underflow();
495 	return __ret;
496       }
497 
498       /**
499        *  @brief  Entry point for xsgetn.
500        *  @param  s  A buffer area.
501        *  @param  n  A count.
502        *
503        *  Returns xsgetn(s,n).  The effect is to fill @a s[0] through
504        *  @a s[n-1] with characters from the input sequence, if possible.
505       */
506       streamsize
507       sgetn(char_type* __s, streamsize __n)
508       { return this->xsgetn(__s, __n); }
509 
510       // [27.5.2.2.4] putback
511       /**
512        *  @brief  Pushing characters back into the input stream.
513        *  @param  c  The character to push back.
514        *  @return  The previous character, if possible.
515        *
516        *  Similar to sungetc(), but @a c is pushed onto the stream instead
517        *  of "the previous character".  If successful, the next character
518        *  fetched from the input stream will be @a c.
519       */
520       int_type
521       sputbackc(char_type __c);
522 
523       /**
524        *  @brief  Moving backwards in the input stream.
525        *  @return  The previous character, if possible.
526        *
527        *  If a putback position is available, this function decrements the
528        *  input pointer and returns that character.  Otherwise, calls and
529        *  returns pbackfail().  The effect is to "unget" the last character
530        *  "gotten".
531       */
532       int_type
533       sungetc();
534 
535       // [27.5.2.2.5] put area
536       /**
537        *  @brief  Entry point for all single-character output functions.
538        *  @param  c  A character to output.
539        *  @return  @a c, if possible.
540        *
541        *  One of two public output functions.
542        *
543        *  If a write position is available for the output sequence (i.e.,
544        *  the buffer is not full), stores @a c in that position, increments
545        *  the position, and returns @c traits::to_int_type(c).  If a write
546        *  position is not available, returns @c overflow(c).
547       */
548       int_type
549       sputc(char_type __c);
550 
551       /**
552        *  @brief  Entry point for all single-character output functions.
553        *  @param  s  A buffer read area.
554        *  @param  n  A count.
555        *
556        *  One of two public output functions.
557        *
558        *
559        *  Returns xsputn(s,n).  The effect is to write @a s[0] through
560        *  @a s[n-1] to the output sequence, if possible.
561       */
562       streamsize
563       sputn(const char_type* __s, streamsize __n)
564       { return this->xsputn(__s, __n); }
565 
566     protected:
567       /**
568        *  @brief  Base constructor.
569        *
570        *  Only called from derived constructors, and sets up all the
571        *  buffer data to zero, including the pointers described in the
572        *  basic_streambuf class description.  Note that, as a result,
573        *  - the class starts with no read nor write positions available,
574        *  - this is not an error
575       */
576       basic_streambuf()
577       : _M_buf(NULL), _M_buf_size(0), _M_buf_size_opt(BUFSIZ),
578       _M_buf_unified(false), _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
579       _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
580       _M_mode(ios_base::openmode(0)), _M_buf_locale(locale()),
581       _M_pback_cur_save(0), _M_pback_end_save(0),
582       _M_pback_init(false)
583       { }
584 
585       // [27.5.2.3.1] get area access
586       //@{
587       /**
588        *  @brief  Access to the get area.
589        *
590        *  These functions are only available to other protected functions,
591        *  including derived classes.
592        *
593        *  - eback() returns the beginning pointer for the input sequence
594        *  - gptr() returns the next pointer for the input sequence
595        *  - egptr() returns the end pointer for the input sequence
596       */
597       char_type*
598       eback() const { return _M_in_beg; }
599 
600       char_type*
601       gptr()  const { return _M_in_cur;  }
602 
603       char_type*
604       egptr() const { return _M_in_end; }
605       //@}
606 
607       /**
608        *  @brief  Moving the read position.
609        *  @param  n  The delta by which to move.
610        *
611        *  This just advances the read position without returning any data.
612       */
613       void
614       gbump(int __n) { _M_in_cur += __n; }
615 
616       /**
617        *  @brief  Setting the three read area pointers.
618        *  @param  gbeg  A pointer.
619        *  @param  gnext  A pointer.
620        *  @param  gend  A pointer.
621        *  @post  @a gbeg == @c eback(), @a gnext == @c gptr(), and
622        *         @a gend == @c egptr()
623       */
624       void
625       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
626       {
627 	_M_in_beg = __gbeg;
628 	_M_in_cur = __gnext;
629 	_M_in_end = __gend;
630 	if (!(_M_mode & ios_base::in) && __gbeg && __gnext && __gend)
631 	  _M_mode = _M_mode | ios_base::in;
632       }
633 
634       // [27.5.2.3.2] put area access
635       //@{
636       /**
637        *  @brief  Access to the put area.
638        *
639        *  These functions are only available to other protected functions,
640        *  including derived classes.
641        *
642        *  - pbase() returns the beginning pointer for the output sequence
643        *  - pptr() returns the next pointer for the output sequence
644        *  - epptr() returns the end pointer for the output sequence
645       */
646       char_type*
647       pbase() const { return _M_out_beg; }
648 
649       char_type*
650       pptr() const { return _M_out_cur; }
651 
652       char_type*
653       epptr() const { return _M_out_end; }
654       //@}
655 
656       /**
657        *  @brief  Moving the write position.
658        *  @param  n  The delta by which to move.
659        *
660        *  This just advances the write position without returning any data.
661       */
662       void
663       pbump(int __n) { _M_out_cur += __n; }
664 
665       /**
666        *  @brief  Setting the three write area pointers.
667        *  @param  pbeg  A pointer.
668        *  @param  pend  A pointer.
669        *  @post  @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
670        *         @a pend == @c epptr()
671       */
672       void
673       setp(char_type* __pbeg, char_type* __pend)
674       {
675 	_M_out_beg = _M_out_cur = __pbeg;
676 	_M_out_end = __pend;
677 	if (!(_M_mode & ios_base::out) && __pbeg && __pend)
678 	  _M_mode = _M_mode | ios_base::out;
679       }
680 
681       // [27.5.2.4] virtual functions
682       // [27.5.2.4.1] locales
683       /**
684        *  @brief  Changes translations.
685        *  @param  loc  A new locale.
686        *
687        *  Translations done during I/O which depend on the current locale
688        *  are changed by this call.  The standard adds, "Between invocations
689        *  of this function a class derived from streambuf can safely cache
690        *  results of calls to locale functions and to members of facets
691        *  so obtained."  This function simply stores the new locale for use
692        *  by derived classes.
693       */
694       virtual void
695       imbue(const locale& __loc)
696       {
697 	if (_M_buf_locale != __loc)
698 	  _M_buf_locale = __loc;
699       }
700 
701       // [27.5.2.4.2] buffer management and positioning
702       /**
703        *  @brief  Maniuplates the buffer.
704        *
705        *  Each derived class provides its own appropriate behavior.  See
706        *  the next-to-last paragraph of
707        *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
708        *  more on this function.
709        *
710        *  @note  Base class version does nothing, returns @c this.
711       */
712       virtual basic_streambuf<char_type,_Traits>*
713       setbuf(char_type*, streamsize)
714       {	return this; }
715 
716       /**
717        *  @brief  Alters the stream positions.
718        *
719        *  Each derived class provides its own appropriate behavior.
720        *  @note  Base class version does nothing, returns a @c pos_type
721        *         that represents an invalid stream position.
722       */
723       virtual pos_type
724       seekoff(off_type, ios_base::seekdir,
725 	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
726       { return pos_type(off_type(-1)); }
727 
728       /**
729        *  @brief  Alters the stream positions.
730        *
731        *  Each derived class provides its own appropriate behavior.
732        *  @note  Base class version does nothing, returns a @c pos_type
733        *         that represents an invalid stream position.
734       */
735       virtual pos_type
736       seekpos(pos_type,
737 	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
738       { return pos_type(off_type(-1)); }
739 
740       /**
741        *  @brief  Synchronizes the buffer arrays with the controlled sequences.
742        *  @return  -1 on failure.
743        *
744        *  Each derived class provides its own appropriate behavior,
745        *  including the definition of "failure".
746        *  @note  Base class version does nothing, returns zero.
747       */
748       virtual int
749       sync() { return 0; }
750 
751       // [27.5.2.4.3] get area
752       /**
753        *  @brief  Investigating the data available.
754        *  @return  An estimate of the number of characters available in the
755        *           input sequence, or -1.
756        *
757        *  "If it returns a positive value, then successive calls to
758        *  @c underflow() will not return @c traits::eof() until at least that
759        *  number of characters have been supplied.  If @c showmanyc()
760        *  returns -1, then calls to @c underflow() or @c uflow() will fail."
761        *  [27.5.2.4.3]/1
762        *
763        *  @note  Base class version does nothing, returns zero.
764        *  @note  The standard adds that "the intention is not only that the
765        *         calls [to underflow or uflow] will not return @c eof() but
766        *         that they will return "immediately".
767        *  @note  The standard adds that "the morphemes of @c showmanyc are
768        *         "es-how-many-see", not "show-manic".
769       */
770       virtual streamsize
771       showmanyc() { return 0; }
772 
773       /**
774        *  @brief  Multiple character extraction.
775        *  @param  s  A buffer area.
776        *  @param  n  Maximum number of characters to assign.
777        *  @return  The number of characters assigned.
778        *
779        *  Fills @a s[0] through @a s[n-1] with characters from the input
780        *  sequence, as if by @c sbumpc().  Stops when either @a n characters
781        *  have been copied, or when @c traits::eof() would be copied.
782        *
783        *  It is expected that derived classes provide a more efficient
784        *  implementation by overriding this definition.
785       */
786       virtual streamsize
787       xsgetn(char_type* __s, streamsize __n);
788 
789       /**
790        *  @brief  Fetches more data from the controlled sequence.
791        *  @return  The first character from the <em>pending sequence</em>.
792        *
793        *  Informally, this function is called when the input buffer is
794        *  exhausted (or does not exist, as buffering need not actually be
795        *  done).  If a buffer exists, it is "refilled".  In either case, the
796        *  next available character is returned, or @c traits::eof() to
797        *  indicate a null pending sequence.
798        *
799        *  For a formal definiton of the pending sequence, see a good text
800        *  such as Langer & Kreft, or [27.5.2.4.3]/7-14.
801        *
802        *  A functioning input streambuf can be created by overriding only
803        *  this function (no buffer area will be used).  For an example, see
804        *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
805        *
806        *  @note  Base class version does nothing, returns eof().
807       */
808       virtual int_type
809       underflow()
810       { return traits_type::eof(); }
811 
812       /**
813        *  @brief  Fetches more data from the controlled sequence.
814        *  @return  The first character from the <em>pending sequence</em>.
815        *
816        *  Informally, this function does the same thing as @c underflow(),
817        *  and in fact is required to call that function.  It also returns
818        *  the new character, like @c underflow() does.  However, this
819        *  function also moves the read position forward by one.
820       */
821       virtual int_type
822       uflow()
823       {
824 	int_type __ret = traits_type::eof();
825 	bool __testeof = traits_type::eq_int_type(this->underflow(), __ret);
826 	bool __testpending = _M_in_cur && _M_in_cur < _M_in_end;
827 	if (!__testeof && __testpending)
828 	  {
829 	    __ret = traits_type::to_int_type(*_M_in_cur);
830 	    ++_M_in_cur;
831 	    if (_M_buf_unified && _M_mode & ios_base::out)
832 	      ++_M_out_cur;
833 	  }
834 	return __ret;
835       }
836 
837       // [27.5.2.4.4] putback
838       /**
839        *  @brief  Tries to back up the input sequence.
840        *  @param  c  The character to be inserted back into the sequence.
841        *  @return  eof() on failure, "some other value" on success
842        *  @post  The constraints of @c gptr(), @c eback(), and @c pptr()
843        *         are the same as for @c underflow().
844        *
845        *  @note  Base class version does nothing, returns eof().
846       */
847       virtual int_type
848       pbackfail(int_type /* __c */  = traits_type::eof())
849       { return traits_type::eof(); }
850 
851       // Put area:
852       /**
853        *  @brief  Multiple character insertion.
854        *  @param  s  A buffer area.
855        *  @param  n  Maximum number of characters to write.
856        *  @return  The number of characters written.
857        *
858        *  Writes @a s[0] through @a s[n-1] to the output sequence, as if
859        *  by @c sputc().  Stops when either @a n characters have been
860        *  copied, or when @c sputc() would return @c traits::eof().
861        *
862        *  It is expected that derived classes provide a more efficient
863        *  implementation by overriding this definition.
864       */
865       virtual streamsize
866       xsputn(const char_type* __s, streamsize __n);
867 
868       /**
869        *  @brief  Consumes data from the buffer; writes to the
870        *          controlled sequence.
871        *  @param  c  An additional character to consume.
872        *  @return  eof() to indicate failure, something else (usually
873        *           @a c, or not_eof())
874        *
875        *  Informally, this function is called when the output buffer is full
876        *  (or does not exist, as buffering need not actually be done).  If a
877        *  buffer exists, it is "consumed", with "some effect" on the
878        *  controlled sequence.  (Typically, the buffer is written out to the
879        *  sequence verbatim.)  In either case, the character @a c is also
880        *  written out, if @a c is not @c eof().
881        *
882        *  For a formal definiton of this function, see a good text
883        *  such as Langer & Kreft, or [27.5.2.4.5]/3-7.
884        *
885        *  A functioning output streambuf can be created by overriding only
886        *  this function (no buffer area will be used).
887        *
888        *  @note  Base class version does nothing, returns eof().
889       */
890       virtual int_type
891       overflow(int_type /* __c */ = traits_type::eof())
892       { return traits_type::eof(); }
893 
894 #ifdef _GLIBCPP_DEPRECATED
895     // Annex D.6
896     public:
897       /**
898        *  @brief  Tosses a character.
899        *
900        *  Advances the read pointer, ignoring the character that would have
901        *  been read.
902        *
903        *  See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
904        *
905        *  @note  This function has been deprecated by the standard.  You
906        *         must define @c _GLIBCPP_DEPRECATED to make this visible; see
907        *         c++config.h.
908       */
909       void
910       stossc()
911       {
912 	if (_M_in_cur < _M_in_end)
913 	  ++_M_in_cur;
914 	else
915 	  this->uflow();
916       }
917 #endif
918 
919 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
920     // Side effect of DR 50.
921     private:
922       basic_streambuf(const __streambuf_type&) { };
923 
924       __streambuf_type&
925       operator=(const __streambuf_type&) { return *this; };
926 #endif
927     };
928 } // namespace std
929 
930 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
931 # define export
932 #endif
933 #ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
934 #include <bits/streambuf.tcc>
935 #endif
936 
937 #endif
938