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