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