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

std_sstream.h

Go to the documentation of this file.
00001 // String based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997-1999 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.7  String-based streams
00032 //
00033 
00034 #ifndef _CPP_SSTREAM
00035 #define _CPP_SSTREAM    1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <bits/std_istream.h>
00040 #include <bits/std_ostream.h>
00041 
00042 namespace std
00043 {
00044   template<typename _CharT, typename _Traits, typename _Alloc>
00045     class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
00046     {
00047     public:
00048       // Types:
00049       typedef _CharT                    char_type;
00050       typedef _Traits                   traits_type;
00051       typedef typename traits_type::int_type        int_type;
00052       typedef typename traits_type::pos_type        pos_type;
00053       typedef typename traits_type::off_type        off_type;
00054 
00055       // Non-standard Types:
00056       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00057       typedef basic_string<char_type, _Traits, _Alloc>  __string_type;
00058       typedef typename __string_type::size_type     __size_type;
00059 
00060     private:
00061       // Data Members:
00062       __string_type         _M_string;
00063       
00064     public:
00065       // Constructors:
00066       explicit 
00067       basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
00068       : __streambuf_type(), _M_string()
00069       { _M_stringbuf_init(__mode); }
00070 
00071       explicit 
00072       basic_stringbuf(const __string_type& __str,
00073               ios_base::openmode __mode = ios_base::in | ios_base::out)
00074       : __streambuf_type(), _M_string(__str)
00075       { _M_stringbuf_init(__mode); }
00076 
00077       // Get and set:
00078       __string_type 
00079       str() const 
00080       {
00081     if (_M_mode & ios_base::in && !(_M_mode & ios_base::out))
00082       return _M_string; 
00083     else
00084       {
00085         // This is the deal: _M_string.size() is value that
00086         // represents the size of the intial string that makes
00087         // _M_string, and may not be the correct size of the
00088         // current stringbuf internal buffer.
00089         __size_type __len = _M_string.size();
00090         if (_M_out_cur > _M_out_beg)
00091           __len = max(__size_type(_M_out_end - _M_out_beg), __len);
00092         return __string_type(_M_out_beg, _M_out_beg + __len);
00093       }
00094       }
00095 
00096       void 
00097       str(const __string_type& __s)
00098       {
00099     _M_string = __s;
00100     _M_stringbuf_init(_M_mode);
00101       }
00102 
00103     protected:
00104       // Common initialization code for both ctors goes here.
00105       void
00106       _M_stringbuf_init(ios_base::openmode __mode)
00107       {
00108     // _M_buf_size is a convenient alias for "what the streambuf
00109     // thinks the allocated size of the string really is." This is
00110     // necessary as ostringstreams are implemented with the
00111     // streambufs having control of the allocation and
00112     // re-allocation of the internal string object, _M_string.
00113     _M_buf_size = _M_string.size();
00114 
00115     // NB: Start ostringstream buffers at 1024 bytes. This is an
00116     // experimental value (pronounced "arbitrary" in some of the
00117     // hipper english-speaking countries), and can be changed to
00118     // suite particular needs.
00119     _M_buf_size_opt = 512;
00120     _M_mode = __mode;
00121     if (_M_mode & ios_base::ate)
00122       _M_really_sync(0, _M_buf_size); 
00123     else  
00124       _M_really_sync(0, 0); 
00125       }
00126 
00127       // Overridden virtual functions:
00128       virtual int_type 
00129       underflow()
00130       {
00131     if (_M_in_cur && _M_in_cur < _M_in_end)
00132       return traits_type::to_int_type(*gptr());
00133     else
00134       return traits_type::eof();
00135       }
00136 
00137       virtual int_type 
00138       pbackfail(int_type __c = traits_type::eof());
00139 
00140       virtual int_type 
00141       overflow(int_type __c = traits_type::eof());
00142 
00143       virtual __streambuf_type* 
00144       setbuf(char_type* __s, streamsize __n)
00145       { 
00146     if (__n) 
00147       {
00148         _M_string = __string_type(__s, __n);
00149         _M_really_sync(0, 0);
00150       }
00151     return this; 
00152       } 
00153 
00154       virtual pos_type 
00155       seekoff(off_type __off, ios_base::seekdir __way,
00156           ios_base::openmode __mode = ios_base::in | ios_base::out);
00157 
00158       virtual pos_type 
00159       seekpos(pos_type __sp, 
00160           ios_base::openmode __mode = ios_base::in | ios_base::out);
00161 
00162       // Internal function for correctly updating the internal buffer
00163       // for a particular _M_string, due to initialization or
00164       // re-sizing of an existing _M_string.
00165       // Assumes: contents of _M_string and internal buffer match exactly.
00166       // __i == _M_in_cur - _M_in_beg      
00167       // __o == _M_out_cur - _M_out_beg
00168       virtual int 
00169       _M_really_sync(__size_type __i, __size_type __o)
00170       {
00171     char_type* __base = const_cast<char_type*>(_M_string.data());
00172     bool __testin = _M_mode & ios_base::in;
00173     bool __testout = _M_mode & ios_base::out;
00174     __size_type __len = _M_string.size();
00175 
00176     _M_buf = __base;
00177     if (__testin)
00178         this->setg(__base, __base + __i, __base + __len);
00179     if (__testout)
00180       {
00181         this->setp(__base, __base + __len);
00182         _M_out_cur += __o;
00183       }
00184     return 0;
00185       }
00186     };
00187 
00188 
00189   // 27.7.2  Template class basic_istringstream
00190   template<typename _CharT, typename _Traits, typename _Alloc>
00191     class basic_istringstream : public basic_istream<_CharT, _Traits>
00192     {
00193     public:
00194       // Types:
00195       typedef _CharT                    char_type;
00196       typedef _Traits                   traits_type;
00197       typedef typename traits_type::int_type        int_type;
00198       typedef typename traits_type::pos_type        pos_type;
00199       typedef typename traits_type::off_type        off_type;
00200 
00201       // Non-standard types:
00202       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00203       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00204       typedef basic_istream<char_type, traits_type> __istream_type;
00205 
00206       // Constructors:
00207       explicit 
00208       basic_istringstream(ios_base::openmode __mode = ios_base::in)
00209       : __istream_type(new __stringbuf_type(__mode | ios_base::in))
00210       { }
00211 
00212       explicit 
00213       basic_istringstream(const __string_type& __str,
00214               ios_base::openmode __mode = ios_base::in)
00215       : __istream_type(new __stringbuf_type(__str, __mode | ios_base::in))
00216       { }
00217 
00218       ~basic_istringstream()
00219       { 
00220     delete _M_streambuf; 
00221     _M_streambuf = NULL;
00222       }
00223 
00224       // Members:
00225       __stringbuf_type* 
00226       rdbuf() const
00227       { return static_cast<__stringbuf_type*>(_M_streambuf); }
00228 
00229       __string_type
00230       str() const
00231       { return this->rdbuf()->str(); }
00232   
00233       void 
00234       str(const __string_type& __s)
00235       { rdbuf()->str(__s); }
00236 
00237     };
00238 
00239 
00240   // 27.7.3  Template class basic_ostringstream
00241   template <typename _CharT, typename _Traits, typename _Alloc>
00242     class basic_ostringstream : public basic_ostream<_CharT, _Traits>
00243     {
00244     public:
00245       // Types:
00246       typedef _CharT                    char_type;
00247       typedef _Traits                   traits_type;
00248       typedef typename traits_type::int_type        int_type;
00249       typedef typename traits_type::pos_type        pos_type;
00250       typedef typename traits_type::off_type        off_type;
00251 
00252       // Non-standard types:
00253       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00254       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00255       typedef basic_ostream<char_type, traits_type> __ostream_type;
00256 
00257       // Constructors/destructor:
00258       explicit 
00259       basic_ostringstream(ios_base::openmode __mode = ios_base::out)
00260       : __ostream_type(new __stringbuf_type(__mode | ios_base::out))
00261       { }
00262 
00263       explicit 
00264       basic_ostringstream(const __string_type __str,
00265               ios_base::openmode __mode = ios_base::out)
00266       : __ostream_type(new __stringbuf_type(__str, __mode | ios_base::out))
00267       { }
00268 
00269       ~basic_ostringstream()
00270       { 
00271     delete _M_streambuf; 
00272     _M_streambuf = NULL;
00273       }
00274 
00275       // Members:
00276       __stringbuf_type* 
00277       rdbuf() const
00278       { return static_cast<__stringbuf_type*>(_M_streambuf); }
00279 
00280       __string_type
00281       str() const
00282       { return this->rdbuf()->str(); }
00283  
00284       void 
00285       str(const __string_type& __s)
00286       { rdbuf()->str(__s); }
00287 
00288     };
00289   
00290   
00291   // 27.7.4  Template class basic_stringstream
00292   template <typename _CharT, typename _Traits, typename _Alloc>
00293     class basic_stringstream : public basic_iostream<_CharT, _Traits>
00294     {
00295     public:
00296       // Types:
00297       typedef _CharT                    char_type;
00298       typedef _Traits                   traits_type;
00299       typedef typename traits_type::int_type        int_type;
00300       typedef typename traits_type::pos_type        pos_type;
00301       typedef typename traits_type::off_type        off_type;
00302 
00303       // Non-standard Types:
00304       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00305       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00306       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00307      
00308       // Constructors/destructors
00309       explicit 
00310       basic_stringstream(ios_base::openmode __mode = 
00311              ios_base::out | ios_base::in)
00312       : __iostream_type(new __stringbuf_type(__mode))
00313       { }
00314 
00315       explicit 
00316       basic_stringstream(const __string_type& __str,
00317              ios_base::openmode __mode = 
00318              ios_base::out | ios_base::in)
00319       : __iostream_type(new __stringbuf_type(__str, __mode))
00320       { }
00321 
00322       ~basic_stringstream()
00323       { 
00324     delete _M_streambuf; 
00325     _M_streambuf = NULL;
00326       }
00327 
00328       // Members:
00329       __stringbuf_type* 
00330       rdbuf() const
00331       { return static_cast<__stringbuf_type*>(_M_streambuf); }
00332 
00333       __string_type
00334       str() const
00335       { return rdbuf()->str(); }
00336 
00337       void 
00338       str(const __string_type& __s)
00339       { rdbuf()->str(__s); }
00340     };
00341 
00342 } // namespace std
00343 
00344 
00345 
00346 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
00347 # define export
00348 #ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
00349 # include <bits/sstream.tcc>
00350 #endif
00351 #endif
00352 
00353 #endif  /* _CPP_SSTREAM */
00354 

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