sstream

Go to the documentation of this file.
00001 // String based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2002, 2003, 2004, 2005
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 
00035 /** @file sstream
00036  *  This is a Standard C++ Library header.
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 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       /**
00081        *  @if maint
00082        *  Place to stash in || out || in | out settings for current stringbuf.
00083        *  @endif
00084       */
00085       ios_base::openmode    _M_mode;
00086 
00087       // Data Members:
00088       __string_type         _M_string;
00089 
00090     public:
00091       // Constructors:
00092       /**
00093        *  @brief  Starts with an empty string buffer.
00094        *  @param  mode  Whether the buffer can read, or write, or both.
00095        *
00096        *  The default constructor initializes the parent class using its
00097        *  own default ctor.
00098       */
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 
00104       /**
00105        *  @brief  Starts with an existing string buffer.
00106        *  @param  str  A string to copy as a starting buffer.
00107        *  @param  mode  Whether the buffer can read, or write, or both.
00108        *
00109        *  This constructor initializes the parent class using its
00110        *  own default ctor.
00111       */
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:
00119       /**
00120        *  @brief  Copying out the string buffer.
00121        *  @return  A copy of one of the underlying sequences.
00122        *
00123        *  "If the buffer is only created in input mode, the underlying
00124        *  character sequence is equal to the input sequence; otherwise, it
00125        *  is equal to the output sequence." [27.7.1.2]/1
00126       */
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 
00144       /**
00145        *  @brief  Setting a new buffer.
00146        *  @param  s  The string to use as a new sequence.
00147        *
00148        *  Deallocates any previous stored sequence, then copies @a s to
00149        *  use as a new one.
00150       */
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 
00192       /**
00193        *  @brief  Manipulates the buffer.
00194        *  @param  s  Pointer to a buffer area.
00195        *  @param  n  Size of @a s.
00196        *  @return  @c this
00197        *
00198        *  If no buffer has already been created, and both @a s and @a n are
00199        *  non-zero, then @c s is used as a buffer; see
00200        *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
00201        *  for more.
00202       */
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.assign(__s, __n);
00215         
00216         // Step 2: Use the external array.
00217         _M_sync(__s, 0, 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       // Assumes: contents of _M_string and internal buffer match exactly.
00234       // __i == _M_in_cur - _M_in_beg
00235       // __o == _M_out_cur - _M_out_beg
00236       void
00237       _M_sync(char_type* __base, __size_type __i, __size_type __o)
00238       {
00239     const bool __testin = _M_mode & ios_base::in;
00240     const bool __testout = _M_mode & ios_base::out;
00241     char_type* __end = __base + _M_string.size();
00242 
00243     if (__testin)
00244       this->setg(__base, __base + __i, __end);
00245     if (__testout)
00246       {
00247         // If __base comes from setbuf we cannot trust capacity()
00248         // to match the size of the buffer area: in general, after
00249         // Step 1 above, _M_string.capacity() >= __n.
00250         if (__base == _M_string.data())
00251           this->setp(__base, __base + _M_string.capacity());
00252         else
00253           this->setp(__base, __end);
00254         this->pbump(__o);
00255         // egptr() always tracks the string end. When !__testin,
00256         // for the correct functioning of the streambuf inlines
00257         // the other get area pointers are identical.
00258         if (!__testin)
00259           this->setg(__end, __end, __end);
00260       }
00261       }
00262 
00263       // Internal function for correctly updating egptr() to the actual
00264       // string end.
00265       void
00266       _M_update_egptr()
00267       {
00268     const bool __testin = _M_mode & ios_base::in;
00269     if (this->pptr() && this->pptr() > this->egptr())
00270       if (__testin)
00271         this->setg(this->eback(), this->gptr(), this->pptr());
00272       else
00273         this->setg(this->pptr(), this->pptr(), this->pptr());
00274       }
00275     };
00276 
00277 
00278   // [27.7.2] Template class basic_istringstream
00279   /**
00280    *  @brief  Controlling input for std::string.
00281    *
00282    *  This class supports reading from objects of type std::basic_string,
00283    *  using the inherited functions from std::basic_istream.  To control
00284    *  the associated sequence, an instance of std::basic_stringbuf is used,
00285    *  which this page refers to as @c sb.
00286   */
00287   template<typename _CharT, typename _Traits, typename _Alloc>
00288     class basic_istringstream : public basic_istream<_CharT, _Traits>
00289     {
00290     public:
00291       // Types:
00292       typedef _CharT                    char_type;
00293       typedef _Traits                   traits_type;
00294       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00295       // 251. basic_stringbuf missing allocator_type
00296       typedef _Alloc                        allocator_type;
00297       typedef typename traits_type::int_type        int_type;
00298       typedef typename traits_type::pos_type        pos_type;
00299       typedef typename traits_type::off_type        off_type;
00300 
00301       // Non-standard types:
00302       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00303       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00304       typedef basic_istream<char_type, traits_type> __istream_type;
00305 
00306     private:
00307       __stringbuf_type  _M_stringbuf;
00308 
00309     public:
00310       // Constructors:
00311       /**
00312        *  @brief  Default constructor starts with an empty string buffer.
00313        *  @param  mode  Whether the buffer can read, or write, or both.
00314        *
00315        *  @c ios_base::in is automatically included in @a mode.
00316        *
00317        *  Initializes @c sb using @c mode|in, and passes @c &sb to the base
00318        *  class initializer.  Does not allocate any buffer.
00319        *
00320        *  @if maint
00321        *  That's a lie.  We initialize the base class with NULL, because the
00322        *  string class does its own memory management.
00323        *  @endif
00324       */
00325       explicit
00326       basic_istringstream(ios_base::openmode __mode = ios_base::in)
00327       : __istream_type(), _M_stringbuf(__mode | ios_base::in)
00328       { this->init(&_M_stringbuf); }
00329 
00330       /**
00331        *  @brief  Starts with an existing string buffer.
00332        *  @param  str  A string to copy as a starting buffer.
00333        *  @param  mode  Whether the buffer can read, or write, or both.
00334        *
00335        *  @c ios_base::in is automatically included in @a mode.
00336        *
00337        *  Initializes @c sb using @a str and @c mode|in, and passes @c &sb
00338        *  to the base class initializer.
00339        *
00340        *  @if maint
00341        *  That's a lie.  We initialize the base class with NULL, because the
00342        *  string class does its own memory management.
00343        *  @endif
00344       */
00345       explicit
00346       basic_istringstream(const __string_type& __str,
00347               ios_base::openmode __mode = ios_base::in)
00348       : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
00349       { this->init(&_M_stringbuf); }
00350 
00351       /**
00352        *  @brief  The destructor does nothing.
00353        *
00354        *  The buffer is deallocated by the stringbuf object, not the
00355        *  formatting stream.
00356       */
00357       ~basic_istringstream()
00358       { }
00359 
00360       // Members:
00361       /**
00362        *  @brief  Accessing the underlying buffer.
00363        *  @return  The current basic_stringbuf buffer.
00364        *
00365        *  This hides both signatures of std::basic_ios::rdbuf().
00366       */
00367       __stringbuf_type*
00368       rdbuf() const
00369       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00370 
00371       /**
00372        *  @brief  Copying out the string buffer.
00373        *  @return  @c rdbuf()->str()
00374       */
00375       __string_type
00376       str() const
00377       { return _M_stringbuf.str(); }
00378 
00379       /**
00380        *  @brief  Setting a new buffer.
00381        *  @param  s  The string to use as a new sequence.
00382        *
00383        *  Calls @c rdbuf()->str(s).
00384       */
00385       void
00386       str(const __string_type& __s)
00387       { _M_stringbuf.str(__s); }
00388     };
00389 
00390 
00391   // [27.7.3] Template class basic_ostringstream
00392   /**
00393    *  @brief  Controlling output for std::string.
00394    *
00395    *  This class supports writing to objects of type std::basic_string,
00396    *  using the inherited functions from std::basic_ostream.  To control
00397    *  the associated sequence, an instance of std::basic_stringbuf is used,
00398    *  which this page refers to as @c sb.
00399   */
00400   template <typename _CharT, typename _Traits, typename _Alloc>
00401     class basic_ostringstream : public basic_ostream<_CharT, _Traits>
00402     {
00403     public:
00404       // Types:
00405       typedef _CharT                    char_type;
00406       typedef _Traits                   traits_type;
00407       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00408       // 251. basic_stringbuf missing allocator_type
00409       typedef _Alloc                        allocator_type;
00410       typedef typename traits_type::int_type        int_type;
00411       typedef typename traits_type::pos_type        pos_type;
00412       typedef typename traits_type::off_type        off_type;
00413 
00414       // Non-standard types:
00415       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00416       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00417       typedef basic_ostream<char_type, traits_type> __ostream_type;
00418 
00419     private:
00420       __stringbuf_type  _M_stringbuf;
00421 
00422     public:
00423       // Constructors/destructor:
00424       /**
00425        *  @brief  Default constructor starts with an empty string buffer.
00426        *  @param  mode  Whether the buffer can read, or write, or both.
00427        *
00428        *  @c ios_base::out is automatically included in @a mode.
00429        *
00430        *  Initializes @c sb using @c mode|out, and passes @c &sb to the base
00431        *  class initializer.  Does not allocate any buffer.
00432        *
00433        *  @if maint
00434        *  That's a lie.  We initialize the base class with NULL, because the
00435        *  string class does its own memory management.
00436        *  @endif
00437       */
00438       explicit
00439       basic_ostringstream(ios_base::openmode __mode = ios_base::out)
00440       : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
00441       { this->init(&_M_stringbuf); }
00442 
00443       /**
00444        *  @brief  Starts with an existing string buffer.
00445        *  @param  str  A string to copy as a starting buffer.
00446        *  @param  mode  Whether the buffer can read, or write, or both.
00447        *
00448        *  @c ios_base::out is automatically included in @a mode.
00449        *
00450        *  Initializes @c sb using @a str and @c mode|out, and passes @c &sb
00451        *  to the base class initializer.
00452        *
00453        *  @if maint
00454        *  That's a lie.  We initialize the base class with NULL, because the
00455        *  string class does its own memory management.
00456        *  @endif
00457       */
00458       explicit
00459       basic_ostringstream(const __string_type& __str,
00460               ios_base::openmode __mode = ios_base::out)
00461       : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
00462       { this->init(&_M_stringbuf); }
00463 
00464       /**
00465        *  @brief  The destructor does nothing.
00466        *
00467        *  The buffer is deallocated by the stringbuf object, not the
00468        *  formatting stream.
00469       */
00470       ~basic_ostringstream()
00471       { }
00472 
00473       // Members:
00474       /**
00475        *  @brief  Accessing the underlying buffer.
00476        *  @return  The current basic_stringbuf buffer.
00477        *
00478        *  This hides both signatures of std::basic_ios::rdbuf().
00479       */
00480       __stringbuf_type*
00481       rdbuf() const
00482       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00483 
00484       /**
00485        *  @brief  Copying out the string buffer.
00486        *  @return  @c rdbuf()->str()
00487       */
00488       __string_type
00489       str() const
00490       { return _M_stringbuf.str(); }
00491 
00492       /**
00493        *  @brief  Setting a new buffer.
00494        *  @param  s  The string to use as a new sequence.
00495        *
00496        *  Calls @c rdbuf()->str(s).
00497       */
00498       void
00499       str(const __string_type& __s)
00500       { _M_stringbuf.str(__s); }
00501     };
00502 
00503 
00504   // [27.7.4] Template class basic_stringstream
00505   /**
00506    *  @brief  Controlling input and output for std::string.
00507    *
00508    *  This class supports reading from and writing to objects of type
00509    *  std::basic_string, using the inherited functions from
00510    *  std::basic_iostream.  To control the associated sequence, an instance
00511    *  of std::basic_stringbuf is used, which this page refers to as @c sb.
00512   */
00513   template <typename _CharT, typename _Traits, typename _Alloc>
00514     class basic_stringstream : public basic_iostream<_CharT, _Traits>
00515     {
00516     public:
00517       // Types:
00518       typedef _CharT                    char_type;
00519       typedef _Traits                   traits_type;
00520       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00521       // 251. basic_stringbuf missing allocator_type
00522       typedef _Alloc                        allocator_type;
00523       typedef typename traits_type::int_type        int_type;
00524       typedef typename traits_type::pos_type        pos_type;
00525       typedef typename traits_type::off_type        off_type;
00526 
00527       // Non-standard Types:
00528       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
00529       typedef basic_stringbuf<_CharT, _Traits, _Alloc>  __stringbuf_type;
00530       typedef basic_iostream<char_type, traits_type>    __iostream_type;
00531 
00532     private:
00533       __stringbuf_type  _M_stringbuf;
00534 
00535     public:
00536       // Constructors/destructors
00537       /**
00538        *  @brief  Default constructor starts with an empty string buffer.
00539        *  @param  mode  Whether the buffer can read, or write, or both.
00540        *
00541        *  Initializes @c sb using @c mode, and passes @c &sb to the base
00542        *  class initializer.  Does not allocate any buffer.
00543        *
00544        *  @if maint
00545        *  That's a lie.  We initialize the base class with NULL, because the
00546        *  string class does its own memory management.
00547        *  @endif
00548       */
00549       explicit
00550       basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
00551       : __iostream_type(), _M_stringbuf(__m)
00552       { this->init(&_M_stringbuf); }
00553 
00554       /**
00555        *  @brief  Starts with an existing string buffer.
00556        *  @param  str  A string to copy as a starting buffer.
00557        *  @param  mode  Whether the buffer can read, or write, or both.
00558        *
00559        *  Initializes @c sb using @a str and @c mode, and passes @c &sb
00560        *  to the base class initializer.
00561        *
00562        *  @if maint
00563        *  That's a lie.  We initialize the base class with NULL, because the
00564        *  string class does its own memory management.
00565        *  @endif
00566       */
00567       explicit
00568       basic_stringstream(const __string_type& __str,
00569              ios_base::openmode __m = ios_base::out | ios_base::in)
00570       : __iostream_type(), _M_stringbuf(__str, __m)
00571       { this->init(&_M_stringbuf); }
00572 
00573       /**
00574        *  @brief  The destructor does nothing.
00575        *
00576        *  The buffer is deallocated by the stringbuf object, not the
00577        *  formatting stream.
00578       */
00579       ~basic_stringstream()
00580       { }
00581 
00582       // Members:
00583       /**
00584        *  @brief  Accessing the underlying buffer.
00585        *  @return  The current basic_stringbuf buffer.
00586        *
00587        *  This hides both signatures of std::basic_ios::rdbuf().
00588       */
00589       __stringbuf_type*
00590       rdbuf() const
00591       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
00592 
00593       /**
00594        *  @brief  Copying out the string buffer.
00595        *  @return  @c rdbuf()->str()
00596       */
00597       __string_type
00598       str() const
00599       { return _M_stringbuf.str(); }
00600 
00601       /**
00602        *  @brief  Setting a new buffer.
00603        *  @param  s  The string to use as a new sequence.
00604        *
00605        *  Calls @c rdbuf()->str(s).
00606       */
00607       void
00608       str(const __string_type& __s)
00609       { _M_stringbuf.str(__s); }
00610     };
00611 } // namespace std
00612 
00613 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00614 # include <bits/sstream.tcc>
00615 #endif
00616 
00617 #endif /* _GLIBCXX_SSTREAM */

Generated on Thu Apr 20 22:14:58 2006 for libstdc++ source by  doxygen 1.4.6