sstream

Go to the documentation of this file.
00001 // String based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2002, 2003, 2004
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.7  String-based streams
00033 //
00034 
00040 #ifndef _GLIBCXX_SSTREAM
00041 #define _GLIBCXX_SSTREAM 1
00042 
00043 #pragma GCC system_header
00044 
00045 #include <istream>
00046 #include <ostream>
00047 
00048 namespace std
00049 {
00050   // [27.7.1] template class basic_stringbuf
00062   template<typename _CharT, typename _Traits, typename _Alloc>
00063     class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
00064     {
00065     public:
00066       // Types:
00067       typedef _CharT                    char_type;
00068       typedef _Traits                   traits_type;
00069       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00070       // 251. basic_stringbuf missing allocator_type
00071       typedef _Alloc                        allocator_type;
00072       typedef typename traits_type::int_type        int_type;
00073       typedef typename traits_type::pos_type        pos_type;
00074       typedef typename traits_type::off_type        off_type;
00075 
00077 
00082       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00083       typedef basic_string<char_type, _Traits, _Alloc>  __string_type;
00084       typedef typename __string_type::size_type     __size_type;
00086 
00087     protected:
00093       ios_base::openmode    _M_mode;
00094 
00095       // Data Members:
00101       __string_type         _M_string;
00102 
00103     public:
00104       // Constructors:
00112       explicit
00113       basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
00114       : __streambuf_type(), _M_mode(__mode), _M_string()
00115       { }
00116 
00125       explicit
00126       basic_stringbuf(const __string_type& __str,
00127               ios_base::openmode __mode = ios_base::in | ios_base::out)
00128       : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
00129       { _M_stringbuf_init(__mode); }
00130 
00131       // Get and set:
00140       __string_type
00141       str() const
00142       {
00143     if (this->pptr())
00144       {
00145         // The current egptr() may not be the actual string end.
00146         if (this->pptr() > this->egptr())
00147           return __string_type(this->pbase(), this->pptr());
00148         else
00149           return __string_type(this->pbase(), this->egptr());
00150       }
00151     else
00152       return _M_string;
00153       }
00154 
00162       void
00163       str(const __string_type& __s)
00164       {
00165     // Cannot use _M_string = __s, since v3 strings are COW.
00166     _M_string.assign(__s.data(), __s.size());
00167     _M_stringbuf_init(this->_M_mode);
00168       }
00169 
00170     protected:
00171       // Common initialization code goes here.
00177       void
00178       _M_stringbuf_init(ios_base::openmode __mode)
00179       {
00180     this->_M_mode = __mode;
00181 
00182     __size_type __len = 0;
00183     if (this->_M_mode & (ios_base::ate | ios_base::app))
00184       __len = _M_string.size();
00185     _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
00186       }
00187 
00188       virtual streamsize
00189       showmanyc()
00190       {
00191     streamsize __ret = -1;
00192     if (_M_mode & ios_base::in)
00193       {
00194         _M_update_egptr();
00195         __ret = this->egptr() - this->gptr();
00196       }
00197     return __ret;
00198       }
00199 
00200       // [documentation is inherited]
00201       virtual int_type
00202       underflow();
00203 
00204       // [documentation is inherited]
00205       virtual int_type
00206       pbackfail(int_type __c = traits_type::eof());
00207 
00208       // [documentation is inherited]
00209       virtual int_type
00210       overflow(int_type __c = traits_type::eof());
00211 
00223       virtual __streambuf_type*
00224       setbuf(char_type* __s, streamsize __n)
00225       {
00226     if (__s && __n >= 0)
00227       {
00228         // This is implementation-defined behavior, and assumes
00229         // that an external char_type array of length __n exists
00230         // and has been pre-allocated. If this is not the case,
00231         // things will quickly blow up.
00232         
00233         // Step 1: Destroy the current internal array.
00234         _M_string = __string_type(__s, __n);
00235         
00236         // Step 2: Use the external array.
00237         _M_sync(__s, 0, 0);
00238       }
00239     return this;
00240       }
00241 
00242       // [documentation is inherited]
00243       virtual pos_type
00244       seekoff(off_type __off, ios_base::seekdir __way,
00245           ios_base::openmode __mode = ios_base::in | ios_base::out);
00246 
00247       // [documentation is inherited]
00248       virtual pos_type
00249       seekpos(pos_type __sp,
00250           ios_base::openmode __mode = ios_base::in | ios_base::out);
00251 
00252       // Internal function for correctly updating the internal buffer
00253       // for a particular _M_string, due to initialization or
00254       // re-sizing of an existing _M_string.
00255       // Assumes: contents of _M_string and internal buffer match exactly.
00256       // __i == _M_in_cur - _M_in_beg
00257       // __o == _M_out_cur - _M_out_beg
00263       void
00264       _M_sync(char_type* __base, __size_type __i, __size_type __o)
00265       {
00266     const bool __testin = this->_M_mode & ios_base::in;
00267     const bool __testout = this->_M_mode & ios_base::out;
00268     const __size_type __len = _M_string.size();
00269 
00270     if (__testin)
00271       this->setg(__base, __base + __i, __base + __len);
00272     if (__testout)
00273       {
00274         this->setp(__base, __base + _M_string.capacity());
00275         this->pbump(__o);
00276         // We need a pointer to the string end anyway, even when
00277         // !__testin: in that case, however, for the correct
00278         // functioning of the streambuf inlines all the get area
00279         // pointers must be identical.
00280         if (!__testin)
00281           this->setg(__base + __len, __base + __len, __base + __len);
00282       }
00283       }
00284 
00285       // Internal function for correctly updating egptr() to the actual
00286       // string end.
00287       void
00288       _M_update_egptr()
00289       {
00290     const bool __testin = this->_M_mode & ios_base::in;
00291 
00292     if (this->pptr() && this->pptr() > this->egptr())
00293       if (__testin)
00294         this->setg(this->eback(), this->gptr(), this->pptr());
00295       else
00296         this->setg(this->pptr(), this->pptr(), this->pptr());
00297       }
00298     };
00299 
00300 
00301   // [27.7.2] Template class basic_istringstream
00310   template<typename _CharT, typename _Traits, typename _Alloc>
00311     class basic_istringstream : public basic_istream<_CharT, _Traits>
00312     {
00313     public:
00314       // Types:
00315       typedef _CharT                    char_type;
00316       typedef _Traits                   traits_type;
00317       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00318       // 251. basic_stringbuf missing allocator_type
00319       typedef _Alloc                        allocator_type;
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_istream<char_type, traits_type> __istream_type;
00328 
00329     private:
00335       __stringbuf_type  _M_stringbuf;
00336 
00337     public:
00338       // Constructors:
00353       explicit
00354       basic_istringstream(ios_base::openmode __mode = ios_base::in)
00355       : __istream_type(), _M_stringbuf(__mode | ios_base::in)
00356       { this->init(&_M_stringbuf); }
00357 
00373       explicit
00374       basic_istringstream(const __string_type& __str,
00375               ios_base::openmode __mode = ios_base::in)
00376       : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
00377       { this->init(&_M_stringbuf); }
00378 
00385       ~basic_istringstream()
00386       { }
00387 
00388       // Members:
00395       __stringbuf_type*
00396       rdbuf() const
00397       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00398 
00403       __string_type
00404       str() const
00405       { return _M_stringbuf.str(); }
00406 
00413       void
00414       str(const __string_type& __s)
00415       { _M_stringbuf.str(__s); }
00416     };
00417 
00418 
00419   // [27.7.3] Template class basic_ostringstream
00428   template <typename _CharT, typename _Traits, typename _Alloc>
00429     class basic_ostringstream : public basic_ostream<_CharT, _Traits>
00430     {
00431     public:
00432       // Types:
00433       typedef _CharT                    char_type;
00434       typedef _Traits                   traits_type;
00435       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00436       // 251. basic_stringbuf missing allocator_type
00437       typedef _Alloc                        allocator_type;
00438       typedef typename traits_type::int_type        int_type;
00439       typedef typename traits_type::pos_type        pos_type;
00440       typedef typename traits_type::off_type        off_type;
00441 
00442       // Non-standard types:
00443       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00444       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00445       typedef basic_ostream<char_type, traits_type> __ostream_type;
00446 
00447     private:
00453       __stringbuf_type  _M_stringbuf;
00454 
00455     public:
00456       // Constructors/destructor:
00471       explicit
00472       basic_ostringstream(ios_base::openmode __mode = ios_base::out)
00473       : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
00474       { this->init(&_M_stringbuf); }
00475 
00491       explicit
00492       basic_ostringstream(const __string_type& __str,
00493               ios_base::openmode __mode = ios_base::out)
00494       : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
00495       { this->init(&_M_stringbuf); }
00496 
00503       ~basic_ostringstream()
00504       { }
00505 
00506       // Members:
00513       __stringbuf_type*
00514       rdbuf() const
00515       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00516 
00521       __string_type
00522       str() const
00523       { return _M_stringbuf.str(); }
00524 
00531       void
00532       str(const __string_type& __s)
00533       { _M_stringbuf.str(__s); }
00534     };
00535 
00536 
00537   // [27.7.4] Template class basic_stringstream
00546   template <typename _CharT, typename _Traits, typename _Alloc>
00547     class basic_stringstream : public basic_iostream<_CharT, _Traits>
00548     {
00549     public:
00550       // Types:
00551       typedef _CharT                    char_type;
00552       typedef _Traits                   traits_type;
00553       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00554       // 251. basic_stringbuf missing allocator_type
00555       typedef _Alloc                        allocator_type;
00556       typedef typename traits_type::int_type        int_type;
00557       typedef typename traits_type::pos_type        pos_type;
00558       typedef typename traits_type::off_type        off_type;
00559 
00560       // Non-standard Types:
00561       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00562       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00563       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00564 
00565     private:
00571       __stringbuf_type  _M_stringbuf;
00572 
00573     public:
00574       // Constructors/destructors
00587       explicit
00588       basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
00589       : __iostream_type(), _M_stringbuf(__m)
00590       { this->init(&_M_stringbuf); }
00591 
00605       explicit
00606       basic_stringstream(const __string_type& __str,
00607              ios_base::openmode __m = ios_base::out | ios_base::in)
00608       : __iostream_type(), _M_stringbuf(__str, __m)
00609       { this->init(&_M_stringbuf); }
00610 
00617       ~basic_stringstream()
00618       { }
00619 
00620       // Members:
00627       __stringbuf_type*
00628       rdbuf() const
00629       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00630 
00635       __string_type
00636       str() const
00637       { return _M_stringbuf.str(); }
00638 
00645       void
00646       str(const __string_type& __s)
00647       { _M_stringbuf.str(__s); }
00648     };
00649 } // namespace std
00650 
00651 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00652 # include <bits/sstream.tcc>
00653 #endif
00654 
00655 #endif /* _GLIBCXX_SSTREAM */

Generated on Tue Jan 30 17:31:53 2007 for GNU C++ STL by doxygen 1.3.6