]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/sstream
PR libstdc++/36104 part four
[thirdparty/gcc.git] / libstdc++-v3 / include / std / sstream
CommitLineData
54c1bf78 1// String based streams -*- C++ -*-
de96ac46 2
4312e020 3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
ee3ee948 4// 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
de96ac46
BK
5//
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
748086b7 9// Free Software Foundation; either version 3, or (at your option)
de96ac46
BK
10// any later version.
11
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.
16
748086b7
JJ
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
de96ac46 25
f910786b 26/** @file include/sstream
0aa06b18 27 * This is a Standard C++ Library header.
2f9d51b8
PE
28 */
29
143c27b0
BK
30//
31// ISO C++ 14882: 27.7 String-based streams
32//
33
1143680e
SE
34#ifndef _GLIBCXX_SSTREAM
35#define _GLIBCXX_SSTREAM 1
54c1bf78
BK
36
37#pragma GCC system_header
38
39#include <istream>
40#include <ostream>
41
12ffa228
BK
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 45
840ceb34
PE
46 // [27.7.1] template class basic_stringbuf
47 /**
48 * @brief The actual work of input and output (for std::string).
5b9daa7e 49 * @ingroup io
840ceb34
PE
50 *
51 * This class associates either or both of its input and output sequences
52 * with a sequence of characters, which can be initialized from, or made
53 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
54 *
55 * For this class, open modes (of type @c ios_base::openmode) have
56 * @c in set if the input sequence can be read, and @c out set if the
57 * output sequence can be written.
58 */
54c1bf78
BK
59 template<typename _CharT, typename _Traits, typename _Alloc>
60 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
61 {
62 public:
63 // Types:
64 typedef _CharT char_type;
65 typedef _Traits traits_type;
f5677b15
PC
66 // _GLIBCXX_RESOLVE_LIB_DEFECTS
67 // 251. basic_stringbuf missing allocator_type
54c1bf78 68 typedef _Alloc allocator_type;
54c1bf78
BK
69 typedef typename traits_type::int_type int_type;
70 typedef typename traits_type::pos_type pos_type;
71 typedef typename traits_type::off_type off_type;
72
54c1bf78
BK
73 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
74 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
75 typedef typename __string_type::size_type __size_type;
76
6f48900c 77 protected:
4312e020 78 /// Place to stash in || out || in | out settings for current stringbuf.
cd16e04b
PC
79 ios_base::openmode _M_mode;
80
54c1bf78
BK
81 // Data Members:
82 __string_type _M_string;
798355a2 83
54c1bf78
BK
84 public:
85 // Constructors:
840ceb34
PE
86 /**
87 * @brief Starts with an empty string buffer.
88 * @param mode Whether the buffer can read, or write, or both.
89 *
90 * The default constructor initializes the parent class using its
91 * own default ctor.
92 */
798355a2 93 explicit
54c1bf78 94 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
983de0da
PC
95 : __streambuf_type(), _M_mode(__mode), _M_string()
96 { }
54c1bf78 97
840ceb34
PE
98 /**
99 * @brief Starts with an existing string buffer.
100 * @param str A string to copy as a starting buffer.
101 * @param mode Whether the buffer can read, or write, or both.
102 *
103 * This constructor initializes the parent class using its
104 * own default ctor.
105 */
798355a2 106 explicit
54c1bf78
BK
107 basic_stringbuf(const __string_type& __str,
108 ios_base::openmode __mode = ios_base::in | ios_base::out)
26c691a8 109 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
54c1bf78
BK
110 { _M_stringbuf_init(__mode); }
111
112 // Get and set:
840ceb34
PE
113 /**
114 * @brief Copying out the string buffer.
115 * @return A copy of one of the underlying sequences.
116 *
2a60a9f6 117 * <em>If the buffer is only created in input mode, the underlying
840ceb34 118 * character sequence is equal to the input sequence; otherwise, it
2a60a9f6 119 * is equal to the output sequence.</em> [27.7.1.2]/1
840ceb34 120 */
798355a2
PE
121 __string_type
122 str() const
54c1bf78 123 {
fdeeef7f 124 __string_type __ret;
983de0da 125 if (this->pptr())
1b170b55
PC
126 {
127 // The current egptr() may not be the actual string end.
128 if (this->pptr() > this->egptr())
fdeeef7f 129 __ret = __string_type(this->pbase(), this->pptr());
1b170b55 130 else
fdeeef7f 131 __ret = __string_type(this->pbase(), this->egptr());
1b170b55
PC
132 }
133 else
fdeeef7f
BK
134 __ret = _M_string;
135 return __ret;
54c1bf78
BK
136 }
137
840ceb34
PE
138 /**
139 * @brief Setting a new buffer.
140 * @param s The string to use as a new sequence.
141 *
142 * Deallocates any previous stored sequence, then copies @a s to
143 * use as a new one.
144 */
798355a2 145 void
54c1bf78
BK
146 str(const __string_type& __s)
147 {
81646a31 148 // Cannot use _M_string = __s, since v3 strings are COW.
93d87ec6 149 _M_string.assign(__s.data(), __s.size());
fdeeef7f 150 _M_stringbuf_init(_M_mode);
54c1bf78
BK
151 }
152
153 protected:
983de0da 154 // Common initialization code goes here.
54c1bf78
BK
155 void
156 _M_stringbuf_init(ios_base::openmode __mode)
157 {
fdeeef7f 158 _M_mode = __mode;
23cac885 159 __size_type __len = 0;
fdeeef7f 160 if (_M_mode & (ios_base::ate | ios_base::app))
23cac885 161 __len = _M_string.size();
655d7821 162 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
54c1bf78
BK
163 }
164
fdeeef7f
BK
165 virtual streamsize
166 showmanyc()
167 {
168 streamsize __ret = -1;
169 if (_M_mode & ios_base::in)
170 {
171 _M_update_egptr();
172 __ret = this->egptr() - this->gptr();
173 }
174 return __ret;
175 }
176
1b170b55 177 virtual int_type
cd16e04b 178 underflow();
54c1bf78 179
798355a2 180 virtual int_type
54c1bf78
BK
181 pbackfail(int_type __c = traits_type::eof());
182
798355a2 183 virtual int_type
54c1bf78
BK
184 overflow(int_type __c = traits_type::eof());
185
840ceb34
PE
186 /**
187 * @brief Manipulates the buffer.
188 * @param s Pointer to a buffer area.
189 * @param n Size of @a s.
190 * @return @c this
191 *
192 * If no buffer has already been created, and both @a s and @a n are
193 * non-zero, then @c s is used as a buffer; see
a40fff0e 194 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
840ceb34
PE
195 * for more.
196 */
798355a2 197 virtual __streambuf_type*
54c1bf78 198 setbuf(char_type* __s, streamsize __n)
798355a2 199 {
b82a33d2 200 if (__s && __n >= 0)
54c1bf78 201 {
23cac885 202 // This is implementation-defined behavior, and assumes
b82a33d2
PC
203 // that an external char_type array of length __n exists
204 // and has been pre-allocated. If this is not the case,
205 // things will quickly blow up.
23cac885
BK
206
207 // Step 1: Destroy the current internal array.
62448787 208 _M_string.clear();
23cac885
BK
209
210 // Step 2: Use the external array.
62448787 211 _M_sync(__s, __n, 0);
54c1bf78 212 }
798355a2
PE
213 return this;
214 }
54c1bf78 215
798355a2 216 virtual pos_type
54c1bf78
BK
217 seekoff(off_type __off, ios_base::seekdir __way,
218 ios_base::openmode __mode = ios_base::in | ios_base::out);
219
798355a2
PE
220 virtual pos_type
221 seekpos(pos_type __sp,
54c1bf78
BK
222 ios_base::openmode __mode = ios_base::in | ios_base::out);
223
224 // Internal function for correctly updating the internal buffer
10d9600d
PC
225 // for a particular _M_string, due to initialization or re-sizing
226 // of an existing _M_string.
50af15ec 227 void
10d9600d 228 _M_sync(char_type* __base, __size_type __i, __size_type __o);
1b170b55
PC
229
230 // Internal function for correctly updating egptr() to the actual
231 // string end.
232 void
233 _M_update_egptr()
234 {
fdeeef7f 235 const bool __testin = _M_mode & ios_base::in;
983de0da 236 if (this->pptr() && this->pptr() > this->egptr())
d9cdfe6a
BK
237 {
238 if (__testin)
239 this->setg(this->eback(), this->gptr(), this->pptr());
240 else
241 this->setg(this->pptr(), this->pptr(), this->pptr());
242 }
1b170b55 243 }
54c1bf78
BK
244 };
245
246
840ceb34
PE
247 // [27.7.2] Template class basic_istringstream
248 /**
249 * @brief Controlling input for std::string.
5b9daa7e 250 * @ingroup io
840ceb34
PE
251 *
252 * This class supports reading from objects of type std::basic_string,
253 * using the inherited functions from std::basic_istream. To control
254 * the associated sequence, an instance of std::basic_stringbuf is used,
255 * which this page refers to as @c sb.
256 */
54c1bf78
BK
257 template<typename _CharT, typename _Traits, typename _Alloc>
258 class basic_istringstream : public basic_istream<_CharT, _Traits>
259 {
260 public:
261 // Types:
262 typedef _CharT char_type;
263 typedef _Traits traits_type;
f5677b15
PC
264 // _GLIBCXX_RESOLVE_LIB_DEFECTS
265 // 251. basic_stringbuf missing allocator_type
54c1bf78 266 typedef _Alloc allocator_type;
54c1bf78
BK
267 typedef typename traits_type::int_type int_type;
268 typedef typename traits_type::pos_type pos_type;
269 typedef typename traits_type::off_type off_type;
270
271 // Non-standard types:
272 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
273 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
274 typedef basic_istream<char_type, traits_type> __istream_type;
275
276 private:
277 __stringbuf_type _M_stringbuf;
278
279 public:
280 // Constructors:
840ceb34
PE
281 /**
282 * @brief Default constructor starts with an empty string buffer.
283 * @param mode Whether the buffer can read, or write, or both.
284 *
285 * @c ios_base::in is automatically included in @a mode.
286 *
287 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
288 * class initializer. Does not allocate any buffer.
289 *
840ceb34
PE
290 * That's a lie. We initialize the base class with NULL, because the
291 * string class does its own memory management.
840ceb34 292 */
798355a2 293 explicit
54c1bf78 294 basic_istringstream(ios_base::openmode __mode = ios_base::in)
d29cc32f 295 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
54c1bf78
BK
296 { this->init(&_M_stringbuf); }
297
840ceb34
PE
298 /**
299 * @brief Starts with an existing string buffer.
300 * @param str A string to copy as a starting buffer.
301 * @param mode Whether the buffer can read, or write, or both.
302 *
303 * @c ios_base::in is automatically included in @a mode.
304 *
305 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
306 * to the base class initializer.
307 *
840ceb34
PE
308 * That's a lie. We initialize the base class with NULL, because the
309 * string class does its own memory management.
840ceb34 310 */
798355a2 311 explicit
54c1bf78
BK
312 basic_istringstream(const __string_type& __str,
313 ios_base::openmode __mode = ios_base::in)
d29cc32f 314 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
54c1bf78
BK
315 { this->init(&_M_stringbuf); }
316
840ceb34
PE
317 /**
318 * @brief The destructor does nothing.
319 *
320 * The buffer is deallocated by the stringbuf object, not the
321 * formatting stream.
322 */
54c1bf78
BK
323 ~basic_istringstream()
324 { }
325
326 // Members:
840ceb34
PE
327 /**
328 * @brief Accessing the underlying buffer.
329 * @return The current basic_stringbuf buffer.
330 *
331 * This hides both signatures of std::basic_ios::rdbuf().
332 */
798355a2 333 __stringbuf_type*
54c1bf78
BK
334 rdbuf() const
335 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
336
840ceb34
PE
337 /**
338 * @brief Copying out the string buffer.
339 * @return @c rdbuf()->str()
340 */
54c1bf78
BK
341 __string_type
342 str() const
343 { return _M_stringbuf.str(); }
798355a2 344
840ceb34
PE
345 /**
346 * @brief Setting a new buffer.
347 * @param s The string to use as a new sequence.
348 *
349 * Calls @c rdbuf()->str(s).
350 */
798355a2 351 void
54c1bf78
BK
352 str(const __string_type& __s)
353 { _M_stringbuf.str(__s); }
354 };
355
356
840ceb34
PE
357 // [27.7.3] Template class basic_ostringstream
358 /**
359 * @brief Controlling output for std::string.
5b9daa7e 360 * @ingroup io
840ceb34
PE
361 *
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.
366 */
54c1bf78
BK
367 template <typename _CharT, typename _Traits, typename _Alloc>
368 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
369 {
370 public:
371 // Types:
372 typedef _CharT char_type;
373 typedef _Traits traits_type;
f5677b15
PC
374 // _GLIBCXX_RESOLVE_LIB_DEFECTS
375 // 251. basic_stringbuf missing allocator_type
54c1bf78 376 typedef _Alloc allocator_type;
54c1bf78
BK
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;
380
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;
385
386 private:
387 __stringbuf_type _M_stringbuf;
388
389 public:
840ceb34
PE
390 // Constructors/destructor:
391 /**
392 * @brief Default constructor starts with an empty string buffer.
393 * @param mode Whether the buffer can read, or write, or both.
394 *
395 * @c ios_base::out is automatically included in @a mode.
396 *
397 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
398 * class initializer. Does not allocate any buffer.
399 *
840ceb34
PE
400 * That's a lie. We initialize the base class with NULL, because the
401 * string class does its own memory management.
840ceb34 402 */
798355a2 403 explicit
54c1bf78 404 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
d29cc32f 405 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
54c1bf78
BK
406 { this->init(&_M_stringbuf); }
407
840ceb34
PE
408 /**
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.
412 *
413 * @c ios_base::out is automatically included in @a mode.
414 *
415 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
416 * to the base class initializer.
417 *
840ceb34
PE
418 * That's a lie. We initialize the base class with NULL, because the
419 * string class does its own memory management.
840ceb34 420 */
798355a2 421 explicit
54c1bf78
BK
422 basic_ostringstream(const __string_type& __str,
423 ios_base::openmode __mode = ios_base::out)
d29cc32f 424 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
54c1bf78
BK
425 { this->init(&_M_stringbuf); }
426
840ceb34
PE
427 /**
428 * @brief The destructor does nothing.
429 *
430 * The buffer is deallocated by the stringbuf object, not the
431 * formatting stream.
432 */
54c1bf78
BK
433 ~basic_ostringstream()
434 { }
435
436 // Members:
840ceb34
PE
437 /**
438 * @brief Accessing the underlying buffer.
439 * @return The current basic_stringbuf buffer.
440 *
441 * This hides both signatures of std::basic_ios::rdbuf().
442 */
798355a2 443 __stringbuf_type*
54c1bf78
BK
444 rdbuf() const
445 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
446
840ceb34
PE
447 /**
448 * @brief Copying out the string buffer.
449 * @return @c rdbuf()->str()
450 */
54c1bf78
BK
451 __string_type
452 str() const
453 { return _M_stringbuf.str(); }
798355a2 454
840ceb34
PE
455 /**
456 * @brief Setting a new buffer.
457 * @param s The string to use as a new sequence.
458 *
459 * Calls @c rdbuf()->str(s).
460 */
798355a2 461 void
54c1bf78
BK
462 str(const __string_type& __s)
463 { _M_stringbuf.str(__s); }
464 };
798355a2
PE
465
466
840ceb34
PE
467 // [27.7.4] Template class basic_stringstream
468 /**
469 * @brief Controlling input and output for std::string.
5b9daa7e 470 * @ingroup io
840ceb34
PE
471 *
472 * This class supports reading from and writing to objects of type
473 * std::basic_string, using the inherited functions from
474 * std::basic_iostream. To control the associated sequence, an instance
475 * of std::basic_stringbuf is used, which this page refers to as @c sb.
476 */
54c1bf78
BK
477 template <typename _CharT, typename _Traits, typename _Alloc>
478 class basic_stringstream : public basic_iostream<_CharT, _Traits>
479 {
480 public:
481 // Types:
482 typedef _CharT char_type;
483 typedef _Traits traits_type;
f5677b15
PC
484 // _GLIBCXX_RESOLVE_LIB_DEFECTS
485 // 251. basic_stringbuf missing allocator_type
54c1bf78 486 typedef _Alloc allocator_type;
54c1bf78
BK
487 typedef typename traits_type::int_type int_type;
488 typedef typename traits_type::pos_type pos_type;
489 typedef typename traits_type::off_type off_type;
490
491 // Non-standard Types:
492 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
493 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
494 typedef basic_iostream<char_type, traits_type> __iostream_type;
495
496 private:
497 __stringbuf_type _M_stringbuf;
498
499 public:
500 // Constructors/destructors
840ceb34
PE
501 /**
502 * @brief Default constructor starts with an empty string buffer.
503 * @param mode Whether the buffer can read, or write, or both.
504 *
505 * Initializes @c sb using @c mode, and passes @c &sb to the base
506 * class initializer. Does not allocate any buffer.
507 *
840ceb34
PE
508 * That's a lie. We initialize the base class with NULL, because the
509 * string class does its own memory management.
840ceb34 510 */
798355a2 511 explicit
54c1bf78 512 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
d29cc32f 513 : __iostream_type(), _M_stringbuf(__m)
54c1bf78
BK
514 { this->init(&_M_stringbuf); }
515
840ceb34
PE
516 /**
517 * @brief Starts with an existing string buffer.
518 * @param str A string to copy as a starting buffer.
519 * @param mode Whether the buffer can read, or write, or both.
520 *
521 * Initializes @c sb using @a str and @c mode, and passes @c &sb
522 * to the base class initializer.
523 *
840ceb34
PE
524 * That's a lie. We initialize the base class with NULL, because the
525 * string class does its own memory management.
840ceb34 526 */
798355a2 527 explicit
54c1bf78
BK
528 basic_stringstream(const __string_type& __str,
529 ios_base::openmode __m = ios_base::out | ios_base::in)
d29cc32f 530 : __iostream_type(), _M_stringbuf(__str, __m)
54c1bf78
BK
531 { this->init(&_M_stringbuf); }
532
840ceb34
PE
533 /**
534 * @brief The destructor does nothing.
535 *
536 * The buffer is deallocated by the stringbuf object, not the
537 * formatting stream.
538 */
54c1bf78
BK
539 ~basic_stringstream()
540 { }
541
542 // Members:
840ceb34
PE
543 /**
544 * @brief Accessing the underlying buffer.
545 * @return The current basic_stringbuf buffer.
546 *
547 * This hides both signatures of std::basic_ios::rdbuf().
548 */
798355a2 549 __stringbuf_type*
54c1bf78
BK
550 rdbuf() const
551 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
552
840ceb34
PE
553 /**
554 * @brief Copying out the string buffer.
555 * @return @c rdbuf()->str()
556 */
54c1bf78
BK
557 __string_type
558 str() const
559 { return _M_stringbuf.str(); }
560
840ceb34
PE
561 /**
562 * @brief Setting a new buffer.
563 * @param s The string to use as a new sequence.
564 *
565 * Calls @c rdbuf()->str(s).
566 */
798355a2 567 void
54c1bf78
BK
568 str(const __string_type& __s)
569 { _M_stringbuf.str(__s); }
570 };
3cbc7af0 571
12ffa228
BK
572_GLIBCXX_END_NAMESPACE_VERSION
573} // namespace
54c1bf78 574
ee3ee948 575#include <bits/sstream.tcc>
54c1bf78 576
1143680e 577#endif /* _GLIBCXX_SSTREAM */