Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

std_streambuf.h

Go to the documentation of this file.
00001 // Stream buffer classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 //
00031 // ISO C++ 14882: 27.5  Stream buffers
00032 //
00033 
00034 #ifndef _CPP_STREAMBUF
00035 #define _CPP_STREAMBUF  1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <bits/c++config.h>
00040 #include <bits/std_iosfwd.h>
00041 #include <bits/std_cstdio.h>    // For SEEK_SET, SEEK_CUR, SEEK_END
00042 #include <bits/localefwd.h>
00043 #include <bits/ios_base.h>
00044 
00045 namespace std
00046 {
00047   template<typename _CharT, typename _Traits>
00048     streamsize
00049     __copy_streambufs(basic_ios<_CharT, _Traits>& _ios,
00050               basic_streambuf<_CharT, _Traits>* __sbin,
00051               basic_streambuf<_CharT, _Traits>* __sbout);
00052   
00053   // 27.5.2 Template class basic_streambuf<_CharT, _Traits>
00054   template<typename _CharT, typename _Traits>
00055     class basic_streambuf 
00056     {
00057     public:
00058       // Types:
00059       typedef _CharT                    char_type;
00060       typedef _Traits                   traits_type;
00061       typedef typename traits_type::int_type        int_type;
00062       typedef typename traits_type::pos_type        pos_type;
00063       typedef typename traits_type::off_type        off_type;
00064 
00065       // Non-standard Types:
00066       typedef ctype<char_type>                  __ctype_type;
00067       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00068       
00069       friend class basic_ios<char_type, traits_type>;
00070       friend class basic_istream<char_type, traits_type>;
00071       friend class basic_ostream<char_type, traits_type>;
00072       friend class istreambuf_iterator<char_type, traits_type>;
00073       friend class ostreambuf_iterator<char_type, traits_type>;
00074 
00075       friend streamsize
00076       __copy_streambufs<>(basic_ios<char_type, traits_type>& __ios,
00077               __streambuf_type* __sbin,__streambuf_type* __sbout);
00078       
00079     protected:
00080 
00081       // Pointer to the beginning of internally-allocated
00082       // space. Filebuf manually allocates/deallocates this, whereas
00083       // stringstreams attempt to use the built-in intelligence of the
00084       // string class. If you are managing memory, set this. If not,
00085       // leave it NULL.
00086       char_type*        _M_buf;     
00087 
00088       // Actual size of allocated internal buffer, in bytes.
00089       int_type          _M_buf_size;
00090 
00091       // Optimal or preferred size of internal buffer, in bytes.
00092       int_type          _M_buf_size_opt;
00093 
00094       // True iff _M_in_* and _M_out_* buffers should always point to
00095       // the same place.  True for fstreams, false for sstreams.
00096       bool          _M_buf_unified; 
00097 
00098        // This is based on _IO_FILE, just reordered to be more
00099       // consistent, and is intended to be the most minimal abstraction
00100       // for an internal buffer.
00101       // get == input == read
00102       // put == output == write
00103       char_type*        _M_in_beg;      // Start of get area. 
00104       char_type*        _M_in_cur;  // Current read area. 
00105       char_type*        _M_in_end;  // End of get area. 
00106       char_type*        _M_out_beg;     // Start of put area. 
00107       char_type*        _M_out_cur;     // Current put area. 
00108       char_type*        _M_out_end;     // End of put area. 
00109 
00110       // Place to stash in || out || in | out settings for current streambuf.
00111       ios_base::openmode    _M_mode;    
00112 
00113       // Current locale setting.
00114       locale            _M_buf_locale;  
00115 
00116       // True iff locale is initialized.
00117       bool          _M_buf_locale_init;
00118 
00119       // Necessary bits for putback buffer management. Only used in
00120       // the basic_filebuf class, as necessary for the standard
00121       // requirements. The only basic_streambuf member function that
00122       // needs access to these data members is in_avail...
00123       // NB: pbacks of over one character are not currently supported.
00124       int_type          _M_pback_size; 
00125       char_type*        _M_pback; 
00126       char_type*        _M_pback_cur_save;
00127       char_type*        _M_pback_end_save;
00128       bool          _M_pback_init; 
00129 
00130       // Initializes pback buffers, and moves normal buffers to safety.
00131       // Assumptions:
00132       // _M_in_cur has already been moved back
00133       void
00134       _M_pback_create()
00135       {
00136     if (!_M_pback_init)
00137       {
00138         int_type __dist = _M_in_end - _M_in_cur;
00139         int_type __len = min(_M_pback_size, __dist);
00140         traits_type::copy(_M_pback, _M_in_cur, __len);
00141         _M_pback_cur_save = _M_in_cur;
00142         _M_pback_end_save = _M_in_end;
00143         this->setg(_M_pback, _M_pback, _M_pback + __len);
00144         _M_pback_init = true;
00145       }
00146       }
00147 
00148       // Deactivates pback buffer contents, and restores normal buffer.
00149       // Assumptions:
00150       // The pback buffer has only moved forward.
00151       void
00152       _M_pback_destroy()
00153       {
00154     if (_M_pback_init)
00155       {
00156         // Length _M_in_cur moved in the pback buffer.
00157         int_type __off_cur = _M_in_cur - _M_pback;
00158         
00159         // For in | out buffers, the end can be pushed back...
00160         int_type __off_end = 0;
00161         int_type __pback_len = _M_in_end - _M_pback;
00162         int_type __save_len = _M_pback_end_save - _M_buf;
00163         if (__pback_len > __save_len)
00164           __off_end = __pback_len - __save_len;
00165 
00166         this->setg(_M_buf, _M_pback_cur_save + __off_cur, 
00167                _M_pback_end_save + __off_end);
00168         _M_pback_cur_save = NULL;
00169         _M_pback_end_save = NULL;
00170         _M_pback_init = false;
00171       }
00172       }
00173 
00174       // Correctly sets the _M_in_cur pointer, and bumps the
00175       // _M_out_cur pointer as well if necessary.
00176       void 
00177       _M_in_cur_move(off_type __n) // argument needs to be +-
00178       {
00179     bool __testout = _M_out_cur;
00180     _M_in_cur += __n;
00181     if (__testout && _M_buf_unified)
00182       _M_out_cur += __n;
00183       }
00184 
00185       // Correctly sets the _M_out_cur pointer, and bumps the
00186       // appropriate _M_*_end pointers as well. Necessary for the
00187       // un-tied stringbufs, in in|out mode.
00188       // Invariant:
00189       // __n + _M_out_[cur, end] <= _M_buf + _M_buf_size
00190       // Assuming all _M_*_[beg, cur, end] pointers are operating on
00191       // the same range:
00192       // _M_buf <= _M_*_ <= _M_buf + _M_buf_size
00193       void 
00194       _M_out_cur_move(off_type __n) // argument needs to be +-
00195       {
00196     bool __testin = _M_in_cur;
00197 
00198     _M_out_cur += __n;
00199     if (__testin && _M_buf_unified)
00200       _M_in_cur += __n;
00201     if (_M_out_cur > _M_out_end)
00202       {
00203         _M_out_end = _M_out_cur;
00204         // NB: in | out buffers drag the _M_in_end pointer along...
00205         if (__testin)
00206           _M_in_end += __n;
00207       }
00208       }
00209 
00210       // Return the size of the output buffer.  This depends on the
00211       // buffer in use: allocated buffers have a stored size in
00212       // _M_buf_size and setbuf() buffers don't.
00213       off_type
00214       _M_out_buf_size()
00215       {
00216     off_type __ret = 0;
00217     if (_M_out_cur)
00218       {
00219         // Using allocated buffer.
00220         if (_M_out_beg == _M_buf)
00221           __ret = _M_out_beg + _M_buf_size - _M_out_cur;
00222         // Using non-allocated buffer.
00223         else
00224           __ret = _M_out_end - _M_out_cur;
00225       }
00226     return __ret;
00227       }
00228 
00229       // These three functions are used to clarify internal buffer
00230       // maintenance. After an overflow, or after a seekoff call that
00231       // started at beg or end, or possibly when the stream becomes
00232       // unbuffered, and a myrid other obscure corner cases, the
00233       // internal buffer does not truly reflect the contents of the
00234       // external buffer. At this point, for whatever reason, it is in
00235       // an indeterminate state.
00236       void
00237       _M_set_indeterminate(void)
00238       {
00239     if (_M_mode & ios_base::in)
00240       this->setg(_M_buf, _M_buf, _M_buf);
00241     if (_M_mode & ios_base::out)
00242       this->setp(_M_buf, _M_buf);
00243       }
00244 
00245       void
00246       _M_set_determinate(off_type __off)
00247       {
00248     bool __testin = _M_mode & ios_base::in;
00249     bool __testout = _M_mode & ios_base::out;
00250     if (__testin)
00251       this->setg(_M_buf, _M_buf, _M_buf + __off);
00252     if (__testout)
00253       this->setp(_M_buf, _M_buf + __off);
00254 
00255       }
00256 
00257       bool
00258       _M_is_indeterminate(void)
00259       { 
00260     bool __ret = false;
00261     if (_M_mode & ios_base::in)
00262       __ret = _M_in_beg == _M_in_cur && _M_in_cur == _M_in_end;
00263     if (_M_mode & ios_base::out)
00264       __ret = _M_out_beg == _M_out_cur && _M_out_cur == _M_out_end;
00265     return __ret;
00266       }
00267 
00268   public:
00269       virtual 
00270       ~basic_streambuf() 
00271       {
00272     _M_buf_unified = false;
00273     _M_buf_size = 0;
00274     _M_buf_size_opt = 0;
00275     _M_mode = ios_base::openmode(0);
00276     _M_buf_locale_init = false;
00277 
00278       }
00279 
00280       // Locales:
00281       locale 
00282       pubimbue(const locale &__loc)
00283       {
00284     locale __tmp(this->getloc());
00285     this->imbue(__loc);
00286     return __tmp;
00287       }
00288 
00289       locale   
00290       getloc() const
00291       {
00292     if (_M_buf_locale_init)
00293       return _M_buf_locale; 
00294     else 
00295       return locale();
00296       } 
00297 
00298       // Buffer and positioning:
00299       __streambuf_type* 
00300       pubsetbuf(char_type* __s, streamsize __n) 
00301       { return this->setbuf(__s, __n); }
00302 
00303       pos_type 
00304       pubseekoff(off_type __off, ios_base::seekdir __way, 
00305          ios_base::openmode __mode = ios_base::in | ios_base::out)
00306       { return this->seekoff(__off, __way, __mode); }
00307 
00308       pos_type 
00309       pubseekpos(pos_type __sp,
00310          ios_base::openmode __mode = ios_base::in | ios_base::out)
00311       { return this->seekpos(__sp, __mode); }
00312 
00313       int 
00314       pubsync() { return this->sync(); }
00315 
00316       // Get and put areas:
00317       // Get area:
00318       streamsize 
00319       in_avail() 
00320       { 
00321     streamsize __ret;
00322     if (_M_in_cur && _M_in_cur < _M_in_end)
00323       {
00324         if (_M_pback_init)
00325           {
00326         int_type __save_len =  _M_pback_end_save - _M_pback_cur_save;
00327         int_type __pback_len = _M_in_cur - _M_pback;
00328         __ret = __save_len - __pback_len;
00329           }
00330         else
00331           __ret = this->egptr() - this->gptr();
00332       }
00333     else
00334       __ret = this->showmanyc();
00335     return __ret;
00336       }
00337 
00338       int_type 
00339       snextc()
00340       {
00341     int_type __eof = traits_type::eof();
00342     return (this->sbumpc() == __eof ? __eof : this->sgetc()); 
00343       }
00344 
00345       int_type 
00346       sbumpc();
00347 
00348       int_type 
00349       sgetc()
00350       {
00351     int_type __ret;
00352     if (_M_in_cur && _M_in_cur < _M_in_end)
00353       __ret = traits_type::to_int_type(*gptr());
00354     else 
00355       __ret = this->underflow();
00356     return __ret;
00357       }
00358 
00359       streamsize 
00360       sgetn(char_type* __s, streamsize __n)
00361       { return this->xsgetn(__s, __n); }
00362 
00363       // Putback:
00364       int_type 
00365       sputbackc(char_type __c);
00366 
00367       int_type 
00368       sungetc();
00369 
00370       // Put area:
00371       int_type 
00372       sputc(char_type __c);
00373 
00374       streamsize 
00375       sputn(const char_type* __s, streamsize __n)
00376       { return this->xsputn(__s, __n); }
00377 
00378     protected:
00379       basic_streambuf()
00380       : _M_buf(NULL), _M_buf_size(0), 
00381       _M_buf_size_opt(static_cast<int_type>(BUFSIZ)), _M_buf_unified(false), 
00382       _M_in_beg(0), _M_in_cur(0), _M_in_end(0), _M_out_beg(0), _M_out_cur(0), 
00383       _M_out_end(0), _M_mode(ios_base::openmode(0)), _M_buf_locale(locale()), 
00384       _M_buf_locale_init(false), _M_pback_size(1), _M_pback(NULL), 
00385       _M_pback_cur_save(NULL), _M_pback_end_save(NULL), _M_pback_init(false)
00386       { }
00387 
00388       // Get area:
00389       char_type* 
00390       eback() const { return _M_in_beg; }
00391 
00392       char_type* 
00393       gptr()  const { return _M_in_cur;  }
00394 
00395       char_type* 
00396       egptr() const { return _M_in_end; }
00397 
00398       void 
00399       gbump(int __n) { _M_in_cur += __n; }
00400 
00401       void 
00402       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
00403       {
00404     _M_in_beg = __gbeg;
00405     _M_in_cur = __gnext;
00406     _M_in_end = __gend;
00407     if (!(_M_mode & ios_base::in) && __gbeg && __gnext && __gend)
00408       _M_mode = _M_mode | ios_base::in;
00409       }
00410 
00411       // Put area:
00412       char_type* 
00413       pbase() const { return _M_out_beg; }
00414 
00415       char_type* 
00416       pptr() const { return _M_out_cur; }
00417 
00418       char_type* 
00419       epptr() const { return _M_out_end; }
00420 
00421       void 
00422       pbump(int __n) { _M_out_cur += __n; }
00423 
00424       void 
00425       setp(char_type* __pbeg, char_type* __pend)
00426       { 
00427     _M_out_beg = _M_out_cur = __pbeg; 
00428     _M_out_end = __pend; 
00429     if (!(_M_mode & ios_base::out) && __pbeg && __pend)
00430       _M_mode = _M_mode | ios_base::out;
00431       }
00432 
00433       // Virtual functions:
00434       // Locales:
00435       virtual void 
00436       imbue(const locale& __loc) 
00437       { 
00438     _M_buf_locale_init = true;
00439     if (_M_buf_locale != __loc)
00440       _M_buf_locale = __loc;
00441       }
00442 
00443       // Buffer management and positioning:
00444       virtual basic_streambuf<char_type,_Traits>* 
00445       setbuf(char_type*, streamsize)
00446       { return this; }
00447       
00448       virtual pos_type 
00449       seekoff(off_type, ios_base::seekdir,
00450           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00451       { return pos_type(off_type(-1)); } 
00452 
00453       virtual pos_type 
00454       seekpos(pos_type, 
00455           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00456       { return pos_type(off_type(-1)); } 
00457 
00458       virtual int 
00459       sync() { return 0; }
00460 
00461       // Get area:
00462       virtual streamsize 
00463       showmanyc() { return 0; }
00464 
00465       virtual streamsize 
00466       xsgetn(char_type* __s, streamsize __n);
00467 
00468       virtual int_type 
00469       underflow()
00470       { return traits_type::eof(); }
00471 
00472       virtual int_type 
00473       uflow() 
00474       {
00475     int_type __ret = traits_type::eof();
00476     bool __testeof = this->underflow() == __ret;
00477     bool __testpending = _M_in_cur && _M_in_cur < _M_in_end;
00478     if (!__testeof && __testpending)
00479       {
00480         __ret = traits_type::to_int_type(*_M_in_cur);
00481         ++_M_in_cur;
00482         if (_M_buf_unified && _M_mode & ios_base::out)
00483           ++_M_out_cur;
00484       }
00485     return __ret;    
00486       }
00487 
00488       // Putback:
00489       virtual int_type 
00490       pbackfail(int_type /* __c */  = traits_type::eof())
00491       { return traits_type::eof(); }
00492 
00493       // Put area:
00494       virtual streamsize 
00495       xsputn(const char_type* __s, streamsize __n);
00496 
00497       virtual int_type 
00498       overflow(int_type /* __c */ = traits_type::eof())
00499       { return traits_type::eof(); }
00500 
00501 #ifdef _GLIBCPP_DEPRECATED
00502     public:
00503       void 
00504       stossc() 
00505       {
00506     if (_M_in_cur < _M_in_end) 
00507       ++_M_in_cur;
00508     else 
00509       this->uflow();
00510       }
00511 #endif
00512 
00513 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00514     private:
00515       basic_streambuf(const __streambuf_type&);
00516 
00517       __streambuf_type& 
00518       operator=(const __streambuf_type&);
00519 #endif
00520     };
00521 
00522 } // namespace std
00523 
00524 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
00525 # define export
00526 #ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
00527 #include <bits/streambuf.tcc>
00528 #endif
00529 #endif
00530 
00531 #endif  /* _CPP_STREAMBUF */
00532 

Generated at Tue May 1 16:28:38 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001