1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2002, 2003, 2004
4 // 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 along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 // ISO C++ 14882: 27.7 String-based streams
36 * This is a Standard C++ Library header. You should @c #include this header
37 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
40 #ifndef _GLIBCXX_SSTREAM
41 #define _GLIBCXX_SSTREAM 1
43 #pragma GCC system_header
50 // [27.7.1] template class basic_stringbuf
52 * @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;
82 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
83 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
84 typedef typename __string_type::size_type __size_type;
90 * Place to stash in || out || in | out settings for current stringbuf.
93 ios_base::openmode _M_mode;
101 __string_type _M_string;
106 * @brief Starts with an empty string buffer.
107 * @param mode Whether the buffer can read, or write, or both.
109 * The default constructor initializes the parent class using its
113 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
114 : __streambuf_type(), _M_mode(__mode), _M_string()
118 * @brief Starts with an existing string buffer.
119 * @param str A string to copy as a starting buffer.
120 * @param mode Whether the buffer can read, or write, or both.
122 * This constructor initializes the parent class using its
126 basic_stringbuf(const __string_type& __str,
127 ios_base::openmode __mode = ios_base::in | ios_base::out)
128 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
129 { _M_stringbuf_init(__mode); }
133 * @brief Copying out the string buffer.
134 * @return A copy of one of the underlying sequences.
136 * "If the buffer is only created in input mode, the underlying
137 * character sequence is equal to the input sequence; otherwise, it
138 * is equal to the output sequence." [27.7.1.2]/1
145 // The current egptr() may not be the actual string end.
146 if (this->pptr() > this->egptr())
147 return __string_type(this->pbase(), this->pptr());
149 return __string_type(this->pbase(), this->egptr());
156 * @brief Setting a new buffer.
157 * @param s The string to use as a new sequence.
159 * Deallocates any previous stored sequence, then copies @a s to
163 str(const __string_type& __s)
165 // Cannot use _M_string = __s, since v3 strings are COW.
166 _M_string.assign(__s.data(), __s.size());
167 _M_stringbuf_init(this->_M_mode);
171 // Common initialization code goes here.
178 _M_stringbuf_init(ios_base::openmode __mode)
180 this->_M_mode = __mode;
182 __size_type __len = 0;
183 if (this->_M_mode & (ios_base::ate | ios_base::app))
184 __len = _M_string.size();
185 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
188 // [documentation is inherited]
192 // [documentation is inherited]
194 pbackfail(int_type __c = traits_type::eof());
196 // [documentation is inherited]
198 overflow(int_type __c = traits_type::eof());
201 * @brief Manipulates the buffer.
202 * @param s Pointer to a buffer area.
203 * @param n Size of @a s.
206 * If no buffer has already been created, and both @a s and @a n are
207 * non-zero, then @c s is used as a buffer; see
208 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
211 virtual __streambuf_type*
212 setbuf(char_type* __s, streamsize __n)
216 // This is implementation-defined behavior, and assumes
217 // that an external char_type array of length __n exists
218 // and has been pre-allocated. If this is not the case,
219 // things will quickly blow up.
221 // Step 1: Destroy the current internal array.
222 _M_string = __string_type(__s, __n);
224 // Step 2: Use the external array.
230 // [documentation is inherited]
232 seekoff(off_type __off, ios_base::seekdir __way,
233 ios_base::openmode __mode = ios_base::in | ios_base::out);
235 // [documentation is inherited]
237 seekpos(pos_type __sp,
238 ios_base::openmode __mode = ios_base::in | ios_base::out);
240 // Internal function for correctly updating the internal buffer
241 // for a particular _M_string, due to initialization or
242 // re-sizing of an existing _M_string.
243 // Assumes: contents of _M_string and internal buffer match exactly.
244 // __i == _M_in_cur - _M_in_beg
245 // __o == _M_out_cur - _M_out_beg
252 _M_sync(char_type* __base, __size_type __i, __size_type __o)
254 const bool __testin = this->_M_mode & ios_base::in;
255 const bool __testout = this->_M_mode & ios_base::out;
256 const __size_type __len = _M_string.size();
259 this->setg(__base, __base + __i, __base + __len);
262 this->setp(__base, __base + _M_string.capacity());
264 // We need a pointer to the string end anyway, even when
265 // !__testin: in that case, however, for the correct
266 // functioning of the streambuf inlines all the get area
267 // pointers must be identical.
269 this->setg(__base + __len, __base + __len, __base + __len);
273 // Internal function for correctly updating egptr() to the actual
278 const bool __testin = this->_M_mode & ios_base::in;
280 if (this->pptr() && this->pptr() > this->egptr())
282 this->setg(this->eback(), this->gptr(), this->pptr());
284 this->setg(this->pptr(), this->pptr(), this->pptr());
289 // [27.7.2] Template class basic_istringstream
291 * @brief Controlling input for std::string.
293 * This class supports reading from objects of type std::basic_string,
294 * using the inherited functions from std::basic_istream. To control
295 * the associated sequence, an instance of std::basic_stringbuf is used,
296 * which this page refers to as @c sb.
298 template<typename _CharT, typename _Traits, typename _Alloc>
299 class basic_istringstream : public basic_istream<_CharT, _Traits>
303 typedef _CharT char_type;
304 typedef _Traits traits_type;
305 // _GLIBCXX_RESOLVE_LIB_DEFECTS
306 // 251. basic_stringbuf missing allocator_type
307 typedef _Alloc allocator_type;
308 typedef typename traits_type::int_type int_type;
309 typedef typename traits_type::pos_type pos_type;
310 typedef typename traits_type::off_type off_type;
312 // Non-standard types:
313 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
314 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
315 typedef basic_istream<char_type, traits_type> __istream_type;
323 __stringbuf_type _M_stringbuf;
328 * @brief Default constructor starts with an empty string buffer.
329 * @param mode Whether the buffer can read, or write, or both.
331 * @c ios_base::in is automatically included in @a mode.
333 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
334 * class initializer. Does not allocate any buffer.
337 * That's a lie. We initialize the base class with NULL, because the
338 * string class does its own memory management.
342 basic_istringstream(ios_base::openmode __mode = ios_base::in)
343 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
344 { this->init(&_M_stringbuf); }
347 * @brief Starts with an existing string buffer.
348 * @param str A string to copy as a starting buffer.
349 * @param mode Whether the buffer can read, or write, or both.
351 * @c ios_base::in is automatically included in @a mode.
353 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
354 * to the base class initializer.
357 * That's a lie. We initialize the base class with NULL, because the
358 * string class does its own memory management.
362 basic_istringstream(const __string_type& __str,
363 ios_base::openmode __mode = ios_base::in)
364 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
365 { this->init(&_M_stringbuf); }
368 * @brief The destructor does nothing.
370 * The buffer is deallocated by the stringbuf object, not the
373 ~basic_istringstream()
378 * @brief Accessing the underlying buffer.
379 * @return The current basic_stringbuf buffer.
381 * This hides both signatures of std::basic_ios::rdbuf().
385 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
388 * @brief Copying out the string buffer.
389 * @return @c rdbuf()->str()
393 { return _M_stringbuf.str(); }
396 * @brief Setting a new buffer.
397 * @param s The string to use as a new sequence.
399 * Calls @c rdbuf()->str(s).
402 str(const __string_type& __s)
403 { _M_stringbuf.str(__s); }
407 // [27.7.3] Template class basic_ostringstream
409 * @brief Controlling output for std::string.
411 * This class supports writing to objects of type std::basic_string,
412 * using the inherited functions from std::basic_ostream. To control
413 * the associated sequence, an instance of std::basic_stringbuf is used,
414 * which this page refers to as @c sb.
416 template <typename _CharT, typename _Traits, typename _Alloc>
417 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
421 typedef _CharT char_type;
422 typedef _Traits traits_type;
423 // _GLIBCXX_RESOLVE_LIB_DEFECTS
424 // 251. basic_stringbuf missing allocator_type
425 typedef _Alloc allocator_type;
426 typedef typename traits_type::int_type int_type;
427 typedef typename traits_type::pos_type pos_type;
428 typedef typename traits_type::off_type off_type;
430 // Non-standard types:
431 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
432 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
433 typedef basic_ostream<char_type, traits_type> __ostream_type;
441 __stringbuf_type _M_stringbuf;
444 // Constructors/destructor:
446 * @brief Default constructor starts with an empty string buffer.
447 * @param mode Whether the buffer can read, or write, or both.
449 * @c ios_base::out is automatically included in @a mode.
451 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
452 * class initializer. Does not allocate any buffer.
455 * That's a lie. We initialize the base class with NULL, because the
456 * string class does its own memory management.
460 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
461 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
462 { this->init(&_M_stringbuf); }
465 * @brief Starts with an existing string buffer.
466 * @param str A string to copy as a starting buffer.
467 * @param mode Whether the buffer can read, or write, or both.
469 * @c ios_base::out is automatically included in @a mode.
471 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
472 * to the base class initializer.
475 * That's a lie. We initialize the base class with NULL, because the
476 * string class does its own memory management.
480 basic_ostringstream(const __string_type& __str,
481 ios_base::openmode __mode = ios_base::out)
482 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
483 { this->init(&_M_stringbuf); }
486 * @brief The destructor does nothing.
488 * The buffer is deallocated by the stringbuf object, not the
491 ~basic_ostringstream()
496 * @brief Accessing the underlying buffer.
497 * @return The current basic_stringbuf buffer.
499 * This hides both signatures of std::basic_ios::rdbuf().
503 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
506 * @brief Copying out the string buffer.
507 * @return @c rdbuf()->str()
511 { return _M_stringbuf.str(); }
514 * @brief Setting a new buffer.
515 * @param s The string to use as a new sequence.
517 * Calls @c rdbuf()->str(s).
520 str(const __string_type& __s)
521 { _M_stringbuf.str(__s); }
525 // [27.7.4] Template class basic_stringstream
527 * @brief Controlling input and output for std::string.
529 * This class supports reading from and writing to objects of type
530 * std::basic_string, using the inherited functions from
531 * std::basic_iostream. To control the associated sequence, an instance
532 * of std::basic_stringbuf is used, which this page refers to as @c sb.
534 template <typename _CharT, typename _Traits, typename _Alloc>
535 class basic_stringstream : public basic_iostream<_CharT, _Traits>
539 typedef _CharT char_type;
540 typedef _Traits traits_type;
541 // _GLIBCXX_RESOLVE_LIB_DEFECTS
542 // 251. basic_stringbuf missing allocator_type
543 typedef _Alloc allocator_type;
544 typedef typename traits_type::int_type int_type;
545 typedef typename traits_type::pos_type pos_type;
546 typedef typename traits_type::off_type off_type;
548 // Non-standard Types:
549 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
550 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
551 typedef basic_iostream<char_type, traits_type> __iostream_type;
559 __stringbuf_type _M_stringbuf;
562 // Constructors/destructors
564 * @brief Default constructor starts with an empty string buffer.
565 * @param mode Whether the buffer can read, or write, or both.
567 * Initializes @c sb using @c mode, and passes @c &sb to the base
568 * class initializer. Does not allocate any buffer.
571 * That's a lie. We initialize the base class with NULL, because the
572 * string class does its own memory management.
576 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
577 : __iostream_type(), _M_stringbuf(__m)
578 { this->init(&_M_stringbuf); }
581 * @brief Starts with an existing string buffer.
582 * @param str A string to copy as a starting buffer.
583 * @param mode Whether the buffer can read, or write, or both.
585 * Initializes @c sb using @a str and @c mode, and passes @c &sb
586 * to the base class initializer.
589 * That's a lie. We initialize the base class with NULL, because the
590 * string class does its own memory management.
594 basic_stringstream(const __string_type& __str,
595 ios_base::openmode __m = ios_base::out | ios_base::in)
596 : __iostream_type(), _M_stringbuf(__str, __m)
597 { this->init(&_M_stringbuf); }
600 * @brief The destructor does nothing.
602 * The buffer is deallocated by the stringbuf object, not the
605 ~basic_stringstream()
610 * @brief Accessing the underlying buffer.
611 * @return The current basic_stringbuf buffer.
613 * This hides both signatures of std::basic_ios::rdbuf().
617 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
620 * @brief Copying out the string buffer.
621 * @return @c rdbuf()->str()
625 { return _M_stringbuf.str(); }
628 * @brief Setting a new buffer.
629 * @param s The string to use as a new sequence.
631 * Calls @c rdbuf()->str(s).
634 str(const __string_type& __s)
635 { _M_stringbuf.str(__s); }
639 #ifndef _GLIBCXX_EXPORT_TEMPLATE
640 # include <bits/sstream.tcc>
643 #endif /* _GLIBCXX_SSTREAM */