sstream

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

Generated on Wed May 1 19:19:34 2002 for libstdc++-v3 Source by doxygen1.2.15