FSF GCC merge 02/23/03
[official-gcc.git] / libstdc++-v3 / include / std / std_sstream.h
blob268478f3339bc547763767b39d7294dc8041147b
1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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
34 /** @file sstream
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 _CPP_SSTREAM
40 #define _CPP_SSTREAM 1
42 #pragma GCC system_header
44 #include <istream>
45 #include <ostream>
47 namespace std
49 // [27.7.1] template class basic_stringbuf
50 /**
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>
64 public:
65 // Types:
66 typedef _CharT char_type;
67 typedef _Traits traits_type;
68 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
69 // 251. basic_stringbuf missing allocator_type
70 typedef _Alloc allocator_type;
71 #endif
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;
76 //@{
77 /**
78 * @if maint
79 * @doctodo
80 * @endif
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;
85 //@}
87 protected:
88 // Data Members:
89 /**
90 * @if maint
91 * @doctodo
92 * @endif
94 __string_type _M_string;
96 public:
97 // Constructors:
98 /**
99 * @brief Starts with an empty string buffer.
100 * @param mode Whether the buffer can read, or write, or both.
102 * The default constructor initializes the parent class using its
103 * own default ctor.
105 explicit
106 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
107 : __streambuf_type(), _M_string()
108 { _M_stringbuf_init(__mode); }
111 * @brief Starts with an existing string buffer.
112 * @param str A string to copy as a starting buffer.
113 * @param mode Whether the buffer can read, or write, or both.
115 * This constructor initializes the parent class using its
116 * own default ctor.
118 explicit
119 basic_stringbuf(const __string_type& __str,
120 ios_base::openmode __mode = ios_base::in | ios_base::out)
121 : __streambuf_type(), _M_string(__str.data(), __str.size())
122 { _M_stringbuf_init(__mode); }
124 // Get and set:
126 * @brief Copying out the string buffer.
127 * @return A copy of one of the underlying sequences.
129 * "If the buffer is only created in input mode, the underlying
130 * character sequence is equal to the input sequence; otherwise, it
131 * is equal to the output sequence." [27.7.1.2]/1
133 __string_type
134 str() const
136 if (this->_M_mode & ios_base::out)
138 // This is the deal: _M_string.size() is a value that
139 // represents the size of the initial string that makes
140 // _M_string, and may not be the correct size of the
141 // current stringbuf internal buffer.
142 __size_type __len = _M_string.size();
143 if (this->_M_out_end > this->_M_out_beg)
144 __len = std::max(__size_type(this->_M_out_end
145 - this->_M_out_beg), __len);
146 return __string_type(this->_M_out_beg, this->_M_out_beg + __len);
148 else
149 return _M_string;
153 * @brief Setting a new buffer.
154 * @param s The string to use as a new sequence.
156 * Deallocates any previous stored sequence, then copies @a s to
157 * use as a new one.
159 void
160 str(const __string_type& __s)
162 // Cannot use _M_string = __s, since v3 strings are COW.
163 _M_string.assign(__s.data(), __s.size());
164 _M_stringbuf_init(this->_M_mode);
167 protected:
168 // Common initialization code for both ctors goes here.
170 * @if maint
171 * @doctodo
172 * @endif
174 void
175 _M_stringbuf_init(ios_base::openmode __mode)
177 // _M_buf_size is a convenient alias for "what the streambuf
178 // thinks the allocated size of the string really is." This is
179 // necessary as ostringstreams are implemented with the
180 // streambufs having control of the allocation and
181 // re-allocation of the internal string object, _M_string.
182 this->_M_buf_size = _M_string.size();
184 // NB: Start ostringstream buffers at 512 bytes. This is an
185 // experimental value (pronounced "arbitrary" in some of the
186 // hipper english-speaking countries), and can be changed to
187 // suit particular needs.
188 this->_M_buf_size_opt = 512;
189 this->_M_mode = __mode;
190 if (this->_M_mode & (ios_base::ate | ios_base::app))
191 _M_really_sync(0, this->_M_buf_size);
192 else
193 _M_really_sync(0, 0);
196 // Overridden virtual functions:
197 // [documentation is inherited]
198 virtual int_type
199 underflow()
201 if (this->_M_in_cur && this->_M_in_cur < this->_M_in_end)
202 return traits_type::to_int_type(*gptr());
203 else
204 return traits_type::eof();
207 // [documentation is inherited]
208 virtual int_type
209 pbackfail(int_type __c = traits_type::eof());
211 // [documentation is inherited]
212 virtual int_type
213 overflow(int_type __c = traits_type::eof());
216 * @brief Manipulates the buffer.
217 * @param s Pointer to a buffer area.
218 * @param n Size of @a s.
219 * @return @c this
221 * If no buffer has already been created, and both @a s and @a n are
222 * non-zero, then @c s is used as a buffer; see
223 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
224 * for more.
226 virtual __streambuf_type*
227 setbuf(char_type* __s, streamsize __n)
229 if (__s && __n)
231 _M_string = __string_type(__s, __n);
232 _M_really_sync(0, 0);
234 return this;
237 // [documentation is inherited]
238 virtual pos_type
239 seekoff(off_type __off, ios_base::seekdir __way,
240 ios_base::openmode __mode = ios_base::in | ios_base::out);
242 // [documentation is inherited]
243 virtual pos_type
244 seekpos(pos_type __sp,
245 ios_base::openmode __mode = ios_base::in | ios_base::out);
247 // Internal function for correctly updating the internal buffer
248 // for a particular _M_string, due to initialization or
249 // re-sizing of an existing _M_string.
250 // Assumes: contents of _M_string and internal buffer match exactly.
251 // __i == _M_in_cur - _M_in_beg
252 // __o == _M_out_cur - _M_out_beg
254 * @if maint
255 * @doctodo
256 * @endif
258 virtual int
259 _M_really_sync(__size_type __i, __size_type __o)
261 char_type* __base = const_cast<char_type*>(_M_string.data());
262 bool __testin = this->_M_mode & ios_base::in;
263 bool __testout = this->_M_mode & ios_base::out;
264 __size_type __len = _M_string.size();
266 this->_M_buf = __base;
267 if (__testin)
268 this->setg(__base, __base + __i, __base + __len);
269 if (__testout)
271 this->setp(__base, __base + __len);
272 this->_M_out_cur += __o;
274 return 0;
279 // [27.7.2] Template class basic_istringstream
281 * @brief Controlling input for std::string.
283 * This class supports reading from objects of type std::basic_string,
284 * using the inherited functions from std::basic_istream. To control
285 * the associated sequence, an instance of std::basic_stringbuf is used,
286 * which this page refers to as @c sb.
288 template<typename _CharT, typename _Traits, typename _Alloc>
289 class basic_istringstream : public basic_istream<_CharT, _Traits>
291 public:
292 // Types:
293 typedef _CharT char_type;
294 typedef _Traits traits_type;
295 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
296 // 251. basic_stringbuf missing allocator_type
297 typedef _Alloc allocator_type;
298 #endif
299 typedef typename traits_type::int_type int_type;
300 typedef typename traits_type::pos_type pos_type;
301 typedef typename traits_type::off_type off_type;
303 // Non-standard types:
304 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
305 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
306 typedef basic_istream<char_type, traits_type> __istream_type;
308 private:
310 * @if maint
311 * @doctodo
312 * @endif
314 __stringbuf_type _M_stringbuf;
316 public:
317 // Constructors:
319 * @brief Default constructor starts with an empty string buffer.
320 * @param mode Whether the buffer can read, or write, or both.
322 * @c ios_base::in is automatically included in @a mode.
324 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
325 * class initializer. Does not allocate any buffer.
327 * @if maint
328 * That's a lie. We initialize the base class with NULL, because the
329 * string class does its own memory management.
330 * @endif
332 explicit
333 basic_istringstream(ios_base::openmode __mode = ios_base::in)
334 : __istream_type(NULL), _M_stringbuf(__mode | ios_base::in)
335 { this->init(&_M_stringbuf); }
338 * @brief Starts with an existing string buffer.
339 * @param str A string to copy as a starting buffer.
340 * @param mode Whether the buffer can read, or write, or both.
342 * @c ios_base::in is automatically included in @a mode.
344 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
345 * to the base class initializer.
347 * @if maint
348 * That's a lie. We initialize the base class with NULL, because the
349 * string class does its own memory management.
350 * @endif
352 explicit
353 basic_istringstream(const __string_type& __str,
354 ios_base::openmode __mode = ios_base::in)
355 : __istream_type(NULL), _M_stringbuf(__str, __mode | ios_base::in)
356 { this->init(&_M_stringbuf); }
359 * @brief The destructor does nothing.
361 * The buffer is deallocated by the stringbuf object, not the
362 * formatting stream.
364 ~basic_istringstream()
367 // Members:
369 * @brief Accessing the underlying buffer.
370 * @return The current basic_stringbuf buffer.
372 * This hides both signatures of std::basic_ios::rdbuf().
374 __stringbuf_type*
375 rdbuf() const
376 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
379 * @brief Copying out the string buffer.
380 * @return @c rdbuf()->str()
382 __string_type
383 str() const
384 { return _M_stringbuf.str(); }
387 * @brief Setting a new buffer.
388 * @param s The string to use as a new sequence.
390 * Calls @c rdbuf()->str(s).
392 void
393 str(const __string_type& __s)
394 { _M_stringbuf.str(__s); }
398 // [27.7.3] Template class basic_ostringstream
400 * @brief Controlling output for std::string.
402 * This class supports writing to objects of type std::basic_string,
403 * using the inherited functions from std::basic_ostream. To control
404 * the associated sequence, an instance of std::basic_stringbuf is used,
405 * which this page refers to as @c sb.
407 template <typename _CharT, typename _Traits, typename _Alloc>
408 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
410 public:
411 // Types:
412 typedef _CharT char_type;
413 typedef _Traits traits_type;
414 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
415 // 251. basic_stringbuf missing allocator_type
416 typedef _Alloc allocator_type;
417 #endif
418 typedef typename traits_type::int_type int_type;
419 typedef typename traits_type::pos_type pos_type;
420 typedef typename traits_type::off_type off_type;
422 // Non-standard types:
423 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
424 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
425 typedef basic_ostream<char_type, traits_type> __ostream_type;
427 private:
429 * @if maint
430 * @doctodo
431 * @endif
433 __stringbuf_type _M_stringbuf;
435 public:
436 // Constructors/destructor:
438 * @brief Default constructor starts with an empty string buffer.
439 * @param mode Whether the buffer can read, or write, or both.
441 * @c ios_base::out is automatically included in @a mode.
443 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
444 * class initializer. Does not allocate any buffer.
446 * @if maint
447 * That's a lie. We initialize the base class with NULL, because the
448 * string class does its own memory management.
449 * @endif
451 explicit
452 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
453 : __ostream_type(NULL), _M_stringbuf(__mode | ios_base::out)
454 { this->init(&_M_stringbuf); }
457 * @brief Starts with an existing string buffer.
458 * @param str A string to copy as a starting buffer.
459 * @param mode Whether the buffer can read, or write, or both.
461 * @c ios_base::out is automatically included in @a mode.
463 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
464 * to the base class initializer.
466 * @if maint
467 * That's a lie. We initialize the base class with NULL, because the
468 * string class does its own memory management.
469 * @endif
471 explicit
472 basic_ostringstream(const __string_type& __str,
473 ios_base::openmode __mode = ios_base::out)
474 : __ostream_type(NULL), _M_stringbuf(__str, __mode | ios_base::out)
475 { this->init(&_M_stringbuf); }
478 * @brief The destructor does nothing.
480 * The buffer is deallocated by the stringbuf object, not the
481 * formatting stream.
483 ~basic_ostringstream()
486 // Members:
488 * @brief Accessing the underlying buffer.
489 * @return The current basic_stringbuf buffer.
491 * This hides both signatures of std::basic_ios::rdbuf().
493 __stringbuf_type*
494 rdbuf() const
495 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
498 * @brief Copying out the string buffer.
499 * @return @c rdbuf()->str()
501 __string_type
502 str() const
503 { return _M_stringbuf.str(); }
506 * @brief Setting a new buffer.
507 * @param s The string to use as a new sequence.
509 * Calls @c rdbuf()->str(s).
511 void
512 str(const __string_type& __s)
513 { _M_stringbuf.str(__s); }
517 // [27.7.4] Template class basic_stringstream
519 * @brief Controlling input and output for std::string.
521 * This class supports reading from and writing to objects of type
522 * std::basic_string, using the inherited functions from
523 * std::basic_iostream. To control the associated sequence, an instance
524 * of std::basic_stringbuf is used, which this page refers to as @c sb.
526 template <typename _CharT, typename _Traits, typename _Alloc>
527 class basic_stringstream : public basic_iostream<_CharT, _Traits>
529 public:
530 // Types:
531 typedef _CharT char_type;
532 typedef _Traits traits_type;
533 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
534 // 251. basic_stringbuf missing allocator_type
535 typedef _Alloc allocator_type;
536 #endif
537 typedef typename traits_type::int_type int_type;
538 typedef typename traits_type::pos_type pos_type;
539 typedef typename traits_type::off_type off_type;
541 // Non-standard Types:
542 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
543 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
544 typedef basic_iostream<char_type, traits_type> __iostream_type;
546 private:
548 * @if maint
549 * @doctodo
550 * @endif
552 __stringbuf_type _M_stringbuf;
554 public:
555 // Constructors/destructors
557 * @brief Default constructor starts with an empty string buffer.
558 * @param mode Whether the buffer can read, or write, or both.
560 * Initializes @c sb using @c mode, and passes @c &sb to the base
561 * class initializer. Does not allocate any buffer.
563 * @if maint
564 * That's a lie. We initialize the base class with NULL, because the
565 * string class does its own memory management.
566 * @endif
568 explicit
569 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
570 : __iostream_type(NULL), _M_stringbuf(__m)
571 { this->init(&_M_stringbuf); }
574 * @brief Starts with an existing string buffer.
575 * @param str A string to copy as a starting buffer.
576 * @param mode Whether the buffer can read, or write, or both.
578 * Initializes @c sb using @a str and @c mode, and passes @c &sb
579 * to the base class initializer.
581 * @if maint
582 * That's a lie. We initialize the base class with NULL, because the
583 * string class does its own memory management.
584 * @endif
586 explicit
587 basic_stringstream(const __string_type& __str,
588 ios_base::openmode __m = ios_base::out | ios_base::in)
589 : __iostream_type(NULL), _M_stringbuf(__str, __m)
590 { this->init(&_M_stringbuf); }
593 * @brief The destructor does nothing.
595 * The buffer is deallocated by the stringbuf object, not the
596 * formatting stream.
598 ~basic_stringstream()
601 // Members:
603 * @brief Accessing the underlying buffer.
604 * @return The current basic_stringbuf buffer.
606 * This hides both signatures of std::basic_ios::rdbuf().
608 __stringbuf_type*
609 rdbuf() const
610 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
613 * @brief Copying out the string buffer.
614 * @return @c rdbuf()->str()
616 __string_type
617 str() const
618 { return _M_stringbuf.str(); }
621 * @brief Setting a new buffer.
622 * @param s The string to use as a new sequence.
624 * Calls @c rdbuf()->str(s).
626 void
627 str(const __string_type& __s)
628 { _M_stringbuf.str(__s); }
630 } // namespace std
632 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
633 # define export
634 #endif
635 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
636 # include <bits/sstream.tcc>
637 #endif
639 #endif