Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / include / std / std_sstream.h
blob66215b2f730de5556f8f2c9e9d9331d8bf6ceaae
1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2002, 2003, 2004
4 // 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.
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.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
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.
32 // ISO C++ 14882: 27.7 String-based streams
35 /** @file sstream
36 * This is a Standard C++ Library header.
39 #ifndef _GLIBCXX_SSTREAM
40 #define _GLIBCXX_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 // _GLIBCXX_RESOLVE_LIB_DEFECTS
69 // 251. basic_stringbuf missing allocator_type
70 typedef _Alloc allocator_type;
71 typedef typename traits_type::int_type int_type;
72 typedef typename traits_type::pos_type pos_type;
73 typedef typename traits_type::off_type off_type;
75 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
76 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
77 typedef typename __string_type::size_type __size_type;
79 protected:
80 /**
81 * @if maint
82 * Place to stash in || out || in | out settings for current stringbuf.
83 * @endif
85 ios_base::openmode _M_mode;
87 // Data Members:
88 __string_type _M_string;
90 public:
91 // Constructors:
92 /**
93 * @brief Starts with an empty string buffer.
94 * @param mode Whether the buffer can read, or write, or both.
96 * The default constructor initializes the parent class using its
97 * own default ctor.
99 explicit
100 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
101 : __streambuf_type(), _M_mode(__mode), _M_string()
105 * @brief Starts with an existing string buffer.
106 * @param str A string to copy as a starting buffer.
107 * @param mode Whether the buffer can read, or write, or both.
109 * This constructor initializes the parent class using its
110 * own default ctor.
112 explicit
113 basic_stringbuf(const __string_type& __str,
114 ios_base::openmode __mode = ios_base::in | ios_base::out)
115 : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
116 { _M_stringbuf_init(__mode); }
118 // Get and set:
120 * @brief Copying out the string buffer.
121 * @return A copy of one of the underlying sequences.
123 * "If the buffer is only created in input mode, the underlying
124 * character sequence is equal to the input sequence; otherwise, it
125 * is equal to the output sequence." [27.7.1.2]/1
127 __string_type
128 str() const
130 if (this->pptr())
132 // The current egptr() may not be the actual string end.
133 if (this->pptr() > this->egptr())
134 return __string_type(this->pbase(), this->pptr());
135 else
136 return __string_type(this->pbase(), this->egptr());
138 else
139 return _M_string;
143 * @brief Setting a new buffer.
144 * @param s The string to use as a new sequence.
146 * Deallocates any previous stored sequence, then copies @a s to
147 * use as a new one.
149 void
150 str(const __string_type& __s)
152 // Cannot use _M_string = __s, since v3 strings are COW.
153 _M_string.assign(__s.data(), __s.size());
154 _M_stringbuf_init(this->_M_mode);
157 protected:
158 // Common initialization code goes here.
159 void
160 _M_stringbuf_init(ios_base::openmode __mode)
162 this->_M_mode = __mode;
164 __size_type __len = 0;
165 if (this->_M_mode & (ios_base::ate | ios_base::app))
166 __len = _M_string.size();
167 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
170 virtual int_type
171 underflow();
173 virtual int_type
174 pbackfail(int_type __c = traits_type::eof());
176 virtual int_type
177 overflow(int_type __c = traits_type::eof());
180 * @brief Manipulates the buffer.
181 * @param s Pointer to a buffer area.
182 * @param n Size of @a s.
183 * @return @c this
185 * If no buffer has already been created, and both @a s and @a n are
186 * non-zero, then @c s is used as a buffer; see
187 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
188 * for more.
190 virtual __streambuf_type*
191 setbuf(char_type* __s, streamsize __n)
193 if (__s && __n >= 0)
195 // This is implementation-defined behavior, and assumes
196 // that an external char_type array of length __n exists
197 // and has been pre-allocated. If this is not the case,
198 // things will quickly blow up.
200 // Step 1: Destroy the current internal array.
201 _M_string.assign(__s, __n);
203 // Step 2: Use the external array.
204 _M_sync(__s, 0, 0);
206 return this;
209 virtual pos_type
210 seekoff(off_type __off, ios_base::seekdir __way,
211 ios_base::openmode __mode = ios_base::in | ios_base::out);
213 virtual pos_type
214 seekpos(pos_type __sp,
215 ios_base::openmode __mode = ios_base::in | ios_base::out);
217 // Internal function for correctly updating the internal buffer
218 // for a particular _M_string, due to initialization or
219 // re-sizing of an existing _M_string.
220 // Assumes: contents of _M_string and internal buffer match exactly.
221 // __i == _M_in_cur - _M_in_beg
222 // __o == _M_out_cur - _M_out_beg
223 void
224 _M_sync(char_type* __base, __size_type __i, __size_type __o)
226 const bool __testin = this->_M_mode & ios_base::in;
227 const bool __testout = this->_M_mode & ios_base::out;
228 char_type* __end = __base + _M_string.size();
230 if (__testin)
231 this->setg(__base, __base + __i, __end);
232 if (__testout)
234 // If __base comes from setbuf we cannot trust capacity()
235 // to match the size of the buffer area: in general, after
236 // Step 1 above, _M_string.capacity() >= __n.
237 if (__base == _M_string.data())
238 this->setp(__base, __base + _M_string.capacity());
239 else
240 this->setp(__base, __end);
241 this->pbump(__o);
242 // egptr() always tracks the string end. When !__testin,
243 // for the correct functioning of the streambuf inlines
244 // the other get area pointers are identical.
245 if (!__testin)
246 this->setg(__end, __end, __end);
250 // Internal function for correctly updating egptr() to the actual
251 // string end.
252 void
253 _M_update_egptr()
255 const bool __testin = this->_M_mode & ios_base::in;
257 if (this->pptr() && this->pptr() > this->egptr())
258 if (__testin)
259 this->setg(this->eback(), this->gptr(), this->pptr());
260 else
261 this->setg(this->pptr(), this->pptr(), this->pptr());
266 // [27.7.2] Template class basic_istringstream
268 * @brief Controlling input for std::string.
270 * This class supports reading from objects of type std::basic_string,
271 * using the inherited functions from std::basic_istream. To control
272 * the associated sequence, an instance of std::basic_stringbuf is used,
273 * which this page refers to as @c sb.
275 template<typename _CharT, typename _Traits, typename _Alloc>
276 class basic_istringstream : public basic_istream<_CharT, _Traits>
278 public:
279 // Types:
280 typedef _CharT char_type;
281 typedef _Traits traits_type;
282 // _GLIBCXX_RESOLVE_LIB_DEFECTS
283 // 251. basic_stringbuf missing allocator_type
284 typedef _Alloc allocator_type;
285 typedef typename traits_type::int_type int_type;
286 typedef typename traits_type::pos_type pos_type;
287 typedef typename traits_type::off_type off_type;
289 // Non-standard types:
290 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
291 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
292 typedef basic_istream<char_type, traits_type> __istream_type;
294 private:
295 __stringbuf_type _M_stringbuf;
297 public:
298 // Constructors:
300 * @brief Default constructor starts with an empty string buffer.
301 * @param mode Whether the buffer can read, or write, or both.
303 * @c ios_base::in is automatically included in @a mode.
305 * Initializes @c sb using @c mode|in, and passes @c &sb to the base
306 * class initializer. Does not allocate any buffer.
308 * @if maint
309 * That's a lie. We initialize the base class with NULL, because the
310 * string class does its own memory management.
311 * @endif
313 explicit
314 basic_istringstream(ios_base::openmode __mode = ios_base::in)
315 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
316 { this->init(&_M_stringbuf); }
319 * @brief Starts with an existing string buffer.
320 * @param str A string to copy as a starting buffer.
321 * @param mode Whether the buffer can read, or write, or both.
323 * @c ios_base::in is automatically included in @a mode.
325 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
326 * to the base class initializer.
328 * @if maint
329 * That's a lie. We initialize the base class with NULL, because the
330 * string class does its own memory management.
331 * @endif
333 explicit
334 basic_istringstream(const __string_type& __str,
335 ios_base::openmode __mode = ios_base::in)
336 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
337 { this->init(&_M_stringbuf); }
340 * @brief The destructor does nothing.
342 * The buffer is deallocated by the stringbuf object, not the
343 * formatting stream.
345 ~basic_istringstream()
348 // Members:
350 * @brief Accessing the underlying buffer.
351 * @return The current basic_stringbuf buffer.
353 * This hides both signatures of std::basic_ios::rdbuf().
355 __stringbuf_type*
356 rdbuf() const
357 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
360 * @brief Copying out the string buffer.
361 * @return @c rdbuf()->str()
363 __string_type
364 str() const
365 { return _M_stringbuf.str(); }
368 * @brief Setting a new buffer.
369 * @param s The string to use as a new sequence.
371 * Calls @c rdbuf()->str(s).
373 void
374 str(const __string_type& __s)
375 { _M_stringbuf.str(__s); }
379 // [27.7.3] Template class basic_ostringstream
381 * @brief Controlling output for std::string.
383 * This class supports writing to objects of type std::basic_string,
384 * using the inherited functions from std::basic_ostream. To control
385 * the associated sequence, an instance of std::basic_stringbuf is used,
386 * which this page refers to as @c sb.
388 template <typename _CharT, typename _Traits, typename _Alloc>
389 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
391 public:
392 // Types:
393 typedef _CharT char_type;
394 typedef _Traits traits_type;
395 // _GLIBCXX_RESOLVE_LIB_DEFECTS
396 // 251. basic_stringbuf missing allocator_type
397 typedef _Alloc allocator_type;
398 typedef typename traits_type::int_type int_type;
399 typedef typename traits_type::pos_type pos_type;
400 typedef typename traits_type::off_type off_type;
402 // Non-standard types:
403 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
404 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
405 typedef basic_ostream<char_type, traits_type> __ostream_type;
407 private:
408 __stringbuf_type _M_stringbuf;
410 public:
411 // Constructors/destructor:
413 * @brief Default constructor starts with an empty string buffer.
414 * @param mode Whether the buffer can read, or write, or both.
416 * @c ios_base::out is automatically included in @a mode.
418 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
419 * class initializer. Does not allocate any buffer.
421 * @if maint
422 * That's a lie. We initialize the base class with NULL, because the
423 * string class does its own memory management.
424 * @endif
426 explicit
427 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
428 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
429 { this->init(&_M_stringbuf); }
432 * @brief Starts with an existing string buffer.
433 * @param str A string to copy as a starting buffer.
434 * @param mode Whether the buffer can read, or write, or both.
436 * @c ios_base::out is automatically included in @a mode.
438 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
439 * to the base class initializer.
441 * @if maint
442 * That's a lie. We initialize the base class with NULL, because the
443 * string class does its own memory management.
444 * @endif
446 explicit
447 basic_ostringstream(const __string_type& __str,
448 ios_base::openmode __mode = ios_base::out)
449 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
450 { this->init(&_M_stringbuf); }
453 * @brief The destructor does nothing.
455 * The buffer is deallocated by the stringbuf object, not the
456 * formatting stream.
458 ~basic_ostringstream()
461 // Members:
463 * @brief Accessing the underlying buffer.
464 * @return The current basic_stringbuf buffer.
466 * This hides both signatures of std::basic_ios::rdbuf().
468 __stringbuf_type*
469 rdbuf() const
470 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
473 * @brief Copying out the string buffer.
474 * @return @c rdbuf()->str()
476 __string_type
477 str() const
478 { return _M_stringbuf.str(); }
481 * @brief Setting a new buffer.
482 * @param s The string to use as a new sequence.
484 * Calls @c rdbuf()->str(s).
486 void
487 str(const __string_type& __s)
488 { _M_stringbuf.str(__s); }
492 // [27.7.4] Template class basic_stringstream
494 * @brief Controlling input and output for std::string.
496 * This class supports reading from and writing to objects of type
497 * std::basic_string, using the inherited functions from
498 * std::basic_iostream. To control the associated sequence, an instance
499 * of std::basic_stringbuf is used, which this page refers to as @c sb.
501 template <typename _CharT, typename _Traits, typename _Alloc>
502 class basic_stringstream : public basic_iostream<_CharT, _Traits>
504 public:
505 // Types:
506 typedef _CharT char_type;
507 typedef _Traits traits_type;
508 // _GLIBCXX_RESOLVE_LIB_DEFECTS
509 // 251. basic_stringbuf missing allocator_type
510 typedef _Alloc allocator_type;
511 typedef typename traits_type::int_type int_type;
512 typedef typename traits_type::pos_type pos_type;
513 typedef typename traits_type::off_type off_type;
515 // Non-standard Types:
516 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
517 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
518 typedef basic_iostream<char_type, traits_type> __iostream_type;
520 private:
521 __stringbuf_type _M_stringbuf;
523 public:
524 // Constructors/destructors
526 * @brief Default constructor starts with an empty string buffer.
527 * @param mode Whether the buffer can read, or write, or both.
529 * Initializes @c sb using @c mode, and passes @c &sb to the base
530 * class initializer. Does not allocate any buffer.
532 * @if maint
533 * That's a lie. We initialize the base class with NULL, because the
534 * string class does its own memory management.
535 * @endif
537 explicit
538 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
539 : __iostream_type(), _M_stringbuf(__m)
540 { this->init(&_M_stringbuf); }
543 * @brief Starts with an existing string buffer.
544 * @param str A string to copy as a starting buffer.
545 * @param mode Whether the buffer can read, or write, or both.
547 * Initializes @c sb using @a str and @c mode, and passes @c &sb
548 * to the base class initializer.
550 * @if maint
551 * That's a lie. We initialize the base class with NULL, because the
552 * string class does its own memory management.
553 * @endif
555 explicit
556 basic_stringstream(const __string_type& __str,
557 ios_base::openmode __m = ios_base::out | ios_base::in)
558 : __iostream_type(), _M_stringbuf(__str, __m)
559 { this->init(&_M_stringbuf); }
562 * @brief The destructor does nothing.
564 * The buffer is deallocated by the stringbuf object, not the
565 * formatting stream.
567 ~basic_stringstream()
570 // Members:
572 * @brief Accessing the underlying buffer.
573 * @return The current basic_stringbuf buffer.
575 * This hides both signatures of std::basic_ios::rdbuf().
577 __stringbuf_type*
578 rdbuf() const
579 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
582 * @brief Copying out the string buffer.
583 * @return @c rdbuf()->str()
585 __string_type
586 str() const
587 { return _M_stringbuf.str(); }
590 * @brief Setting a new buffer.
591 * @param s The string to use as a new sequence.
593 * Calls @c rdbuf()->str(s).
595 void
596 str(const __string_type& __s)
597 { _M_stringbuf.str(__s); }
599 } // namespace std
601 #ifndef _GLIBCXX_EXPORT_TEMPLATE
602 # include <bits/sstream.tcc>
603 #endif
605 #endif /* _GLIBCXX_SSTREAM */