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.
39 #ifndef _GLIBCXX_SSTREAM
40 #define _GLIBCXX_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 // _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
;
81 typedef basic_streambuf
<char_type
, traits_type
> __streambuf_type
;
82 typedef basic_string
<char_type
, _Traits
, _Alloc
> __string_type
;
83 typedef typename
__string_type::size_type __size_type
;
89 * Place to stash in || out || in | out settings for current stringbuf.
92 ios_base::openmode _M_mode
;
100 __string_type _M_string
;
105 * @brief Starts with an empty string buffer.
106 * @param mode Whether the buffer can read, or write, or both.
108 * The default constructor initializes the parent class using its
112 basic_stringbuf(ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
113 : __streambuf_type(), _M_string()
114 { _M_stringbuf_init(__mode
); }
117 * @brief Starts with an existing string buffer.
118 * @param str A string to copy as a starting buffer.
119 * @param mode Whether the buffer can read, or write, or both.
121 * This constructor initializes the parent class using its
125 basic_stringbuf(const __string_type
& __str
,
126 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
127 : __streambuf_type(), _M_string(__str
.data(), __str
.size())
128 { _M_stringbuf_init(__mode
); }
132 * @brief Copying out the string buffer.
133 * @return A copy of one of the underlying sequences.
135 * "If the buffer is only created in input mode, the underlying
136 * character sequence is equal to the input sequence; otherwise, it
137 * is equal to the output sequence." [27.7.1.2]/1
142 const bool __testout
= this->_M_mode
& ios_base::out
;
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 for both ctors 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
;
279 const bool __testout
= this->_M_mode
& ios_base::out
;
281 if (__testout
&& this->pptr() > this->egptr())
283 this->setg(this->eback(), this->gptr(), this->pptr());
285 this->setg(this->pptr(), this->pptr(), this->pptr());
290 // [27.7.2] Template class basic_istringstream
292 * @brief Controlling input for std::string.
294 * This class supports reading from objects of type std::basic_string,
295 * using the inherited functions from std::basic_istream. To control
296 * the associated sequence, an instance of std::basic_stringbuf is used,
297 * which this page refers to as @c sb.
299 template<typename _CharT
, typename _Traits
, typename _Alloc
>
300 class basic_istringstream
: public basic_istream
<_CharT
, _Traits
>
304 typedef _CharT char_type
;
305 typedef _Traits traits_type
;
306 // _GLIBCXX_RESOLVE_LIB_DEFECTS
307 // 251. basic_stringbuf missing allocator_type
308 typedef _Alloc allocator_type
;
309 typedef typename
traits_type::int_type int_type
;
310 typedef typename
traits_type::pos_type pos_type
;
311 typedef typename
traits_type::off_type off_type
;
313 // Non-standard types:
314 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
315 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
316 typedef basic_istream
<char_type
, traits_type
> __istream_type
;
324 __stringbuf_type _M_stringbuf
;
329 * @brief Default constructor starts with an empty string buffer.
330 * @param mode Whether the buffer can read, or write, or both.
332 * @c ios_base::in is automatically included in @a mode.
334 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
335 * class initializer. Does not allocate any buffer.
338 * That's a lie. We initialize the base class with NULL, because the
339 * string class does its own memory management.
343 basic_istringstream(ios_base::openmode __mode
= ios_base::in
)
344 : __istream_type(), _M_stringbuf(__mode
| ios_base::in
)
345 { this->init(&_M_stringbuf
); }
348 * @brief Starts with an existing string buffer.
349 * @param str A string to copy as a starting buffer.
350 * @param mode Whether the buffer can read, or write, or both.
352 * @c ios_base::in is automatically included in @a mode.
354 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
355 * to the base class initializer.
358 * That's a lie. We initialize the base class with NULL, because the
359 * string class does its own memory management.
363 basic_istringstream(const __string_type
& __str
,
364 ios_base::openmode __mode
= ios_base::in
)
365 : __istream_type(), _M_stringbuf(__str
, __mode
| ios_base::in
)
366 { this->init(&_M_stringbuf
); }
369 * @brief The destructor does nothing.
371 * The buffer is deallocated by the stringbuf object, not the
374 ~basic_istringstream()
379 * @brief Accessing the underlying buffer.
380 * @return The current basic_stringbuf buffer.
382 * This hides both signatures of std::basic_ios::rdbuf().
386 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
389 * @brief Copying out the string buffer.
390 * @return @c rdbuf()->str()
394 { return _M_stringbuf
.str(); }
397 * @brief Setting a new buffer.
398 * @param s The string to use as a new sequence.
400 * Calls @c rdbuf()->str(s).
403 str(const __string_type
& __s
)
404 { _M_stringbuf
.str(__s
); }
408 // [27.7.3] Template class basic_ostringstream
410 * @brief Controlling output for std::string.
412 * This class supports writing to objects of type std::basic_string,
413 * using the inherited functions from std::basic_ostream. To control
414 * the associated sequence, an instance of std::basic_stringbuf is used,
415 * which this page refers to as @c sb.
417 template <typename _CharT
, typename _Traits
, typename _Alloc
>
418 class basic_ostringstream
: public basic_ostream
<_CharT
, _Traits
>
422 typedef _CharT char_type
;
423 typedef _Traits traits_type
;
424 // _GLIBCXX_RESOLVE_LIB_DEFECTS
425 // 251. basic_stringbuf missing allocator_type
426 typedef _Alloc allocator_type
;
427 typedef typename
traits_type::int_type int_type
;
428 typedef typename
traits_type::pos_type pos_type
;
429 typedef typename
traits_type::off_type off_type
;
431 // Non-standard types:
432 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
433 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
434 typedef basic_ostream
<char_type
, traits_type
> __ostream_type
;
442 __stringbuf_type _M_stringbuf
;
445 // Constructors/destructor:
447 * @brief Default constructor starts with an empty string buffer.
448 * @param mode Whether the buffer can read, or write, or both.
450 * @c ios_base::out is automatically included in @a mode.
452 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
453 * class initializer. Does not allocate any buffer.
456 * That's a lie. We initialize the base class with NULL, because the
457 * string class does its own memory management.
461 basic_ostringstream(ios_base::openmode __mode
= ios_base::out
)
462 : __ostream_type(), _M_stringbuf(__mode
| ios_base::out
)
463 { this->init(&_M_stringbuf
); }
466 * @brief Starts with an existing string buffer.
467 * @param str A string to copy as a starting buffer.
468 * @param mode Whether the buffer can read, or write, or both.
470 * @c ios_base::out is automatically included in @a mode.
472 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
473 * to the base class initializer.
476 * That's a lie. We initialize the base class with NULL, because the
477 * string class does its own memory management.
481 basic_ostringstream(const __string_type
& __str
,
482 ios_base::openmode __mode
= ios_base::out
)
483 : __ostream_type(), _M_stringbuf(__str
, __mode
| ios_base::out
)
484 { this->init(&_M_stringbuf
); }
487 * @brief The destructor does nothing.
489 * The buffer is deallocated by the stringbuf object, not the
492 ~basic_ostringstream()
497 * @brief Accessing the underlying buffer.
498 * @return The current basic_stringbuf buffer.
500 * This hides both signatures of std::basic_ios::rdbuf().
504 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
507 * @brief Copying out the string buffer.
508 * @return @c rdbuf()->str()
512 { return _M_stringbuf
.str(); }
515 * @brief Setting a new buffer.
516 * @param s The string to use as a new sequence.
518 * Calls @c rdbuf()->str(s).
521 str(const __string_type
& __s
)
522 { _M_stringbuf
.str(__s
); }
526 // [27.7.4] Template class basic_stringstream
528 * @brief Controlling input and output for std::string.
530 * This class supports reading from and writing to objects of type
531 * std::basic_string, using the inherited functions from
532 * std::basic_iostream. To control the associated sequence, an instance
533 * of std::basic_stringbuf is used, which this page refers to as @c sb.
535 template <typename _CharT
, typename _Traits
, typename _Alloc
>
536 class basic_stringstream
: public basic_iostream
<_CharT
, _Traits
>
540 typedef _CharT char_type
;
541 typedef _Traits traits_type
;
542 // _GLIBCXX_RESOLVE_LIB_DEFECTS
543 // 251. basic_stringbuf missing allocator_type
544 typedef _Alloc allocator_type
;
545 typedef typename
traits_type::int_type int_type
;
546 typedef typename
traits_type::pos_type pos_type
;
547 typedef typename
traits_type::off_type off_type
;
549 // Non-standard Types:
550 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
551 typedef basic_stringbuf
<_CharT
, _Traits
, _Alloc
> __stringbuf_type
;
552 typedef basic_iostream
<char_type
, traits_type
> __iostream_type
;
560 __stringbuf_type _M_stringbuf
;
563 // Constructors/destructors
565 * @brief Default constructor starts with an empty string buffer.
566 * @param mode Whether the buffer can read, or write, or both.
568 * Initializes @c sb using @c mode, and passes @c &sb to the base
569 * class initializer. Does not allocate any buffer.
572 * That's a lie. We initialize the base class with NULL, because the
573 * string class does its own memory management.
577 basic_stringstream(ios_base::openmode __m
= ios_base::out
| ios_base::in
)
578 : __iostream_type(), _M_stringbuf(__m
)
579 { this->init(&_M_stringbuf
); }
582 * @brief Starts with an existing string buffer.
583 * @param str A string to copy as a starting buffer.
584 * @param mode Whether the buffer can read, or write, or both.
586 * Initializes @c sb using @a str and @c mode, and passes @c &sb
587 * to the base class initializer.
590 * That's a lie. We initialize the base class with NULL, because the
591 * string class does its own memory management.
595 basic_stringstream(const __string_type
& __str
,
596 ios_base::openmode __m
= ios_base::out
| ios_base::in
)
597 : __iostream_type(), _M_stringbuf(__str
, __m
)
598 { this->init(&_M_stringbuf
); }
601 * @brief The destructor does nothing.
603 * The buffer is deallocated by the stringbuf object, not the
606 ~basic_stringstream()
611 * @brief Accessing the underlying buffer.
612 * @return The current basic_stringbuf buffer.
614 * This hides both signatures of std::basic_ios::rdbuf().
618 { return const_cast<__stringbuf_type
*>(&_M_stringbuf
); }
621 * @brief Copying out the string buffer.
622 * @return @c rdbuf()->str()
626 { return _M_stringbuf
.str(); }
629 * @brief Setting a new buffer.
630 * @param s The string to use as a new sequence.
632 * Calls @c rdbuf()->str(s).
635 str(const __string_type
& __s
)
636 { _M_stringbuf
.str(__s
); }
640 #ifndef _GLIBCXX_EXPORT_TEMPLATE
641 # include <bits/sstream.tcc>
644 #endif /* _GLIBCXX_SSTREAM */