2003-07-04 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / std / std_sstream.h
blobdcdb7a713bf355ed41f6cbef74aaadd10d00fdfb
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 _SSTREAM
40 #define _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 _GLIBCXX_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 /**
89 * @if maint
90 * Place to stash in || out || in | out settings for current stringbuf.
91 * @endif
93 ios_base::openmode _M_mode;
95 // Data Members:
96 /**
97 * @if maint
98 * @doctodo
99 * @endif
101 __string_type _M_string;
103 public:
104 // Constructors:
106 * @brief Starts with an empty string buffer.
107 * @param mode Whether the buffer can read, or write, or both.
109 * The default constructor initializes the parent class using its
110 * own default ctor.
112 explicit
113 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
114 : __streambuf_type(), _M_string()
115 { _M_stringbuf_init(__mode); }
118 * @brief Starts with an existing string buffer.
119 * @param str A string to copy as a starting buffer.
120 * @param mode Whether the buffer can read, or write, or both.
122 * This constructor initializes the parent class using its
123 * own default ctor.
125 explicit
126 basic_stringbuf(const __string_type& __str,
127 ios_base::openmode __mode = ios_base::in | ios_base::out)
128 : __streambuf_type(), _M_string(__str.data(), __str.size())
129 { _M_stringbuf_init(__mode); }
131 // Get and set:
133 * @brief Copying out the string buffer.
134 * @return A copy of one of the underlying sequences.
136 * "If the buffer is only created in input mode, the underlying
137 * character sequence is equal to the input sequence; otherwise, it
138 * is equal to the output sequence." [27.7.1.2]/1
140 __string_type
141 str() const
143 const bool __testout = this->_M_mode & ios_base::out;
144 if (__testout)
146 // The current egptr() may not be the actual string end.
147 if (this->pptr() > this->egptr())
148 return __string_type(this->pbase(), this->pptr());
149 else
150 return __string_type(this->pbase(), this->egptr());
152 else
153 return _M_string;
157 * @brief Setting a new buffer.
158 * @param s The string to use as a new sequence.
160 * Deallocates any previous stored sequence, then copies @a s to
161 * use as a new one.
163 void
164 str(const __string_type& __s)
166 // Cannot use _M_string = __s, since v3 strings are COW.
167 _M_string.assign(__s.data(), __s.size());
168 _M_stringbuf_init(this->_M_mode);
171 protected:
172 // Common initialization code for both ctors goes here.
174 * @if maint
175 * @doctodo
176 * @endif
178 void
179 _M_stringbuf_init(ios_base::openmode __mode)
181 this->_M_mode = __mode;
183 __size_type __len = 0;
184 if (this->_M_mode & (ios_base::ate | ios_base::app))
185 __len = _M_string.size();
186 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
189 // [documentation is inherited]
190 virtual int_type
191 underflow();
193 // [documentation is inherited]
194 virtual int_type
195 pbackfail(int_type __c = traits_type::eof());
197 // [documentation is inherited]
198 virtual int_type
199 overflow(int_type __c = traits_type::eof());
202 * @brief Manipulates the buffer.
203 * @param s Pointer to a buffer area.
204 * @param n Size of @a s.
205 * @return @c this
207 * If no buffer has already been created, and both @a s and @a n are
208 * non-zero, then @c s is used as a buffer; see
209 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
210 * for more.
212 virtual __streambuf_type*
213 setbuf(char_type* __s, streamsize __n)
215 if (__s && __n >= 0)
217 // This is implementation-defined behavior, and assumes
218 // that an external char_type array of length __n exists
219 // and has been pre-allocated. If this is not the case,
220 // things will quickly blow up.
222 // Step 1: Destroy the current internal array.
223 _M_string = __string_type(__s, __n);
225 // Step 2: Use the external array.
226 _M_sync(__s, 0, 0);
228 return this;
231 // [documentation is inherited]
232 virtual pos_type
233 seekoff(off_type __off, ios_base::seekdir __way,
234 ios_base::openmode __mode = ios_base::in | ios_base::out);
236 // [documentation is inherited]
237 virtual pos_type
238 seekpos(pos_type __sp,
239 ios_base::openmode __mode = ios_base::in | ios_base::out);
241 // Internal function for correctly updating the internal buffer
242 // for a particular _M_string, due to initialization or
243 // re-sizing of an existing _M_string.
244 // Assumes: contents of _M_string and internal buffer match exactly.
245 // __i == _M_in_cur - _M_in_beg
246 // __o == _M_out_cur - _M_out_beg
248 * @if maint
249 * @doctodo
250 * @endif
252 void
253 _M_sync(char_type* __base, __size_type __i, __size_type __o)
255 const bool __testin = this->_M_mode & ios_base::in;
256 const bool __testout = this->_M_mode & ios_base::out;
257 const __size_type __len = _M_string.size();
259 if (__testin)
260 this->setg(__base, __base + __i, __base + __len);
261 if (__testout)
263 this->setp(__base, __base + _M_string.capacity());
264 this->pbump(__o);
265 // We need a pointer to the string end anyway, even when
266 // !__testin: in that case, however, for the correct
267 // functioning of the streambuf inlines all the get area
268 // pointers must be identical.
269 if (!__testin)
270 this->setg(__base + __len, __base + __len, __base + __len);
274 // Internal function for correctly updating egptr() to the actual
275 // string end.
276 void
277 _M_update_egptr()
279 const bool __testin = this->_M_mode & ios_base::in;
280 const bool __testout = this->_M_mode & ios_base::out;
282 if (__testout && this->pptr() > this->egptr())
283 if (__testin)
284 this->setg(this->eback(), this->gptr(), this->pptr());
285 else
286 this->setg(this->pptr(), this->pptr(), this->pptr());
291 // [27.7.2] Template class basic_istringstream
293 * @brief Controlling input for std::string.
295 * This class supports reading from objects of type std::basic_string,
296 * using the inherited functions from std::basic_istream. To control
297 * the associated sequence, an instance of std::basic_stringbuf is used,
298 * which this page refers to as @c sb.
300 template<typename _CharT, typename _Traits, typename _Alloc>
301 class basic_istringstream : public basic_istream<_CharT, _Traits>
303 public:
304 // Types:
305 typedef _CharT char_type;
306 typedef _Traits traits_type;
307 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
308 // 251. basic_stringbuf missing allocator_type
309 typedef _Alloc allocator_type;
310 #endif
311 typedef typename traits_type::int_type int_type;
312 typedef typename traits_type::pos_type pos_type;
313 typedef typename traits_type::off_type off_type;
315 // Non-standard types:
316 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
317 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
318 typedef basic_istream<char_type, traits_type> __istream_type;
320 private:
322 * @if maint
323 * @doctodo
324 * @endif
326 __stringbuf_type _M_stringbuf;
328 public:
329 // Constructors:
331 * @brief Default constructor starts with an empty string buffer.
332 * @param mode Whether the buffer can read, or write, or both.
334 * @c ios_base::in is automatically included in @a mode.
336 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
337 * class initializer. Does not allocate any buffer.
339 * @if maint
340 * That's a lie. We initialize the base class with NULL, because the
341 * string class does its own memory management.
342 * @endif
344 explicit
345 basic_istringstream(ios_base::openmode __mode = ios_base::in)
346 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
347 { this->init(&_M_stringbuf); }
350 * @brief Starts with an existing string buffer.
351 * @param str A string to copy as a starting buffer.
352 * @param mode Whether the buffer can read, or write, or both.
354 * @c ios_base::in is automatically included in @a mode.
356 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
357 * to the base class initializer.
359 * @if maint
360 * That's a lie. We initialize the base class with NULL, because the
361 * string class does its own memory management.
362 * @endif
364 explicit
365 basic_istringstream(const __string_type& __str,
366 ios_base::openmode __mode = ios_base::in)
367 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
368 { this->init(&_M_stringbuf); }
371 * @brief The destructor does nothing.
373 * The buffer is deallocated by the stringbuf object, not the
374 * formatting stream.
376 ~basic_istringstream()
379 // Members:
381 * @brief Accessing the underlying buffer.
382 * @return The current basic_stringbuf buffer.
384 * This hides both signatures of std::basic_ios::rdbuf().
386 __stringbuf_type*
387 rdbuf() const
388 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
391 * @brief Copying out the string buffer.
392 * @return @c rdbuf()->str()
394 __string_type
395 str() const
396 { return _M_stringbuf.str(); }
399 * @brief Setting a new buffer.
400 * @param s The string to use as a new sequence.
402 * Calls @c rdbuf()->str(s).
404 void
405 str(const __string_type& __s)
406 { _M_stringbuf.str(__s); }
410 // [27.7.3] Template class basic_ostringstream
412 * @brief Controlling output for std::string.
414 * This class supports writing to objects of type std::basic_string,
415 * using the inherited functions from std::basic_ostream. To control
416 * the associated sequence, an instance of std::basic_stringbuf is used,
417 * which this page refers to as @c sb.
419 template <typename _CharT, typename _Traits, typename _Alloc>
420 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
422 public:
423 // Types:
424 typedef _CharT char_type;
425 typedef _Traits traits_type;
426 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
427 // 251. basic_stringbuf missing allocator_type
428 typedef _Alloc allocator_type;
429 #endif
430 typedef typename traits_type::int_type int_type;
431 typedef typename traits_type::pos_type pos_type;
432 typedef typename traits_type::off_type off_type;
434 // Non-standard types:
435 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
436 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
437 typedef basic_ostream<char_type, traits_type> __ostream_type;
439 private:
441 * @if maint
442 * @doctodo
443 * @endif
445 __stringbuf_type _M_stringbuf;
447 public:
448 // Constructors/destructor:
450 * @brief Default constructor starts with an empty string buffer.
451 * @param mode Whether the buffer can read, or write, or both.
453 * @c ios_base::out is automatically included in @a mode.
455 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
456 * class initializer. Does not allocate any buffer.
458 * @if maint
459 * That's a lie. We initialize the base class with NULL, because the
460 * string class does its own memory management.
461 * @endif
463 explicit
464 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
465 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
466 { this->init(&_M_stringbuf); }
469 * @brief Starts with an existing string buffer.
470 * @param str A string to copy as a starting buffer.
471 * @param mode Whether the buffer can read, or write, or both.
473 * @c ios_base::out is automatically included in @a mode.
475 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
476 * to the base class initializer.
478 * @if maint
479 * That's a lie. We initialize the base class with NULL, because the
480 * string class does its own memory management.
481 * @endif
483 explicit
484 basic_ostringstream(const __string_type& __str,
485 ios_base::openmode __mode = ios_base::out)
486 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
487 { this->init(&_M_stringbuf); }
490 * @brief The destructor does nothing.
492 * The buffer is deallocated by the stringbuf object, not the
493 * formatting stream.
495 ~basic_ostringstream()
498 // Members:
500 * @brief Accessing the underlying buffer.
501 * @return The current basic_stringbuf buffer.
503 * This hides both signatures of std::basic_ios::rdbuf().
505 __stringbuf_type*
506 rdbuf() const
507 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
510 * @brief Copying out the string buffer.
511 * @return @c rdbuf()->str()
513 __string_type
514 str() const
515 { return _M_stringbuf.str(); }
518 * @brief Setting a new buffer.
519 * @param s The string to use as a new sequence.
521 * Calls @c rdbuf()->str(s).
523 void
524 str(const __string_type& __s)
525 { _M_stringbuf.str(__s); }
529 // [27.7.4] Template class basic_stringstream
531 * @brief Controlling input and output for std::string.
533 * This class supports reading from and writing to objects of type
534 * std::basic_string, using the inherited functions from
535 * std::basic_iostream. To control the associated sequence, an instance
536 * of std::basic_stringbuf is used, which this page refers to as @c sb.
538 template <typename _CharT, typename _Traits, typename _Alloc>
539 class basic_stringstream : public basic_iostream<_CharT, _Traits>
541 public:
542 // Types:
543 typedef _CharT char_type;
544 typedef _Traits traits_type;
545 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
546 // 251. basic_stringbuf missing allocator_type
547 typedef _Alloc allocator_type;
548 #endif
549 typedef typename traits_type::int_type int_type;
550 typedef typename traits_type::pos_type pos_type;
551 typedef typename traits_type::off_type off_type;
553 // Non-standard Types:
554 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
555 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
556 typedef basic_iostream<char_type, traits_type> __iostream_type;
558 private:
560 * @if maint
561 * @doctodo
562 * @endif
564 __stringbuf_type _M_stringbuf;
566 public:
567 // Constructors/destructors
569 * @brief Default constructor starts with an empty string buffer.
570 * @param mode Whether the buffer can read, or write, or both.
572 * Initializes @c sb using @c mode, and passes @c &sb to the base
573 * class initializer. Does not allocate any buffer.
575 * @if maint
576 * That's a lie. We initialize the base class with NULL, because the
577 * string class does its own memory management.
578 * @endif
580 explicit
581 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
582 : __iostream_type(), _M_stringbuf(__m)
583 { this->init(&_M_stringbuf); }
586 * @brief Starts with an existing string buffer.
587 * @param str A string to copy as a starting buffer.
588 * @param mode Whether the buffer can read, or write, or both.
590 * Initializes @c sb using @a str and @c mode, and passes @c &sb
591 * to the base class initializer.
593 * @if maint
594 * That's a lie. We initialize the base class with NULL, because the
595 * string class does its own memory management.
596 * @endif
598 explicit
599 basic_stringstream(const __string_type& __str,
600 ios_base::openmode __m = ios_base::out | ios_base::in)
601 : __iostream_type(), _M_stringbuf(__str, __m)
602 { this->init(&_M_stringbuf); }
605 * @brief The destructor does nothing.
607 * The buffer is deallocated by the stringbuf object, not the
608 * formatting stream.
610 ~basic_stringstream()
613 // Members:
615 * @brief Accessing the underlying buffer.
616 * @return The current basic_stringbuf buffer.
618 * This hides both signatures of std::basic_ios::rdbuf().
620 __stringbuf_type*
621 rdbuf() const
622 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
625 * @brief Copying out the string buffer.
626 * @return @c rdbuf()->str()
628 __string_type
629 str() const
630 { return _M_stringbuf.str(); }
633 * @brief Setting a new buffer.
634 * @param s The string to use as a new sequence.
636 * Calls @c rdbuf()->str(s).
638 void
639 str(const __string_type& __s)
640 { _M_stringbuf.str(__s); }
642 } // namespace std
644 #ifdef _GLIBCXX_NO_TEMPLATE_EXPORT
645 # define export
646 #endif
647 #ifdef _GLIBCXX_FULLY_COMPLIANT_HEADERS
648 # include <bits/sstream.tcc>
649 #endif
651 #endif