# 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,
    00004 // 2006, 2008 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
    00018 // along with this library; see the file COPYING.  If not, write to
    00019 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
    00020 // Boston, MA 02110-1301, 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 /** @file sstream
    00032  *  This is a Standard C++ Library header.
    00033  */
    00034
    00035 //
    00036 // ISO C++ 14882: 27.7  String-based streams
    00037 //
    00038
    00039 #ifndef _GLIBCXX_SSTREAM
    00040 #define _GLIBCXX_SSTREAM 1
    00041
    00042 #pragma GCC system_header
    00043
    00044 #include <istream>
    00045 #include <ostream>
    00046
    00047 _GLIBCXX_BEGIN_NAMESPACE(std)
    00048
    00049   // [27.7.1] template class basic_stringbuf
    00050   /**
    00051    *  @brief  The actual work of input and output (for std::string).
    00052    *
    00053    *  This class associates either or both of its input and output sequences
    00054    *  with a sequence of characters, which can be initialized from, or made
    00055    *  available as, a @c std::basic_string.  (Paraphrased from [27.7.1]/1.)
    00056    *
    00057    *  For this class, open modes (of type @c ios_base::openmode) have
    00058    *  @c in set if the input sequence can be read, and @c out set if the
    00059    *  output sequence can be written.
    00060   */
    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:
    00080       /// Place to stash in || out || in | out settings for current stringbuf.
    00081       ios_base::openmode    _M_mode;
    00082
    00083       // Data Members:
    00084       __string_type         _M_string;
    00085
    00086     public:
    00087       // Constructors:
    00088       /**
    00089        *  @brief  Starts with an empty string buffer.
    00090        *  @param  mode  Whether the buffer can read, or write, or both.
    00091        *
    00092        *  The default constructor initializes the parent class using its
    00093        *  own default ctor.
    00094       */
    00095       explicit
    00096       basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
    00097       : __streambuf_type(), _M_mode(__mode), _M_string()
    00098       { }
    00099
    00100       /**
    00101        *  @brief  Starts with an existing string buffer.
    00102        *  @param  str  A string to copy as a starting buffer.
    00103        *  @param  mode  Whether the buffer can read, or write, or both.
    00104        *
    00105        *  This constructor initializes the parent class using its
    00106        *  own default ctor.
    00107       */
    00108       explicit
    00109       basic_stringbuf(const __string_type& __str,
    00110               ios_base::openmode __mode = ios_base::in | ios_base::out)
    00111       : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
    00112       { _M_stringbuf_init(__mode); }
    00113
    00114       // Get and set:
    00115       /**
    00116        *  @brief  Copying out the string buffer.
    00117        *  @return  A copy of one of the underlying sequences.
    00118        *
    00119        *  "If the buffer is only created in input mode, the underlying
    00120        *  character sequence is equal to the input sequence; otherwise, it
    00121        *  is equal to the output sequence." [27.7.1.2]/1
    00122       */
    00123       __string_type
    00124       str() const
    00125       {
    00126     __string_type __ret;
    00127     if (this->pptr())
    00128       {
    00129         // The current egptr() may not be the actual string end.
    00130         if (this->pptr() > this->egptr())
    00131           __ret = __string_type(this->pbase(), this->pptr());
    00132         else
    00133           __ret = __string_type(this->pbase(), this->egptr());
    00134       }
    00135     else
    00136       __ret = _M_string;
    00137     return __ret;
    00138       }
    00139
    00140       /**
    00141        *  @brief  Setting a new buffer.
    00142        *  @param  s  The string to use as a new sequence.
    00143        *
    00144        *  Deallocates any previous stored sequence, then copies @a s to
    00145        *  use as a new one.
    00146       */
    00147       void
    00148       str(const __string_type& __s)
    00149       {
    00150     // Cannot use _M_string = __s, since v3 strings are COW.
    00151     _M_string.assign(__s.data(), __s.size());
    00152     _M_stringbuf_init(_M_mode);
    00153       }
    00154
    00155     protected:
    00156       // Common initialization code goes here.
    00157       void
    00158       _M_stringbuf_init(ios_base::openmode __mode)
    00159       {
    00160     _M_mode = __mode;
    00161     __size_type __len = 0;
    00162     if (_M_mode & (ios_base::ate | ios_base::app))
    00163       __len = _M_string.size();
    00164     _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
    00165       }
    00166
    00167       virtual streamsize
    00168       showmanyc()
    00169       {
    00170     streamsize __ret = -1;
    00171     if (_M_mode & ios_base::in)
    00172       {
    00173         _M_update_egptr();
    00174         __ret = this->egptr() - this->gptr();
    00175       }
    00176     return __ret;
    00177       }
    00178
    00179       virtual int_type
    00180       underflow();
    00181
    00182       virtual int_type
    00183       pbackfail(int_type __c = traits_type::eof());
    00184
    00185       virtual int_type
    00186       overflow(int_type __c = traits_type::eof());
    00187
    00188       /**
    00189        *  @brief  Manipulates the buffer.
    00190        *  @param  s  Pointer to a buffer area.
    00191        *  @param  n  Size of @a s.
    00192        *  @return  @c this
    00193        *
    00194        *  If no buffer has already been created, and both @a s and @a n are
    00195        *  non-zero, then @c s is used as a buffer; see
    00196        *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
    00197        *  for more.
    00198       */
    00199       virtual __streambuf_type*
    00200       setbuf(char_type* __s, streamsize __n)
    00201       {
    00202     if (__s && __n >= 0)
    00203       {
    00204         // This is implementation-defined behavior, and assumes
    00205         // that an external char_type array of length __n exists
    00206         // and has been pre-allocated. If this is not the case,
    00207         // things will quickly blow up.
    00208
    00209         // Step 1: Destroy the current internal array.
    00210         _M_string.clear();
    00211
    00212         // Step 2: Use the external array.
    00213         _M_sync(__s, __n, 0);
    00214       }
    00215     return this;
    00216       }
    00217
    00218       virtual pos_type
    00219       seekoff(off_type __off, ios_base::seekdir __way,
    00220           ios_base::openmode __mode = ios_base::in | ios_base::out);
    00221
    00222       virtual pos_type
    00223       seekpos(pos_type __sp,
    00224           ios_base::openmode __mode = ios_base::in | ios_base::out);
    00225
    00226       // Internal function for correctly updating the internal buffer
    00227       // for a particular _M_string, due to initialization or re-sizing
    00228       // of an existing _M_string.
    00229       void
    00230       _M_sync(char_type* __base, __size_type __i, __size_type __o);
    00231
    00232       // Internal function for correctly updating egptr() to the actual
    00233       // string end.
    00234       void
    00235       _M_update_egptr()
    00236       {
    00237     const bool __testin = _M_mode & ios_base::in;
    00238     if (this->pptr() && this->pptr() > this->egptr())
    00239       if (__testin)
    00240         this->setg(this->eback(), this->gptr(), this->pptr());
    00241       else
    00242         this->setg(this->pptr(), this->pptr(), this->pptr());
    00243       }
    00244     };
    00245
    00246
    00247   // [27.7.2] Template class basic_istringstream
    00248   /**
    00249    *  @brief  Controlling input for std::string.
    00250    *
    00251    *  This class supports reading from objects of type std::basic_string,
    00252    *  using the inherited functions from std::basic_istream.  To control
    00253    *  the associated sequence, an instance of std::basic_stringbuf is used,
    00254    *  which this page refers to as @c sb.
    00255   */
    00256   template<typename _CharT, typename _Traits, typename _Alloc>
    00257     class basic_istringstream : public basic_istream<_CharT, _Traits>
    00258     {
    00259     public:
    00260       // Types:
    00261       typedef _CharT                    char_type;
    00262       typedef _Traits                   traits_type;
    00263       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00264       // 251. basic_stringbuf missing allocator_type
    00265       typedef _Alloc                        allocator_type;
    00266       typedef typename traits_type::int_type        int_type;
    00267       typedef typename traits_type::pos_type        pos_type;
    00268       typedef typename traits_type::off_type        off_type;
    00269
    00270       // Non-standard types:
    00271       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
    00272       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
    00273       typedef basic_istream<char_type, traits_type> __istream_type;
    00274
    00275     private:
    00276       __stringbuf_type  _M_stringbuf;
    00277
    00278     public:
    00279       // Constructors:
    00280       /**
    00281        *  @brief  Default constructor starts with an empty string buffer.
    00282        *  @param  mode  Whether the buffer can read, or write, or both.
    00283        *
    00284        *  @c ios_base::in is automatically included in @a mode.
    00285        *
    00286        *  Initializes @c sb using @c mode|in, and passes @c &sb to the base
    00287        *  class initializer.  Does not allocate any buffer.
    00288        *
    00289        *  That's a lie.  We initialize the base class with NULL, because the
    00290        *  string class does its own memory management.
    00291       */
    00292       explicit
    00293       basic_istringstream(ios_base::openmode __mode = ios_base::in)
    00294       : __istream_type(), _M_stringbuf(__mode | ios_base::in)
    00295       { this->init(&_M_stringbuf); }
    00296
    00297       /**
    00298        *  @brief  Starts with an existing string buffer.
    00299        *  @param  str  A string to copy as a starting buffer.
    00300        *  @param  mode  Whether the buffer can read, or write, or both.
    00301        *
    00302        *  @c ios_base::in is automatically included in @a mode.
    00303        *
    00304        *  Initializes @c sb using @a str and @c mode|in, and passes @c &sb
    00305        *  to the base class initializer.
    00306        *
    00307        *  That's a lie.  We initialize the base class with NULL, because the
    00308        *  string class does its own memory management.
    00309       */
    00310       explicit
    00311       basic_istringstream(const __string_type& __str,
    00312               ios_base::openmode __mode = ios_base::in)
    00313       : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
    00314       { this->init(&_M_stringbuf); }
    00315
    00316       /**
    00317        *  @brief  The destructor does nothing.
    00318        *
    00319        *  The buffer is deallocated by the stringbuf object, not the
    00320        *  formatting stream.
    00321       */
    00322       ~basic_istringstream()
    00323       { }
    00324
    00325       // Members:
    00326       /**
    00327        *  @brief  Accessing the underlying buffer.
    00328        *  @return  The current basic_stringbuf buffer.
    00329        *
    00330        *  This hides both signatures of std::basic_ios::rdbuf().
    00331       */
    00332       __stringbuf_type*
    00333       rdbuf() const
    00334       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
    00335
    00336       /**
    00337        *  @brief  Copying out the string buffer.
    00338        *  @return  @c rdbuf()->str()
    00339       */
    00340       __string_type
    00341       str() const
    00342       { return _M_stringbuf.str(); }
    00343
    00344       /**
    00345        *  @brief  Setting a new buffer.
    00346        *  @param  s  The string to use as a new sequence.
    00347        *
    00348        *  Calls @c rdbuf()->str(s).
    00349       */
    00350       void
    00351       str(const __string_type& __s)
    00352       { _M_stringbuf.str(__s); }
    00353     };
    00354
    00355
    00356   // [27.7.3] Template class basic_ostringstream
    00357   /**
    00358    *  @brief  Controlling output for std::string.
    00359    *
    00360    *  This class supports writing to objects of type std::basic_string,
    00361    *  using the inherited functions from std::basic_ostream.  To control
    00362    *  the associated sequence, an instance of std::basic_stringbuf is used,
    00363    *  which this page refers to as @c sb.
    00364   */
    00365   template <typename _CharT, typename _Traits, typename _Alloc>
    00366     class basic_ostringstream : public basic_ostream<_CharT, _Traits>
    00367     {
    00368     public:
    00369       // Types:
    00370       typedef _CharT                    char_type;
    00371       typedef _Traits                   traits_type;
    00372       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00373       // 251. basic_stringbuf missing allocator_type
    00374       typedef _Alloc                        allocator_type;
    00375       typedef typename traits_type::int_type        int_type;
    00376       typedef typename traits_type::pos_type        pos_type;
    00377       typedef typename traits_type::off_type        off_type;
    00378
    00379       // Non-standard types:
    00380       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
    00381       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
    00382       typedef basic_ostream<char_type, traits_type> __ostream_type;
    00383
    00384     private:
    00385       __stringbuf_type  _M_stringbuf;
    00386
    00387     public:
    00388       // Constructors/destructor:
    00389       /**
    00390        *  @brief  Default constructor starts with an empty string buffer.
    00391        *  @param  mode  Whether the buffer can read, or write, or both.
    00392        *
    00393        *  @c ios_base::out is automatically included in @a mode.
    00394        *
    00395        *  Initializes @c sb using @c mode|out, and passes @c &sb to the base
    00396        *  class initializer.  Does not allocate any buffer.
    00397        *
    00398        *  That's a lie.  We initialize the base class with NULL, because the
    00399        *  string class does its own memory management.
    00400       */
    00401       explicit
    00402       basic_ostringstream(ios_base::openmode __mode = ios_base::out)
    00403       : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
    00404       { this->init(&_M_stringbuf); }
    00405
    00406       /**
    00407        *  @brief  Starts with an existing string buffer.
    00408        *  @param  str  A string to copy as a starting buffer.
    00409        *  @param  mode  Whether the buffer can read, or write, or both.
    00410        *
    00411        *  @c ios_base::out is automatically included in @a mode.
    00412        *
    00413        *  Initializes @c sb using @a str and @c mode|out, and passes @c &sb
    00414        *  to the base class initializer.
    00415        *
    00416        *  That's a lie.  We initialize the base class with NULL, because the
    00417        *  string class does its own memory management.
    00418       */
    00419       explicit
    00420       basic_ostringstream(const __string_type& __str,
    00421               ios_base::openmode __mode = ios_base::out)
    00422       : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
    00423       { this->init(&_M_stringbuf); }
    00424
    00425       /**
    00426        *  @brief  The destructor does nothing.
    00427        *
    00428        *  The buffer is deallocated by the stringbuf object, not the
    00429        *  formatting stream.
    00430       */
    00431       ~basic_ostringstream()
    00432       { }
    00433
    00434       // Members:
    00435       /**
    00436        *  @brief  Accessing the underlying buffer.
    00437        *  @return  The current basic_stringbuf buffer.
    00438        *
    00439        *  This hides both signatures of std::basic_ios::rdbuf().
    00440       */
    00441       __stringbuf_type*
    00442       rdbuf() const
    00443       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
    00444
    00445       /**
    00446        *  @brief  Copying out the string buffer.
    00447        *  @return  @c rdbuf()->str()
    00448       */
    00449       __string_type
    00450       str() const
    00451       { return _M_stringbuf.str(); }
    00452
    00453       /**
    00454        *  @brief  Setting a new buffer.
    00455        *  @param  s  The string to use as a new sequence.
    00456        *
    00457        *  Calls @c rdbuf()->str(s).
    00458       */
    00459       void
    00460       str(const __string_type& __s)
    00461       { _M_stringbuf.str(__s); }
    00462     };
    00463
    00464
    00465   // [27.7.4] Template class basic_stringstream
    00466   /**
    00467    *  @brief  Controlling input and output for std::string.
    00468    *
    00469    *  This class supports reading from and writing to objects of type
    00470    *  std::basic_string, using the inherited functions from
    00471    *  std::basic_iostream.  To control the associated sequence, an instance
    00472    *  of std::basic_stringbuf is used, which this page refers to as @c sb.
    00473   */
    00474   template <typename _CharT, typename _Traits, typename _Alloc>
    00475     class basic_stringstream : public basic_iostream<_CharT, _Traits>
    00476     {
    00477     public:
    00478       // Types:
    00479       typedef _CharT                    char_type;
    00480       typedef _Traits                   traits_type;
    00481       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00482       // 251. basic_stringbuf missing allocator_type
    00483       typedef _Alloc                        allocator_type;
    00484       typedef typename traits_type::int_type        int_type;
    00485       typedef typename traits_type::pos_type        pos_type;
    00486       typedef typename traits_type::off_type        off_type;
    00487
    00488       // Non-standard Types:
    00489       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
    00490       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
    00491       typedef basic_iostream<char_type, traits_type>    __iostream_type;
    00492
    00493     private:
    00494       __stringbuf_type  _M_stringbuf;
    00495
    00496     public:
    00497       // Constructors/destructors
    00498       /**
    00499        *  @brief  Default constructor starts with an empty string buffer.
    00500        *  @param  mode  Whether the buffer can read, or write, or both.
    00501        *
    00502        *  Initializes @c sb using @c mode, and passes @c &sb to the base
    00503        *  class initializer.  Does not allocate any buffer.
    00504        *
    00505        *  That's a lie.  We initialize the base class with NULL, because the
    00506        *  string class does its own memory management.
    00507       */
    00508       explicit
    00509       basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
    00510       : __iostream_type(), _M_stringbuf(__m)
    00511       { this->init(&_M_stringbuf); }
    00512
    00513       /**
    00514        *  @brief  Starts with an existing string buffer.
    00515        *  @param  str  A string to copy as a starting buffer.
    00516        *  @param  mode  Whether the buffer can read, or write, or both.
    00517        *
    00518        *  Initializes @c sb using @a str and @c mode, and passes @c &sb
    00519        *  to the base class initializer.
    00520        *
    00521        *  That's a lie.  We initialize the base class with NULL, because the
    00522        *  string class does its own memory management.
    00523       */
    00524       explicit
    00525       basic_stringstream(const __string_type& __str,
    00526              ios_base::openmode __m = ios_base::out | ios_base::in)
    00527       : __iostream_type(), _M_stringbuf(__str, __m)
    00528       { this->init(&_M_stringbuf); }
    00529
    00530       /**
    00531        *  @brief  The destructor does nothing.
    00532        *
    00533        *  The buffer is deallocated by the stringbuf object, not the
    00534        *  formatting stream.
    00535       */
    00536       ~basic_stringstream()
    00537       { }
    00538
    00539       // Members:
    00540       /**
    00541        *  @brief  Accessing the underlying buffer.
    00542        *  @return  The current basic_stringbuf buffer.
    00543        *
    00544        *  This hides both signatures of std::basic_ios::rdbuf().
    00545       */
    00546       __stringbuf_type*
    00547       rdbuf() const
    00548       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
    00549
    00550       /**
    00551        *  @brief  Copying out the string buffer.
    00552        *  @return  @c rdbuf()->str()
    00553       */
    00554       __string_type
    00555       str() const
    00556       { return _M_stringbuf.str(); }
    00557
    00558       /**
    00559        *  @brief  Setting a new buffer.
    00560        *  @param  s  The string to use as a new sequence.
    00561        *
    00562        *  Calls @c rdbuf()->str(s).
    00563       */
    00564       void
    00565       str(const __string_type& __s)
    00566       { _M_stringbuf.str(__s); }
    00567     };
    00568
    00569 _GLIBCXX_END_NAMESPACE
    00570
    00571 #ifndef _GLIBCXX_EXPORT_TEMPLATE
    00572 # include <bits/sstream.tcc>
    00573 #endif
    00574
    00575 #endif /* _GLIBCXX_SSTREAM */