Whole document tree
    

Whole document tree

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

Generated on Mon Apr 8 03:11:34 2002 for libstdc++-v3 Source by doxygen1.2.15