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>
49 _GLIBCXX_BEGIN_NAMESPACE(std
)
56 template<typename _CharT
, typename _Traits
>
58 __copy_streambufs_eof(basic_streambuf
<_CharT
, _Traits
>* __sbin
,
59 basic_streambuf
<_CharT
, _Traits
>* __sbout
,
63 * @brief The actual work of input and output (interface).
65 * This is a base class. Derived stream buffers each control a
66 * pair of character sequences: one for input, and one for output.
68 * Section [27.5.1] of the standard describes the requirements and
69 * behavior of stream buffer classes. That section (three paragraphs)
70 * is reproduced here, for simplicity and accuracy.
72 * -# Stream buffers can impose various constraints on the sequences
73 * they control. Some constraints are:
74 * - The controlled input sequence can be not readable.
75 * - The controlled output sequence can be not writable.
76 * - The controlled sequences can be associated with the contents of
77 * other representations for character sequences, such as external
79 * - The controlled sequences can support operations @e directly to or
80 * from associated sequences.
81 * - The controlled sequences can impose limitations on how the
82 * program can read characters from a sequence, write characters to
83 * a sequence, put characters back into an input sequence, or alter
84 * the stream position.
86 * -# Each sequence is characterized by three pointers which, if non-null,
87 * all point into the same @c charT array object. The array object
88 * represents, at any moment, a (sub)sequence of characters from the
89 * sequence. Operations performed on a sequence alter the values
90 * stored in these pointers, perform reads and writes directly to or
91 * from associated sequences, and alter "the stream position" and
92 * conversion state as needed to maintain this subsequence relationship.
93 * The three pointers are:
94 * - the <em>beginning pointer</em>, or lowest element address in the
95 * array (called @e xbeg here);
96 * - the <em>next pointer</em>, or next element address that is a
97 * current candidate for reading or writing (called @e xnext here);
98 * - the <em>end pointer</em>, or first element address beyond the
99 * end of the array (called @e xend here).
101 * -# The following semantic constraints shall always apply for any set
102 * of three pointers for a sequence, using the pointer names given
104 * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
105 * also be non-null pointers into the same @c charT array, as
106 * described above; otherwise, @e xbeg and @e xend shall also be null.
107 * - If @e xnext is not a null pointer and @e xnext < @e xend for an
108 * output sequence, then a <em>write position</em> is available.
109 * In this case, @e *xnext shall be assignable as the next element
110 * to write (to put, or to store a character value, into the sequence).
111 * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
112 * input sequence, then a <em>putback position</em> is available.
113 * In this case, @e xnext[-1] shall have a defined value and is the
114 * next (preceding) element to store a character that is put back
115 * into the input sequence.
116 * - If @e xnext is not a null pointer and @e xnext< @e xend for an
117 * input sequence, then a <em>read position</em> is available.
118 * In this case, @e *xnext shall have a defined value and is the
119 * next element to read (to get, or to obtain a character value,
120 * from the sequence).
122 template<typename _CharT
, typename _Traits
>
123 class basic_streambuf
128 * These are standard types. They permit a standardized way of
129 * referring to names of (or names dependant on) the template
130 * parameters, which are specific to the implementation.
132 typedef _CharT char_type
;
133 typedef _Traits traits_type
;
134 typedef typename
traits_type::int_type int_type
;
135 typedef typename
traits_type::pos_type pos_type
;
136 typedef typename
traits_type::off_type off_type
;
142 * This is a non-standard type.
145 typedef basic_streambuf
<char_type
, traits_type
> __streambuf_type
;
148 friend class basic_ios
<char_type
, traits_type
>;
149 friend class basic_istream
<char_type
, traits_type
>;
150 friend class basic_ostream
<char_type
, traits_type
>;
151 friend class istreambuf_iterator
<char_type
, traits_type
>;
152 friend class ostreambuf_iterator
<char_type
, traits_type
>;
155 __copy_streambufs_eof
<>(__streambuf_type
* __sbin
,
156 __streambuf_type
* __sbout
, bool& __ineof
);
158 template<typename _CharT2
, typename _Traits2
>
159 friend basic_istream
<_CharT2
, _Traits2
>&
160 operator>>(basic_istream
<_CharT2
, _Traits2
>&, _CharT2
*);
162 template<typename _CharT2
, typename _Traits2
, typename _Alloc
>
163 friend basic_istream
<_CharT2
, _Traits2
>&
164 operator>>(basic_istream
<_CharT2
, _Traits2
>&,
165 basic_string
<_CharT2
, _Traits2
, _Alloc
>&);
167 template<typename _CharT2
, typename _Traits2
, typename _Alloc
>
168 friend basic_istream
<_CharT2
, _Traits2
>&
169 getline(basic_istream
<_CharT2
, _Traits2
>&,
170 basic_string
<_CharT2
, _Traits2
, _Alloc
>&, _CharT2
);
176 * This is based on _IO_FILE, just reordered to be more consistent,
177 * and is intended to be the most minimal abstraction for an
179 * - get == input == read
180 * - put == output == write
183 char_type
* _M_in_beg
; // Start of get area.
184 char_type
* _M_in_cur
; // Current read area.
185 char_type
* _M_in_end
; // End of get area.
186 char_type
* _M_out_beg
; // Start of put area.
187 char_type
* _M_out_cur
; // Current put area.
188 char_type
* _M_out_end
; // End of put area.
192 * Current locale setting.
195 locale _M_buf_locale
;
198 /// Destructor deallocates no buffer space.
203 // [27.5.2.2.1] locales
205 * @brief Entry point for imbue().
206 * @param loc The new locale.
207 * @return The previous locale.
209 * Calls the derived imbue(loc).
212 pubimbue(const locale
&__loc
)
214 locale
__tmp(this->getloc());
216 _M_buf_locale
= __loc
;
221 * @brief Locale access.
222 * @return The current locale in effect.
224 * If pubimbue(loc) has been called, then the most recent @c loc
225 * is returned. Otherwise the global locale in effect at the time
226 * of construction is returned.
230 { return _M_buf_locale
; }
232 // [27.5.2.2.2] buffer management and positioning
235 * @brief Entry points for derived buffer functions.
237 * The public versions of @c pubfoo dispatch to the protected
238 * derived @c foo member functions, passing the arguments (if any)
239 * and returning the result unchanged.
242 pubsetbuf(char_type
* __s
, streamsize __n
)
243 { return this->setbuf(__s
, __n
); }
246 pubseekoff(off_type __off
, ios_base::seekdir __way
,
247 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
248 { return this->seekoff(__off
, __way
, __mode
); }
251 pubseekpos(pos_type __sp
,
252 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
253 { return this->seekpos(__sp
, __mode
); }
256 pubsync() { return this->sync(); }
259 // [27.5.2.2.3] get area
261 * @brief Looking ahead into the stream.
262 * @return The number of characters available.
264 * If a read position is available, returns the number of characters
265 * available for reading before the buffer must be refilled.
266 * Otherwise returns the derived @c showmanyc().
271 const streamsize __ret
= this->egptr() - this->gptr();
272 return __ret
? __ret
: this->showmanyc();
276 * @brief Getting the next character.
277 * @return The next character, or eof.
279 * Calls @c sbumpc(), and if that function returns
280 * @c traits::eof(), so does this function. Otherwise, @c sgetc().
285 int_type __ret
= traits_type::eof();
286 if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
288 __ret
= this->sgetc();
293 * @brief Getting the next character.
294 * @return The next character, or eof.
296 * If the input read position is available, returns that character
297 * and increments the read pointer, otherwise calls and returns
304 if (__builtin_expect(this->gptr() < this->egptr(), true))
306 __ret
= traits_type::to_int_type(*this->gptr());
310 __ret
= this->uflow();
315 * @brief Getting the next character.
316 * @return The next character, or eof.
318 * If the input read position is available, returns that character,
319 * otherwise calls and returns @c underflow(). Does not move the
320 * read position after fetching the character.
326 if (__builtin_expect(this->gptr() < this->egptr(), true))
327 __ret
= traits_type::to_int_type(*this->gptr());
329 __ret
= this->underflow();
334 * @brief Entry point for xsgetn.
335 * @param s A buffer area.
338 * Returns xsgetn(s,n). The effect is to fill @a s[0] through
339 * @a s[n-1] with characters from the input sequence, if possible.
342 sgetn(char_type
* __s
, streamsize __n
)
343 { return this->xsgetn(__s
, __n
); }
345 // [27.5.2.2.4] putback
347 * @brief Pushing characters back into the input stream.
348 * @param c The character to push back.
349 * @return The previous character, if possible.
351 * Similar to sungetc(), but @a c is pushed onto the stream instead
352 * of "the previous character". If successful, the next character
353 * fetched from the input stream will be @a c.
356 sputbackc(char_type __c
)
359 const bool __testpos
= this->eback() < this->gptr();
360 if (__builtin_expect(!__testpos
||
361 !traits_type::eq(__c
, this->gptr()[-1]), false))
362 __ret
= this->pbackfail(traits_type::to_int_type(__c
));
366 __ret
= traits_type::to_int_type(*this->gptr());
372 * @brief Moving backwards in the input stream.
373 * @return The previous character, if possible.
375 * If a putback position is available, this function decrements the
376 * input pointer and returns that character. Otherwise, calls and
377 * returns pbackfail(). The effect is to "unget" the last character
384 if (__builtin_expect(this->eback() < this->gptr(), true))
387 __ret
= traits_type::to_int_type(*this->gptr());
390 __ret
= this->pbackfail();
394 // [27.5.2.2.5] put area
396 * @brief Entry point for all single-character output functions.
397 * @param c A character to output.
398 * @return @a c, if possible.
400 * One of two public output functions.
402 * If a write position is available for the output sequence (i.e.,
403 * the buffer is not full), stores @a c in that position, increments
404 * the position, and returns @c traits::to_int_type(c). If a write
405 * position is not available, returns @c overflow(c).
411 if (__builtin_expect(this->pptr() < this->epptr(), true))
415 __ret
= traits_type::to_int_type(__c
);
418 __ret
= this->overflow(traits_type::to_int_type(__c
));
423 * @brief Entry point for all single-character output functions.
424 * @param s A buffer read area.
427 * One of two public output functions.
430 * Returns xsputn(s,n). The effect is to write @a s[0] through
431 * @a s[n-1] to the output sequence, if possible.
434 sputn(const char_type
* __s
, streamsize __n
)
435 { return this->xsputn(__s
, __n
); }
439 * @brief Base constructor.
441 * Only called from derived constructors, and sets up all the
442 * buffer data to zero, including the pointers described in the
443 * basic_streambuf class description. Note that, as a result,
444 * - the class starts with no read nor write positions available,
445 * - this is not an error
448 : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
449 _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
450 _M_buf_locale(locale())
453 // [27.5.2.3.1] get area access
456 * @brief Access to the get area.
458 * These functions are only available to other protected functions,
459 * including derived classes.
461 * - eback() returns the beginning pointer for the input sequence
462 * - gptr() returns the next pointer for the input sequence
463 * - egptr() returns the end pointer for the input sequence
466 eback() const { return _M_in_beg
; }
469 gptr() const { return _M_in_cur
; }
472 egptr() const { return _M_in_end
; }
476 * @brief Moving the read position.
477 * @param n The delta by which to move.
479 * This just advances the read position without returning any data.
482 gbump(int __n
) { _M_in_cur
+= __n
; }
485 * @brief Setting the three read area pointers.
486 * @param gbeg A pointer.
487 * @param gnext A pointer.
488 * @param gend A pointer.
489 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and
490 * @a gend == @c egptr()
493 setg(char_type
* __gbeg
, char_type
* __gnext
, char_type
* __gend
)
500 // [27.5.2.3.2] put area access
503 * @brief Access to the put area.
505 * These functions are only available to other protected functions,
506 * including derived classes.
508 * - pbase() returns the beginning pointer for the output sequence
509 * - pptr() returns the next pointer for the output sequence
510 * - epptr() returns the end pointer for the output sequence
513 pbase() const { return _M_out_beg
; }
516 pptr() const { return _M_out_cur
; }
519 epptr() const { return _M_out_end
; }
523 * @brief Moving the write position.
524 * @param n The delta by which to move.
526 * This just advances the write position without returning any data.
529 pbump(int __n
) { _M_out_cur
+= __n
; }
532 * @brief Setting the three write area pointers.
533 * @param pbeg A pointer.
534 * @param pend A pointer.
535 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and
536 * @a pend == @c epptr()
539 setp(char_type
* __pbeg
, char_type
* __pend
)
541 _M_out_beg
= _M_out_cur
= __pbeg
;
545 // [27.5.2.4] virtual functions
546 // [27.5.2.4.1] locales
548 * @brief Changes translations.
549 * @param loc A new locale.
551 * Translations done during I/O which depend on the current locale
552 * are changed by this call. The standard adds, "Between invocations
553 * of this function a class derived from streambuf can safely cache
554 * results of calls to locale functions and to members of facets
557 * @note Base class version does nothing.
563 // [27.5.2.4.2] buffer management and positioning
565 * @brief Maniuplates the buffer.
567 * Each derived class provides its own appropriate behavior. See
568 * the next-to-last paragraph of
569 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for
570 * more on this function.
572 * @note Base class version does nothing, returns @c this.
574 virtual basic_streambuf
<char_type
,_Traits
>*
575 setbuf(char_type
*, streamsize
)
579 * @brief Alters the stream positions.
581 * Each derived class provides its own appropriate behavior.
582 * @note Base class version does nothing, returns a @c pos_type
583 * that represents an invalid stream position.
586 seekoff(off_type
, ios_base::seekdir
,
587 ios_base::openmode
/*__mode*/ = ios_base::in
| ios_base::out
)
588 { return pos_type(off_type(-1)); }
591 * @brief Alters the stream positions.
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.
599 ios_base::openmode
/*__mode*/ = ios_base::in
| ios_base::out
)
600 { return pos_type(off_type(-1)); }
603 * @brief Synchronizes the buffer arrays with the controlled sequences.
604 * @return -1 on failure.
606 * Each derived class provides its own appropriate behavior,
607 * including the definition of "failure".
608 * @note Base class version does nothing, returns zero.
613 // [27.5.2.4.3] get area
615 * @brief Investigating the data available.
616 * @return An estimate of the number of characters available in the
617 * input sequence, or -1.
619 * "If it returns a positive value, then successive calls to
620 * @c underflow() will not return @c traits::eof() until at least that
621 * number of characters have been supplied. If @c showmanyc()
622 * returns -1, then calls to @c underflow() or @c uflow() will fail."
625 * @note Base class version does nothing, returns zero.
626 * @note The standard adds that "the intention is not only that the
627 * calls [to underflow or uflow] will not return @c eof() but
628 * that they will return "immediately".
629 * @note The standard adds that "the morphemes of @c showmanyc are
630 * "es-how-many-see", not "show-manic".
633 showmanyc() { return 0; }
636 * @brief Multiple character extraction.
637 * @param s A buffer area.
638 * @param n Maximum number of characters to assign.
639 * @return The number of characters assigned.
641 * Fills @a s[0] through @a s[n-1] with characters from the input
642 * sequence, as if by @c sbumpc(). Stops when either @a n characters
643 * have been copied, or when @c traits::eof() would be copied.
645 * It is expected that derived classes provide a more efficient
646 * implementation by overriding this definition.
649 xsgetn(char_type
* __s
, streamsize __n
);
652 * @brief Fetches more data from the controlled sequence.
653 * @return The first character from the <em>pending sequence</em>.
655 * Informally, this function is called when the input buffer is
656 * exhausted (or does not exist, as buffering need not actually be
657 * done). If a buffer exists, it is "refilled". In either case, the
658 * next available character is returned, or @c traits::eof() to
659 * indicate a null pending sequence.
661 * For a formal definiton of the pending sequence, see a good text
662 * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
664 * A functioning input streambuf can be created by overriding only
665 * this function (no buffer area will be used). For an example, see
666 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6
668 * @note Base class version does nothing, returns eof().
672 { return traits_type::eof(); }
675 * @brief Fetches more data from the controlled sequence.
676 * @return The first character from the <em>pending sequence</em>.
678 * Informally, this function does the same thing as @c underflow(),
679 * and in fact is required to call that function. It also returns
680 * the new character, like @c underflow() does. However, this
681 * function also moves the read position forward by one.
686 int_type __ret
= traits_type::eof();
687 const bool __testeof
= traits_type::eq_int_type(this->underflow(),
691 __ret
= traits_type::to_int_type(*this->gptr());
697 // [27.5.2.4.4] putback
699 * @brief Tries to back up the input sequence.
700 * @param c The character to be inserted back into the sequence.
701 * @return eof() on failure, "some other value" on success
702 * @post The constraints of @c gptr(), @c eback(), and @c pptr()
703 * are the same as for @c underflow().
705 * @note Base class version does nothing, returns eof().
708 pbackfail(int_type
/* __c */ = traits_type::eof())
709 { return traits_type::eof(); }
713 * @brief Multiple character insertion.
714 * @param s A buffer area.
715 * @param n Maximum number of characters to write.
716 * @return The number of characters written.
718 * Writes @a s[0] through @a s[n-1] to the output sequence, as if
719 * by @c sputc(). Stops when either @a n characters have been
720 * copied, or when @c sputc() would return @c traits::eof().
722 * It is expected that derived classes provide a more efficient
723 * implementation by overriding this definition.
726 xsputn(const char_type
* __s
, streamsize __n
);
729 * @brief Consumes data from the buffer; writes to the
730 * controlled sequence.
731 * @param c An additional character to consume.
732 * @return eof() to indicate failure, something else (usually
733 * @a c, or not_eof())
735 * Informally, this function is called when the output buffer is full
736 * (or does not exist, as buffering need not actually be done). If a
737 * buffer exists, it is "consumed", with "some effect" on the
738 * controlled sequence. (Typically, the buffer is written out to the
739 * sequence verbatim.) In either case, the character @a c is also
740 * written out, if @a c is not @c eof().
742 * For a formal definiton of this function, see a good text
743 * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
745 * A functioning output streambuf can be created by overriding only
746 * this function (no buffer area will be used).
748 * @note Base class version does nothing, returns eof().
751 overflow(int_type
/* __c */ = traits_type::eof())
752 { return traits_type::eof(); }
754 #ifdef _GLIBCXX_DEPRECATED
758 * @brief Tosses a character.
760 * Advances the read pointer, ignoring the character that would have
763 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
765 * @note This function has been deprecated by the standard. You
766 * must define @c _GLIBCXX_DEPRECATED to make this visible; see
772 if (this->gptr() < this->egptr())
780 // _GLIBCXX_RESOLVE_LIB_DEFECTS
781 // Side effect of DR 50.
782 basic_streambuf(const __streambuf_type
& __sb
)
783 : _M_in_beg(__sb
._M_in_beg
), _M_in_cur(__sb
._M_in_cur
),
784 _M_in_end(__sb
._M_in_end
), _M_out_beg(__sb
._M_out_beg
),
785 _M_out_cur(__sb
._M_out_cur
), _M_out_end(__sb
._M_out_cur
),
786 _M_buf_locale(__sb
._M_buf_locale
)
790 operator=(const __streambuf_type
&) { return *this; };
793 // Explicit specialization declarations, defined in src/streambuf.cc.
796 __copy_streambufs_eof(basic_streambuf
<char>* __sbin
,
797 basic_streambuf
<char>* __sbout
, bool& __ineof
);
798 #ifdef _GLIBCXX_USE_WCHAR_T
801 __copy_streambufs_eof(basic_streambuf
<wchar_t>* __sbin
,
802 basic_streambuf
<wchar_t>* __sbout
, bool& __ineof
);
805 _GLIBCXX_END_NAMESPACE
807 #ifndef _GLIBCXX_EXPORT_TEMPLATE
808 # include <bits/streambuf.tcc>
811 #endif /* _GLIBCXX_STREAMBUF */