# 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, 2009, 2010 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
    00018 // permissions described in the GCC Runtime Library Exception, version
    00019 // 3.1, as published by the Free Software Foundation.
    00020
    00021 // You should have received a copy of the GNU General Public License and
    00022 // a copy of the GCC Runtime Library Exception along with this program;
    00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00024 // <http://www.gnu.org/licenses/>.
    00025
    00026 /** @file sstream
    00027  *  This is a Standard C++ Library header.
    00028  */
    00029
    00030 //
    00031 // ISO C++ 14882: 27.7  String-based streams
    00032 //
    00033
    00034 #ifndef _GLIBCXX_SSTREAM
    00035 #define _GLIBCXX_SSTREAM 1
    00036
    00037 #pragma GCC system_header
    00038
    00039 #include <istream>
    00040 #include <ostream>
    00041
    00042 _GLIBCXX_BEGIN_NAMESPACE(std)
    00043
    00044   // [27.7.1] template class basic_stringbuf
    00045   /**
    00046    *  @brief  The actual work of input and output (for std::string).
    00047    *  @ingroup io
    00048    *
    00049    *  This class associates either or both of its input and output sequences
    00050    *  with a sequence of characters, which can be initialized from, or made
    00051    *  available as, a @c std::basic_string.  (Paraphrased from [27.7.1]/1.)
    00052    *
    00053    *  For this class, open modes (of type @c ios_base::openmode) have
    00054    *  @c in set if the input sequence can be read, and @c out set if the
    00055    *  output sequence can be written.
    00056   */
    00057   template<typename _CharT, typename _Traits, typename _Alloc>
    00058     class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
    00059     {
    00060     public:
    00061       // Types:
    00062       typedef _CharT                    char_type;
    00063       typedef _Traits                   traits_type;
    00064       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00065       // 251. basic_stringbuf missing allocator_type
    00066       typedef _Alloc                        allocator_type;
    00067       typedef typename traits_type::int_type        int_type;
    00068       typedef typename traits_type::pos_type        pos_type;
    00069       typedef typename traits_type::off_type        off_type;
    00070
    00071       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
    00072       typedef basic_string<char_type, _Traits, _Alloc>  __string_type;
    00073       typedef typename __string_type::size_type     __size_type;
    00074
    00075     protected:
    00076       /// Place to stash in || out || in | out settings for current stringbuf.
    00077       ios_base::openmode    _M_mode;
    00078
    00079       // Data Members:
    00080       __string_type         _M_string;
    00081
    00082     public:
    00083       // Constructors:
    00084       /**
    00085        *  @brief  Starts with an empty string buffer.
    00086        *  @param  mode  Whether the buffer can read, or write, or both.
    00087        *
    00088        *  The default constructor initializes the parent class using its
    00089        *  own default ctor.
    00090       */
    00091       explicit
    00092       basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
    00093       : __streambuf_type(), _M_mode(__mode), _M_string()
    00094       { }
    00095
    00096       /**
    00097        *  @brief  Starts with an existing string buffer.
    00098        *  @param  str  A string to copy as a starting buffer.
    00099        *  @param  mode  Whether the buffer can read, or write, or both.
    00100        *
    00101        *  This constructor initializes the parent class using its
    00102        *  own default ctor.
    00103       */
    00104       explicit
    00105       basic_stringbuf(const __string_type& __str,
    00106               ios_base::openmode __mode = ios_base::in | ios_base::out)
    00107       : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
    00108       { _M_stringbuf_init(__mode); }
    00109
    00110       // Get and set:
    00111       /**
    00112        *  @brief  Copying out the string buffer.
    00113        *  @return  A copy of one of the underlying sequences.
    00114        *
    00115        *  <em>If the buffer is only created in input mode, the underlying
    00116        *  character sequence is equal to the input sequence; otherwise, it
    00117        *  is equal to the output sequence.</em> [27.7.1.2]/1
    00118       */
    00119       __string_type
    00120       str() const
    00121       {
    00122     __string_type __ret;
    00123     if (this->pptr())
    00124       {
    00125         // The current egptr() may not be the actual string end.
    00126         if (this->pptr() > this->egptr())
    00127           __ret = __string_type(this->pbase(), this->pptr());
    00128         else
    00129           __ret = __string_type(this->pbase(), this->egptr());
    00130       }
    00131     else
    00132       __ret = _M_string;
    00133     return __ret;
    00134       }
    00135
    00136       /**
    00137        *  @brief  Setting a new buffer.
    00138        *  @param  s  The string to use as a new sequence.
    00139        *
    00140        *  Deallocates any previous stored sequence, then copies @a s to
    00141        *  use as a new one.
    00142       */
    00143       void
    00144       str(const __string_type& __s)
    00145       {
    00146     // Cannot use _M_string = __s, since v3 strings are COW.
    00147     _M_string.assign(__s.data(), __s.size());
    00148     _M_stringbuf_init(_M_mode);
    00149       }
    00150
    00151     protected:
    00152       // Common initialization code goes here.
    00153       void
    00154       _M_stringbuf_init(ios_base::openmode __mode)
    00155       {
    00156     _M_mode = __mode;
    00157     __size_type __len = 0;
    00158     if (_M_mode & (ios_base::ate | ios_base::app))
    00159       __len = _M_string.size();
    00160     _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
    00161       }
    00162
    00163       virtual streamsize
    00164       showmanyc()
    00165       {
    00166     streamsize __ret = -1;
    00167     if (_M_mode & ios_base::in)
    00168       {
    00169         _M_update_egptr();
    00170         __ret = this->egptr() - this->gptr();
    00171       }
    00172     return __ret;
    00173       }
    00174
    00175       virtual int_type
    00176       underflow();
    00177
    00178       virtual int_type
    00179       pbackfail(int_type __c = traits_type::eof());
    00180
    00181       virtual int_type
    00182       overflow(int_type __c = traits_type::eof());
    00183
    00184       /**
    00185        *  @brief  Manipulates the buffer.
    00186        *  @param  s  Pointer to a buffer area.
    00187        *  @param  n  Size of @a s.
    00188        *  @return  @c this
    00189        *
    00190        *  If no buffer has already been created, and both @a s and @a n are
    00191        *  non-zero, then @c s is used as a buffer; see
    00192        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
    00193        *  for more.
    00194       */
    00195       virtual __streambuf_type*
    00196       setbuf(char_type* __s, streamsize __n)
    00197       {
    00198     if (__s && __n >= 0)
    00199       {
    00200         // This is implementation-defined behavior, and assumes
    00201         // that an external char_type array of length __n exists
    00202         // and has been pre-allocated. If this is not the case,
    00203         // things will quickly blow up.
    00204
    00205         // Step 1: Destroy the current internal array.
    00206         _M_string.clear();
    00207
    00208         // Step 2: Use the external array.
    00209         _M_sync(__s, __n, 0);
    00210       }
    00211     return this;
    00212       }
    00213
    00214       virtual pos_type
    00215       seekoff(off_type __off, ios_base::seekdir __way,
    00216           ios_base::openmode __mode = ios_base::in | ios_base::out);
    00217
    00218       virtual pos_type
    00219       seekpos(pos_type __sp,
    00220           ios_base::openmode __mode = ios_base::in | ios_base::out);
    00221
    00222       // Internal function for correctly updating the internal buffer
    00223       // for a particular _M_string, due to initialization or re-sizing
    00224       // of an existing _M_string.
    00225       void
    00226       _M_sync(char_type* __base, __size_type __i, __size_type __o);
    00227
    00228       // Internal function for correctly updating egptr() to the actual
    00229       // string end.
    00230       void
    00231       _M_update_egptr()
    00232       {
    00233     const bool __testin = _M_mode & ios_base::in;
    00234     if (this->pptr() && this->pptr() > this->egptr())
    00235       {
    00236         if (__testin)
    00237           this->setg(this->eback(), this->gptr(), this->pptr());
    00238         else
    00239           this->setg(this->pptr(), this->pptr(), this->pptr());
    00240       }
    00241       }
    00242     };
    00243
    00244
    00245   // [27.7.2] Template class basic_istringstream
    00246   /**
    00247    *  @brief  Controlling input for std::string.
    00248    *  @ingroup io
    00249    *
    00250    *  This class supports reading from objects of type std::basic_string,
    00251    *  using the inherited functions from std::basic_istream.  To control
    00252    *  the associated sequence, an instance of std::basic_stringbuf is used,
    00253    *  which this page refers to as @c sb.
    00254   */
    00255   template<typename _CharT, typename _Traits, typename _Alloc>
    00256     class basic_istringstream : public basic_istream<_CharT, _Traits>
    00257     {
    00258     public:
    00259       // Types:
    00260       typedef _CharT                    char_type;
    00261       typedef _Traits                   traits_type;
    00262       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00263       // 251. basic_stringbuf missing allocator_type
    00264       typedef _Alloc                        allocator_type;
    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_istream<char_type, traits_type> __istream_type;
    00273
    00274     private:
    00275       __stringbuf_type  _M_stringbuf;
    00276
    00277     public:
    00278       // Constructors:
    00279       /**
    00280        *  @brief  Default constructor starts with an empty string buffer.
    00281        *  @param  mode  Whether the buffer can read, or write, or both.
    00282        *
    00283        *  @c ios_base::in is automatically included in @a mode.
    00284        *
    00285        *  Initializes @c sb using @c mode|in, and passes @c &sb to the base
    00286        *  class initializer.  Does not allocate any buffer.
    00287        *
    00288        *  That's a lie.  We initialize the base class with NULL, because the
    00289        *  string class does its own memory management.
    00290       */
    00291       explicit
    00292       basic_istringstream(ios_base::openmode __mode = ios_base::in)
    00293       : __istream_type(), _M_stringbuf(__mode | ios_base::in)
    00294       { this->init(&_M_stringbuf); }
    00295
    00296       /**
    00297        *  @brief  Starts with an existing string buffer.
    00298        *  @param  str  A string to copy as a starting buffer.
    00299        *  @param  mode  Whether the buffer can read, or write, or both.
    00300        *
    00301        *  @c ios_base::in is automatically included in @a mode.
    00302        *
    00303        *  Initializes @c sb using @a str and @c mode|in, and passes @c &sb
    00304        *  to the base class initializer.
    00305        *
    00306        *  That's a lie.  We initialize the base class with NULL, because the
    00307        *  string class does its own memory management.
    00308       */
    00309       explicit
    00310       basic_istringstream(const __string_type& __str,
    00311               ios_base::openmode __mode = ios_base::in)
    00312       : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
    00313       { this->init(&_M_stringbuf); }
    00314
    00315       /**
    00316        *  @brief  The destructor does nothing.
    00317        *
    00318        *  The buffer is deallocated by the stringbuf object, not the
    00319        *  formatting stream.
    00320       */
    00321       ~basic_istringstream()
    00322       { }
    00323
    00324       // Members:
    00325       /**
    00326        *  @brief  Accessing the underlying buffer.
    00327        *  @return  The current basic_stringbuf buffer.
    00328        *
    00329        *  This hides both signatures of std::basic_ios::rdbuf().
    00330       */
    00331       __stringbuf_type*
    00332       rdbuf() const
    00333       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
    00334
    00335       /**
    00336        *  @brief  Copying out the string buffer.
    00337        *  @return  @c rdbuf()->str()
    00338       */
    00339       __string_type
    00340       str() const
    00341       { return _M_stringbuf.str(); }
    00342
    00343       /**
    00344        *  @brief  Setting a new buffer.
    00345        *  @param  s  The string to use as a new sequence.
    00346        *
    00347        *  Calls @c rdbuf()->str(s).
    00348       */
    00349       void
    00350       str(const __string_type& __s)
    00351       { _M_stringbuf.str(__s); }
    00352     };
    00353
    00354
    00355   // [27.7.3] Template class basic_ostringstream
    00356   /**
    00357    *  @brief  Controlling output for std::string.
    00358    *  @ingroup io
    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    *  @ingroup io
    00469    *
    00470    *  This class supports reading from and writing to objects of type
    00471    *  std::basic_string, using the inherited functions from
    00472    *  std::basic_iostream.  To control the associated sequence, an instance
    00473    *  of std::basic_stringbuf is used, which this page refers to as @c sb.
    00474   */
    00475   template <typename _CharT, typename _Traits, typename _Alloc>
    00476     class basic_stringstream : public basic_iostream<_CharT, _Traits>
    00477     {
    00478     public:
    00479       // Types:
    00480       typedef _CharT                    char_type;
    00481       typedef _Traits                   traits_type;
    00482       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00483       // 251. basic_stringbuf missing allocator_type
    00484       typedef _Alloc                        allocator_type;
    00485       typedef typename traits_type::int_type        int_type;
    00486       typedef typename traits_type::pos_type        pos_type;
    00487       typedef typename traits_type::off_type        off_type;
    00488
    00489       // Non-standard Types:
    00490       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
    00491       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
    00492       typedef basic_iostream<char_type, traits_type>    __iostream_type;
    00493
    00494     private:
    00495       __stringbuf_type  _M_stringbuf;
    00496
    00497     public:
    00498       // Constructors/destructors
    00499       /**
    00500        *  @brief  Default constructor starts with an empty string buffer.
    00501        *  @param  mode  Whether the buffer can read, or write, or both.
    00502        *
    00503        *  Initializes @c sb using @c mode, and passes @c &sb to the base
    00504        *  class initializer.  Does not allocate any buffer.
    00505        *
    00506        *  That's a lie.  We initialize the base class with NULL, because the
    00507        *  string class does its own memory management.
    00508       */
    00509       explicit
    00510       basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
    00511       : __iostream_type(), _M_stringbuf(__m)
    00512       { this->init(&_M_stringbuf); }
    00513
    00514       /**
    00515        *  @brief  Starts with an existing string buffer.
    00516        *  @param  str  A string to copy as a starting buffer.
    00517        *  @param  mode  Whether the buffer can read, or write, or both.
    00518        *
    00519        *  Initializes @c sb using @a str and @c mode, and passes @c &sb
    00520        *  to the base class initializer.
    00521        *
    00522        *  That's a lie.  We initialize the base class with NULL, because the
    00523        *  string class does its own memory management.
    00524       */
    00525       explicit
    00526       basic_stringstream(const __string_type& __str,
    00527              ios_base::openmode __m = ios_base::out | ios_base::in)
    00528       : __iostream_type(), _M_stringbuf(__str, __m)
    00529       { this->init(&_M_stringbuf); }
    00530
    00531       /**
    00532        *  @brief  The destructor does nothing.
    00533        *
    00534        *  The buffer is deallocated by the stringbuf object, not the
    00535        *  formatting stream.
    00536       */
    00537       ~basic_stringstream()
    00538       { }
    00539
    00540       // Members:
    00541       /**
    00542        *  @brief  Accessing the underlying buffer.
    00543        *  @return  The current basic_stringbuf buffer.
    00544        *
    00545        *  This hides both signatures of std::basic_ios::rdbuf().
    00546       */
    00547       __stringbuf_type*
    00548       rdbuf() const
    00549       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
    00550
    00551       /**
    00552        *  @brief  Copying out the string buffer.
    00553        *  @return  @c rdbuf()->str()
    00554       */
    00555       __string_type
    00556       str() const
    00557       { return _M_stringbuf.str(); }
    00558
    00559       /**
    00560        *  @brief  Setting a new buffer.
    00561        *  @param  s  The string to use as a new sequence.
    00562        *
    00563        *  Calls @c rdbuf()->str(s).
    00564       */
    00565       void
    00566       str(const __string_type& __s)
    00567       { _M_stringbuf.str(__s); }
    00568     };
    00569
    00570 _GLIBCXX_END_NAMESPACE
    00571
    00572 #ifndef _GLIBCXX_EXPORT_TEMPLATE
    00573 # include <bits/sstream.tcc>
    00574 #endif
    00575
    00576 #endif /* _GLIBCXX_SSTREAM */