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