# streambuf

Go to the documentation of this file.

    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
    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 {
    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
    00124   template<typename _CharT, typename _Traits>
    00125     class basic_streambuf
    00126     {
    00127     public:
    00129
    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;
    00140
    00142
    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;
    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:
    00171       char_type*        _M_buf;
    00172
    00178       size_t            _M_buf_size;
    00179
    00185       size_t            _M_buf_size_opt;
    00186
    00193       bool          _M_buf_unified;
    00194
    00196
    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.
    00212
    00218       ios_base::openmode    _M_mode;
    00219
    00225       locale            _M_buf_locale;
    00226
    00232       bool          _M_buf_locale_init;
    00233
    00235
    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;
    00251
    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:
    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
    00377       locale
    00378       pubimbue(const locale &__loc)
    00379       {
    00380     locale __tmp(this->getloc());
    00381     this->imbue(__loc);
    00382     return __tmp;
    00383       }
    00384
    00393       locale
    00394       getloc() const
    00395       { return _M_buf_locale; }
    00396
    00397       // [27.5.2.2.2] buffer management and positioning
    00399
    00406       __streambuf_type*
    00407       pubsetbuf(char_type* __s, streamsize __n)
    00408       { return this->setbuf(__s, __n); }
    00409
    00410       pos_type
    00411       pubseekoff(off_type __off, ios_base::seekdir __way,
    00412          ios_base::openmode __mode = ios_base::in | ios_base::out)
    00413       { return this->seekoff(__off, __way, __mode); }
    00414
    00415       pos_type
    00416       pubseekpos(pos_type __sp,
    00417          ios_base::openmode __mode = ios_base::in | ios_base::out)
    00418       { return this->seekpos(__sp, __mode); }
    00419
    00420       int
    00421       pubsync() { return this->sync(); }
    00423
    00424       // [27.5.2.2.3] get area
    00433       streamsize
    00434       in_avail()
    00435       {
    00436     streamsize __ret;
    00437     if (_M_in_cur && _M_in_cur < _M_in_end)
    00438       {
    00439         if (_M_pback_init)
    00440           {
    00441         size_t __save_len =  _M_pback_end_save - _M_pback_cur_save;
    00442         size_t __pback_len = _M_in_cur - _M_pback;
    00443         __ret = __save_len - __pback_len;
    00444           }
    00445         else
    00446           __ret = this->egptr() - this->gptr();
    00447       }
    00448     else
    00449       __ret = this->showmanyc();
    00450     return __ret;
    00451       }
    00452
    00460       int_type
    00461       snextc()
    00462       {
    00463     int_type __eof = traits_type::eof();
    00464     return (traits_type::eq_int_type(this->sbumpc(), __eof)
    00465         ? __eof : this->sgetc());
    00466       }
    00467
    00476       int_type
    00477       sbumpc();
    00478
    00487       int_type
    00488       sgetc()
    00489       {
    00490     int_type __ret;
    00491     if (_M_in_cur && _M_in_cur < _M_in_end)
    00492       __ret = traits_type::to_int_type(*(this->gptr()));
    00493     else
    00494       __ret = this->underflow();
    00495     return __ret;
    00496       }
    00497
    00506       streamsize
    00507       sgetn(char_type* __s, streamsize __n)
    00508       { return this->xsgetn(__s, __n); }
    00509
    00510       // [27.5.2.2.4] putback
    00520       int_type
    00521       sputbackc(char_type __c);
    00522
    00532       int_type
    00533       sungetc();
    00534
    00535       // [27.5.2.2.5] put area
    00548       int_type
    00549       sputc(char_type __c);
    00550
    00562       streamsize
    00563       sputn(const char_type* __s, streamsize __n)
    00564       { return this->xsputn(__s, __n); }
    00565
    00566     protected:
    00576       basic_streambuf()
    00577       : _M_buf(NULL), _M_buf_size(0), _M_buf_size_opt(BUFSIZ),
    00578       _M_buf_unified(false), _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
    00579       _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
    00580       _M_mode(ios_base::openmode(0)), _M_buf_locale(locale()),
    00581       _M_pback_cur_save(0), _M_pback_end_save(0),
    00582       _M_pback_init(false)
    00583       { }
    00584
    00585       // [27.5.2.3.1] get area access
    00587
    00597       char_type*
    00598       eback() const { return _M_in_beg; }
    00599
    00600       char_type*
    00601       gptr()  const { return _M_in_cur;  }
    00602
    00603       char_type*
    00604       egptr() const { return _M_in_end; }
    00606
    00613       void
    00614       gbump(int __n) { _M_in_cur += __n; }
    00615
    00624       void
    00625       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
    00626       {
    00627     _M_in_beg = __gbeg;
    00628     _M_in_cur = __gnext;
    00629     _M_in_end = __gend;
    00630     if (!(_M_mode & ios_base::in) && __gbeg && __gnext && __gend)
    00631       _M_mode = _M_mode | ios_base::in;
    00632       }
    00633
    00634       // [27.5.2.3.2] put area access
    00636
    00646       char_type*
    00647       pbase() const { return _M_out_beg; }
    00648
    00649       char_type*
    00650       pptr() const { return _M_out_cur; }
    00651
    00652       char_type*
    00653       epptr() const { return _M_out_end; }
    00655
    00662       void
    00663       pbump(int __n) { _M_out_cur += __n; }
    00664
    00672       void
    00673       setp(char_type* __pbeg, char_type* __pend)
    00674       {
    00675     _M_out_beg = _M_out_cur = __pbeg;
    00676     _M_out_end = __pend;
    00677     if (!(_M_mode & ios_base::out) && __pbeg && __pend)
    00678       _M_mode = _M_mode | ios_base::out;
    00679       }
    00680
    00681       // [27.5.2.4] virtual functions
    00682       // [27.5.2.4.1] locales
    00694       virtual void
    00695       imbue(const locale& __loc)
    00696       {
    00697     if (_M_buf_locale != __loc)
    00698       _M_buf_locale = __loc;
    00699       }
    00700
    00701       // [27.5.2.4.2] buffer management and positioning
    00712       virtual basic_streambuf<char_type,_Traits>*
    00713       setbuf(char_type*, streamsize)
    00714       { return this; }
    00715
    00723       virtual pos_type
    00724       seekoff(off_type, ios_base::seekdir,
    00725           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
    00726       { return pos_type(off_type(-1)); }
    00727
    00735       virtual pos_type
    00736       seekpos(pos_type,
    00737           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
    00738       { return pos_type(off_type(-1)); }
    00739
    00748       virtual int
    00749       sync() { return 0; }
    00750
    00751       // [27.5.2.4.3] get area
    00770       virtual streamsize
    00771       showmanyc() { return 0; }
    00772
    00786       virtual streamsize
    00787       xsgetn(char_type* __s, streamsize __n);
    00788
    00808       virtual int_type
    00809       underflow()
    00810       { return traits_type::eof(); }
    00811
    00821       virtual int_type
    00822       uflow()
    00823       {
    00824     int_type __ret = traits_type::eof();
    00825     bool __testeof = traits_type::eq_int_type(this->underflow(), __ret);
    00826     bool __testpending = _M_in_cur && _M_in_cur < _M_in_end;
    00827     if (!__testeof && __testpending)
    00828       {
    00829         __ret = traits_type::to_int_type(*_M_in_cur);
    00830         ++_M_in_cur;
    00831         if (_M_buf_unified && _M_mode & ios_base::out)
    00832           ++_M_out_cur;
    00833       }
    00834     return __ret;
    00835       }
    00836
    00837       // [27.5.2.4.4] putback
    00847       virtual int_type
    00848       pbackfail(int_type /* __c */  = traits_type::eof())
    00849       { return traits_type::eof(); }
    00850
    00851       // Put area:
    00865       virtual streamsize
    00866       xsputn(const char_type* __s, streamsize __n);
    00867
    00890       virtual int_type
    00891       overflow(int_type /* __c */ = traits_type::eof())
    00892       { return traits_type::eof(); }
    00893
    00894 #ifdef _GLIBCPP_DEPRECATED
    00895     // Annex D.6
    00896     public:
    00909       void
    00910       stossc()
    00911       {
    00912     if (_M_in_cur < _M_in_end)
    00913       ++_M_in_cur;
    00914     else
    00915       this->uflow();
    00916       }
    00917 #endif
    00918
    00919 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00920     // Side effect of DR 50.
    00921     private:
    00922       basic_streambuf(const __streambuf_type&) { };
    00923
    00924       __streambuf_type&
    00925       operator=(const __streambuf_type&) { return *this; };
    00926 #endif
    00927     };
    00928 } // namespace std
    00929
    00930 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
    00931 # define export
    00932 #endif
    00933 #ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
    00934 #include <bits/streambuf.tcc>
    00935 #endif
    00936
    00937 #endif