Create startup files from the GCC sources and drop our versions.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / std / std_sstream.h
1 // String based streams -*- C++ -*-
2
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.
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 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.
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 //
32 // ISO C++ 14882: 27.7  String-based streams
33 //
34
35 /** @file sstream
36  *  This is a Standard C++ Library header.
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 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    *
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.)
56    *
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.
60   */
61   template<typename _CharT, typename _Traits, typename _Alloc>
62     class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
63     {
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;
74
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;
78
79     protected:
80       /**
81        *  @if maint
82        *  Place to stash in || out || in | out settings for current stringbuf.
83        *  @endif
84       */
85       ios_base::openmode        _M_mode;
86
87       // Data Members:
88       __string_type             _M_string;
89
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.
95        *
96        *  The default constructor initializes the parent class using its
97        *  own default ctor.
98       */
99       explicit
100       basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
101       : __streambuf_type(), _M_mode(__mode), _M_string()
102       { }
103
104       /**
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.
108        *
109        *  This constructor initializes the parent class using its
110        *  own default ctor.
111       */
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); }
117
118       // Get and set:
119       /**
120        *  @brief  Copying out the string buffer.
121        *  @return  A copy of one of the underlying sequences.
122        *
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
126       */
127       __string_type
128       str() const
129       {
130         if (this->pptr())
131           {
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());
137           }
138         else
139           return _M_string;
140       }
141
142       /**
143        *  @brief  Setting a new buffer.
144        *  @param  s  The string to use as a new sequence.
145        *
146        *  Deallocates any previous stored sequence, then copies @a s to
147        *  use as a new one.
148       */
149       void
150       str(const __string_type& __s)
151       {
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);
155       }
156
157     protected:
158       // Common initialization code goes here.
159       void
160       _M_stringbuf_init(ios_base::openmode __mode)
161       {
162         this->_M_mode = __mode;
163
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);
168       }
169
170       virtual int_type
171       underflow();
172
173       virtual int_type
174       pbackfail(int_type __c = traits_type::eof());
175
176       virtual int_type
177       overflow(int_type __c = traits_type::eof());
178
179       /**
180        *  @brief  Manipulates the buffer.
181        *  @param  s  Pointer to a buffer area.
182        *  @param  n  Size of @a s.
183        *  @return  @c this
184        *
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.
189       */
190       virtual __streambuf_type*
191       setbuf(char_type* __s, streamsize __n)
192       {
193         if (__s && __n >= 0)
194           {
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.
199             
200             // Step 1: Destroy the current internal array.
201             _M_string.assign(__s, __n);
202             
203             // Step 2: Use the external array.
204             _M_sync(__s, 0, 0);
205           }
206         return this;
207       }
208
209       virtual pos_type
210       seekoff(off_type __off, ios_base::seekdir __way,
211               ios_base::openmode __mode = ios_base::in | ios_base::out);
212
213       virtual pos_type
214       seekpos(pos_type __sp,
215               ios_base::openmode __mode = ios_base::in | ios_base::out);
216
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)
225       {
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();
229
230         if (__testin)
231           this->setg(__base, __base + __i, __end);
232         if (__testout)
233           {
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);
247           }
248       }
249
250       // Internal function for correctly updating egptr() to the actual
251       // string end.
252       void
253       _M_update_egptr()
254       {
255         const bool __testin = this->_M_mode & ios_base::in;
256
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());
262       }
263     };
264
265
266   // [27.7.2] Template class basic_istringstream
267   /**
268    *  @brief  Controlling input for std::string.
269    *
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.
274   */
275   template<typename _CharT, typename _Traits, typename _Alloc>
276     class basic_istringstream : public basic_istream<_CharT, _Traits>
277     {
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;
288
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;
293
294     private:
295       __stringbuf_type  _M_stringbuf;
296
297     public:
298       // Constructors:
299       /**
300        *  @brief  Default constructor starts with an empty string buffer.
301        *  @param  mode  Whether the buffer can read, or write, or both.
302        *
303        *  @c ios_base::in is automatically included in @a mode.
304        *
305        *  Initializes @c sb using @c mode|in, and passes @c &sb to the base
306        *  class initializer.  Does not allocate any buffer.
307        *
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
312       */
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); }
317
318       /**
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.
322        *
323        *  @c ios_base::in is automatically included in @a mode.
324        *
325        *  Initializes @c sb using @a str and @c mode|in, and passes @c &sb
326        *  to the base class initializer.
327        *
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
332       */
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); }
338
339       /**
340        *  @brief  The destructor does nothing.
341        *
342        *  The buffer is deallocated by the stringbuf object, not the
343        *  formatting stream.
344       */
345       ~basic_istringstream()
346       { }
347
348       // Members:
349       /**
350        *  @brief  Accessing the underlying buffer.
351        *  @return  The current basic_stringbuf buffer.
352        *
353        *  This hides both signatures of std::basic_ios::rdbuf().
354       */
355       __stringbuf_type*
356       rdbuf() const
357       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
358
359       /**
360        *  @brief  Copying out the string buffer.
361        *  @return  @c rdbuf()->str()
362       */
363       __string_type
364       str() const
365       { return _M_stringbuf.str(); }
366
367       /**
368        *  @brief  Setting a new buffer.
369        *  @param  s  The string to use as a new sequence.
370        *
371        *  Calls @c rdbuf()->str(s).
372       */
373       void
374       str(const __string_type& __s)
375       { _M_stringbuf.str(__s); }
376     };
377
378
379   // [27.7.3] Template class basic_ostringstream
380   /**
381    *  @brief  Controlling output for std::string.
382    *
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.
387   */
388   template <typename _CharT, typename _Traits, typename _Alloc>
389     class basic_ostringstream : public basic_ostream<_CharT, _Traits>
390     {
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;
401
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;
406
407     private:
408       __stringbuf_type  _M_stringbuf;
409
410     public:
411       // Constructors/destructor:
412       /**
413        *  @brief  Default constructor starts with an empty string 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 @c mode|out, and passes @c &sb to the base
419        *  class initializer.  Does not allocate any buffer.
420        *
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
425       */
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); }
430
431       /**
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.
435        *
436        *  @c ios_base::out is automatically included in @a mode.
437        *
438        *  Initializes @c sb using @a str and @c mode|out, and passes @c &sb
439        *  to the base class initializer.
440        *
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
445       */
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); }
451
452       /**
453        *  @brief  The destructor does nothing.
454        *
455        *  The buffer is deallocated by the stringbuf object, not the
456        *  formatting stream.
457       */
458       ~basic_ostringstream()
459       { }
460
461       // Members:
462       /**
463        *  @brief  Accessing the underlying buffer.
464        *  @return  The current basic_stringbuf buffer.
465        *
466        *  This hides both signatures of std::basic_ios::rdbuf().
467       */
468       __stringbuf_type*
469       rdbuf() const
470       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
471
472       /**
473        *  @brief  Copying out the string buffer.
474        *  @return  @c rdbuf()->str()
475       */
476       __string_type
477       str() const
478       { return _M_stringbuf.str(); }
479
480       /**
481        *  @brief  Setting a new buffer.
482        *  @param  s  The string to use as a new sequence.
483        *
484        *  Calls @c rdbuf()->str(s).
485       */
486       void
487       str(const __string_type& __s)
488       { _M_stringbuf.str(__s); }
489     };
490
491
492   // [27.7.4] Template class basic_stringstream
493   /**
494    *  @brief  Controlling input and output for std::string.
495    *
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.
500   */
501   template <typename _CharT, typename _Traits, typename _Alloc>
502     class basic_stringstream : public basic_iostream<_CharT, _Traits>
503     {
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;
514
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;
519
520     private:
521       __stringbuf_type  _M_stringbuf;
522
523     public:
524       // Constructors/destructors
525       /**
526        *  @brief  Default constructor starts with an empty string buffer.
527        *  @param  mode  Whether the buffer can read, or write, or both.
528        *
529        *  Initializes @c sb using @c mode, and passes @c &sb to the base
530        *  class initializer.  Does not allocate any buffer.
531        *
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
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); }
541
542       /**
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.
546        *
547        *  Initializes @c sb using @a str and @c mode, and passes @c &sb
548        *  to the base class initializer.
549        *
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
554       */
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); }
560
561       /**
562        *  @brief  The destructor does nothing.
563        *
564        *  The buffer is deallocated by the stringbuf object, not the
565        *  formatting stream.
566       */
567       ~basic_stringstream()
568       { }
569
570       // Members:
571       /**
572        *  @brief  Accessing the underlying buffer.
573        *  @return  The current basic_stringbuf buffer.
574        *
575        *  This hides both signatures of std::basic_ios::rdbuf().
576       */
577       __stringbuf_type*
578       rdbuf() const
579       { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
580
581       /**
582        *  @brief  Copying out the string buffer.
583        *  @return  @c rdbuf()->str()
584       */
585       __string_type
586       str() const
587       { return _M_stringbuf.str(); }
588
589       /**
590        *  @brief  Setting a new buffer.
591        *  @param  s  The string to use as a new sequence.
592        *
593        *  Calls @c rdbuf()->str(s).
594       */
595       void
596       str(const __string_type& __s)
597       { _M_stringbuf.str(__s); }
598     };
599 } // namespace std
600
601 #ifndef _GLIBCXX_EXPORT_TEMPLATE
602 # include <bits/sstream.tcc>
603 #endif
604
605 #endif /* _GLIBCXX_SSTREAM */