00001 // Stream buffer classes -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 // 00032 // ISO C++ 14882: 27.5 Stream buffers 00033 // 00034 00035 /** @file streambuf 00036 * This is a Standard C++ Library header. You should @c #include this header 00037 * in your programs, rather than any of the "st[dl]_*.h" implementation files. 00038 */ 00039 00040 #ifndef _CPP_STREAMBUF 00041 #define _CPP_STREAMBUF 1 00042 00043 #pragma GCC system_header 00044 00045 #include <bits/c++config.h> 00046 #include <iosfwd> 00047 #include <cstdio> // For SEEK_SET, SEEK_CUR, SEEK_END 00048 #include <bits/localefwd.h> 00049 #include <bits/ios_base.h> 00050 00051 namespace std 00052 { 00053 /** 00054 * @if maint 00055 * Does stuff. 00056 * @endif 00057 */ 00058 template<typename _CharT, typename _Traits> 00059 streamsize 00060 __copy_streambufs(basic_ios<_CharT, _Traits>& _ios, 00061 basic_streambuf<_CharT, _Traits>* __sbin, 00062 basic_streambuf<_CharT, _Traits>* __sbout); 00063 00064 /** 00065 * @brief The actual work of input and output (interface). 00066 * 00067 * This is a base class. Derived stream buffers each control a 00068 * pair of character sequences: one for input, and one for output. 00069 * 00070 * Section [27.5.1] of the standard describes the requirements and 00071 * behavior of stream buffer classes. That section (three paragraphs) 00072 * is reproduced here, for simplicity and accuracy. 00073 * 00074 * -# Stream buffers can impose various constraints on the sequences 00075 * they control. Some constraints are: 00076 * - The controlled input sequence can be not readable. 00077 * - The controlled output sequence can be not writable. 00078 * - The controlled sequences can be associated with the contents of 00079 * other representations for character sequences, such as external 00080 * files. 00081 * - The controlled sequences can support operations @e directly to or 00082 * from associated sequences. 00083 * - The controlled sequences can impose limitations on how the 00084 * program can read characters from a sequence, write characters to 00085 * a sequence, put characters back into an input sequence, or alter 00086 * the stream position. 00087 * . 00088 * -# Each sequence is characterized by three pointers which, if non-null, 00089 * all point into the same @c charT array object. The array object 00090 * represents, at any moment, a (sub)sequence of characters from the 00091 * sequence. Operations performed on a sequence alter the values 00092 * stored in these pointers, perform reads and writes directly to or 00093 * from associated sequences, and alter "the stream position" and 00094 * conversion state as needed to maintain this subsequence relationship. 00095 * The three pointers are: 00096 * - the <em>beginning pointer</em>, or lowest element address in the 00097 * array (called @e xbeg here); 00098 * - the <em>next pointer</em>, or next element address that is a 00099 * current candidate for reading or writing (called @e xnext here); 00100 * - the <em>end pointer</em>, or first element address beyond the 00101 * end of the array (called @e xend here). 00102 * . 00103 * -# The following semantic constraints shall always apply for any set 00104 * of three pointers for a sequence, using the pointer names given 00105 * immediately above: 00106 * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall 00107 * also be non-null pointers into the same @c charT array, as 00108 * described above; otherwise, @e xbeg and @e xend shall also be null. 00109 * - If @e xnext is not a null pointer and @e xnext < @e xend for an 00110 * output sequence, then a <em>write position</em> is available. 00111 * In this case, @e *xnext shall be assignable as the next element 00112 * to write (to put, or to store a character value, into the sequence). 00113 * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an 00114 * input sequence, then a <em>putback position</em> is available. 00115 * In this case, @e xnext[-1] shall have a defined value and is the 00116 * next (preceding) element to store a character that is put back 00117 * into the input sequence. 00118 * - If @e xnext is not a null pointer and @e xnext< @e xend for an 00119 * input sequence, then a <em>read position</em> is available. 00120 * In this case, @e *xnext shall have a defined value and is the 00121 * next element to read (to get, or to obtain a character value, 00122 * from the sequence). 00123 */ 00124 template<typename _CharT, typename _Traits> 00125 class basic_streambuf 00126 { 00127 public: 00128 //@{ 00129 /** 00130 * These are standard types. They permit a standardized way of 00131 * referring to names of (or names dependant on) the template 00132 * parameters, which are specific to the implementation. 00133 */ 00134 typedef _CharT char_type; 00135 typedef _Traits traits_type; 00136 typedef typename traits_type::int_type int_type; 00137 typedef typename traits_type::pos_type pos_type; 00138 typedef typename traits_type::off_type off_type; 00139 //@} 00140 00141 //@{ 00142 /** 00143 * @if maint 00144 * These are non-standard types. 00145 * @endif 00146 */ 00147 typedef ctype<char_type> __ctype_type; 00148 typedef basic_streambuf<char_type, traits_type> __streambuf_type; 00149 typedef typename traits_type::state_type __state_type; 00150 //@} 00151 00152 friend class basic_ios<char_type, traits_type>; 00153 friend class basic_istream<char_type, traits_type>; 00154 friend class basic_ostream<char_type, traits_type>; 00155 friend class istreambuf_iterator<char_type, traits_type>; 00156 friend class ostreambuf_iterator<char_type, traits_type>; 00157 00158 friend streamsize 00159 __copy_streambufs<>(basic_ios<char_type, traits_type>& __ios, 00160 __streambuf_type* __sbin,__streambuf_type* __sbout); 00161 00162 protected: 00163 /** 00164 * @if maint 00165 * Pointer to the beginning of internally-allocated space. Filebuf 00166 * manually allocates/deallocates this, whereas stringstreams attempt 00167 * to use the built-in intelligence of the string class. If you are 00168 * managing memory, set this. If not, leave it NULL. 00169 * @endif 00170 */ 00171 char_type* _M_buf; 00172 00173 /** 00174 * @if maint 00175 * Actual size of allocated internal buffer, in bytes. 00176 * @endif 00177 */ 00178 size_t _M_buf_size; 00179 00180 /** 00181 * @if maint 00182 * Optimal or preferred size of internal buffer, in bytes. 00183 * @endif 00184 */ 00185 size_t _M_buf_size_opt; 00186 00187 /** 00188 * @if maint 00189 * True iff _M_in_* and _M_out_* buffers should always point to 00190 * the same place. True for fstreams, false for sstreams. 00191 * @endif 00192 */ 00193 bool _M_buf_unified; 00194 00195 //@{ 00196 /** 00197 * @if maint 00198 * This is based on _IO_FILE, just reordered to be more consistent, 00199 * and is intended to be the most minimal abstraction for an 00200 * internal buffer. 00201 * - get == input == read 00202 * - put == output == write 00203 * @endif 00204 */ 00205 char_type* _M_in_beg; // Start of get area. 00206 char_type* _M_in_cur; // Current read area. 00207 char_type* _M_in_end; // End of get area. 00208 char_type* _M_out_beg; // Start of put area. 00209 char_type* _M_out_cur; // Current put area. 00210 char_type* _M_out_end; // End of put area. 00211 //@} 00212 00213 /** 00214 * @if maint 00215 * Place to stash in || out || in | out settings for current streambuf. 00216 * @endif 00217 */ 00218 ios_base::openmode _M_mode; 00219 00220 /** 00221 * @if maint 00222 * Current locale setting. 00223 * @endif 00224 */ 00225 locale _M_buf_locale; 00226 00227 /** 00228 * @if maint 00229 * True iff locale is initialized. 00230 * @endif 00231 */ 00232 bool _M_buf_locale_init; 00233 00234 //@{ 00235 /** 00236 * @if maint 00237 * Necessary bits for putback buffer management. Only used in 00238 * the basic_filebuf class, as necessary for the standard 00239 * requirements. The only basic_streambuf member function that 00240 * needs access to these data members is in_avail... 00241 * 00242 * @note pbacks of over one character are not currently supported. 00243 * @endif 00244 */ 00245 static const size_t _S_pback_size = 1; 00246 char_type _M_pback[_S_pback_size]; 00247 char_type* _M_pback_cur_save; 00248 char_type* _M_pback_end_save; 00249 bool _M_pback_init; 00250 //@} 00251 00252 /** 00253 * @if maint 00254 * Yet unused. 00255 * @endif 00256 */ 00257 fpos<__state_type> _M_pos; 00258 00259 // Initializes pback buffers, and moves normal buffers to safety. 00260 // Assumptions: 00261 // _M_in_cur has already been moved back 00262 void 00263 _M_pback_create() 00264 { 00265 if (!_M_pback_init) 00266 { 00267 size_t __dist = _M_in_end - _M_in_cur; 00268 size_t __len = min(_S_pback_size, __dist); 00269 traits_type::copy(_M_pback, _M_in_cur, __len); 00270 _M_pback_cur_save = _M_in_cur; 00271 _M_pback_end_save = _M_in_end; 00272 this->setg(_M_pback, _M_pback, _M_pback + __len); 00273 _M_pback_init = true; 00274 } 00275 } 00276 00277 // Deactivates pback buffer contents, and restores normal buffer. 00278 // Assumptions: 00279 // The pback buffer has only moved forward. 00280 void 00281 _M_pback_destroy() throw() 00282 { 00283 if (_M_pback_init) 00284 { 00285 // Length _M_in_cur moved in the pback buffer. 00286 size_t __off_cur = _M_in_cur - _M_pback; 00287 00288 // For in | out buffers, the end can be pushed back... 00289 size_t __off_end = 0; 00290 size_t __pback_len = _M_in_end - _M_pback; 00291 size_t __save_len = _M_pback_end_save - _M_buf; 00292 if (__pback_len > __save_len) 00293 __off_end = __pback_len - __save_len; 00294 00295 this->setg(_M_buf, _M_pback_cur_save + __off_cur, 00296 _M_pback_end_save + __off_end); 00297 _M_pback_cur_save = NULL; 00298 _M_pback_end_save = NULL; 00299 _M_pback_init = false; 00300 } 00301 } 00302 00303 // Correctly sets the _M_in_cur pointer, and bumps the 00304 // _M_out_cur pointer as well if necessary. 00305 void 00306 _M_in_cur_move(off_type __n) // argument needs to be +- 00307 { 00308 bool __testout = _M_out_cur; 00309 _M_in_cur += __n; 00310 if (__testout && _M_buf_unified) 00311 _M_out_cur += __n; 00312 } 00313 00314 // Correctly sets the _M_out_cur pointer, and bumps the 00315 // appropriate _M_*_end pointers as well. Necessary for the 00316 // un-tied stringbufs, in in|out mode. 00317 // Invariant: 00318 // __n + _M_out_[cur, end] <= _M_buf + _M_buf_size 00319 // Assuming all _M_*_[beg, cur, end] pointers are operating on 00320 // the same range: 00321 // _M_buf <= _M_*_ <= _M_buf + _M_buf_size 00322 void 00323 _M_out_cur_move(off_type __n) // argument needs to be +- 00324 { 00325 bool __testin = _M_in_cur; 00326 00327 _M_out_cur += __n; 00328 if (__testin && _M_buf_unified) 00329 _M_in_cur += __n; 00330 if (_M_out_cur > _M_out_end) 00331 { 00332 _M_out_end = _M_out_cur; 00333 // NB: in | out buffers drag the _M_in_end pointer along... 00334 if (__testin) 00335 _M_in_end += __n; 00336 } 00337 } 00338 00339 // Return the size of the output buffer. This depends on the 00340 // buffer in use: allocated buffers have a stored size in 00341 // _M_buf_size and setbuf() buffers don't. 00342 off_type 00343 _M_out_buf_size() 00344 { 00345 off_type __ret = 0; 00346 if (_M_out_cur) 00347 { 00348 // Using allocated buffer. 00349 if (_M_out_beg == _M_buf) 00350 __ret = _M_out_beg + _M_buf_size - _M_out_cur; 00351 // Using non-allocated buffer. 00352 else 00353 __ret = _M_out_end - _M_out_cur; 00354 } 00355 return __ret; 00356 } 00357 00358 public: 00359 /// Destructor deallocates no buffer space. 00360 virtual 00361 ~basic_streambuf() 00362 { 00363 _M_buf_unified = false; 00364 _M_buf_size = 0; 00365 _M_buf_size_opt = 0; 00366 _M_mode = ios_base::openmode(0); 00367 } 00368 00369 // [27.5.2.2.1] locales 00370 /** 00371 * @brief Entry point for imbue(). 00372 * @param loc The new locale. 00373 * @return The previous locale. 00374 * 00375 * Calls the derived imbue(loc). 00376 */ 00377 locale 00378 pubimbue(const locale &__loc) 00379 { 00380 locale __tmp(this->getloc()); 00381 this->imbue(__loc); 00382 _M_buf_locale = __loc; 00383 return __tmp; 00384 } 00385 00386 /** 00387 * @brief Locale access. 00388 * @return The current locale in effect. 00389 * 00390 * If pubimbue(loc) has been called, then the most recent @c loc 00391 * is returned. Otherwise the global locale in effect at the time 00392 * of construction is returned. 00393 */ 00394 locale 00395 getloc() const 00396 { return _M_buf_locale; } 00397 00398 // [27.5.2.2.2] buffer management and positioning 00399 //@{ 00400 /** 00401 * @brief Entry points for derived buffer functions. 00402 * 00403 * The public versions of @c pubfoo dispatch to the protected 00404 * derived @c foo member functions, passing the arguments (if any) 00405 * and returning the result unchanged. 00406 */ 00407 __streambuf_type* 00408 pubsetbuf(char_type* __s, streamsize __n) 00409 { return this->setbuf(__s, __n); } 00410 00411 pos_type 00412 pubseekoff(off_type __off, ios_base::seekdir __way, 00413 ios_base::openmode __mode = ios_base::in | ios_base::out) 00414 { return this->seekoff(__off, __way, __mode); } 00415 00416 pos_type 00417 pubseekpos(pos_type __sp, 00418 ios_base::openmode __mode = ios_base::in | ios_base::out) 00419 { return this->seekpos(__sp, __mode); } 00420 00421 int 00422 pubsync() { return this->sync(); } 00423 //@} 00424 00425 // [27.5.2.2.3] get area 00426 /** 00427 * @brief Looking ahead into the stream. 00428 * @return The number of characters available. 00429 * 00430 * If a read position is available, returns the number of characters 00431 * available for reading before the buffer must be refilled. 00432 * Otherwise returns the derived @c showmanyc(). 00433 */ 00434 streamsize 00435 in_avail() 00436 { 00437 streamsize __ret; 00438 if (_M_in_cur && _M_in_cur < _M_in_end) 00439 { 00440 if (_M_pback_init) 00441 { 00442 size_t __save_len = _M_pback_end_save - _M_pback_cur_save; 00443 size_t __pback_len = _M_in_cur - _M_pback; 00444 __ret = __save_len - __pback_len; 00445 } 00446 else 00447 __ret = this->egptr() - this->gptr(); 00448 } 00449 else 00450 __ret = this->showmanyc(); 00451 return __ret; 00452 } 00453 00454 /** 00455 * @brief Getting the next character. 00456 * @return The next character, or eof. 00457 * 00458 * Calls @c sbumpc(), and if that function returns 00459 * @c traits::eof(), so does this function. Otherwise, @c sgetc(). 00460 */ 00461 int_type 00462 snextc() 00463 { 00464 int_type __eof = traits_type::eof(); 00465 return (traits_type::eq_int_type(this->sbumpc(), __eof) 00466 ? __eof : this->sgetc()); 00467 } 00468 00469 /** 00470 * @brief Getting the next character. 00471 * @return The next character, or eof. 00472 * 00473 * If the input read position is available, returns that character 00474 * and increments the read pointer, otherwise calls and returns 00475 * @c uflow(). 00476 */ 00477 int_type 00478 sbumpc(); 00479 00480 /** 00481 * @brief Getting the next character. 00482 * @return The next character, or eof. 00483 * 00484 * If the input read position is available, returns that character, 00485 * otherwise calls and returns @c underflow(). Does not move the 00486 * read position after fetching the character. 00487 */ 00488 int_type 00489 sgetc() 00490 { 00491 int_type __ret; 00492 if (_M_in_cur && _M_in_cur < _M_in_end) 00493 __ret = traits_type::to_int_type(*(this->gptr())); 00494 else 00495 __ret = this->underflow(); 00496 return __ret; 00497 } 00498 00499 /** 00500 * @brief Entry point for xsgetn. 00501 * @param s A buffer area. 00502 * @param n A count. 00503 * 00504 * Returns xsgetn(s,n). The effect is to fill @a s[0] through 00505 * @a s[n-1] with characters from the input sequence, if possible. 00506 */ 00507 streamsize 00508 sgetn(char_type* __s, streamsize __n) 00509 { return this->xsgetn(__s, __n); } 00510 00511 // [27.5.2.2.4] putback 00512 /** 00513 * @brief Pushing characters back into the input stream. 00514 * @param c The character to push back. 00515 * @return The previous character, if possible. 00516 * 00517 * Similar to sungetc(), but @a c is pushed onto the stream instead 00518 * of "the previous character". If successful, the next character 00519 * fetched from the input stream will be @a c. 00520 */ 00521 int_type 00522 sputbackc(char_type __c); 00523 00524 /** 00525 * @brief Moving backwards in the input stream. 00526 * @return The previous character, if possible. 00527 * 00528 * If a putback position is available, this function decrements the 00529 * input pointer and returns that character. Otherwise, calls and 00530 * returns pbackfail(). The effect is to "unget" the last character 00531 * "gotten". 00532 */ 00533 int_type 00534 sungetc(); 00535 00536 // [27.5.2.2.5] put area 00537 /** 00538 * @brief Entry point for all single-character output functions. 00539 * @param c A character to output. 00540 * @return @a c, if possible. 00541 * 00542 * One of two public output functions. 00543 * 00544 * If a write position is available for the output sequence (i.e., 00545 * the buffer is not full), stores @a c in that position, increments 00546 * the position, and returns @c traits::to_int_type(c). If a write 00547 * position is not available, returns @c overflow(c). 00548 */ 00549 int_type 00550 sputc(char_type __c); 00551 00552 /** 00553 * @brief Entry point for all single-character output functions. 00554 * @param s A buffer read area. 00555 * @param n A count. 00556 * 00557 * One of two public output functions. 00558 * 00559 * 00560 * Returns xsputn(s,n). The effect is to write @a s[0] through 00561 * @a s[n-1] to the output sequence, if possible. 00562 */ 00563 streamsize 00564 sputn(const char_type* __s, streamsize __n) 00565 { return this->xsputn(__s, __n); } 00566 00567 protected: 00568 /** 00569 * @brief Base constructor. 00570 * 00571 * Only called from derived constructors, and sets up all the 00572 * buffer data to zero, including the pointers described in the 00573 * basic_streambuf class description. Note that, as a result, 00574 * - the class starts with no read nor write positions available, 00575 * - this is not an error 00576 */ 00577 basic_streambuf() 00578 : _M_buf(NULL), _M_buf_size(0), _M_buf_size_opt(BUFSIZ), 00579 _M_buf_unified(false), _M_in_beg(0), _M_in_cur(0), _M_in_end(0), 00580 _M_out_beg(0), _M_out_cur(0), _M_out_end(0), 00581 _M_mode(ios_base::openmode(0)), _M_buf_locale(locale()), 00582 _M_pback_cur_save(0), _M_pback_end_save(0), 00583 _M_pback_init(false) 00584 { } 00585 00586 // [27.5.2.3.1] get area access 00587 //@{ 00588 /** 00589 * @brief Access to the get area. 00590 * 00591 * These functions are only available to other protected functions, 00592 * including derived classes. 00593 * 00594 * - eback() returns the beginning pointer for the input sequence 00595 * - gptr() returns the next pointer for the input sequence 00596 * - egptr() returns the end pointer for the input sequence 00597 */ 00598 char_type* 00599 eback() const { return _M_in_beg; } 00600 00601 char_type* 00602 gptr() const { return _M_in_cur; } 00603 00604 char_type* 00605 egptr() const { return _M_in_end; } 00606 //@} 00607 00608 /** 00609 * @brief Moving the read position. 00610 * @param n The delta by which to move. 00611 * 00612 * This just advances the read position without returning any data. 00613 */ 00614 void 00615 gbump(int __n) { _M_in_cur += __n; } 00616 00617 /** 00618 * @brief Setting the three read area pointers. 00619 * @param gbeg A pointer. 00620 * @param gnext A pointer. 00621 * @param gend A pointer. 00622 * @post @a gbeg == @c eback(), @a gnext == @c gptr(), and 00623 * @a gend == @c egptr() 00624 */ 00625 void 00626 setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) 00627 { 00628 _M_in_beg = __gbeg; 00629 _M_in_cur = __gnext; 00630 _M_in_end = __gend; 00631 if (!(_M_mode & ios_base::in) && __gbeg && __gnext && __gend) 00632 _M_mode = _M_mode | ios_base::in; 00633 } 00634 00635 // [27.5.2.3.2] put area access 00636 //@{ 00637 /** 00638 * @brief Access to the put area. 00639 * 00640 * These functions are only available to other protected functions, 00641 * including derived classes. 00642 * 00643 * - pbase() returns the beginning pointer for the output sequence 00644 * - pptr() returns the next pointer for the output sequence 00645 * - epptr() returns the end pointer for the output sequence 00646 */ 00647 char_type* 00648 pbase() const { return _M_out_beg; } 00649 00650 char_type* 00651 pptr() const { return _M_out_cur; } 00652 00653 char_type* 00654 epptr() const { return _M_out_end; } 00655 //@} 00656 00657 /** 00658 * @brief Moving the write position. 00659 * @param n The delta by which to move. 00660 * 00661 * This just advances the write position without returning any data. 00662 */ 00663 void 00664 pbump(int __n) { _M_out_cur += __n; } 00665 00666 /** 00667 * @brief Setting the three write area pointers. 00668 * @param pbeg A pointer. 00669 * @param pend A pointer. 00670 * @post @a pbeg == @c pbase(), @a pbeg == @c pptr(), and 00671 * @a pend == @c epptr() 00672 */ 00673 void 00674 setp(char_type* __pbeg, char_type* __pend) 00675 { 00676 _M_out_beg = _M_out_cur = __pbeg; 00677 _M_out_end = __pend; 00678 if (!(_M_mode & ios_base::out) && __pbeg && __pend) 00679 _M_mode = _M_mode | ios_base::out; 00680 } 00681 00682 // [27.5.2.4] virtual functions 00683 // [27.5.2.4.1] locales 00684 /** 00685 * @brief Changes translations. 00686 * @param loc A new locale. 00687 * 00688 * Translations done during I/O which depend on the current locale 00689 * are changed by this call. The standard adds, "Between invocations 00690 * of this function a class derived from streambuf can safely cache 00691 * results of calls to locale functions and to members of facets 00692 * so obtained." 00693 * 00694 * @note Base class version does nothing. 00695 */ 00696 virtual void 00697 imbue(const locale&) 00698 { } 00699 00700 // [27.5.2.4.2] buffer management and positioning 00701 /** 00702 * @brief Maniuplates the buffer. 00703 * 00704 * Each derived class provides its own appropriate behavior. See 00705 * the next-to-last paragraph of 00706 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for 00707 * more on this function. 00708 * 00709 * @note Base class version does nothing, returns @c this. 00710 */ 00711 virtual basic_streambuf<char_type,_Traits>* 00712 setbuf(char_type*, streamsize) 00713 { return this; } 00714 00715 /** 00716 * @brief Alters the stream positions. 00717 * 00718 * Each derived class provides its own appropriate behavior. 00719 * @note Base class version does nothing, returns a @c pos_type 00720 * that represents an invalid stream position. 00721 */ 00722 virtual pos_type 00723 seekoff(off_type, ios_base::seekdir, 00724 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out) 00725 { return pos_type(off_type(-1)); } 00726 00727 /** 00728 * @brief Alters the stream positions. 00729 * 00730 * Each derived class provides its own appropriate behavior. 00731 * @note Base class version does nothing, returns a @c pos_type 00732 * that represents an invalid stream position. 00733 */ 00734 virtual pos_type 00735 seekpos(pos_type, 00736 ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out) 00737 { return pos_type(off_type(-1)); } 00738 00739 /** 00740 * @brief Synchronizes the buffer arrays with the controlled sequences. 00741 * @return -1 on failure. 00742 * 00743 * Each derived class provides its own appropriate behavior, 00744 * including the definition of "failure". 00745 * @note Base class version does nothing, returns zero. 00746 */ 00747 virtual int 00748 sync() { return 0; } 00749 00750 // [27.5.2.4.3] get area 00751 /** 00752 * @brief Investigating the data available. 00753 * @return An estimate of the number of characters available in the 00754 * input sequence, or -1. 00755 * 00756 * "If it returns a positive value, then successive calls to 00757 * @c underflow() will not return @c traits::eof() until at least that 00758 * number of characters have been supplied. If @c showmanyc() 00759 * returns -1, then calls to @c underflow() or @c uflow() will fail." 00760 * [27.5.2.4.3]/1 00761 * 00762 * @note Base class version does nothing, returns zero. 00763 * @note The standard adds that "the intention is not only that the 00764 * calls [to underflow or uflow] will not return @c eof() but 00765 * that they will return "immediately". 00766 * @note The standard adds that "the morphemes of @c showmanyc are 00767 * "es-how-many-see", not "show-manic". 00768 */ 00769 virtual streamsize 00770 showmanyc() { return 0; } 00771 00772 /** 00773 * @brief Multiple character extraction. 00774 * @param s A buffer area. 00775 * @param n Maximum number of characters to assign. 00776 * @return The number of characters assigned. 00777 * 00778 * Fills @a s[0] through @a s[n-1] with characters from the input 00779 * sequence, as if by @c sbumpc(). Stops when either @a n characters 00780 * have been copied, or when @c traits::eof() would be copied. 00781 * 00782 * It is expected that derived classes provide a more efficient 00783 * implementation by overriding this definition. 00784 */ 00785 virtual streamsize 00786 xsgetn(char_type* __s, streamsize __n); 00787 00788 /** 00789 * @brief Fetches more data from the controlled sequence. 00790 * @return The first character from the <em>pending sequence</em>. 00791 * 00792 * Informally, this function is called when the input buffer is 00793 * exhausted (or does not exist, as buffering need not actually be 00794 * done). If a buffer exists, it is "refilled". In either case, the 00795 * next available character is returned, or @c traits::eof() to 00796 * indicate a null pending sequence. 00797 * 00798 * For a formal definiton of the pending sequence, see a good text 00799 * such as Langer & Kreft, or [27.5.2.4.3]/7-14. 00800 * 00801 * A functioning input streambuf can be created by overriding only 00802 * this function (no buffer area will be used). For an example, see 00803 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#6 00804 * 00805 * @note Base class version does nothing, returns eof(). 00806 */ 00807 virtual int_type 00808 underflow() 00809 { return traits_type::eof(); } 00810 00811 /** 00812 * @brief Fetches more data from the controlled sequence. 00813 * @return The first character from the <em>pending sequence</em>. 00814 * 00815 * Informally, this function does the same thing as @c underflow(), 00816 * and in fact is required to call that function. It also returns 00817 * the new character, like @c underflow() does. However, this 00818 * function also moves the read position forward by one. 00819 */ 00820 virtual int_type 00821 uflow() 00822 { 00823 int_type __ret = traits_type::eof(); 00824 bool __testeof = traits_type::eq_int_type(this->underflow(), __ret); 00825 bool __testpending = _M_in_cur && _M_in_cur < _M_in_end; 00826 if (!__testeof && __testpending) 00827 { 00828 __ret = traits_type::to_int_type(*_M_in_cur); 00829 ++_M_in_cur; 00830 if (_M_buf_unified && _M_mode & ios_base::out) 00831 ++_M_out_cur; 00832 } 00833 return __ret; 00834 } 00835 00836 // [27.5.2.4.4] putback 00837 /** 00838 * @brief Tries to back up the input sequence. 00839 * @param c The character to be inserted back into the sequence. 00840 * @return eof() on failure, "some other value" on success 00841 * @post The constraints of @c gptr(), @c eback(), and @c pptr() 00842 * are the same as for @c underflow(). 00843 * 00844 * @note Base class version does nothing, returns eof(). 00845 */ 00846 virtual int_type 00847 pbackfail(int_type /* __c */ = traits_type::eof()) 00848 { return traits_type::eof(); } 00849 00850 // Put area: 00851 /** 00852 * @brief Multiple character insertion. 00853 * @param s A buffer area. 00854 * @param n Maximum number of characters to write. 00855 * @return The number of characters written. 00856 * 00857 * Writes @a s[0] through @a s[n-1] to the output sequence, as if 00858 * by @c sputc(). Stops when either @a n characters have been 00859 * copied, or when @c sputc() would return @c traits::eof(). 00860 * 00861 * It is expected that derived classes provide a more efficient 00862 * implementation by overriding this definition. 00863 */ 00864 virtual streamsize 00865 xsputn(const char_type* __s, streamsize __n); 00866 00867 /** 00868 * @brief Consumes data from the buffer; writes to the 00869 * controlled sequence. 00870 * @param c An additional character to consume. 00871 * @return eof() to indicate failure, something else (usually 00872 * @a c, or not_eof()) 00873 * 00874 * Informally, this function is called when the output buffer is full 00875 * (or does not exist, as buffering need not actually be done). If a 00876 * buffer exists, it is "consumed", with "some effect" on the 00877 * controlled sequence. (Typically, the buffer is written out to the 00878 * sequence verbatim.) In either case, the character @a c is also 00879 * written out, if @a c is not @c eof(). 00880 * 00881 * For a formal definiton of this function, see a good text 00882 * such as Langer & Kreft, or [27.5.2.4.5]/3-7. 00883 * 00884 * A functioning output streambuf can be created by overriding only 00885 * this function (no buffer area will be used). 00886 * 00887 * @note Base class version does nothing, returns eof(). 00888 */ 00889 virtual int_type 00890 overflow(int_type /* __c */ = traits_type::eof()) 00891 { return traits_type::eof(); } 00892 00893 #ifdef _GLIBCPP_DEPRECATED 00894 // Annex D.6 00895 public: 00896 /** 00897 * @brief Tosses a character. 00898 * 00899 * Advances the read pointer, ignoring the character that would have 00900 * been read. 00901 * 00902 * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html 00903 * 00904 * @note This function has been deprecated by the standard. You 00905 * must define @c _GLIBCPP_DEPRECATED to make this visible; see 00906 * c++config.h. 00907 */ 00908 void 00909 stossc() 00910 { 00911 if (_M_in_cur < _M_in_end) 00912 ++_M_in_cur; 00913 else 00914 this->uflow(); 00915 } 00916 #endif 00917 00918 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00919 // Side effect of DR 50. 00920 private: 00921 basic_streambuf(const __streambuf_type&) { }; 00922 00923 __streambuf_type& 00924 operator=(const __streambuf_type&) { return *this; }; 00925 #endif 00926 }; 00927 } // namespace std 00928 00929 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT 00930 # define export 00931 #endif 00932 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS 00933 #include <bits/streambuf.tcc> 00934 #endif 00935 00936 #endif