1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
32 // ISO C++ 14882: 27.5 Stream buffers
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.
40 #ifndef _CPP_STREAMBUF
41 #define _CPP_STREAMBUF 1
43 #pragma GCC system_header
45 #include <bits/c++config.h>
47 #include <bits/localefwd.h>
48 #include <bits/ios_base.h>
57 template<typename _CharT, typename _Traits>
59 __copy_streambufs(basic_ios<_CharT, _Traits>& _ios,
60 basic_streambuf<_CharT, _Traits>* __sbin,
61 basic_streambuf<_CharT, _Traits>* __sbout);
64 * @brief The actual work of input and output (interface).
66 * This is a base class. Derived stream buffers each control a
67 * pair of character sequences: one for input, and one for output.
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.
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
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.
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).
102 * -# The following semantic constraints shall always apply for any set
103 * of three pointers for a sequence, using the pointer names given
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).
123 template<typename _CharT, typename _Traits>
124 class basic_streambuf
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.
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;
143 * These are non-standard types.
146 typedef ctype<char_type> __ctype_type;
147 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
148 typedef typename traits_type::state_type __state_type;
151 friend class basic_ios<char_type, traits_type>;
152 friend class basic_istream<char_type, traits_type>;
153 friend class basic_ostream<char_type, traits_type>;
154 friend class istreambuf_iterator<char_type, traits_type>;
155 friend class ostreambuf_iterator<char_type, traits_type>;
158 __copy_streambufs<>(basic_ios<char_type, traits_type>& __ios,
159 __streambuf_type* __sbin,__streambuf_type* __sbout);
164 * True iff _M_in_* and _M_out_* buffers should always point to
165 * the same place. True for fstreams, false for sstreams.
173 * This is based on _IO_FILE, just reordered to be more consistent,
174 * and is intended to be the most minimal abstraction for an
176 * - get == input == read
177 * - put == output == write
180 char_type* _M_in_beg; // Start of get area.
181 char_type* _M_in_cur; // Current read area.
182 char_type* _M_in_end; // End of get area.
183 char_type* _M_out_beg; // Start of put area.
184 char_type* _M_out_cur; // Current put area.
185 char_type* _M_out_end; // End of put area.
190 * setp (and _M_set_buffer(0) in basic_filebuf) set it equal to
191 * _M_out_beg, then at each put operation it may be moved
192 * forward (toward _M_out_end) by _M_out_cur_move.
195 char_type* _M_out_lim; // End limit of used put area.
201 * Place to stash in || out || in | out settings for current streambuf.
204 ios_base::openmode _M_mode;
208 * Current locale setting.
211 locale _M_buf_locale;
218 fpos<__state_type> _M_pos;
220 // Correctly sets the _M_in_cur pointer, and bumps the
221 // _M_out_cur pointer as well if necessary.
223 _M_move_in_cur(off_type __n) // argument needs to be +-
225 const bool __testout = _M_out_cur;
227 if (__testout && _M_buf_unified)
231 // Correctly sets the _M_out_cur pointer, and bumps the
232 // appropriate _M_out_lim and _M_in_end pointers as well. Necessary
233 // for the un-tied stringbufs, in in|out mode.
235 // __n + _M_out_[cur, lim] <= _M_out_end
236 // Assuming all _M_out_[beg, cur, lim] pointers are operating on
238 // _M_out_beg <= _M_*_ <= _M_out_end
240 _M_move_out_cur(off_type __n) // argument needs to be +-
242 const bool __testin = _M_in_cur;
245 if (__testin && _M_buf_unified)
247 if (_M_out_cur > _M_out_lim)
249 _M_out_lim = _M_out_cur;
250 // NB: in | out buffers drag the _M_in_end pointer along...
257 /// Destructor deallocates no buffer space.
261 _M_buf_unified = false;
262 _M_mode = ios_base::openmode(0);
265 // [27.5.2.2.1] locales
267 * @brief Entry point for imbue().
268 * @param loc The new locale.
269 * @return The previous locale.
271 * Calls the derived imbue(loc).
274 pubimbue(const locale &__loc)
276 locale __tmp(this->getloc());
282 * @brief Locale access.
283 * @return The current locale in effect.
285 * If pubimbue(loc) has been called, then the most recent @c loc
286 * is returned. Otherwise the global locale in effect at the time
287 * of construction is returned.
291 { return _M_buf_locale; }
293 // [27.5.2.2.2] buffer management and positioning
296 * @brief Entry points for derived buffer functions.
298 * The public versions of @c pubfoo dispatch to the protected
299 * derived @c foo member functions, passing the arguments (if any)
300 * and returning the result unchanged.
303 pubsetbuf(char_type* __s, streamsize __n)
304 { return this->setbuf(__s, __n); }
307 pubseekoff(off_type __off, ios_base::seekdir __way,
308 ios_base::openmode __mode = ios_base::in | ios_base::out)
309 { return this->seekoff(__off, __way, __mode); }
312 pubseekpos(pos_type __sp,
313 ios_base::openmode __mode = ios_base::in | ios_base::out)
314 { return this->seekpos(__sp, __mode); }
317 pubsync() { return this->sync(); }
320 // [27.5.2.2.3] get area
322 * @brief Looking ahead into the stream.
323 * @return The number of characters available.
325 * If a read position is available, returns the number of characters
326 * available for reading before the buffer must be refilled.
327 * Otherwise returns the derived @c showmanyc().
332 streamsize __ret = _M_in_end - _M_in_cur;
333 return __ret ? __ret : this->showmanyc();
337 * @brief Getting the next character.
338 * @return The next character, or eof.
340 * Calls @c sbumpc(), and if that function returns
341 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
346 int_type __ret = traits_type::eof();
347 if (!traits_type::eq_int_type(this->sbumpc(), __ret))
348 __ret = this->sgetc();
353 * @brief Getting the next character.
354 * @return The next character, or eof.
356 * If the input read position is available, returns that character
357 * and increments the read pointer, otherwise calls and returns
364 * @brief Getting the next character.
365 * @return The next character, or eof.
367 * If the input read position is available, returns that character,
368 * otherwise calls and returns @c underflow(). Does not move the
369 * read position after fetching the character.
375 if (_M_in_cur < _M_in_end)
376 __ret = traits_type::to_int_type(*this->_M_in_cur);
378 __ret = this->underflow();
383 * @brief Entry point for xsgetn.
384 * @param s A buffer area.
387 * Returns xsgetn(s,n). The effect is to fill @a s[0] through
388 * @a s[n-1] with characters from the input sequence, if possible.
391 sgetn(char_type* __s, streamsize __n)
392 { return this->xsgetn(__s, __n); }
394 // [27.5.2.2.4] putback
396 * @brief Pushing characters back into the input stream.
397 * @param c The character to push back.
398 * @return The previous character, if possible.
400 * Similar to sungetc(), but @a c is pushed onto the stream instead
401 * of "the previous character". If successful, the next character
402 * fetched from the input stream will be @a c.
405 sputbackc(char_type __c);
408 * @brief Moving backwards in the input stream.
409 * @return The previous character, if possible.
411 * If a putback position is available, this function decrements the
412 * input pointer and returns that character. Otherwise, calls and
413 * returns pbackfail(). The effect is to "unget" the last character
419 // [27.5.2.2.5] put area
421 * @brief Entry point for all single-character output functions.
422 * @param c A character to output.
423 * @return @a c, if possible.
425 * One of two public output functions.
427 * If a write position is available for the output sequence (i.e.,
428 * the buffer is not full), stores @a c in that position, increments
429 * the position, and returns @c traits::to_int_type(c). If a write
430 * position is not available, returns @c overflow(c).
433 sputc(char_type __c);
436 * @brief Entry point for all single-character output functions.
437 * @param s A buffer read area.
440 * One of two public output functions.
443 * Returns xsputn(s,n). The effect is to write @a s[0] through
444 * @a s[n-1] to the output sequence, if possible.
447 sputn(const char_type* __s, streamsize __n)
448 { return this->xsputn(__s, __n); }
452 * @brief Base constructor.
454 * Only called from derived constructors, and sets up all the
455 * buffer data to zero, including the pointers described in the
456 * basic_streambuf class description. Note that, as a result,
457 * - the class starts with no read nor write positions available,
458 * - this is not an error
461 : _M_buf_unified(false), _M_in_beg(0), _M_in_cur(0),
462 _M_in_end(0), _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
463 _M_out_lim(0), _M_mode(ios_base::openmode(0)),
464 _M_buf_locale(locale())
467 // [27.5.2.3.1] get area access
470 * @brief Access to the get area.
472 * These functions are only available to other protected functions,
473 * including derived classes.
475 * - eback() returns the beginning pointer for the input sequence
476 * - gptr() returns the next pointer for the input sequence
477 * - egptr() returns the end pointer for the input sequence
480 eback() const { return _M_in_beg; }
483 gptr() const { return _M_in_cur; }
486 egptr() const { return _M_in_end; }
490 * @brief Moving the read position.
491 * @param n The delta by which to move.
493 * This just advances the read position without returning any data.
496 gbump(int __n) { _M_in_cur += __n; }
499 * @brief Setting the three read area pointers.
500 * @param gbeg A pointer.
501 * @param gnext A pointer.
502 * @param gend A pointer.
503 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and
504 * @a gend == @c egptr()
507 setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
514 // [27.5.2.3.2] put area access
517 * @brief Access to the put area.
519 * These functions are only available to other protected functions,
520 * including derived classes.
522 * - pbase() returns the beginning pointer for the output sequence
523 * - pptr() returns the next pointer for the output sequence
524 * - epptr() returns the end pointer for the output sequence
527 pbase() const { return _M_out_beg; }
530 pptr() const { return _M_out_cur; }
533 epptr() const { return _M_out_end; }
537 * @brief Moving the write position.
538 * @param n The delta by which to move.
540 * This just advances the write position without returning any data.
543 pbump(int __n) { _M_out_cur += __n; }
546 * @brief Setting the three write area pointers.
547 * @param pbeg A pointer.
548 * @param pend A pointer.
549 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
550 * @a pend == @c epptr()
553 setp(char_type* __pbeg, char_type* __pend)
555 _M_out_beg = _M_out_cur = _M_out_lim = __pbeg;
559 // [27.5.2.4] virtual functions
560 // [27.5.2.4.1] locales
562 * @brief Changes translations.
563 * @param loc A new locale.
565 * Translations done during I/O which depend on the current locale
566 * are changed by this call. The standard adds, "Between invocations
567 * of this function a class derived from streambuf can safely cache
568 * results of calls to locale functions and to members of facets
569 * so obtained." This function simply stores the new locale for use
570 * by derived classes.
573 imbue(const locale& __loc)
575 if (_M_buf_locale != __loc)
576 _M_buf_locale = __loc;
579 // [27.5.2.4.2] buffer management and positioning
581 * @brief Maniuplates the buffer.
583 * Each derived class provides its own appropriate behavior. See
584 * the next-to-last paragraph of
585 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
586 * more on this function.
588 * @note Base class version does nothing, returns @c this.
590 virtual basic_streambuf<char_type,_Traits>*
591 setbuf(char_type*, streamsize)
595 * @brief Alters the stream positions.
597 * Each derived class provides its own appropriate behavior.
598 * @note Base class version does nothing, returns a @c pos_type
599 * that represents an invalid stream position.
602 seekoff(off_type, ios_base::seekdir,
603 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
604 { return pos_type(off_type(-1)); }
607 * @brief Alters the stream positions.
609 * Each derived class provides its own appropriate behavior.
610 * @note Base class version does nothing, returns a @c pos_type
611 * that represents an invalid stream position.
615 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
616 { return pos_type(off_type(-1)); }
619 * @brief Synchronizes the buffer arrays with the controlled sequences.
620 * @return -1 on failure.
622 * Each derived class provides its own appropriate behavior,
623 * including the definition of "failure".
624 * @note Base class version does nothing, returns zero.
629 // [27.5.2.4.3] get area
631 * @brief Investigating the data available.
632 * @return An estimate of the number of characters available in the
633 * input sequence, or -1.
635 * "If it returns a positive value, then successive calls to
636 * @c underflow() will not return @c traits::eof() until at least that
637 * number of characters have been supplied. If @c showmanyc()
638 * returns -1, then calls to @c underflow() or @c uflow() will fail."
641 * @note Base class version does nothing, returns zero.
642 * @note The standard adds that "the intention is not only that the
643 * calls [to underflow or uflow] will not return @c eof() but
644 * that they will return "immediately".
645 * @note The standard adds that "the morphemes of @c showmanyc are
646 * "es-how-many-see", not "show-manic".
649 showmanyc() { return 0; }
652 * @brief Multiple character extraction.
653 * @param s A buffer area.
654 * @param n Maximum number of characters to assign.
655 * @return The number of characters assigned.
657 * Fills @a s[0] through @a s[n-1] with characters from the input
658 * sequence, as if by @c sbumpc(). Stops when either @a n characters
659 * have been copied, or when @c traits::eof() would be copied.
661 * It is expected that derived classes provide a more efficient
662 * implementation by overriding this definition.
665 xsgetn(char_type* __s, streamsize __n);
668 * @brief Fetches more data from the controlled sequence.
669 * @return The first character from the <em>pending sequence</em>.
671 * Informally, this function is called when the input buffer is
672 * exhausted (or does not exist, as buffering need not actually be
673 * done). If a buffer exists, it is "refilled". In either case, the
674 * next available character is returned, or @c traits::eof() to
675 * indicate a null pending sequence.
677 * For a formal definiton of the pending sequence, see a good text
678 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
680 * A functioning input streambuf can be created by overriding only
681 * this function (no buffer area will be used). For an example, see
682 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
684 * @note Base class version does nothing, returns eof().
688 { return traits_type::eof(); }
691 * @brief Fetches more data from the controlled sequence.
692 * @return The first character from the <em>pending sequence</em>.
694 * Informally, this function does the same thing as @c underflow(),
695 * and in fact is required to call that function. It also returns
696 * the new character, like @c underflow() does. However, this
697 * function also moves the read position forward by one.
702 int_type __ret = traits_type::eof();
703 const bool __testeof = traits_type::eq_int_type(this->underflow(),
705 if (!__testeof && _M_in_cur < _M_in_end)
707 __ret = traits_type::to_int_type(*_M_in_cur);
713 // [27.5.2.4.4] putback
715 * @brief Tries to back up the input sequence.
716 * @param c The character to be inserted back into the sequence.
717 * @return eof() on failure, "some other value" on success
718 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
719 * are the same as for @c underflow().
721 * @note Base class version does nothing, returns eof().
724 pbackfail(int_type /* __c */ = traits_type::eof())
725 { return traits_type::eof(); }
729 * @brief Multiple character insertion.
730 * @param s A buffer area.
731 * @param n Maximum number of characters to write.
732 * @return The number of characters written.
734 * Writes @a s[0] through @a s[n-1] to the output sequence, as if
735 * by @c sputc(). Stops when either @a n characters have been
736 * copied, or when @c sputc() would return @c traits::eof().
738 * It is expected that derived classes provide a more efficient
739 * implementation by overriding this definition.
742 xsputn(const char_type* __s, streamsize __n);
745 * @brief Consumes data from the buffer; writes to the
746 * controlled sequence.
747 * @param c An additional character to consume.
748 * @return eof() to indicate failure, something else (usually
749 * @a c, or not_eof())
751 * Informally, this function is called when the output buffer is full
752 * (or does not exist, as buffering need not actually be done). If a
753 * buffer exists, it is "consumed", with "some effect" on the
754 * controlled sequence. (Typically, the buffer is written out to the
755 * sequence verbatim.) In either case, the character @a c is also
756 * written out, if @a c is not @c eof().
758 * For a formal definiton of this function, see a good text
759 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
761 * A functioning output streambuf can be created by overriding only
762 * this function (no buffer area will be used).
764 * @note Base class version does nothing, returns eof().
767 overflow(int_type /* __c */ = traits_type::eof())
768 { return traits_type::eof(); }
770 #ifdef _GLIBCPP_DEPRECATED
774 * @brief Tosses a character.
776 * Advances the read pointer, ignoring the character that would have
779 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
781 * @note This function has been deprecated by the standard. You
782 * must define @c _GLIBCPP_DEPRECATED to make this visible; see
788 if (_M_in_cur < _M_in_end)
795 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
796 // Side effect of DR 50.
798 basic_streambuf(const __streambuf_type&) { };
801 operator=(const __streambuf_type&) { return *this; };
806 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
809 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
810 #include <bits/streambuf.tcc>