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