1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2008 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).
53 * This class associates either or both of its input and output sequences
54 * with a sequence of characters, which can be initialized from, or made
55 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
57 * For this class, open modes (of type @c ios_base::openmode) have
58 * @c in set if the input sequence can be read, and @c out set if the
59 * output sequence can be written.
61 template<typename _CharT, typename _Traits, typename _Alloc>
62 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
66 typedef _CharT char_type;
67 typedef _Traits traits_type;
68 // _GLIBCXX_RESOLVE_LIB_DEFECTS
69 // 251. basic_stringbuf missing allocator_type
70 typedef _Alloc allocator_type;
71 typedef typename traits_type::int_type int_type;
72 typedef typename traits_type::pos_type pos_type;
73 typedef typename traits_type::off_type off_type;
75 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
76 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
77 typedef typename __string_type::size_type __size_type;
80 /// Place to stash in || out || in | out settings for current stringbuf.
81 ios_base::openmode _M_mode;
84 __string_type _M_string;
89 * @brief Starts with an empty string buffer.
90 * @param mode Whether the buffer can read, or write, or both.
92 * The default constructor initializes the parent class using its
96 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
97 : __streambuf_type(), _M_mode(__mode), _M_string()
101 * @brief Starts with an existing string buffer.
102 * @param str A string to copy as a starting buffer.
103 * @param mode Whether the buffer can read, or write, or both.
105 * This constructor initializes the parent class using its
109 basic_stringbuf(const __string_type& __str,
110 ios_base::openmode __mode = ios_base::in | ios_base::out)
111 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
112 { _M_stringbuf_init(__mode); }
116 * @brief Copying out the string buffer.
117 * @return A copy of one of the underlying sequences.
119 * "If the buffer is only created in input mode, the underlying
120 * character sequence is equal to the input sequence; otherwise, it
121 * is equal to the output sequence." [27.7.1.2]/1
129 // The current egptr() may not be the actual string end.
130 if (this->pptr() > this->egptr())
131 __ret = __string_type(this->pbase(), this->pptr());
133 __ret = __string_type(this->pbase(), this->egptr());
141 * @brief Setting a new buffer.
142 * @param s The string to use as a new sequence.
144 * Deallocates any previous stored sequence, then copies @a s to
148 str(const __string_type& __s)
150 // Cannot use _M_string = __s, since v3 strings are COW.
151 _M_string.assign(__s.data(), __s.size());
152 _M_stringbuf_init(_M_mode);
156 // Common initialization code goes here.
158 _M_stringbuf_init(ios_base::openmode __mode)
161 __size_type __len = 0;
162 if (_M_mode & (ios_base::ate | ios_base::app))
163 __len = _M_string.size();
164 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
170 streamsize __ret = -1;
171 if (_M_mode & ios_base::in)
174 __ret = this->egptr() - this->gptr();
183 pbackfail(int_type __c = traits_type::eof());
186 overflow(int_type __c = traits_type::eof());
189 * @brief Manipulates the buffer.
190 * @param s Pointer to a buffer area.
191 * @param n Size of @a s.
194 * If no buffer has already been created, and both @a s and @a n are
195 * non-zero, then @c s is used as a buffer; see
196 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
199 virtual __streambuf_type*
200 setbuf(char_type* __s, streamsize __n)
204 // This is implementation-defined behavior, and assumes
205 // that an external char_type array of length __n exists
206 // and has been pre-allocated. If this is not the case,
207 // things will quickly blow up.
209 // Step 1: Destroy the current internal array.
212 // Step 2: Use the external array.
213 _M_sync(__s, __n, 0);
219 seekoff(off_type __off, ios_base::seekdir __way,
220 ios_base::openmode __mode = ios_base::in | ios_base::out);
223 seekpos(pos_type __sp,
224 ios_base::openmode __mode = ios_base::in | ios_base::out);
226 // Internal function for correctly updating the internal buffer
227 // for a particular _M_string, due to initialization or re-sizing
228 // of an existing _M_string.
230 _M_sync(char_type* __base, __size_type __i, __size_type __o);
232 // Internal function for correctly updating egptr() to the actual
237 const bool __testin = _M_mode & ios_base::in;
238 if (this->pptr() && this->pptr() > this->egptr())
241 this->setg(this->eback(), this->gptr(), this->pptr());
243 this->setg(this->pptr(), this->pptr(), this->pptr());
249 // [27.7.2] Template class basic_istringstream
251 * @brief Controlling input for std::string.
253 * This class supports reading from objects of type std::basic_string,
254 * using the inherited functions from std::basic_istream. To control
255 * the associated sequence, an instance of std::basic_stringbuf is used,
256 * which this page refers to as @c sb.
258 template<typename _CharT, typename _Traits, typename _Alloc>
259 class basic_istringstream : public basic_istream<_CharT, _Traits>
263 typedef _CharT char_type;
264 typedef _Traits traits_type;
265 // _GLIBCXX_RESOLVE_LIB_DEFECTS
266 // 251. basic_stringbuf missing allocator_type
267 typedef _Alloc allocator_type;
268 typedef typename traits_type::int_type int_type;
269 typedef typename traits_type::pos_type pos_type;
270 typedef typename traits_type::off_type off_type;
272 // Non-standard types:
273 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
274 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
275 typedef basic_istream<char_type, traits_type> __istream_type;
278 __stringbuf_type _M_stringbuf;
283 * @brief Default constructor starts with an empty string buffer.
284 * @param mode Whether the buffer can read, or write, or both.
286 * @c ios_base::in is automatically included in @a mode.
288 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
289 * class initializer. Does not allocate any buffer.
291 * That's a lie. We initialize the base class with NULL, because the
292 * string class does its own memory management.
295 basic_istringstream(ios_base::openmode __mode = ios_base::in)
296 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
297 { this->init(&_M_stringbuf); }
300 * @brief Starts with an existing string buffer.
301 * @param str A string to copy as a starting buffer.
302 * @param mode Whether the buffer can read, or write, or both.
304 * @c ios_base::in is automatically included in @a mode.
306 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
307 * to the base class initializer.
309 * That's a lie. We initialize the base class with NULL, because the
310 * string class does its own memory management.
313 basic_istringstream(const __string_type& __str,
314 ios_base::openmode __mode = ios_base::in)
315 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
316 { this->init(&_M_stringbuf); }
319 * @brief The destructor does nothing.
321 * The buffer is deallocated by the stringbuf object, not the
324 ~basic_istringstream()
329 * @brief Accessing the underlying buffer.
330 * @return The current basic_stringbuf buffer.
332 * This hides both signatures of std::basic_ios::rdbuf().
336 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
339 * @brief Copying out the string buffer.
340 * @return @c rdbuf()->str()
344 { return _M_stringbuf.str(); }
347 * @brief Setting a new buffer.
348 * @param s The string to use as a new sequence.
350 * Calls @c rdbuf()->str(s).
353 str(const __string_type& __s)
354 { _M_stringbuf.str(__s); }
358 // [27.7.3] Template class basic_ostringstream
360 * @brief Controlling output for std::string.
362 * This class supports writing to objects of type std::basic_string,
363 * using the inherited functions from std::basic_ostream. To control
364 * the associated sequence, an instance of std::basic_stringbuf is used,
365 * which this page refers to as @c sb.
367 template <typename _CharT, typename _Traits, typename _Alloc>
368 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
372 typedef _CharT char_type;
373 typedef _Traits traits_type;
374 // _GLIBCXX_RESOLVE_LIB_DEFECTS
375 // 251. basic_stringbuf missing allocator_type
376 typedef _Alloc allocator_type;
377 typedef typename traits_type::int_type int_type;
378 typedef typename traits_type::pos_type pos_type;
379 typedef typename traits_type::off_type off_type;
381 // Non-standard types:
382 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
383 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
384 typedef basic_ostream<char_type, traits_type> __ostream_type;
387 __stringbuf_type _M_stringbuf;
390 // Constructors/destructor:
392 * @brief Default constructor starts with an empty string buffer.
393 * @param mode Whether the buffer can read, or write, or both.
395 * @c ios_base::out is automatically included in @a mode.
397 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
398 * class initializer. Does not allocate any buffer.
400 * That's a lie. We initialize the base class with NULL, because the
401 * string class does its own memory management.
404 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
405 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
406 { this->init(&_M_stringbuf); }
409 * @brief Starts with an existing string buffer.
410 * @param str A string to copy as a starting buffer.
411 * @param mode Whether the buffer can read, or write, or both.
413 * @c ios_base::out is automatically included in @a mode.
415 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
416 * to the base class initializer.
418 * That's a lie. We initialize the base class with NULL, because the
419 * string class does its own memory management.
422 basic_ostringstream(const __string_type& __str,
423 ios_base::openmode __mode = ios_base::out)
424 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
425 { this->init(&_M_stringbuf); }
428 * @brief The destructor does nothing.
430 * The buffer is deallocated by the stringbuf object, not the
433 ~basic_ostringstream()
438 * @brief Accessing the underlying buffer.
439 * @return The current basic_stringbuf buffer.
441 * This hides both signatures of std::basic_ios::rdbuf().
445 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
448 * @brief Copying out the string buffer.
449 * @return @c rdbuf()->str()
453 { return _M_stringbuf.str(); }
456 * @brief Setting a new buffer.
457 * @param s The string to use as a new sequence.
459 * Calls @c rdbuf()->str(s).
462 str(const __string_type& __s)
463 { _M_stringbuf.str(__s); }
467 // [27.7.4] Template class basic_stringstream
469 * @brief Controlling input and output for std::string.
471 * This class supports reading from and writing to objects of type
472 * std::basic_string, using the inherited functions from
473 * std::basic_iostream. To control the associated sequence, an instance
474 * of std::basic_stringbuf is used, which this page refers to as @c sb.
476 template <typename _CharT, typename _Traits, typename _Alloc>
477 class basic_stringstream : public basic_iostream<_CharT, _Traits>
481 typedef _CharT char_type;
482 typedef _Traits traits_type;
483 // _GLIBCXX_RESOLVE_LIB_DEFECTS
484 // 251. basic_stringbuf missing allocator_type
485 typedef _Alloc allocator_type;
486 typedef typename traits_type::int_type int_type;
487 typedef typename traits_type::pos_type pos_type;
488 typedef typename traits_type::off_type off_type;
490 // Non-standard Types:
491 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
492 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
493 typedef basic_iostream<char_type, traits_type> __iostream_type;
496 __stringbuf_type _M_stringbuf;
499 // Constructors/destructors
501 * @brief Default constructor starts with an empty string buffer.
502 * @param mode Whether the buffer can read, or write, or both.
504 * Initializes @c sb using @c mode, and passes @c &sb to the base
505 * class initializer. Does not allocate any buffer.
507 * That's a lie. We initialize the base class with NULL, because the
508 * string class does its own memory management.
511 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
512 : __iostream_type(), _M_stringbuf(__m)
513 { this->init(&_M_stringbuf); }
516 * @brief Starts with an existing string buffer.
517 * @param str A string to copy as a starting buffer.
518 * @param mode Whether the buffer can read, or write, or both.
520 * Initializes @c sb using @a str and @c mode, and passes @c &sb
521 * to the base class initializer.
523 * That's a lie. We initialize the base class with NULL, because the
524 * string class does its own memory management.
527 basic_stringstream(const __string_type& __str,
528 ios_base::openmode __m = ios_base::out | ios_base::in)
529 : __iostream_type(), _M_stringbuf(__str, __m)
530 { this->init(&_M_stringbuf); }
533 * @brief The destructor does nothing.
535 * The buffer is deallocated by the stringbuf object, not the
538 ~basic_stringstream()
543 * @brief Accessing the underlying buffer.
544 * @return The current basic_stringbuf buffer.
546 * This hides both signatures of std::basic_ios::rdbuf().
550 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
553 * @brief Copying out the string buffer.
554 * @return @c rdbuf()->str()
558 { return _M_stringbuf.str(); }
561 * @brief Setting a new buffer.
562 * @param s The string to use as a new sequence.
564 * Calls @c rdbuf()->str(s).
567 str(const __string_type& __s)
568 { _M_stringbuf.str(__s); }
571 _GLIBCXX_END_NAMESPACE
573 #ifndef _GLIBCXX_EXPORT_TEMPLATE
574 # include <bits/sstream.tcc>
577 #endif /* _GLIBCXX_SSTREAM */