sstream

Go to the documentation of this file.
00001 // String based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 
00039 #ifndef _GLIBCXX_SSTREAM
00040 #define _GLIBCXX_SSTREAM 1
00041 
00042 #pragma GCC system_header
00043 
00044 #include <istream>
00045 #include <ostream>
00046 
00047 namespace std
00048 {
00049   // [27.7.1] template class basic_stringbuf
00061   template<typename _CharT, typename _Traits, typename _Alloc>
00062     class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
00063     {
00064     public:
00065       // Types:
00066       typedef _CharT                    char_type;
00067       typedef _Traits                   traits_type;
00068       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00069       // 251. basic_stringbuf missing allocator_type
00070       typedef _Alloc                        allocator_type;
00071       typedef typename traits_type::int_type        int_type;
00072       typedef typename traits_type::pos_type        pos_type;
00073       typedef typename traits_type::off_type        off_type;
00074 
00075       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00076       typedef basic_string<char_type, _Traits, _Alloc>  __string_type;
00077       typedef typename __string_type::size_type     __size_type;
00078 
00079     protected:
00085       ios_base::openmode    _M_mode;
00086 
00087       // Data Members:
00088       __string_type         _M_string;
00089 
00090     public:
00091       // Constructors:
00099       explicit
00100       basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
00101       : __streambuf_type(), _M_mode(__mode), _M_string()
00102       { }
00103 
00112       explicit
00113       basic_stringbuf(const __string_type& __str,
00114               ios_base::openmode __mode = ios_base::in | ios_base::out)
00115       : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
00116       { _M_stringbuf_init(__mode); }
00117 
00118       // Get and set:
00127       __string_type
00128       str() const
00129       {
00130     __string_type __ret;
00131     if (this->pptr())
00132       {
00133         // The current egptr() may not be the actual string end.
00134         if (this->pptr() > this->egptr())
00135           __ret = __string_type(this->pbase(), this->pptr());
00136         else
00137           __ret = __string_type(this->pbase(), this->egptr());
00138       }
00139     else
00140       __ret = _M_string;
00141     return __ret;
00142       }
00143 
00151       void
00152       str(const __string_type& __s)
00153       {
00154     // Cannot use _M_string = __s, since v3 strings are COW.
00155     _M_string.assign(__s.data(), __s.size());
00156     _M_stringbuf_init(_M_mode);
00157       }
00158 
00159     protected:
00160       // Common initialization code goes here.
00161       void
00162       _M_stringbuf_init(ios_base::openmode __mode)
00163       {
00164     _M_mode = __mode;
00165     __size_type __len = 0;
00166     if (_M_mode & (ios_base::ate | ios_base::app))
00167       __len = _M_string.size();
00168     _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
00169       }
00170 
00171       virtual streamsize
00172       showmanyc()
00173       { 
00174     streamsize __ret = -1;
00175     if (_M_mode & ios_base::in)
00176       {
00177         _M_update_egptr();
00178         __ret = this->egptr() - this->gptr();
00179       }
00180     return __ret;
00181       }
00182 
00183       virtual int_type
00184       underflow();
00185 
00186       virtual int_type
00187       pbackfail(int_type __c = traits_type::eof());
00188 
00189       virtual int_type
00190       overflow(int_type __c = traits_type::eof());
00191 
00203       virtual __streambuf_type*
00204       setbuf(char_type* __s, streamsize __n)
00205       {
00206     if (__s && __n >= 0)
00207       {
00208         // This is implementation-defined behavior, and assumes
00209         // that an external char_type array of length __n exists
00210         // and has been pre-allocated. If this is not the case,
00211         // things will quickly blow up.
00212         
00213         // Step 1: Destroy the current internal array.
00214         _M_string.clear();
00215         
00216         // Step 2: Use the external array.
00217         _M_sync(__s, __n, 0);
00218       }
00219     return this;
00220       }
00221 
00222       virtual pos_type
00223       seekoff(off_type __off, ios_base::seekdir __way,
00224           ios_base::openmode __mode = ios_base::in | ios_base::out);
00225 
00226       virtual pos_type
00227       seekpos(pos_type __sp,
00228           ios_base::openmode __mode = ios_base::in | ios_base::out);
00229 
00230       // Internal function for correctly updating the internal buffer
00231       // for a particular _M_string, due to initialization or
00232       // re-sizing of an existing _M_string.
00233       void
00234       _M_sync(char_type* __base, __size_type __i, __size_type __o);
00235 
00236       // Internal function for correctly updating egptr() to the actual
00237       // string end.
00238       void
00239       _M_update_egptr()
00240       {
00241     const bool __testin = _M_mode & ios_base::in;
00242     if (this->pptr() && this->pptr() > this->egptr())
00243       if (__testin)
00244         this->setg(this->eback(), this->gptr(), this->pptr());
00245       else
00246         this->setg(this->pptr(), this->pptr(), this->pptr());
00247       }
00248     };
00249 
00250 
00251   // [27.7.2] Template class basic_istringstream
00260   template<typename _CharT, typename _Traits, typename _Alloc>
00261     class basic_istringstream : public basic_istream<_CharT, _Traits>
00262     {
00263     public:
00264       // Types:
00265       typedef _CharT                    char_type;
00266       typedef _Traits                   traits_type;
00267       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00268       // 251. basic_stringbuf missing allocator_type
00269       typedef _Alloc                        allocator_type;
00270       typedef typename traits_type::int_type        int_type;
00271       typedef typename traits_type::pos_type        pos_type;
00272       typedef typename traits_type::off_type        off_type;
00273 
00274       // Non-standard types:
00275       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00276       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00277       typedef basic_istream<char_type, traits_type> __istream_type;
00278 
00279     private:
00280       __stringbuf_type  _M_stringbuf;
00281 
00282     public:
00283       // Constructors:
00298       explicit
00299       basic_istringstream(ios_base::openmode __mode = ios_base::in)
00300       : __istream_type(), _M_stringbuf(__mode | ios_base::in)
00301       { this->init(&_M_stringbuf); }
00302 
00318       explicit
00319       basic_istringstream(const __string_type& __str,
00320               ios_base::openmode __mode = ios_base::in)
00321       : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
00322       { this->init(&_M_stringbuf); }
00323 
00330       ~basic_istringstream()
00331       { }
00332 
00333       // Members:
00340       __stringbuf_type*
00341       rdbuf() const
00342       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00343 
00348       __string_type
00349       str() const
00350       { return _M_stringbuf.str(); }
00351 
00358       void
00359       str(const __string_type& __s)
00360       { _M_stringbuf.str(__s); }
00361     };
00362 
00363 
00364   // [27.7.3] Template class basic_ostringstream
00373   template <typename _CharT, typename _Traits, typename _Alloc>
00374     class basic_ostringstream : public basic_ostream<_CharT, _Traits>
00375     {
00376     public:
00377       // Types:
00378       typedef _CharT                    char_type;
00379       typedef _Traits                   traits_type;
00380       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00381       // 251. basic_stringbuf missing allocator_type
00382       typedef _Alloc                        allocator_type;
00383       typedef typename traits_type::int_type        int_type;
00384       typedef typename traits_type::pos_type        pos_type;
00385       typedef typename traits_type::off_type        off_type;
00386 
00387       // Non-standard types:
00388       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00389       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00390       typedef basic_ostream<char_type, traits_type> __ostream_type;
00391 
00392     private:
00393       __stringbuf_type  _M_stringbuf;
00394 
00395     public:
00396       // Constructors/destructor:
00411       explicit
00412       basic_ostringstream(ios_base::openmode __mode = ios_base::out)
00413       : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
00414       { this->init(&_M_stringbuf); }
00415 
00431       explicit
00432       basic_ostringstream(const __string_type& __str,
00433               ios_base::openmode __mode = ios_base::out)
00434       : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
00435       { this->init(&_M_stringbuf); }
00436 
00443       ~basic_ostringstream()
00444       { }
00445 
00446       // Members:
00453       __stringbuf_type*
00454       rdbuf() const
00455       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00456 
00461       __string_type
00462       str() const
00463       { return _M_stringbuf.str(); }
00464 
00471       void
00472       str(const __string_type& __s)
00473       { _M_stringbuf.str(__s); }
00474     };
00475 
00476 
00477   // [27.7.4] Template class basic_stringstream
00486   template <typename _CharT, typename _Traits, typename _Alloc>
00487     class basic_stringstream : public basic_iostream<_CharT, _Traits>
00488     {
00489     public:
00490       // Types:
00491       typedef _CharT                    char_type;
00492       typedef _Traits                   traits_type;
00493       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00494       // 251. basic_stringbuf missing allocator_type
00495       typedef _Alloc                        allocator_type;
00496       typedef typename traits_type::int_type        int_type;
00497       typedef typename traits_type::pos_type        pos_type;
00498       typedef typename traits_type::off_type        off_type;
00499 
00500       // Non-standard Types:
00501       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00502       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00503       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00504 
00505     private:
00506       __stringbuf_type  _M_stringbuf;
00507 
00508     public:
00509       // Constructors/destructors
00522       explicit
00523       basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
00524       : __iostream_type(), _M_stringbuf(__m)
00525       { this->init(&_M_stringbuf); }
00526 
00540       explicit
00541       basic_stringstream(const __string_type& __str,
00542              ios_base::openmode __m = ios_base::out | ios_base::in)
00543       : __iostream_type(), _M_stringbuf(__str, __m)
00544       { this->init(&_M_stringbuf); }
00545 
00552       ~basic_stringstream()
00553       { }
00554 
00555       // Members:
00562       __stringbuf_type*
00563       rdbuf() const
00564       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00565 
00570       __string_type
00571       str() const
00572       { return _M_stringbuf.str(); }
00573 
00580       void
00581       str(const __string_type& __s)
00582       { _M_stringbuf.str(__s); }
00583     };
00584 } // namespace std
00585 
00586 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00587 # include <bits/sstream.tcc>
00588 #endif
00589 
00590 #endif /* _GLIBCXX_SSTREAM */

Generated on Tue Feb 2 16:56:34 2010 for GNU C++ STL by  doxygen 1.4.7