1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2008, 2009, 2010, 2011, 2012 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file include/sstream
27 * This is a Standard C++ Library header.
31 // ISO C++ 14882: 27.7 String-based streams
34 #ifndef _GLIBCXX_SSTREAM
35 #define _GLIBCXX_SSTREAM 1
37 #pragma GCC system_header
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 // [27.7.1] template class basic_stringbuf
48 * @brief The actual work of input and output (for std::string).
51 * @tparam _CharT Type of character stream.
52 * @tparam _Traits Traits for character type, defaults to
53 * char_traits<_CharT>.
54 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
56 * This class associates either or both of its input and output sequences
57 * with a sequence of characters, which can be initialized from, or made
58 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
60 * For this class, open modes (of type @c ios_base::openmode) have
61 * @c in set if the input sequence can be read, and @c out set if the
62 * output sequence can be written.
64 template<typename _CharT, typename _Traits, typename _Alloc>
65 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
69 typedef _CharT char_type;
70 typedef _Traits traits_type;
71 // _GLIBCXX_RESOLVE_LIB_DEFECTS
72 // 251. basic_stringbuf missing allocator_type
73 typedef _Alloc allocator_type;
74 typedef typename traits_type::int_type int_type;
75 typedef typename traits_type::pos_type pos_type;
76 typedef typename traits_type::off_type off_type;
78 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
79 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
80 typedef typename __string_type::size_type __size_type;
83 /// Place to stash in || out || in | out settings for current stringbuf.
84 ios_base::openmode _M_mode;
87 __string_type _M_string;
92 * @brief Starts with an empty string buffer.
93 * @param __mode Whether the buffer can read, or write, or both.
95 * The default constructor initializes the parent class using its
99 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
100 : __streambuf_type(), _M_mode(__mode), _M_string()
104 * @brief Starts with an existing string buffer.
105 * @param __str A string to copy as a starting buffer.
106 * @param __mode Whether the buffer can read, or write, or both.
108 * This constructor initializes the parent class using its
112 basic_stringbuf(const __string_type& __str,
113 ios_base::openmode __mode = ios_base::in | ios_base::out)
114 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
115 { _M_stringbuf_init(__mode); }
119 * @brief Copying out the string buffer.
120 * @return A copy of one of the underlying sequences.
122 * <em>If the buffer is only created in input mode, the underlying
123 * character sequence is equal to the input sequence; otherwise, it
124 * is equal to the output sequence.</em> [27.7.1.2]/1
132 // The current egptr() may not be the actual string end.
133 if (this->pptr() > this->egptr())
134 __ret = __string_type(this->pbase(), this->pptr());
136 __ret = __string_type(this->pbase(), this->egptr());
144 * @brief Setting a new buffer.
145 * @param __s The string to use as a new sequence.
147 * Deallocates any previous stored sequence, then copies @a s to
151 str(const __string_type& __s)
153 // Cannot use _M_string = __s, since v3 strings are COW.
154 _M_string.assign(__s.data(), __s.size());
155 _M_stringbuf_init(_M_mode);
159 // Common initialization code goes here.
161 _M_stringbuf_init(ios_base::openmode __mode)
164 __size_type __len = 0;
165 if (_M_mode & (ios_base::ate | ios_base::app))
166 __len = _M_string.size();
167 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
173 streamsize __ret = -1;
174 if (_M_mode & ios_base::in)
177 __ret = this->egptr() - this->gptr();
186 pbackfail(int_type __c = traits_type::eof());
189 overflow(int_type __c = traits_type::eof());
192 * @brief Manipulates the buffer.
193 * @param __s Pointer to a buffer area.
194 * @param __n Size of @a __s.
197 * If no buffer has already been created, and both @a __s and @a __n are
198 * non-zero, then @c __s is used as a buffer; see
199 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
202 virtual __streambuf_type*
203 setbuf(char_type* __s, streamsize __n)
207 // This is implementation-defined behavior, and assumes
208 // that an external char_type array of length __n exists
209 // and has been pre-allocated. If this is not the case,
210 // things will quickly blow up.
212 // Step 1: Destroy the current internal array.
215 // Step 2: Use the external array.
216 _M_sync(__s, __n, 0);
222 seekoff(off_type __off, ios_base::seekdir __way,
223 ios_base::openmode __mode = ios_base::in | ios_base::out);
226 seekpos(pos_type __sp,
227 ios_base::openmode __mode = ios_base::in | ios_base::out);
229 // Internal function for correctly updating the internal buffer
230 // for a particular _M_string, due to initialization or re-sizing
231 // of an existing _M_string.
233 _M_sync(char_type* __base, __size_type __i, __size_type __o);
235 // Internal function for correctly updating egptr() to the actual
240 const bool __testin = _M_mode & ios_base::in;
241 if (this->pptr() && this->pptr() > this->egptr())
244 this->setg(this->eback(), this->gptr(), this->pptr());
246 this->setg(this->pptr(), this->pptr(), this->pptr());
250 // Works around the issue with pbump, part of the protected
251 // interface of basic_streambuf, taking just an int.
253 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
257 // [27.7.2] Template class basic_istringstream
259 * @brief Controlling input for std::string.
262 * @tparam _CharT Type of character stream.
263 * @tparam _Traits Traits for character type, defaults to
264 * char_traits<_CharT>.
265 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
267 * This class supports reading from objects of type std::basic_string,
268 * using the inherited functions from std::basic_istream. To control
269 * the associated sequence, an instance of std::basic_stringbuf is used,
270 * which this page refers to as @c sb.
272 template<typename _CharT, typename _Traits, typename _Alloc>
273 class basic_istringstream : public basic_istream<_CharT, _Traits>
277 typedef _CharT char_type;
278 typedef _Traits traits_type;
279 // _GLIBCXX_RESOLVE_LIB_DEFECTS
280 // 251. basic_stringbuf missing allocator_type
281 typedef _Alloc allocator_type;
282 typedef typename traits_type::int_type int_type;
283 typedef typename traits_type::pos_type pos_type;
284 typedef typename traits_type::off_type off_type;
286 // Non-standard types:
287 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
288 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
289 typedef basic_istream<char_type, traits_type> __istream_type;
292 __stringbuf_type _M_stringbuf;
297 * @brief Default constructor starts with an empty string buffer.
298 * @param __mode Whether the buffer can read, or write, or both.
300 * @c ios_base::in is automatically included in @a __mode.
302 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
303 * class initializer. Does not allocate any buffer.
305 * That's a lie. We initialize the base class with NULL, because the
306 * string class does its own memory management.
309 basic_istringstream(ios_base::openmode __mode = ios_base::in)
310 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
311 { this->init(&_M_stringbuf); }
314 * @brief Starts with an existing string buffer.
315 * @param __str A string to copy as a starting buffer.
316 * @param __mode Whether the buffer can read, or write, or both.
318 * @c ios_base::in is automatically included in @a mode.
320 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
321 * to the base class initializer.
323 * That's a lie. We initialize the base class with NULL, because the
324 * string class does its own memory management.
327 basic_istringstream(const __string_type& __str,
328 ios_base::openmode __mode = ios_base::in)
329 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
330 { this->init(&_M_stringbuf); }
333 * @brief The destructor does nothing.
335 * The buffer is deallocated by the stringbuf object, not the
338 ~basic_istringstream()
343 * @brief Accessing the underlying buffer.
344 * @return The current basic_stringbuf buffer.
346 * This hides both signatures of std::basic_ios::rdbuf().
350 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
353 * @brief Copying out the string buffer.
354 * @return @c rdbuf()->str()
358 { return _M_stringbuf.str(); }
361 * @brief Setting a new buffer.
362 * @param __s The string to use as a new sequence.
364 * Calls @c rdbuf()->str(s).
367 str(const __string_type& __s)
368 { _M_stringbuf.str(__s); }
372 // [27.7.3] Template class basic_ostringstream
374 * @brief Controlling output for std::string.
377 * @tparam _CharT Type of character stream.
378 * @tparam _Traits Traits for character type, defaults to
379 * char_traits<_CharT>.
380 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
382 * This class supports writing to objects of type std::basic_string,
383 * using the inherited functions from std::basic_ostream. To control
384 * the associated sequence, an instance of std::basic_stringbuf is used,
385 * which this page refers to as @c sb.
387 template <typename _CharT, typename _Traits, typename _Alloc>
388 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
392 typedef _CharT char_type;
393 typedef _Traits traits_type;
394 // _GLIBCXX_RESOLVE_LIB_DEFECTS
395 // 251. basic_stringbuf missing allocator_type
396 typedef _Alloc allocator_type;
397 typedef typename traits_type::int_type int_type;
398 typedef typename traits_type::pos_type pos_type;
399 typedef typename traits_type::off_type off_type;
401 // Non-standard types:
402 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
403 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
404 typedef basic_ostream<char_type, traits_type> __ostream_type;
407 __stringbuf_type _M_stringbuf;
410 // Constructors/destructor:
412 * @brief Default constructor starts with an empty string buffer.
413 * @param __mode Whether the buffer can read, or write, or both.
415 * @c ios_base::out is automatically included in @a mode.
417 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
418 * class initializer. Does not allocate any buffer.
420 * That's a lie. We initialize the base class with NULL, because the
421 * string class does its own memory management.
424 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
425 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
426 { this->init(&_M_stringbuf); }
429 * @brief Starts with an existing string buffer.
430 * @param __str A string to copy as a starting buffer.
431 * @param __mode Whether the buffer can read, or write, or both.
433 * @c ios_base::out is automatically included in @a mode.
435 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
436 * to the base class initializer.
438 * That's a lie. We initialize the base class with NULL, because the
439 * string class does its own memory management.
442 basic_ostringstream(const __string_type& __str,
443 ios_base::openmode __mode = ios_base::out)
444 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
445 { this->init(&_M_stringbuf); }
448 * @brief The destructor does nothing.
450 * The buffer is deallocated by the stringbuf object, not the
453 ~basic_ostringstream()
458 * @brief Accessing the underlying buffer.
459 * @return The current basic_stringbuf buffer.
461 * This hides both signatures of std::basic_ios::rdbuf().
465 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
468 * @brief Copying out the string buffer.
469 * @return @c rdbuf()->str()
473 { return _M_stringbuf.str(); }
476 * @brief Setting a new buffer.
477 * @param __s The string to use as a new sequence.
479 * Calls @c rdbuf()->str(s).
482 str(const __string_type& __s)
483 { _M_stringbuf.str(__s); }
487 // [27.7.4] Template class basic_stringstream
489 * @brief Controlling input and output for std::string.
492 * @tparam _CharT Type of character stream.
493 * @tparam _Traits Traits for character type, defaults to
494 * char_traits<_CharT>.
495 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
497 * This class supports reading from and writing to objects of type
498 * std::basic_string, using the inherited functions from
499 * std::basic_iostream. To control the associated sequence, an instance
500 * of std::basic_stringbuf is used, which this page refers to as @c sb.
502 template <typename _CharT, typename _Traits, typename _Alloc>
503 class basic_stringstream : public basic_iostream<_CharT, _Traits>
507 typedef _CharT char_type;
508 typedef _Traits traits_type;
509 // _GLIBCXX_RESOLVE_LIB_DEFECTS
510 // 251. basic_stringbuf missing allocator_type
511 typedef _Alloc allocator_type;
512 typedef typename traits_type::int_type int_type;
513 typedef typename traits_type::pos_type pos_type;
514 typedef typename traits_type::off_type off_type;
516 // Non-standard Types:
517 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
518 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
519 typedef basic_iostream<char_type, traits_type> __iostream_type;
522 __stringbuf_type _M_stringbuf;
525 // Constructors/destructors
527 * @brief Default constructor starts with an empty string buffer.
528 * @param __m Whether the buffer can read, or write, or both.
530 * Initializes @c sb using the mode from @c __m, and passes @c
531 * &sb to the base class initializer. Does not allocate any
534 * That's a lie. We initialize the base class with NULL, because the
535 * string class does its own memory management.
538 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
539 : __iostream_type(), _M_stringbuf(__m)
540 { this->init(&_M_stringbuf); }
543 * @brief Starts with an existing string buffer.
544 * @param __str A string to copy as a starting buffer.
545 * @param __m Whether the buffer can read, or write, or both.
547 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
548 * to the base class initializer.
550 * That's a lie. We initialize the base class with NULL, because the
551 * string class does its own memory management.
554 basic_stringstream(const __string_type& __str,
555 ios_base::openmode __m = ios_base::out | ios_base::in)
556 : __iostream_type(), _M_stringbuf(__str, __m)
557 { this->init(&_M_stringbuf); }
560 * @brief The destructor does nothing.
562 * The buffer is deallocated by the stringbuf object, not the
565 ~basic_stringstream()
570 * @brief Accessing the underlying buffer.
571 * @return The current basic_stringbuf buffer.
573 * This hides both signatures of std::basic_ios::rdbuf().
577 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
580 * @brief Copying out the string buffer.
581 * @return @c rdbuf()->str()
585 { return _M_stringbuf.str(); }
588 * @brief Setting a new buffer.
589 * @param __s The string to use as a new sequence.
591 * Calls @c rdbuf()->str(s).
594 str(const __string_type& __s)
595 { _M_stringbuf.str(__s); }
598 _GLIBCXX_END_NAMESPACE_VERSION
601 #include <bits/sstream.tcc>
603 #endif /* _GLIBCXX_SSTREAM */