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