1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2002, 2003 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.
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 _GLIBCXX_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
;
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_string()
115 { _M_stringbuf_init(__mode
); }
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_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
143 const bool __testout
= this->_M_mode
& ios_base::out
;
146 // The current egptr() may not be the actual string end.
147 if (this->pptr() > this->egptr())
148 return __string_type(this->pbase(), this->pptr());
150 return __string_type(this->pbase(), this->egptr());
157 * @brief Setting a new buffer.
158 * @param s The string to use as a new sequence.
160 * Deallocates any previous stored sequence, then copies @a s to
164 str(const __string_type
& __s
)
166 // Cannot use _M_string = __s, since v3 strings are COW.
167 _M_string
.assign(__s
.data(), __s
.size());
168 _M_stringbuf_init(this->_M_mode
);
172 // Common initialization code for both ctors goes here.
179 _M_stringbuf_init(ios_base::openmode __mode
)
181 this->_M_mode
= __mode
;
183 __size_type __len
= 0;
184 if (this->_M_mode
& (ios_base::ate
| ios_base::app
))
185 __len
= _M_string
.size();
186 _M_sync(const_cast<char_type
*>(_M_string
.data()), 0, __len
);
189 // [documentation is inherited]
193 // [documentation is inherited]
195 pbackfail(int_type __c
= traits_type::eof());
197 // [documentation is inherited]
199 overflow(int_type __c
= traits_type::eof());
202 * @brief Manipulates the buffer.
203 * @param s Pointer to a buffer area.
204 * @param n Size of @a s.
207 * If no buffer has already been created, and both @a s and @a n are
208 * non-zero, then @c s is used as a buffer; see
209 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
212 virtual __streambuf_type
*
213 setbuf(char_type
* __s
, streamsize __n
)
217 // This is implementation-defined behavior, and assumes
218 // that an external char_type array of length __n exists
219 // and has been pre-allocated. If this is not the case,
220 // things will quickly blow up.
222 // Step 1: Destroy the current internal array.
223 _M_string
= __string_type(__s
, __n
);
225 // Step 2: Use the external array.
231 // [documentation is inherited]
233 seekoff(off_type __off
, ios_base::seekdir __way
,
234 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
236 // [documentation is inherited]
238 seekpos(pos_type __sp
,
239 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
241 // Internal function for correctly updating the internal buffer
242 // for a particular _M_string, due to initialization or
243 // re-sizing of an existing _M_string.
244 // Assumes: contents of _M_string and internal buffer match exactly.
245 // __i == _M_in_cur - _M_in_beg
246 // __o == _M_out_cur - _M_out_beg
253 _M_sync(char_type
* __base
, __size_type __i
, __size_type __o
)
255 const bool __testin
= this->_M_mode
& ios_base::in
;
256 const bool __testout
= this->_M_mode
& ios_base::out
;
257 const __size_type __len
= _M_string
.size();
260 this->setg(__base
, __base
+ __i
, __base
+ __len
);
263 this->setp(__base
, __base
+ _M_string
.capacity());
265 // We need a pointer to the string end anyway, even when
266 // !__testin: in that case, however, for the correct
267 // functioning of the streambuf inlines all the get area
268 // pointers must be identical.
270 this->setg(__base
+ __len
, __base
+ __len
, __base
+ __len
);
274 // Internal function for correctly updating egptr() to the actual
279 const bool __testin
= this->_M_mode
& ios_base::in
;
280 const bool __testout
= this->_M_mode
& ios_base::out
;
282 if (__testout
&& this->pptr() > this->egptr())
284 this->setg(this->eback(), this->gptr(), this->pptr());
286 this->setg(this->pptr(), this->pptr(), this->pptr());
291 // [27.7.2] Template class basic_istringstream
293 * @brief Controlling input for std::string.
295 * This class supports reading from objects of type std::basic_string,
296 * using the inherited functions from std::basic_istream. To control
297 * the associated sequence, an instance of std::basic_stringbuf is used,
298 * which this page refers to as @c sb.
300 template<typename _CharT
, typename _Traits
, typename _Alloc
>
301 class basic_istringstream
: public basic_istream
<_CharT
, _Traits
>
305 typedef _CharT char_type
;
306 typedef _Traits traits_type
;
307 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
308 // 251. basic_stringbuf missing allocator_type
309 typedef _Alloc allocator_type
;
311 typedef typename
traits_type::int_type int_type
;
312 typedef typename
traits_type::pos_type pos_type
;
313 typedef typename
traits_type::off_type off_type
;
315 // Non-standard types:
316 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
317 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
318 typedef basic_istream
<char_type
, traits_type
> __istream_type
;
326 __stringbuf_type _M_stringbuf
;
331 * @brief Default constructor starts with an empty string buffer.
332 * @param mode Whether the buffer can read, or write, or both.
334 * @c ios_base::in is automatically included in @a mode.
336 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
337 * class initializer. Does not allocate any buffer.
340 * That's a lie. We initialize the base class with NULL, because the
341 * string class does its own memory management.
345 basic_istringstream(ios_base::openmode __mode
= ios_base::in
)
346 : __istream_type(), _M_stringbuf(__mode
| ios_base::in
)
347 { this->init(&_M_stringbuf
); }
350 * @brief Starts with an existing string buffer.
351 * @param str A string to copy as a starting buffer.
352 * @param mode Whether the buffer can read, or write, or both.
354 * @c ios_base::in is automatically included in @a mode.
356 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
357 * to the base class initializer.
360 * That's a lie. We initialize the base class with NULL, because the
361 * string class does its own memory management.
365 basic_istringstream(const __string_type
& __str
,
366 ios_base::openmode __mode
= ios_base::in
)
367 : __istream_type(), _M_stringbuf(__str
, __mode
| ios_base::in
)
368 { this->init(&_M_stringbuf
); }
371 * @brief The destructor does nothing.
373 * The buffer is deallocated by the stringbuf object, not the
376 ~basic_istringstream()
381 * @brief Accessing the underlying buffer.
382 * @return The current basic_stringbuf buffer.
384 * This hides both signatures of std::basic_ios::rdbuf().
388 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
391 * @brief Copying out the string buffer.
392 * @return @c rdbuf()->str()
396 { return _M_stringbuf
.str(); }
399 * @brief Setting a new buffer.
400 * @param s The string to use as a new sequence.
402 * Calls @c rdbuf()->str(s).
405 str(const __string_type
& __s
)
406 { _M_stringbuf
.str(__s
); }
410 // [27.7.3] Template class basic_ostringstream
412 * @brief Controlling output for std::string.
414 * This class supports writing to objects of type std::basic_string,
415 * using the inherited functions from std::basic_ostream. To control
416 * the associated sequence, an instance of std::basic_stringbuf is used,
417 * which this page refers to as @c sb.
419 template <typename _CharT
, typename _Traits
, typename _Alloc
>
420 class basic_ostringstream
: public basic_ostream
<_CharT
, _Traits
>
424 typedef _CharT char_type
;
425 typedef _Traits traits_type
;
426 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
427 // 251. basic_stringbuf missing allocator_type
428 typedef _Alloc allocator_type
;
430 typedef typename
traits_type::int_type int_type
;
431 typedef typename
traits_type::pos_type pos_type
;
432 typedef typename
traits_type::off_type off_type
;
434 // Non-standard types:
435 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
436 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
437 typedef basic_ostream
<char_type
, traits_type
> __ostream_type
;
445 __stringbuf_type _M_stringbuf
;
448 // Constructors/destructor:
450 * @brief Default constructor starts with an empty string buffer.
451 * @param mode Whether the buffer can read, or write, or both.
453 * @c ios_base::out is automatically included in @a mode.
455 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
456 * class initializer. Does not allocate any buffer.
459 * That's a lie. We initialize the base class with NULL, because the
460 * string class does its own memory management.
464 basic_ostringstream(ios_base::openmode __mode
= ios_base::out
)
465 : __ostream_type(), _M_stringbuf(__mode
| ios_base::out
)
466 { this->init(&_M_stringbuf
); }
469 * @brief Starts with an existing string buffer.
470 * @param str A string to copy as a starting buffer.
471 * @param mode Whether the buffer can read, or write, or both.
473 * @c ios_base::out is automatically included in @a mode.
475 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
476 * to the base class initializer.
479 * That's a lie. We initialize the base class with NULL, because the
480 * string class does its own memory management.
484 basic_ostringstream(const __string_type
& __str
,
485 ios_base::openmode __mode
= ios_base::out
)
486 : __ostream_type(), _M_stringbuf(__str
, __mode
| ios_base::out
)
487 { this->init(&_M_stringbuf
); }
490 * @brief The destructor does nothing.
492 * The buffer is deallocated by the stringbuf object, not the
495 ~basic_ostringstream()
500 * @brief Accessing the underlying buffer.
501 * @return The current basic_stringbuf buffer.
503 * This hides both signatures of std::basic_ios::rdbuf().
507 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
510 * @brief Copying out the string buffer.
511 * @return @c rdbuf()->str()
515 { return _M_stringbuf
.str(); }
518 * @brief Setting a new buffer.
519 * @param s The string to use as a new sequence.
521 * Calls @c rdbuf()->str(s).
524 str(const __string_type
& __s
)
525 { _M_stringbuf
.str(__s
); }
529 // [27.7.4] Template class basic_stringstream
531 * @brief Controlling input and output for std::string.
533 * This class supports reading from and writing to objects of type
534 * std::basic_string, using the inherited functions from
535 * std::basic_iostream. To control the associated sequence, an instance
536 * of std::basic_stringbuf is used, which this page refers to as @c sb.
538 template <typename _CharT
, typename _Traits
, typename _Alloc
>
539 class basic_stringstream
: public basic_iostream
<_CharT
, _Traits
>
543 typedef _CharT char_type
;
544 typedef _Traits traits_type
;
545 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
546 // 251. basic_stringbuf missing allocator_type
547 typedef _Alloc allocator_type
;
549 typedef typename
traits_type::int_type int_type
;
550 typedef typename
traits_type::pos_type pos_type
;
551 typedef typename
traits_type::off_type off_type
;
553 // Non-standard Types:
554 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
555 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
556 typedef basic_iostream
<char_type
, traits_type
> __iostream_type
;
564 __stringbuf_type _M_stringbuf
;
567 // Constructors/destructors
569 * @brief Default constructor starts with an empty string buffer.
570 * @param mode Whether the buffer can read, or write, or both.
572 * Initializes @c sb using @c mode, and passes @c &sb to the base
573 * class initializer. Does not allocate any buffer.
576 * That's a lie. We initialize the base class with NULL, because the
577 * string class does its own memory management.
581 basic_stringstream(ios_base::openmode __m
= ios_base::out
| ios_base::in
)
582 : __iostream_type(), _M_stringbuf(__m
)
583 { this->init(&_M_stringbuf
); }
586 * @brief Starts with an existing string buffer.
587 * @param str A string to copy as a starting buffer.
588 * @param mode Whether the buffer can read, or write, or both.
590 * Initializes @c sb using @a str and @c mode, and passes @c &sb
591 * to the base class initializer.
594 * That's a lie. We initialize the base class with NULL, because the
595 * string class does its own memory management.
599 basic_stringstream(const __string_type
& __str
,
600 ios_base::openmode __m
= ios_base::out
| ios_base::in
)
601 : __iostream_type(), _M_stringbuf(__str
, __m
)
602 { this->init(&_M_stringbuf
); }
605 * @brief The destructor does nothing.
607 * The buffer is deallocated by the stringbuf object, not the
610 ~basic_stringstream()
615 * @brief Accessing the underlying buffer.
616 * @return The current basic_stringbuf buffer.
618 * This hides both signatures of std::basic_ios::rdbuf().
622 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
625 * @brief Copying out the string buffer.
626 * @return @c rdbuf()->str()
630 { return _M_stringbuf
.str(); }
633 * @brief Setting a new buffer.
634 * @param s The string to use as a new sequence.
636 * Calls @c rdbuf()->str(s).
639 str(const __string_type
& __s
)
640 { _M_stringbuf
.str(__s
); }
644 #ifdef _GLIBCXX_NO_TEMPLATE_EXPORT
647 #ifdef _GLIBCXX_FULLY_COMPLIANT_HEADERS
648 # include <bits/sstream.tcc>