1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2008, 2009 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this library; see the file COPYING. If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 * This is a Standard C++ Library header.
36 // ISO C++ 14882: 27.7 String-based streams
39 #ifndef _GLIBCXX_SSTREAM
40 #define _GLIBCXX_SSTREAM 1
42 #pragma GCC system_header
47 _GLIBCXX_BEGIN_NAMESPACE(std)
49 // [27.7.1] template class basic_stringbuf
51 * @brief The actual work of input and output (for std::string).
54 * This class associates either or both of its input and output sequences
55 * with a sequence of characters, which can be initialized from, or made
56 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
58 * For this class, open modes (of type @c ios_base::openmode) have
59 * @c in set if the input sequence can be read, and @c out set if the
60 * output sequence can be written.
62 template<typename _CharT, typename _Traits, typename _Alloc>
63 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
67 typedef _CharT char_type;
68 typedef _Traits traits_type;
69 // _GLIBCXX_RESOLVE_LIB_DEFECTS
70 // 251. basic_stringbuf missing allocator_type
71 typedef _Alloc allocator_type;
72 typedef typename traits_type::int_type int_type;
73 typedef typename traits_type::pos_type pos_type;
74 typedef typename traits_type::off_type off_type;
76 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
77 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
78 typedef typename __string_type::size_type __size_type;
81 /// Place to stash in || out || in | out settings for current stringbuf.
82 ios_base::openmode _M_mode;
85 __string_type _M_string;
90 * @brief Starts with an empty string buffer.
91 * @param mode Whether the buffer can read, or write, or both.
93 * The default constructor initializes the parent class using its
97 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
98 : __streambuf_type(), _M_mode(__mode), _M_string()
102 * @brief Starts with an existing string buffer.
103 * @param str A string to copy as a starting buffer.
104 * @param mode Whether the buffer can read, or write, or both.
106 * This constructor initializes the parent class using its
110 basic_stringbuf(const __string_type& __str,
111 ios_base::openmode __mode = ios_base::in | ios_base::out)
112 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
113 { _M_stringbuf_init(__mode); }
117 * @brief Copying out the string buffer.
118 * @return A copy of one of the underlying sequences.
120 * "If the buffer is only created in input mode, the underlying
121 * character sequence is equal to the input sequence; otherwise, it
122 * is equal to the output sequence." [27.7.1.2]/1
130 // The current egptr() may not be the actual string end.
131 if (this->pptr() > this->egptr())
132 __ret = __string_type(this->pbase(), this->pptr());
134 __ret = __string_type(this->pbase(), this->egptr());
142 * @brief Setting a new buffer.
143 * @param s The string to use as a new sequence.
145 * Deallocates any previous stored sequence, then copies @a s to
149 str(const __string_type& __s)
151 // Cannot use _M_string = __s, since v3 strings are COW.
152 _M_string.assign(__s.data(), __s.size());
153 _M_stringbuf_init(_M_mode);
157 // Common initialization code goes here.
159 _M_stringbuf_init(ios_base::openmode __mode)
162 __size_type __len = 0;
163 if (_M_mode & (ios_base::ate | ios_base::app))
164 __len = _M_string.size();
165 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
171 streamsize __ret = -1;
172 if (_M_mode & ios_base::in)
175 __ret = this->egptr() - this->gptr();
184 pbackfail(int_type __c = traits_type::eof());
187 overflow(int_type __c = traits_type::eof());
190 * @brief Manipulates the buffer.
191 * @param s Pointer to a buffer area.
192 * @param n Size of @a s.
195 * If no buffer has already been created, and both @a s and @a n are
196 * non-zero, then @c s is used as a buffer; see
197 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
200 virtual __streambuf_type*
201 setbuf(char_type* __s, streamsize __n)
205 // This is implementation-defined behavior, and assumes
206 // that an external char_type array of length __n exists
207 // and has been pre-allocated. If this is not the case,
208 // things will quickly blow up.
210 // Step 1: Destroy the current internal array.
213 // Step 2: Use the external array.
214 _M_sync(__s, __n, 0);
220 seekoff(off_type __off, ios_base::seekdir __way,
221 ios_base::openmode __mode = ios_base::in | ios_base::out);
224 seekpos(pos_type __sp,
225 ios_base::openmode __mode = ios_base::in | ios_base::out);
227 // Internal function for correctly updating the internal buffer
228 // for a particular _M_string, due to initialization or re-sizing
229 // of an existing _M_string.
231 _M_sync(char_type* __base, __size_type __i, __size_type __o);
233 // Internal function for correctly updating egptr() to the actual
238 const bool __testin = _M_mode & ios_base::in;
239 if (this->pptr() && this->pptr() > this->egptr())
242 this->setg(this->eback(), this->gptr(), this->pptr());
244 this->setg(this->pptr(), this->pptr(), this->pptr());
250 // [27.7.2] Template class basic_istringstream
252 * @brief Controlling input for std::string.
255 * This class supports reading from objects of type std::basic_string,
256 * using the inherited functions from std::basic_istream. To control
257 * the associated sequence, an instance of std::basic_stringbuf is used,
258 * which this page refers to as @c sb.
260 template<typename _CharT, typename _Traits, typename _Alloc>
261 class basic_istringstream : public basic_istream<_CharT, _Traits>
265 typedef _CharT char_type;
266 typedef _Traits traits_type;
267 // _GLIBCXX_RESOLVE_LIB_DEFECTS
268 // 251. basic_stringbuf missing allocator_type
269 typedef _Alloc allocator_type;
270 typedef typename traits_type::int_type int_type;
271 typedef typename traits_type::pos_type pos_type;
272 typedef typename traits_type::off_type off_type;
274 // Non-standard types:
275 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
276 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
277 typedef basic_istream<char_type, traits_type> __istream_type;
280 __stringbuf_type _M_stringbuf;
285 * @brief Default constructor starts with an empty string buffer.
286 * @param mode Whether the buffer can read, or write, or both.
288 * @c ios_base::in is automatically included in @a mode.
290 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
291 * class initializer. Does not allocate any buffer.
293 * That's a lie. We initialize the base class with NULL, because the
294 * string class does its own memory management.
297 basic_istringstream(ios_base::openmode __mode = ios_base::in)
298 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
299 { this->init(&_M_stringbuf); }
302 * @brief Starts with an existing string buffer.
303 * @param str A string to copy as a starting buffer.
304 * @param mode Whether the buffer can read, or write, or both.
306 * @c ios_base::in is automatically included in @a mode.
308 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
309 * to the base class initializer.
311 * That's a lie. We initialize the base class with NULL, because the
312 * string class does its own memory management.
315 basic_istringstream(const __string_type& __str,
316 ios_base::openmode __mode = ios_base::in)
317 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
318 { this->init(&_M_stringbuf); }
321 * @brief The destructor does nothing.
323 * The buffer is deallocated by the stringbuf object, not the
326 ~basic_istringstream()
331 * @brief Accessing the underlying buffer.
332 * @return The current basic_stringbuf buffer.
334 * This hides both signatures of std::basic_ios::rdbuf().
338 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
341 * @brief Copying out the string buffer.
342 * @return @c rdbuf()->str()
346 { return _M_stringbuf.str(); }
349 * @brief Setting a new buffer.
350 * @param s The string to use as a new sequence.
352 * Calls @c rdbuf()->str(s).
355 str(const __string_type& __s)
356 { _M_stringbuf.str(__s); }
360 // [27.7.3] Template class basic_ostringstream
362 * @brief Controlling output for std::string.
365 * This class supports writing to objects of type std::basic_string,
366 * using the inherited functions from std::basic_ostream. To control
367 * the associated sequence, an instance of std::basic_stringbuf is used,
368 * which this page refers to as @c sb.
370 template <typename _CharT, typename _Traits, typename _Alloc>
371 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
375 typedef _CharT char_type;
376 typedef _Traits traits_type;
377 // _GLIBCXX_RESOLVE_LIB_DEFECTS
378 // 251. basic_stringbuf missing allocator_type
379 typedef _Alloc allocator_type;
380 typedef typename traits_type::int_type int_type;
381 typedef typename traits_type::pos_type pos_type;
382 typedef typename traits_type::off_type off_type;
384 // Non-standard types:
385 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
386 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
387 typedef basic_ostream<char_type, traits_type> __ostream_type;
390 __stringbuf_type _M_stringbuf;
393 // Constructors/destructor:
395 * @brief Default constructor starts with an empty string buffer.
396 * @param mode Whether the buffer can read, or write, or both.
398 * @c ios_base::out is automatically included in @a mode.
400 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
401 * class initializer. Does not allocate any buffer.
403 * That's a lie. We initialize the base class with NULL, because the
404 * string class does its own memory management.
407 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
408 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
409 { this->init(&_M_stringbuf); }
412 * @brief Starts with an existing string buffer.
413 * @param str A string to copy as a starting buffer.
414 * @param mode Whether the buffer can read, or write, or both.
416 * @c ios_base::out is automatically included in @a mode.
418 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
419 * to the base class initializer.
421 * That's a lie. We initialize the base class with NULL, because the
422 * string class does its own memory management.
425 basic_ostringstream(const __string_type& __str,
426 ios_base::openmode __mode = ios_base::out)
427 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
428 { this->init(&_M_stringbuf); }
431 * @brief The destructor does nothing.
433 * The buffer is deallocated by the stringbuf object, not the
436 ~basic_ostringstream()
441 * @brief Accessing the underlying buffer.
442 * @return The current basic_stringbuf buffer.
444 * This hides both signatures of std::basic_ios::rdbuf().
448 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
451 * @brief Copying out the string buffer.
452 * @return @c rdbuf()->str()
456 { return _M_stringbuf.str(); }
459 * @brief Setting a new buffer.
460 * @param s The string to use as a new sequence.
462 * Calls @c rdbuf()->str(s).
465 str(const __string_type& __s)
466 { _M_stringbuf.str(__s); }
470 // [27.7.4] Template class basic_stringstream
472 * @brief Controlling input and output for std::string.
475 * This class supports reading from and writing to objects of type
476 * std::basic_string, using the inherited functions from
477 * std::basic_iostream. To control the associated sequence, an instance
478 * of std::basic_stringbuf is used, which this page refers to as @c sb.
480 template <typename _CharT, typename _Traits, typename _Alloc>
481 class basic_stringstream : public basic_iostream<_CharT, _Traits>
485 typedef _CharT char_type;
486 typedef _Traits traits_type;
487 // _GLIBCXX_RESOLVE_LIB_DEFECTS
488 // 251. basic_stringbuf missing allocator_type
489 typedef _Alloc allocator_type;
490 typedef typename traits_type::int_type int_type;
491 typedef typename traits_type::pos_type pos_type;
492 typedef typename traits_type::off_type off_type;
494 // Non-standard Types:
495 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
496 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
497 typedef basic_iostream<char_type, traits_type> __iostream_type;
500 __stringbuf_type _M_stringbuf;
503 // Constructors/destructors
505 * @brief Default constructor starts with an empty string buffer.
506 * @param mode Whether the buffer can read, or write, or both.
508 * Initializes @c sb using @c mode, and passes @c &sb to the base
509 * class initializer. Does not allocate any buffer.
511 * That's a lie. We initialize the base class with NULL, because the
512 * string class does its own memory management.
515 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
516 : __iostream_type(), _M_stringbuf(__m)
517 { this->init(&_M_stringbuf); }
520 * @brief Starts with an existing string buffer.
521 * @param str A string to copy as a starting buffer.
522 * @param mode Whether the buffer can read, or write, or both.
524 * Initializes @c sb using @a str and @c mode, and passes @c &sb
525 * to the base class initializer.
527 * That's a lie. We initialize the base class with NULL, because the
528 * string class does its own memory management.
531 basic_stringstream(const __string_type& __str,
532 ios_base::openmode __m = ios_base::out | ios_base::in)
533 : __iostream_type(), _M_stringbuf(__str, __m)
534 { this->init(&_M_stringbuf); }
537 * @brief The destructor does nothing.
539 * The buffer is deallocated by the stringbuf object, not the
542 ~basic_stringstream()
547 * @brief Accessing the underlying buffer.
548 * @return The current basic_stringbuf buffer.
550 * This hides both signatures of std::basic_ios::rdbuf().
554 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
557 * @brief Copying out the string buffer.
558 * @return @c rdbuf()->str()
562 { return _M_stringbuf.str(); }
565 * @brief Setting a new buffer.
566 * @param s The string to use as a new sequence.
568 * Calls @c rdbuf()->str(s).
571 str(const __string_type& __s)
572 { _M_stringbuf.str(__s); }
575 _GLIBCXX_END_NAMESPACE
577 #ifndef _GLIBCXX_EXPORT_TEMPLATE
578 # include <bits/sstream.tcc>
581 #endif /* _GLIBCXX_SSTREAM */