1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.7 String-based streams
35 * This is a Standard C++ Library header. You should @c #include this header
36 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
40 #define _CPP_SSTREAM 1
42 #pragma GCC system_header
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 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
69 // 251. basic_stringbuf missing allocator_type
70 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
;
94 __string_type _M_string
;
99 * @brief Starts with an empty string buffer.
100 * @param mode Whether the buffer can read, or write, or both.
102 * The default constructor initializes the parent class using its
106 basic_stringbuf(ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
107 : __streambuf_type(), _M_string()
108 { _M_stringbuf_init(__mode
); }
111 * @brief Starts with an existing string buffer.
112 * @param str A string to copy as a starting buffer.
113 * @param mode Whether the buffer can read, or write, or both.
115 * This constructor initializes the parent class using its
119 basic_stringbuf(const __string_type
& __str
,
120 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
121 : __streambuf_type(), _M_string(__str
.data(), __str
.size())
122 { _M_stringbuf_init(__mode
); }
126 * @brief Copying out the string buffer.
127 * @return A copy of one of the underlying sequences.
129 * "If the buffer is only created in input mode, the underlying
130 * character sequence is equal to the input sequence; otherwise, it
131 * is equal to the output sequence." [27.7.1.2]/1
136 if (_M_mode
& ios_base::out
)
138 // This is the deal: _M_string.size() is a value that
139 // represents the size of the initial string that makes
140 // _M_string, and may not be the correct size of the
141 // current stringbuf internal buffer.
142 __size_type __len
= _M_string
.size();
143 if (_M_out_cur
> _M_out_beg
)
144 __len
= max(__size_type(_M_out_end
- _M_out_beg
), __len
);
145 return __string_type(_M_out_beg
, _M_out_beg
+ __len
);
152 * @brief Setting a new buffer.
153 * @param s The string to use as a new sequence.
155 * Deallocates any previous stored sequence, then copies @a s to
159 str(const __string_type
& __s
)
161 // Cannot use _M_string = __s, since v3 strings are COW.
162 _M_string
.assign(__s
.data(), __s
.size());
163 _M_stringbuf_init(_M_mode
);
167 // Common initialization code for both ctors goes here.
174 _M_stringbuf_init(ios_base::openmode __mode
)
176 // _M_buf_size is a convenient alias for "what the streambuf
177 // thinks the allocated size of the string really is." This is
178 // necessary as ostringstreams are implemented with the
179 // streambufs having control of the allocation and
180 // re-allocation of the internal string object, _M_string.
181 _M_buf_size
= _M_string
.size();
183 // NB: Start ostringstream buffers at 512 bytes. This is an
184 // experimental value (pronounced "arbitrary" in some of the
185 // hipper english-speaking countries), and can be changed to
186 // suit particular needs.
187 _M_buf_size_opt
= 512;
189 if (_M_mode
& (ios_base::ate
| ios_base::app
))
190 _M_really_sync(0, _M_buf_size
);
192 _M_really_sync(0, 0);
195 // Overridden virtual functions:
196 // [documentation is inherited]
200 if (_M_in_cur
&& _M_in_cur
< _M_in_end
)
201 return traits_type::to_int_type(*gptr());
203 return traits_type::eof();
206 // [documentation is inherited]
208 pbackfail(int_type __c
= traits_type::eof());
210 // [documentation is inherited]
212 overflow(int_type __c
= traits_type::eof());
215 * @brief Manipulates the buffer.
216 * @param s Pointer to a buffer area.
217 * @param n Size of @a s.
220 * If no buffer has already been created, and both @a s and @a n are
221 * non-zero, then @c s is used as a buffer; see
222 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
225 virtual __streambuf_type
*
226 setbuf(char_type
* __s
, streamsize __n
)
230 _M_string
= __string_type(__s
, __n
);
231 _M_really_sync(0, 0);
236 // [documentation is inherited]
238 seekoff(off_type __off
, ios_base::seekdir __way
,
239 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
241 // [documentation is inherited]
243 seekpos(pos_type __sp
,
244 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
246 // Internal function for correctly updating the internal buffer
247 // for a particular _M_string, due to initialization or
248 // re-sizing of an existing _M_string.
249 // Assumes: contents of _M_string and internal buffer match exactly.
250 // __i == _M_in_cur - _M_in_beg
251 // __o == _M_out_cur - _M_out_beg
258 _M_really_sync(__size_type __i
, __size_type __o
)
260 char_type
* __base
= const_cast<char_type
*>(_M_string
.data());
261 bool __testin
= _M_mode
& ios_base::in
;
262 bool __testout
= _M_mode
& ios_base::out
;
263 __size_type __len
= _M_string
.size();
267 this->setg(__base
, __base
+ __i
, __base
+ __len
);
270 this->setp(__base
, __base
+ __len
);
278 // [27.7.2] Template class basic_istringstream
280 * @brief Controlling input for std::string.
282 * This class supports reading from objects of type std::basic_string,
283 * using the inherited functions from std::basic_istream. To control
284 * the associated sequence, an instance of std::basic_stringbuf is used,
285 * which this page refers to as @c sb.
287 template<typename _CharT
, typename _Traits
, typename _Alloc
>
288 class basic_istringstream
: public basic_istream
<_CharT
, _Traits
>
292 typedef _CharT char_type
;
293 typedef _Traits traits_type
;
294 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
295 // 251. basic_stringbuf missing allocator_type
296 typedef _Alloc allocator_type
;
298 typedef typename
traits_type::int_type int_type
;
299 typedef typename
traits_type::pos_type pos_type
;
300 typedef typename
traits_type::off_type off_type
;
302 // Non-standard types:
303 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
304 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
305 typedef basic_istream
<char_type
, traits_type
> __istream_type
;
313 __stringbuf_type _M_stringbuf
;
318 * @brief Default constructor starts with an empty string buffer.
319 * @param mode Whether the buffer can read, or write, or both.
321 * @c ios_base::in is automatically included in @a mode.
323 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
324 * class initializer. Does not allocate any buffer.
327 * That's a lie. We initialize the base class with NULL, because the
328 * string class does its own memory management.
332 basic_istringstream(ios_base::openmode __mode
= ios_base::in
)
333 : __istream_type(NULL
), _M_stringbuf(__mode
| ios_base::in
)
334 { this->init(&_M_stringbuf
); }
337 * @brief Starts with an existing string buffer.
338 * @param str A string to copy as a starting buffer.
339 * @param mode Whether the buffer can read, or write, or both.
341 * @c ios_base::in is automatically included in @a mode.
343 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
344 * to the base class initializer.
347 * That's a lie. We initialize the base class with NULL, because the
348 * string class does its own memory management.
352 basic_istringstream(const __string_type
& __str
,
353 ios_base::openmode __mode
= ios_base::in
)
354 : __istream_type(NULL
), _M_stringbuf(__str
, __mode
| ios_base::in
)
355 { this->init(&_M_stringbuf
); }
358 * @brief The destructor does nothing.
360 * The buffer is deallocated by the stringbuf object, not the
363 ~basic_istringstream()
368 * @brief Accessing the underlying buffer.
369 * @return The current basic_stringbuf buffer.
371 * This hides both signatures of std::basic_ios::rdbuf().
375 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
378 * @brief Copying out the string buffer.
379 * @return @c rdbuf()->str()
383 { return _M_stringbuf
.str(); }
386 * @brief Setting a new buffer.
387 * @param s The string to use as a new sequence.
389 * Calls @c rdbuf()->str(s).
392 str(const __string_type
& __s
)
393 { _M_stringbuf
.str(__s
); }
397 // [27.7.3] Template class basic_ostringstream
399 * @brief Controlling output for std::string.
401 * This class supports writing to objects of type std::basic_string,
402 * using the inherited functions from std::basic_ostream. To control
403 * the associated sequence, an instance of std::basic_stringbuf is used,
404 * which this page refers to as @c sb.
406 template <typename _CharT
, typename _Traits
, typename _Alloc
>
407 class basic_ostringstream
: public basic_ostream
<_CharT
, _Traits
>
411 typedef _CharT char_type
;
412 typedef _Traits traits_type
;
413 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
414 // 251. basic_stringbuf missing allocator_type
415 typedef _Alloc allocator_type
;
417 typedef typename
traits_type::int_type int_type
;
418 typedef typename
traits_type::pos_type pos_type
;
419 typedef typename
traits_type::off_type off_type
;
421 // Non-standard types:
422 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
423 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
424 typedef basic_ostream
<char_type
, traits_type
> __ostream_type
;
432 __stringbuf_type _M_stringbuf
;
435 // Constructors/destructor:
437 * @brief Default constructor starts with an empty string buffer.
438 * @param mode Whether the buffer can read, or write, or both.
440 * @c ios_base::out is automatically included in @a mode.
442 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
443 * class initializer. Does not allocate any buffer.
446 * That's a lie. We initialize the base class with NULL, because the
447 * string class does its own memory management.
451 basic_ostringstream(ios_base::openmode __mode
= ios_base::out
)
452 : __ostream_type(NULL
), _M_stringbuf(__mode
| ios_base::out
)
453 { this->init(&_M_stringbuf
); }
456 * @brief Starts with an existing string buffer.
457 * @param str A string to copy as a starting buffer.
458 * @param mode Whether the buffer can read, or write, or both.
460 * @c ios_base::out is automatically included in @a mode.
462 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
463 * to the base class initializer.
466 * That's a lie. We initialize the base class with NULL, because the
467 * string class does its own memory management.
471 basic_ostringstream(const __string_type
& __str
,
472 ios_base::openmode __mode
= ios_base::out
)
473 : __ostream_type(NULL
), _M_stringbuf(__str
, __mode
| ios_base::out
)
474 { this->init(&_M_stringbuf
); }
477 * @brief The destructor does nothing.
479 * The buffer is deallocated by the stringbuf object, not the
482 ~basic_ostringstream()
487 * @brief Accessing the underlying buffer.
488 * @return The current basic_stringbuf buffer.
490 * This hides both signatures of std::basic_ios::rdbuf().
494 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
497 * @brief Copying out the string buffer.
498 * @return @c rdbuf()->str()
502 { return _M_stringbuf
.str(); }
505 * @brief Setting a new buffer.
506 * @param s The string to use as a new sequence.
508 * Calls @c rdbuf()->str(s).
511 str(const __string_type
& __s
)
512 { _M_stringbuf
.str(__s
); }
516 // [27.7.4] Template class basic_stringstream
518 * @brief Controlling input and output for std::string.
520 * This class supports reading from and writing to objects of type
521 * std::basic_string, using the inherited functions from
522 * std::basic_iostream. To control the associated sequence, an instance
523 * of std::basic_stringbuf is used, which this page refers to as @c sb.
525 template <typename _CharT
, typename _Traits
, typename _Alloc
>
526 class basic_stringstream
: public basic_iostream
<_CharT
, _Traits
>
530 typedef _CharT char_type
;
531 typedef _Traits traits_type
;
532 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
533 // 251. basic_stringbuf missing allocator_type
534 typedef _Alloc allocator_type
;
536 typedef typename
traits_type::int_type int_type
;
537 typedef typename
traits_type::pos_type pos_type
;
538 typedef typename
traits_type::off_type off_type
;
540 // Non-standard Types:
541 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
542 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
543 typedef basic_iostream
<char_type
, traits_type
> __iostream_type
;
551 __stringbuf_type _M_stringbuf
;
554 // Constructors/destructors
556 * @brief Default constructor starts with an empty string buffer.
557 * @param mode Whether the buffer can read, or write, or both.
559 * Initializes @c sb using @c mode, and passes @c &sb to the base
560 * class initializer. Does not allocate any buffer.
563 * That's a lie. We initialize the base class with NULL, because the
564 * string class does its own memory management.
568 basic_stringstream(ios_base::openmode __m
= ios_base::out
| ios_base::in
)
569 : __iostream_type(NULL
), _M_stringbuf(__m
)
570 { this->init(&_M_stringbuf
); }
573 * @brief Starts with an existing string buffer.
574 * @param str A string to copy as a starting buffer.
575 * @param mode Whether the buffer can read, or write, or both.
577 * Initializes @c sb using @a str and @c mode, and passes @c &sb
578 * to the base class initializer.
581 * That's a lie. We initialize the base class with NULL, because the
582 * string class does its own memory management.
586 basic_stringstream(const __string_type
& __str
,
587 ios_base::openmode __m
= ios_base::out
| ios_base::in
)
588 : __iostream_type(NULL
), _M_stringbuf(__str
, __m
)
589 { this->init(&_M_stringbuf
); }
592 * @brief The destructor does nothing.
594 * The buffer is deallocated by the stringbuf object, not the
597 ~basic_stringstream()
602 * @brief Accessing the underlying buffer.
603 * @return The current basic_stringbuf buffer.
605 * This hides both signatures of std::basic_ios::rdbuf().
609 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
612 * @brief Copying out the string buffer.
613 * @return @c rdbuf()->str()
617 { return _M_stringbuf
.str(); }
620 * @brief Setting a new buffer.
621 * @param s The string to use as a new sequence.
623 * Calls @c rdbuf()->str(s).
626 str(const __string_type
& __s
)
627 { _M_stringbuf
.str(__s
); }
631 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
634 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
635 # include <bits/sstream.tcc>