1*e4b17023SJohn Marino// String based streams -*- C++ -*- 2*e4b17023SJohn Marino 3*e4b17023SJohn Marino// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4*e4b17023SJohn Marino// 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 5*e4b17023SJohn Marino// 6*e4b17023SJohn Marino// This file is part of the GNU ISO C++ Library. This library is free 7*e4b17023SJohn Marino// software; you can redistribute it and/or modify it under the 8*e4b17023SJohn Marino// terms of the GNU General Public License as published by the 9*e4b17023SJohn Marino// Free Software Foundation; either version 3, or (at your option) 10*e4b17023SJohn Marino// any later version. 11*e4b17023SJohn Marino 12*e4b17023SJohn Marino// This library is distributed in the hope that it will be useful, 13*e4b17023SJohn Marino// but WITHOUT ANY WARRANTY; without even the implied warranty of 14*e4b17023SJohn Marino// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*e4b17023SJohn Marino// GNU General Public License for more details. 16*e4b17023SJohn Marino 17*e4b17023SJohn Marino// Under Section 7 of GPL version 3, you are granted additional 18*e4b17023SJohn Marino// permissions described in the GCC Runtime Library Exception, version 19*e4b17023SJohn Marino// 3.1, as published by the Free Software Foundation. 20*e4b17023SJohn Marino 21*e4b17023SJohn Marino// You should have received a copy of the GNU General Public License and 22*e4b17023SJohn Marino// a copy of the GCC Runtime Library Exception along with this program; 23*e4b17023SJohn Marino// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24*e4b17023SJohn Marino// <http://www.gnu.org/licenses/>. 25*e4b17023SJohn Marino 26*e4b17023SJohn Marino/** @file include/sstream 27*e4b17023SJohn Marino * This is a Standard C++ Library header. 28*e4b17023SJohn Marino */ 29*e4b17023SJohn Marino 30*e4b17023SJohn Marino// 31*e4b17023SJohn Marino// ISO C++ 14882: 27.7 String-based streams 32*e4b17023SJohn Marino// 33*e4b17023SJohn Marino 34*e4b17023SJohn Marino#ifndef _GLIBCXX_SSTREAM 35*e4b17023SJohn Marino#define _GLIBCXX_SSTREAM 1 36*e4b17023SJohn Marino 37*e4b17023SJohn Marino#pragma GCC system_header 38*e4b17023SJohn Marino 39*e4b17023SJohn Marino#include <istream> 40*e4b17023SJohn Marino#include <ostream> 41*e4b17023SJohn Marino 42*e4b17023SJohn Marinonamespace std _GLIBCXX_VISIBILITY(default) 43*e4b17023SJohn Marino{ 44*e4b17023SJohn Marino_GLIBCXX_BEGIN_NAMESPACE_VERSION 45*e4b17023SJohn Marino 46*e4b17023SJohn Marino // [27.7.1] template class basic_stringbuf 47*e4b17023SJohn Marino /** 48*e4b17023SJohn Marino * @brief The actual work of input and output (for std::string). 49*e4b17023SJohn Marino * @ingroup io 50*e4b17023SJohn Marino * 51*e4b17023SJohn Marino * This class associates either or both of its input and output sequences 52*e4b17023SJohn Marino * with a sequence of characters, which can be initialized from, or made 53*e4b17023SJohn Marino * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.) 54*e4b17023SJohn Marino * 55*e4b17023SJohn Marino * For this class, open modes (of type @c ios_base::openmode) have 56*e4b17023SJohn Marino * @c in set if the input sequence can be read, and @c out set if the 57*e4b17023SJohn Marino * output sequence can be written. 58*e4b17023SJohn Marino */ 59*e4b17023SJohn Marino template<typename _CharT, typename _Traits, typename _Alloc> 60*e4b17023SJohn Marino class basic_stringbuf : public basic_streambuf<_CharT, _Traits> 61*e4b17023SJohn Marino { 62*e4b17023SJohn Marino public: 63*e4b17023SJohn Marino // Types: 64*e4b17023SJohn Marino typedef _CharT char_type; 65*e4b17023SJohn Marino typedef _Traits traits_type; 66*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 67*e4b17023SJohn Marino // 251. basic_stringbuf missing allocator_type 68*e4b17023SJohn Marino typedef _Alloc allocator_type; 69*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 70*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 71*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 72*e4b17023SJohn Marino 73*e4b17023SJohn Marino typedef basic_streambuf<char_type, traits_type> __streambuf_type; 74*e4b17023SJohn Marino typedef basic_string<char_type, _Traits, _Alloc> __string_type; 75*e4b17023SJohn Marino typedef typename __string_type::size_type __size_type; 76*e4b17023SJohn Marino 77*e4b17023SJohn Marino protected: 78*e4b17023SJohn Marino /// Place to stash in || out || in | out settings for current stringbuf. 79*e4b17023SJohn Marino ios_base::openmode _M_mode; 80*e4b17023SJohn Marino 81*e4b17023SJohn Marino // Data Members: 82*e4b17023SJohn Marino __string_type _M_string; 83*e4b17023SJohn Marino 84*e4b17023SJohn Marino public: 85*e4b17023SJohn Marino // Constructors: 86*e4b17023SJohn Marino /** 87*e4b17023SJohn Marino * @brief Starts with an empty string buffer. 88*e4b17023SJohn Marino * @param __mode Whether the buffer can read, or write, or both. 89*e4b17023SJohn Marino * 90*e4b17023SJohn Marino * The default constructor initializes the parent class using its 91*e4b17023SJohn Marino * own default ctor. 92*e4b17023SJohn Marino */ 93*e4b17023SJohn Marino explicit 94*e4b17023SJohn Marino basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out) 95*e4b17023SJohn Marino : __streambuf_type(), _M_mode(__mode), _M_string() 96*e4b17023SJohn Marino { } 97*e4b17023SJohn Marino 98*e4b17023SJohn Marino /** 99*e4b17023SJohn Marino * @brief Starts with an existing string buffer. 100*e4b17023SJohn Marino * @param __str A string to copy as a starting buffer. 101*e4b17023SJohn Marino * @param __mode Whether the buffer can read, or write, or both. 102*e4b17023SJohn Marino * 103*e4b17023SJohn Marino * This constructor initializes the parent class using its 104*e4b17023SJohn Marino * own default ctor. 105*e4b17023SJohn Marino */ 106*e4b17023SJohn Marino explicit 107*e4b17023SJohn Marino basic_stringbuf(const __string_type& __str, 108*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out) 109*e4b17023SJohn Marino : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size()) 110*e4b17023SJohn Marino { _M_stringbuf_init(__mode); } 111*e4b17023SJohn Marino 112*e4b17023SJohn Marino // Get and set: 113*e4b17023SJohn Marino /** 114*e4b17023SJohn Marino * @brief Copying out the string buffer. 115*e4b17023SJohn Marino * @return A copy of one of the underlying sequences. 116*e4b17023SJohn Marino * 117*e4b17023SJohn Marino * <em>If the buffer is only created in input mode, the underlying 118*e4b17023SJohn Marino * character sequence is equal to the input sequence; otherwise, it 119*e4b17023SJohn Marino * is equal to the output sequence.</em> [27.7.1.2]/1 120*e4b17023SJohn Marino */ 121*e4b17023SJohn Marino __string_type 122*e4b17023SJohn Marino str() const 123*e4b17023SJohn Marino { 124*e4b17023SJohn Marino __string_type __ret; 125*e4b17023SJohn Marino if (this->pptr()) 126*e4b17023SJohn Marino { 127*e4b17023SJohn Marino // The current egptr() may not be the actual string end. 128*e4b17023SJohn Marino if (this->pptr() > this->egptr()) 129*e4b17023SJohn Marino __ret = __string_type(this->pbase(), this->pptr()); 130*e4b17023SJohn Marino else 131*e4b17023SJohn Marino __ret = __string_type(this->pbase(), this->egptr()); 132*e4b17023SJohn Marino } 133*e4b17023SJohn Marino else 134*e4b17023SJohn Marino __ret = _M_string; 135*e4b17023SJohn Marino return __ret; 136*e4b17023SJohn Marino } 137*e4b17023SJohn Marino 138*e4b17023SJohn Marino /** 139*e4b17023SJohn Marino * @brief Setting a new buffer. 140*e4b17023SJohn Marino * @param __s The string to use as a new sequence. 141*e4b17023SJohn Marino * 142*e4b17023SJohn Marino * Deallocates any previous stored sequence, then copies @a s to 143*e4b17023SJohn Marino * use as a new one. 144*e4b17023SJohn Marino */ 145*e4b17023SJohn Marino void 146*e4b17023SJohn Marino str(const __string_type& __s) 147*e4b17023SJohn Marino { 148*e4b17023SJohn Marino // Cannot use _M_string = __s, since v3 strings are COW. 149*e4b17023SJohn Marino _M_string.assign(__s.data(), __s.size()); 150*e4b17023SJohn Marino _M_stringbuf_init(_M_mode); 151*e4b17023SJohn Marino } 152*e4b17023SJohn Marino 153*e4b17023SJohn Marino protected: 154*e4b17023SJohn Marino // Common initialization code goes here. 155*e4b17023SJohn Marino void 156*e4b17023SJohn Marino _M_stringbuf_init(ios_base::openmode __mode) 157*e4b17023SJohn Marino { 158*e4b17023SJohn Marino _M_mode = __mode; 159*e4b17023SJohn Marino __size_type __len = 0; 160*e4b17023SJohn Marino if (_M_mode & (ios_base::ate | ios_base::app)) 161*e4b17023SJohn Marino __len = _M_string.size(); 162*e4b17023SJohn Marino _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len); 163*e4b17023SJohn Marino } 164*e4b17023SJohn Marino 165*e4b17023SJohn Marino virtual streamsize 166*e4b17023SJohn Marino showmanyc() 167*e4b17023SJohn Marino { 168*e4b17023SJohn Marino streamsize __ret = -1; 169*e4b17023SJohn Marino if (_M_mode & ios_base::in) 170*e4b17023SJohn Marino { 171*e4b17023SJohn Marino _M_update_egptr(); 172*e4b17023SJohn Marino __ret = this->egptr() - this->gptr(); 173*e4b17023SJohn Marino } 174*e4b17023SJohn Marino return __ret; 175*e4b17023SJohn Marino } 176*e4b17023SJohn Marino 177*e4b17023SJohn Marino virtual int_type 178*e4b17023SJohn Marino underflow(); 179*e4b17023SJohn Marino 180*e4b17023SJohn Marino virtual int_type 181*e4b17023SJohn Marino pbackfail(int_type __c = traits_type::eof()); 182*e4b17023SJohn Marino 183*e4b17023SJohn Marino virtual int_type 184*e4b17023SJohn Marino overflow(int_type __c = traits_type::eof()); 185*e4b17023SJohn Marino 186*e4b17023SJohn Marino /** 187*e4b17023SJohn Marino * @brief Manipulates the buffer. 188*e4b17023SJohn Marino * @param __s Pointer to a buffer area. 189*e4b17023SJohn Marino * @param __n Size of @a __s. 190*e4b17023SJohn Marino * @return @c this 191*e4b17023SJohn Marino * 192*e4b17023SJohn Marino * If no buffer has already been created, and both @a __s and @a __n are 193*e4b17023SJohn Marino * non-zero, then @c __s is used as a buffer; see 194*e4b17023SJohn Marino * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html 195*e4b17023SJohn Marino * for more. 196*e4b17023SJohn Marino */ 197*e4b17023SJohn Marino virtual __streambuf_type* 198*e4b17023SJohn Marino setbuf(char_type* __s, streamsize __n) 199*e4b17023SJohn Marino { 200*e4b17023SJohn Marino if (__s && __n >= 0) 201*e4b17023SJohn Marino { 202*e4b17023SJohn Marino // This is implementation-defined behavior, and assumes 203*e4b17023SJohn Marino // that an external char_type array of length __n exists 204*e4b17023SJohn Marino // and has been pre-allocated. If this is not the case, 205*e4b17023SJohn Marino // things will quickly blow up. 206*e4b17023SJohn Marino 207*e4b17023SJohn Marino // Step 1: Destroy the current internal array. 208*e4b17023SJohn Marino _M_string.clear(); 209*e4b17023SJohn Marino 210*e4b17023SJohn Marino // Step 2: Use the external array. 211*e4b17023SJohn Marino _M_sync(__s, __n, 0); 212*e4b17023SJohn Marino } 213*e4b17023SJohn Marino return this; 214*e4b17023SJohn Marino } 215*e4b17023SJohn Marino 216*e4b17023SJohn Marino virtual pos_type 217*e4b17023SJohn Marino seekoff(off_type __off, ios_base::seekdir __way, 218*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out); 219*e4b17023SJohn Marino 220*e4b17023SJohn Marino virtual pos_type 221*e4b17023SJohn Marino seekpos(pos_type __sp, 222*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in | ios_base::out); 223*e4b17023SJohn Marino 224*e4b17023SJohn Marino // Internal function for correctly updating the internal buffer 225*e4b17023SJohn Marino // for a particular _M_string, due to initialization or re-sizing 226*e4b17023SJohn Marino // of an existing _M_string. 227*e4b17023SJohn Marino void 228*e4b17023SJohn Marino _M_sync(char_type* __base, __size_type __i, __size_type __o); 229*e4b17023SJohn Marino 230*e4b17023SJohn Marino // Internal function for correctly updating egptr() to the actual 231*e4b17023SJohn Marino // string end. 232*e4b17023SJohn Marino void 233*e4b17023SJohn Marino _M_update_egptr() 234*e4b17023SJohn Marino { 235*e4b17023SJohn Marino const bool __testin = _M_mode & ios_base::in; 236*e4b17023SJohn Marino if (this->pptr() && this->pptr() > this->egptr()) 237*e4b17023SJohn Marino { 238*e4b17023SJohn Marino if (__testin) 239*e4b17023SJohn Marino this->setg(this->eback(), this->gptr(), this->pptr()); 240*e4b17023SJohn Marino else 241*e4b17023SJohn Marino this->setg(this->pptr(), this->pptr(), this->pptr()); 242*e4b17023SJohn Marino } 243*e4b17023SJohn Marino } 244*e4b17023SJohn Marino 245*e4b17023SJohn Marino // Works around the issue with pbump, part of the protected 246*e4b17023SJohn Marino // interface of basic_streambuf, taking just an int. 247*e4b17023SJohn Marino void 248*e4b17023SJohn Marino _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off); 249*e4b17023SJohn Marino }; 250*e4b17023SJohn Marino 251*e4b17023SJohn Marino 252*e4b17023SJohn Marino // [27.7.2] Template class basic_istringstream 253*e4b17023SJohn Marino /** 254*e4b17023SJohn Marino * @brief Controlling input for std::string. 255*e4b17023SJohn Marino * @ingroup io 256*e4b17023SJohn Marino * 257*e4b17023SJohn Marino * This class supports reading from objects of type std::basic_string, 258*e4b17023SJohn Marino * using the inherited functions from std::basic_istream. To control 259*e4b17023SJohn Marino * the associated sequence, an instance of std::basic_stringbuf is used, 260*e4b17023SJohn Marino * which this page refers to as @c sb. 261*e4b17023SJohn Marino */ 262*e4b17023SJohn Marino template<typename _CharT, typename _Traits, typename _Alloc> 263*e4b17023SJohn Marino class basic_istringstream : public basic_istream<_CharT, _Traits> 264*e4b17023SJohn Marino { 265*e4b17023SJohn Marino public: 266*e4b17023SJohn Marino // Types: 267*e4b17023SJohn Marino typedef _CharT char_type; 268*e4b17023SJohn Marino typedef _Traits traits_type; 269*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 270*e4b17023SJohn Marino // 251. basic_stringbuf missing allocator_type 271*e4b17023SJohn Marino typedef _Alloc allocator_type; 272*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 273*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 274*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 275*e4b17023SJohn Marino 276*e4b17023SJohn Marino // Non-standard types: 277*e4b17023SJohn Marino typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 278*e4b17023SJohn Marino typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 279*e4b17023SJohn Marino typedef basic_istream<char_type, traits_type> __istream_type; 280*e4b17023SJohn Marino 281*e4b17023SJohn Marino private: 282*e4b17023SJohn Marino __stringbuf_type _M_stringbuf; 283*e4b17023SJohn Marino 284*e4b17023SJohn Marino public: 285*e4b17023SJohn Marino // Constructors: 286*e4b17023SJohn Marino /** 287*e4b17023SJohn Marino * @brief Default constructor starts with an empty string buffer. 288*e4b17023SJohn Marino * @param __mode Whether the buffer can read, or write, or both. 289*e4b17023SJohn Marino * 290*e4b17023SJohn Marino * @c ios_base::in is automatically included in @a __mode. 291*e4b17023SJohn Marino * 292*e4b17023SJohn Marino * Initializes @c sb using @c __mode|in, and passes @c &sb to the base 293*e4b17023SJohn Marino * class initializer. Does not allocate any buffer. 294*e4b17023SJohn Marino * 295*e4b17023SJohn Marino * That's a lie. We initialize the base class with NULL, because the 296*e4b17023SJohn Marino * string class does its own memory management. 297*e4b17023SJohn Marino */ 298*e4b17023SJohn Marino explicit 299*e4b17023SJohn Marino basic_istringstream(ios_base::openmode __mode = ios_base::in) 300*e4b17023SJohn Marino : __istream_type(), _M_stringbuf(__mode | ios_base::in) 301*e4b17023SJohn Marino { this->init(&_M_stringbuf); } 302*e4b17023SJohn Marino 303*e4b17023SJohn Marino /** 304*e4b17023SJohn Marino * @brief Starts with an existing string buffer. 305*e4b17023SJohn Marino * @param __str A string to copy as a starting buffer. 306*e4b17023SJohn Marino * @param __mode Whether the buffer can read, or write, or both. 307*e4b17023SJohn Marino * 308*e4b17023SJohn Marino * @c ios_base::in is automatically included in @a mode. 309*e4b17023SJohn Marino * 310*e4b17023SJohn Marino * Initializes @c sb using @a str and @c mode|in, and passes @c &sb 311*e4b17023SJohn Marino * to the base class initializer. 312*e4b17023SJohn Marino * 313*e4b17023SJohn Marino * That's a lie. We initialize the base class with NULL, because the 314*e4b17023SJohn Marino * string class does its own memory management. 315*e4b17023SJohn Marino */ 316*e4b17023SJohn Marino explicit 317*e4b17023SJohn Marino basic_istringstream(const __string_type& __str, 318*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::in) 319*e4b17023SJohn Marino : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in) 320*e4b17023SJohn Marino { this->init(&_M_stringbuf); } 321*e4b17023SJohn Marino 322*e4b17023SJohn Marino /** 323*e4b17023SJohn Marino * @brief The destructor does nothing. 324*e4b17023SJohn Marino * 325*e4b17023SJohn Marino * The buffer is deallocated by the stringbuf object, not the 326*e4b17023SJohn Marino * formatting stream. 327*e4b17023SJohn Marino */ 328*e4b17023SJohn Marino ~basic_istringstream() 329*e4b17023SJohn Marino { } 330*e4b17023SJohn Marino 331*e4b17023SJohn Marino // Members: 332*e4b17023SJohn Marino /** 333*e4b17023SJohn Marino * @brief Accessing the underlying buffer. 334*e4b17023SJohn Marino * @return The current basic_stringbuf buffer. 335*e4b17023SJohn Marino * 336*e4b17023SJohn Marino * This hides both signatures of std::basic_ios::rdbuf(). 337*e4b17023SJohn Marino */ 338*e4b17023SJohn Marino __stringbuf_type* 339*e4b17023SJohn Marino rdbuf() const 340*e4b17023SJohn Marino { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 341*e4b17023SJohn Marino 342*e4b17023SJohn Marino /** 343*e4b17023SJohn Marino * @brief Copying out the string buffer. 344*e4b17023SJohn Marino * @return @c rdbuf()->str() 345*e4b17023SJohn Marino */ 346*e4b17023SJohn Marino __string_type 347*e4b17023SJohn Marino str() const 348*e4b17023SJohn Marino { return _M_stringbuf.str(); } 349*e4b17023SJohn Marino 350*e4b17023SJohn Marino /** 351*e4b17023SJohn Marino * @brief Setting a new buffer. 352*e4b17023SJohn Marino * @param __s The string to use as a new sequence. 353*e4b17023SJohn Marino * 354*e4b17023SJohn Marino * Calls @c rdbuf()->str(s). 355*e4b17023SJohn Marino */ 356*e4b17023SJohn Marino void 357*e4b17023SJohn Marino str(const __string_type& __s) 358*e4b17023SJohn Marino { _M_stringbuf.str(__s); } 359*e4b17023SJohn Marino }; 360*e4b17023SJohn Marino 361*e4b17023SJohn Marino 362*e4b17023SJohn Marino // [27.7.3] Template class basic_ostringstream 363*e4b17023SJohn Marino /** 364*e4b17023SJohn Marino * @brief Controlling output for std::string. 365*e4b17023SJohn Marino * @ingroup io 366*e4b17023SJohn Marino * 367*e4b17023SJohn Marino * This class supports writing to objects of type std::basic_string, 368*e4b17023SJohn Marino * using the inherited functions from std::basic_ostream. To control 369*e4b17023SJohn Marino * the associated sequence, an instance of std::basic_stringbuf is used, 370*e4b17023SJohn Marino * which this page refers to as @c sb. 371*e4b17023SJohn Marino */ 372*e4b17023SJohn Marino template <typename _CharT, typename _Traits, typename _Alloc> 373*e4b17023SJohn Marino class basic_ostringstream : public basic_ostream<_CharT, _Traits> 374*e4b17023SJohn Marino { 375*e4b17023SJohn Marino public: 376*e4b17023SJohn Marino // Types: 377*e4b17023SJohn Marino typedef _CharT char_type; 378*e4b17023SJohn Marino typedef _Traits traits_type; 379*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 380*e4b17023SJohn Marino // 251. basic_stringbuf missing allocator_type 381*e4b17023SJohn Marino typedef _Alloc allocator_type; 382*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 383*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 384*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 385*e4b17023SJohn Marino 386*e4b17023SJohn Marino // Non-standard types: 387*e4b17023SJohn Marino typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 388*e4b17023SJohn Marino typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 389*e4b17023SJohn Marino typedef basic_ostream<char_type, traits_type> __ostream_type; 390*e4b17023SJohn Marino 391*e4b17023SJohn Marino private: 392*e4b17023SJohn Marino __stringbuf_type _M_stringbuf; 393*e4b17023SJohn Marino 394*e4b17023SJohn Marino public: 395*e4b17023SJohn Marino // Constructors/destructor: 396*e4b17023SJohn Marino /** 397*e4b17023SJohn Marino * @brief Default constructor starts with an empty string buffer. 398*e4b17023SJohn Marino * @param __mode Whether the buffer can read, or write, or both. 399*e4b17023SJohn Marino * 400*e4b17023SJohn Marino * @c ios_base::out is automatically included in @a mode. 401*e4b17023SJohn Marino * 402*e4b17023SJohn Marino * Initializes @c sb using @c mode|out, and passes @c &sb to the base 403*e4b17023SJohn Marino * class initializer. Does not allocate any buffer. 404*e4b17023SJohn Marino * 405*e4b17023SJohn Marino * That's a lie. We initialize the base class with NULL, because the 406*e4b17023SJohn Marino * string class does its own memory management. 407*e4b17023SJohn Marino */ 408*e4b17023SJohn Marino explicit 409*e4b17023SJohn Marino basic_ostringstream(ios_base::openmode __mode = ios_base::out) 410*e4b17023SJohn Marino : __ostream_type(), _M_stringbuf(__mode | ios_base::out) 411*e4b17023SJohn Marino { this->init(&_M_stringbuf); } 412*e4b17023SJohn Marino 413*e4b17023SJohn Marino /** 414*e4b17023SJohn Marino * @brief Starts with an existing string buffer. 415*e4b17023SJohn Marino * @param __str A string to copy as a starting buffer. 416*e4b17023SJohn Marino * @param __mode Whether the buffer can read, or write, or both. 417*e4b17023SJohn Marino * 418*e4b17023SJohn Marino * @c ios_base::out is automatically included in @a mode. 419*e4b17023SJohn Marino * 420*e4b17023SJohn Marino * Initializes @c sb using @a str and @c mode|out, and passes @c &sb 421*e4b17023SJohn Marino * to the base class initializer. 422*e4b17023SJohn Marino * 423*e4b17023SJohn Marino * That's a lie. We initialize the base class with NULL, because the 424*e4b17023SJohn Marino * string class does its own memory management. 425*e4b17023SJohn Marino */ 426*e4b17023SJohn Marino explicit 427*e4b17023SJohn Marino basic_ostringstream(const __string_type& __str, 428*e4b17023SJohn Marino ios_base::openmode __mode = ios_base::out) 429*e4b17023SJohn Marino : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out) 430*e4b17023SJohn Marino { this->init(&_M_stringbuf); } 431*e4b17023SJohn Marino 432*e4b17023SJohn Marino /** 433*e4b17023SJohn Marino * @brief The destructor does nothing. 434*e4b17023SJohn Marino * 435*e4b17023SJohn Marino * The buffer is deallocated by the stringbuf object, not the 436*e4b17023SJohn Marino * formatting stream. 437*e4b17023SJohn Marino */ 438*e4b17023SJohn Marino ~basic_ostringstream() 439*e4b17023SJohn Marino { } 440*e4b17023SJohn Marino 441*e4b17023SJohn Marino // Members: 442*e4b17023SJohn Marino /** 443*e4b17023SJohn Marino * @brief Accessing the underlying buffer. 444*e4b17023SJohn Marino * @return The current basic_stringbuf buffer. 445*e4b17023SJohn Marino * 446*e4b17023SJohn Marino * This hides both signatures of std::basic_ios::rdbuf(). 447*e4b17023SJohn Marino */ 448*e4b17023SJohn Marino __stringbuf_type* 449*e4b17023SJohn Marino rdbuf() const 450*e4b17023SJohn Marino { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 451*e4b17023SJohn Marino 452*e4b17023SJohn Marino /** 453*e4b17023SJohn Marino * @brief Copying out the string buffer. 454*e4b17023SJohn Marino * @return @c rdbuf()->str() 455*e4b17023SJohn Marino */ 456*e4b17023SJohn Marino __string_type 457*e4b17023SJohn Marino str() const 458*e4b17023SJohn Marino { return _M_stringbuf.str(); } 459*e4b17023SJohn Marino 460*e4b17023SJohn Marino /** 461*e4b17023SJohn Marino * @brief Setting a new buffer. 462*e4b17023SJohn Marino * @param __s The string to use as a new sequence. 463*e4b17023SJohn Marino * 464*e4b17023SJohn Marino * Calls @c rdbuf()->str(s). 465*e4b17023SJohn Marino */ 466*e4b17023SJohn Marino void 467*e4b17023SJohn Marino str(const __string_type& __s) 468*e4b17023SJohn Marino { _M_stringbuf.str(__s); } 469*e4b17023SJohn Marino }; 470*e4b17023SJohn Marino 471*e4b17023SJohn Marino 472*e4b17023SJohn Marino // [27.7.4] Template class basic_stringstream 473*e4b17023SJohn Marino /** 474*e4b17023SJohn Marino * @brief Controlling input and output for std::string. 475*e4b17023SJohn Marino * @ingroup io 476*e4b17023SJohn Marino * 477*e4b17023SJohn Marino * This class supports reading from and writing to objects of type 478*e4b17023SJohn Marino * std::basic_string, using the inherited functions from 479*e4b17023SJohn Marino * std::basic_iostream. To control the associated sequence, an instance 480*e4b17023SJohn Marino * of std::basic_stringbuf is used, which this page refers to as @c sb. 481*e4b17023SJohn Marino */ 482*e4b17023SJohn Marino template <typename _CharT, typename _Traits, typename _Alloc> 483*e4b17023SJohn Marino class basic_stringstream : public basic_iostream<_CharT, _Traits> 484*e4b17023SJohn Marino { 485*e4b17023SJohn Marino public: 486*e4b17023SJohn Marino // Types: 487*e4b17023SJohn Marino typedef _CharT char_type; 488*e4b17023SJohn Marino typedef _Traits traits_type; 489*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 490*e4b17023SJohn Marino // 251. basic_stringbuf missing allocator_type 491*e4b17023SJohn Marino typedef _Alloc allocator_type; 492*e4b17023SJohn Marino typedef typename traits_type::int_type int_type; 493*e4b17023SJohn Marino typedef typename traits_type::pos_type pos_type; 494*e4b17023SJohn Marino typedef typename traits_type::off_type off_type; 495*e4b17023SJohn Marino 496*e4b17023SJohn Marino // Non-standard Types: 497*e4b17023SJohn Marino typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 498*e4b17023SJohn Marino typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type; 499*e4b17023SJohn Marino typedef basic_iostream<char_type, traits_type> __iostream_type; 500*e4b17023SJohn Marino 501*e4b17023SJohn Marino private: 502*e4b17023SJohn Marino __stringbuf_type _M_stringbuf; 503*e4b17023SJohn Marino 504*e4b17023SJohn Marino public: 505*e4b17023SJohn Marino // Constructors/destructors 506*e4b17023SJohn Marino /** 507*e4b17023SJohn Marino * @brief Default constructor starts with an empty string buffer. 508*e4b17023SJohn Marino * @param __m Whether the buffer can read, or write, or both. 509*e4b17023SJohn Marino * 510*e4b17023SJohn Marino * Initializes @c sb using the mode from @c __m, and passes @c 511*e4b17023SJohn Marino * &sb to the base class initializer. Does not allocate any 512*e4b17023SJohn Marino * buffer. 513*e4b17023SJohn Marino * 514*e4b17023SJohn Marino * That's a lie. We initialize the base class with NULL, because the 515*e4b17023SJohn Marino * string class does its own memory management. 516*e4b17023SJohn Marino */ 517*e4b17023SJohn Marino explicit 518*e4b17023SJohn Marino basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in) 519*e4b17023SJohn Marino : __iostream_type(), _M_stringbuf(__m) 520*e4b17023SJohn Marino { this->init(&_M_stringbuf); } 521*e4b17023SJohn Marino 522*e4b17023SJohn Marino /** 523*e4b17023SJohn Marino * @brief Starts with an existing string buffer. 524*e4b17023SJohn Marino * @param __str A string to copy as a starting buffer. 525*e4b17023SJohn Marino * @param __m Whether the buffer can read, or write, or both. 526*e4b17023SJohn Marino * 527*e4b17023SJohn Marino * Initializes @c sb using @a __str and @c __m, and passes @c &sb 528*e4b17023SJohn Marino * to the base class initializer. 529*e4b17023SJohn Marino * 530*e4b17023SJohn Marino * That's a lie. We initialize the base class with NULL, because the 531*e4b17023SJohn Marino * string class does its own memory management. 532*e4b17023SJohn Marino */ 533*e4b17023SJohn Marino explicit 534*e4b17023SJohn Marino basic_stringstream(const __string_type& __str, 535*e4b17023SJohn Marino ios_base::openmode __m = ios_base::out | ios_base::in) 536*e4b17023SJohn Marino : __iostream_type(), _M_stringbuf(__str, __m) 537*e4b17023SJohn Marino { this->init(&_M_stringbuf); } 538*e4b17023SJohn Marino 539*e4b17023SJohn Marino /** 540*e4b17023SJohn Marino * @brief The destructor does nothing. 541*e4b17023SJohn Marino * 542*e4b17023SJohn Marino * The buffer is deallocated by the stringbuf object, not the 543*e4b17023SJohn Marino * formatting stream. 544*e4b17023SJohn Marino */ 545*e4b17023SJohn Marino ~basic_stringstream() 546*e4b17023SJohn Marino { } 547*e4b17023SJohn Marino 548*e4b17023SJohn Marino // Members: 549*e4b17023SJohn Marino /** 550*e4b17023SJohn Marino * @brief Accessing the underlying buffer. 551*e4b17023SJohn Marino * @return The current basic_stringbuf buffer. 552*e4b17023SJohn Marino * 553*e4b17023SJohn Marino * This hides both signatures of std::basic_ios::rdbuf(). 554*e4b17023SJohn Marino */ 555*e4b17023SJohn Marino __stringbuf_type* 556*e4b17023SJohn Marino rdbuf() const 557*e4b17023SJohn Marino { return const_cast<__stringbuf_type*>(&_M_stringbuf); } 558*e4b17023SJohn Marino 559*e4b17023SJohn Marino /** 560*e4b17023SJohn Marino * @brief Copying out the string buffer. 561*e4b17023SJohn Marino * @return @c rdbuf()->str() 562*e4b17023SJohn Marino */ 563*e4b17023SJohn Marino __string_type 564*e4b17023SJohn Marino str() const 565*e4b17023SJohn Marino { return _M_stringbuf.str(); } 566*e4b17023SJohn Marino 567*e4b17023SJohn Marino /** 568*e4b17023SJohn Marino * @brief Setting a new buffer. 569*e4b17023SJohn Marino * @param __s The string to use as a new sequence. 570*e4b17023SJohn Marino * 571*e4b17023SJohn Marino * Calls @c rdbuf()->str(s). 572*e4b17023SJohn Marino */ 573*e4b17023SJohn Marino void 574*e4b17023SJohn Marino str(const __string_type& __s) 575*e4b17023SJohn Marino { _M_stringbuf.str(__s); } 576*e4b17023SJohn Marino }; 577*e4b17023SJohn Marino 578*e4b17023SJohn Marino_GLIBCXX_END_NAMESPACE_VERSION 579*e4b17023SJohn Marino} // namespace 580*e4b17023SJohn Marino 581*e4b17023SJohn Marino#include <bits/sstream.tcc> 582*e4b17023SJohn Marino 583*e4b17023SJohn Marino#endif /* _GLIBCXX_SSTREAM */ 584