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