1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
39 #ifndef _GLIBXX_STREAMBUF
40 #define _GLIBXX_STREAMBUF 1
42 #pragma GCC system_header
44 #include <bits/c++config.h>
46 #include <bits/localefwd.h>
47 #include <bits/ios_base.h>
48 #include <bits/cpp_type_traits.h>
49 #include <ext/type_traits.h>
51 _GLIBCXX_BEGIN_NAMESPACE(std)
58 template<typename _CharT, typename _Traits>
60 __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
61 basic_streambuf<_CharT, _Traits>*, bool&);
63 template<typename _CharT>
64 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
66 __copy_aux(istreambuf_iterator<_CharT>,
67 istreambuf_iterator<_CharT>, _CharT*);
69 template<typename _CharT>
70 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
71 istreambuf_iterator<_CharT> >::__type
72 find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
76 * @brief The actual work of input and output (interface).
78 * This is a base class. Derived stream buffers each control a
79 * pair of character sequences: one for input, and one for output.
81 * Section [27.5.1] of the standard describes the requirements and
82 * behavior of stream buffer classes. That section (three paragraphs)
83 * is reproduced here, for simplicity and accuracy.
85 * -# Stream buffers can impose various constraints on the sequences
86 * they control. Some constraints are:
87 * - The controlled input sequence can be not readable.
88 * - The controlled output sequence can be not writable.
89 * - The controlled sequences can be associated with the contents of
90 * other representations for character sequences, such as external
92 * - The controlled sequences can support operations @e directly to or
93 * from associated sequences.
94 * - The controlled sequences can impose limitations on how the
95 * program can read characters from a sequence, write characters to
96 * a sequence, put characters back into an input sequence, or alter
97 * the stream position.
99 * -# Each sequence is characterized by three pointers which, if non-null,
100 * all point into the same @c charT array object. The array object
101 * represents, at any moment, a (sub)sequence of characters from the
102 * sequence. Operations performed on a sequence alter the values
103 * stored in these pointers, perform reads and writes directly to or
104 * from associated sequences, and alter "the stream position" and
105 * conversion state as needed to maintain this subsequence relationship.
106 * The three pointers are:
107 * - the <em>beginning pointer</em>, or lowest element address in the
108 * array (called @e xbeg here);
109 * - the <em>next pointer</em>, or next element address that is a
110 * current candidate for reading or writing (called @e xnext here);
111 * - the <em>end pointer</em>, or first element address beyond the
112 * end of the array (called @e xend here).
114 * -# The following semantic constraints shall always apply for any set
115 * of three pointers for a sequence, using the pointer names given
117 * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
118 * also be non-null pointers into the same @c charT array, as
119 * described above; otherwise, @e xbeg and @e xend shall also be null.
120 * - If @e xnext is not a null pointer and @e xnext < @e xend for an
121 * output sequence, then a <em>write position</em> is available.
122 * In this case, @e *xnext shall be assignable as the next element
123 * to write (to put, or to store a character value, into the sequence).
124 * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
125 * input sequence, then a <em>putback position</em> is available.
126 * In this case, @e xnext[-1] shall have a defined value and is the
127 * next (preceding) element to store a character that is put back
128 * into the input sequence.
129 * - If @e xnext is not a null pointer and @e xnext< @e xend for an
130 * input sequence, then a <em>read position</em> is available.
131 * In this case, @e *xnext shall have a defined value and is the
132 * next element to read (to get, or to obtain a character value,
133 * from the sequence).
135 template<typename _CharT, typename _Traits>
136 class basic_streambuf
141 * These are standard types. They permit a standardized way of
142 * referring to names of (or names dependant on) the template
143 * parameters, which are specific to the implementation.
145 typedef _CharT char_type;
146 typedef _Traits traits_type;
147 typedef typename traits_type::int_type int_type;
148 typedef typename traits_type::pos_type pos_type;
149 typedef typename traits_type::off_type off_type;
155 * This is a non-standard type.
158 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
161 friend class basic_ios<char_type, traits_type>;
162 friend class basic_istream<char_type, traits_type>;
163 friend class basic_ostream<char_type, traits_type>;
164 friend class istreambuf_iterator<char_type, traits_type>;
165 friend class ostreambuf_iterator<char_type, traits_type>;
168 __copy_streambufs_eof<>(__streambuf_type*, __streambuf_type*, bool&);
170 template<typename _CharT2>
171 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
173 __copy_aux(istreambuf_iterator<_CharT2>,
174 istreambuf_iterator<_CharT2>, _CharT2*);
176 template<typename _CharT2>
177 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
178 istreambuf_iterator<_CharT2> >::__type
179 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
182 template<typename _CharT2, typename _Traits2>
183 friend basic_istream<_CharT2, _Traits2>&
184 operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
186 template<typename _CharT2, typename _Traits2, typename _Alloc>
187 friend basic_istream<_CharT2, _Traits2>&
188 operator>>(basic_istream<_CharT2, _Traits2>&,
189 basic_string<_CharT2, _Traits2, _Alloc>&);
191 template<typename _CharT2, typename _Traits2, typename _Alloc>
192 friend basic_istream<_CharT2, _Traits2>&
193 getline(basic_istream<_CharT2, _Traits2>&,
194 basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
200 * This is based on _IO_FILE, just reordered to be more consistent,
201 * and is intended to be the most minimal abstraction for an
203 * - get == input == read
204 * - put == output == write
207 char_type* _M_in_beg; // Start of get area.
208 char_type* _M_in_cur; // Current read area.
209 char_type* _M_in_end; // End of get area.
210 char_type* _M_out_beg; // Start of put area.
211 char_type* _M_out_cur; // Current put area.
212 char_type* _M_out_end; // End of put area.
216 * Current locale setting.
219 locale _M_buf_locale;
222 /// Destructor deallocates no buffer space.
227 // [27.5.2.2.1] locales
229 * @brief Entry point for imbue().
230 * @param loc The new locale.
231 * @return The previous locale.
233 * Calls the derived imbue(loc).
236 pubimbue(const locale &__loc)
238 locale __tmp(this->getloc());
240 _M_buf_locale = __loc;
245 * @brief Locale access.
246 * @return The current locale in effect.
248 * If pubimbue(loc) has been called, then the most recent @c loc
249 * is returned. Otherwise the global locale in effect at the time
250 * of construction is returned.
254 { return _M_buf_locale; }
256 // [27.5.2.2.2] buffer management and positioning
259 * @brief Entry points for derived buffer functions.
261 * The public versions of @c pubfoo dispatch to the protected
262 * derived @c foo member functions, passing the arguments (if any)
263 * and returning the result unchanged.
266 pubsetbuf(char_type* __s, streamsize __n)
267 { return this->setbuf(__s, __n); }
270 pubseekoff(off_type __off, ios_base::seekdir __way,
271 ios_base::openmode __mode = ios_base::in | ios_base::out)
272 { return this->seekoff(__off, __way, __mode); }
275 pubseekpos(pos_type __sp,
276 ios_base::openmode __mode = ios_base::in | ios_base::out)
277 { return this->seekpos(__sp, __mode); }
280 pubsync() { return this->sync(); }
283 // [27.5.2.2.3] get area
285 * @brief Looking ahead into the stream.
286 * @return The number of characters available.
288 * If a read position is available, returns the number of characters
289 * available for reading before the buffer must be refilled.
290 * Otherwise returns the derived @c showmanyc().
295 const streamsize __ret = this->egptr() - this->gptr();
296 return __ret ? __ret : this->showmanyc();
300 * @brief Getting the next character.
301 * @return The next character, or eof.
303 * Calls @c sbumpc(), and if that function returns
304 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
309 int_type __ret = traits_type::eof();
310 if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
312 __ret = this->sgetc();
317 * @brief Getting the next character.
318 * @return The next character, or eof.
320 * If the input read position is available, returns that character
321 * and increments the read pointer, otherwise calls and returns
328 if (__builtin_expect(this->gptr() < this->egptr(), true))
330 __ret = traits_type::to_int_type(*this->gptr());
334 __ret = this->uflow();
339 * @brief Getting the next character.
340 * @return The next character, or eof.
342 * If the input read position is available, returns that character,
343 * otherwise calls and returns @c underflow(). Does not move the
344 * read position after fetching the character.
350 if (__builtin_expect(this->gptr() < this->egptr(), true))
351 __ret = traits_type::to_int_type(*this->gptr());
353 __ret = this->underflow();
358 * @brief Entry point for xsgetn.
359 * @param s A buffer area.
362 * Returns xsgetn(s,n). The effect is to fill @a s[0] through
363 * @a s[n-1] with characters from the input sequence, if possible.
366 sgetn(char_type* __s, streamsize __n)
367 { return this->xsgetn(__s, __n); }
369 // [27.5.2.2.4] putback
371 * @brief Pushing characters back into the input stream.
372 * @param c The character to push back.
373 * @return The previous character, if possible.
375 * Similar to sungetc(), but @a c is pushed onto the stream instead
376 * of "the previous character". If successful, the next character
377 * fetched from the input stream will be @a c.
380 sputbackc(char_type __c)
383 const bool __testpos = this->eback() < this->gptr();
384 if (__builtin_expect(!__testpos ||
385 !traits_type::eq(__c, this->gptr()[-1]), false))
386 __ret = this->pbackfail(traits_type::to_int_type(__c));
390 __ret = traits_type::to_int_type(*this->gptr());
396 * @brief Moving backwards in the input stream.
397 * @return The previous character, if possible.
399 * If a putback position is available, this function decrements the
400 * input pointer and returns that character. Otherwise, calls and
401 * returns pbackfail(). The effect is to "unget" the last character
408 if (__builtin_expect(this->eback() < this->gptr(), true))
411 __ret = traits_type::to_int_type(*this->gptr());
414 __ret = this->pbackfail();
418 // [27.5.2.2.5] put area
420 * @brief Entry point for all single-character output functions.
421 * @param c A character to output.
422 * @return @a c, if possible.
424 * One of two public output functions.
426 * If a write position is available for the output sequence (i.e.,
427 * the buffer is not full), stores @a c in that position, increments
428 * the position, and returns @c traits::to_int_type(c). If a write
429 * position is not available, returns @c overflow(c).
435 if (__builtin_expect(this->pptr() < this->epptr(), true))
439 __ret = traits_type::to_int_type(__c);
442 __ret = this->overflow(traits_type::to_int_type(__c));
447 * @brief Entry point for all single-character output functions.
448 * @param s A buffer read area.
451 * One of two public output functions.
454 * Returns xsputn(s,n). The effect is to write @a s[0] through
455 * @a s[n-1] to the output sequence, if possible.
458 sputn(const char_type* __s, streamsize __n)
459 { return this->xsputn(__s, __n); }
463 * @brief Base constructor.
465 * Only called from derived constructors, and sets up all the
466 * buffer data to zero, including the pointers described in the
467 * basic_streambuf class description. Note that, as a result,
468 * - the class starts with no read nor write positions available,
469 * - this is not an error
472 : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
473 _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
474 _M_buf_locale(locale())
477 // [27.5.2.3.1] get area access
480 * @brief Access to the get area.
482 * These functions are only available to other protected functions,
483 * including derived classes.
485 * - eback() returns the beginning pointer for the input sequence
486 * - gptr() returns the next pointer for the input sequence
487 * - egptr() returns the end pointer for the input sequence
490 eback() const { return _M_in_beg; }
493 gptr() const { return _M_in_cur; }
496 egptr() const { return _M_in_end; }
500 * @brief Moving the read position.
501 * @param n The delta by which to move.
503 * This just advances the read position without returning any data.
506 gbump(int __n) { _M_in_cur += __n; }
509 * @brief Setting the three read area pointers.
510 * @param gbeg A pointer.
511 * @param gnext A pointer.
512 * @param gend A pointer.
513 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and
514 * @a gend == @c egptr()
517 setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
524 // [27.5.2.3.2] put area access
527 * @brief Access to the put area.
529 * These functions are only available to other protected functions,
530 * including derived classes.
532 * - pbase() returns the beginning pointer for the output sequence
533 * - pptr() returns the next pointer for the output sequence
534 * - epptr() returns the end pointer for the output sequence
537 pbase() const { return _M_out_beg; }
540 pptr() const { return _M_out_cur; }
543 epptr() const { return _M_out_end; }
547 * @brief Moving the write position.
548 * @param n The delta by which to move.
550 * This just advances the write position without returning any data.
553 pbump(int __n) { _M_out_cur += __n; }
556 * @brief Setting the three write area pointers.
557 * @param pbeg A pointer.
558 * @param pend A pointer.
559 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
560 * @a pend == @c epptr()
563 setp(char_type* __pbeg, char_type* __pend)
565 _M_out_beg = _M_out_cur = __pbeg;
569 // [27.5.2.4] virtual functions
570 // [27.5.2.4.1] locales
572 * @brief Changes translations.
573 * @param loc A new locale.
575 * Translations done during I/O which depend on the current locale
576 * are changed by this call. The standard adds, "Between invocations
577 * of this function a class derived from streambuf can safely cache
578 * results of calls to locale functions and to members of facets
581 * @note Base class version does nothing.
587 // [27.5.2.4.2] buffer management and positioning
589 * @brief Maniuplates the buffer.
591 * Each derived class provides its own appropriate behavior. See
592 * the next-to-last paragraph of
593 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
594 * more on this function.
596 * @note Base class version does nothing, returns @c this.
598 virtual basic_streambuf<char_type,_Traits>*
599 setbuf(char_type*, streamsize)
603 * @brief Alters the stream positions.
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.
610 seekoff(off_type, ios_base::seekdir,
611 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
612 { return pos_type(off_type(-1)); }
615 * @brief Alters the stream positions.
617 * Each derived class provides its own appropriate behavior.
618 * @note Base class version does nothing, returns a @c pos_type
619 * that represents an invalid stream position.
623 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
624 { return pos_type(off_type(-1)); }
627 * @brief Synchronizes the buffer arrays with the controlled sequences.
628 * @return -1 on failure.
630 * Each derived class provides its own appropriate behavior,
631 * including the definition of "failure".
632 * @note Base class version does nothing, returns zero.
637 // [27.5.2.4.3] get area
639 * @brief Investigating the data available.
640 * @return An estimate of the number of characters available in the
641 * input sequence, or -1.
643 * "If it returns a positive value, then successive calls to
644 * @c underflow() will not return @c traits::eof() until at least that
645 * number of characters have been supplied. If @c showmanyc()
646 * returns -1, then calls to @c underflow() or @c uflow() will fail."
649 * @note Base class version does nothing, returns zero.
650 * @note The standard adds that "the intention is not only that the
651 * calls [to underflow or uflow] will not return @c eof() but
652 * that they will return "immediately".
653 * @note The standard adds that "the morphemes of @c showmanyc are
654 * "es-how-many-see", not "show-manic".
657 showmanyc() { return 0; }
660 * @brief Multiple character extraction.
661 * @param s A buffer area.
662 * @param n Maximum number of characters to assign.
663 * @return The number of characters assigned.
665 * Fills @a s[0] through @a s[n-1] with characters from the input
666 * sequence, as if by @c sbumpc(). Stops when either @a n characters
667 * have been copied, or when @c traits::eof() would be copied.
669 * It is expected that derived classes provide a more efficient
670 * implementation by overriding this definition.
673 xsgetn(char_type* __s, streamsize __n);
676 * @brief Fetches more data from the controlled sequence.
677 * @return The first character from the <em>pending sequence</em>.
679 * Informally, this function is called when the input buffer is
680 * exhausted (or does not exist, as buffering need not actually be
681 * done). If a buffer exists, it is "refilled". In either case, the
682 * next available character is returned, or @c traits::eof() to
683 * indicate a null pending sequence.
685 * For a formal definiton of the pending sequence, see a good text
686 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
688 * A functioning input streambuf can be created by overriding only
689 * this function (no buffer area will be used). For an example, see
690 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
692 * @note Base class version does nothing, returns eof().
696 { return traits_type::eof(); }
699 * @brief Fetches more data from the controlled sequence.
700 * @return The first character from the <em>pending sequence</em>.
702 * Informally, this function does the same thing as @c underflow(),
703 * and in fact is required to call that function. It also returns
704 * the new character, like @c underflow() does. However, this
705 * function also moves the read position forward by one.
710 int_type __ret = traits_type::eof();
711 const bool __testeof = traits_type::eq_int_type(this->underflow(),
715 __ret = traits_type::to_int_type(*this->gptr());
721 // [27.5.2.4.4] putback
723 * @brief Tries to back up the input sequence.
724 * @param c The character to be inserted back into the sequence.
725 * @return eof() on failure, "some other value" on success
726 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
727 * are the same as for @c underflow().
729 * @note Base class version does nothing, returns eof().
732 pbackfail(int_type /* __c */ = traits_type::eof())
733 { return traits_type::eof(); }
737 * @brief Multiple character insertion.
738 * @param s A buffer area.
739 * @param n Maximum number of characters to write.
740 * @return The number of characters written.
742 * Writes @a s[0] through @a s[n-1] to the output sequence, as if
743 * by @c sputc(). Stops when either @a n characters have been
744 * copied, or when @c sputc() would return @c traits::eof().
746 * It is expected that derived classes provide a more efficient
747 * implementation by overriding this definition.
750 xsputn(const char_type* __s, streamsize __n);
753 * @brief Consumes data from the buffer; writes to the
754 * controlled sequence.
755 * @param c An additional character to consume.
756 * @return eof() to indicate failure, something else (usually
757 * @a c, or not_eof())
759 * Informally, this function is called when the output buffer is full
760 * (or does not exist, as buffering need not actually be done). If a
761 * buffer exists, it is "consumed", with "some effect" on the
762 * controlled sequence. (Typically, the buffer is written out to the
763 * sequence verbatim.) In either case, the character @a c is also
764 * written out, if @a c is not @c eof().
766 * For a formal definiton of this function, see a good text
767 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
769 * A functioning output streambuf can be created by overriding only
770 * this function (no buffer area will be used).
772 * @note Base class version does nothing, returns eof().
775 overflow(int_type /* __c */ = traits_type::eof())
776 { return traits_type::eof(); }
778 #ifdef _GLIBCXX_DEPRECATED
782 * @brief Tosses a character.
784 * Advances the read pointer, ignoring the character that would have
787 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
789 * @note This function has been deprecated by the standard. You
790 * must define @c _GLIBCXX_DEPRECATED to make this visible; see
796 if (this->gptr() < this->egptr())
804 // _GLIBCXX_RESOLVE_LIB_DEFECTS
805 // Side effect of DR 50.
806 basic_streambuf(const __streambuf_type& __sb)
807 : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur),
808 _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg),
809 _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
810 _M_buf_locale(__sb._M_buf_locale)
814 operator=(const __streambuf_type&) { return *this; };
817 // Explicit specialization declarations, defined in src/streambuf.cc.
820 __copy_streambufs_eof(basic_streambuf<char>* __sbin,
821 basic_streambuf<char>* __sbout, bool& __ineof);
822 #ifdef _GLIBCXX_USE_WCHAR_T
825 __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
826 basic_streambuf<wchar_t>* __sbout, bool& __ineof);
829 _GLIBCXX_END_NAMESPACE
831 #ifndef _GLIBCXX_EXPORT_TEMPLATE
832 # include <bits/streambuf.tcc>
835 #endif /* _GLIBCXX_STREAMBUF */