]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/_sstream.h
Add STLport 5.1.4
[polintos/scott/priv.git] / include / c++ / stl / stl / _sstream.h
1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18
19
20 // This header defines classes basic_stringbuf, basic_istringstream,
21 // basic_ostringstream, and basic_stringstream.  These classes
22 // represent streamsbufs and streams whose sources or destinations are
23 // C++ strings.
24
25 #ifndef _STLP_INTERNAL_SSTREAM
26 #define _STLP_INTERNAL_SSTREAM
27
28 #ifndef _STLP_INTERNAL_STREAMBUF
29 #  include <stl/_streambuf.h>
30 #endif
31
32 #ifndef _STLP_INTERNAL_ISTREAM
33 #  include <stl/_istream.h> // Includes <ostream>, <ios>, <iosfwd>
34 #endif
35
36 #ifndef _STLP_INTERNAL_STRING_H
37 #  include <stl/_string.h>
38 #endif
39
40 _STLP_BEGIN_NAMESPACE
41
42 //----------------------------------------------------------------------
43 // This version of basic_stringbuf relies on the internal details of
44 // basic_string.  It relies on the fact that, in this implementation,
45 // basic_string's iterators are pointers.  It also assumes (as allowed
46 // by the standard) that _CharT is a POD type.
47
48 // We have a very small buffer for the put area, just so that we don't
49 // have to use append() for every sputc.  Conceptually, the buffer
50 // immediately follows the end of the underlying string.  We use this
51 // buffer when appending to write-only streambufs, but we don't use it
52 // for read-write streambufs.
53
54 template <class _CharT, class _Traits, class _Alloc>
55 class basic_stringbuf : public basic_streambuf<_CharT, _Traits> {
56 public:                         // Typedefs.
57   typedef _CharT                     char_type;
58   typedef typename _Traits::int_type int_type;
59   typedef typename _Traits::pos_type pos_type;
60   typedef typename _Traits::off_type off_type;
61   typedef _Traits                    traits_type;
62
63   typedef basic_streambuf<_CharT, _Traits>          _Base;
64   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Self;
65   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
66
67 public:                         // Constructors, destructor.
68   explicit basic_stringbuf(ios_base::openmode __mode
69                                       = ios_base::in | ios_base::out);
70   explicit basic_stringbuf(const _String& __s, ios_base::openmode __mode
71                                       = ios_base::in | ios_base::out);
72   virtual ~basic_stringbuf();
73
74 public:                         // Get or set the string.
75   _String str() const { _M_append_buffer(); return _M_str; }
76   void str(const _String& __s);
77
78 protected:                      // Overridden virtual member functions.
79   virtual int_type underflow();
80   virtual int_type uflow();
81   virtual int_type pbackfail(int_type __c);
82   virtual int_type overflow(int_type __c);
83   int_type pbackfail() {return pbackfail(_Traits::eof());}
84   int_type overflow() {return overflow(_Traits::eof());}
85
86   virtual streamsize xsputn(const char_type* __s, streamsize __n);
87   virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
88
89   virtual _Base* setbuf(_CharT* __buf, streamsize __n);
90   virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
91                            ios_base::openmode __mode
92                                       = ios_base::in | ios_base::out);
93   virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
94                                       = ios_base::in | ios_base::out);
95
96 private:                        // Helper functions.
97   // Append the internal buffer to the string if necessary.
98   void _M_append_buffer() const;
99   void _M_set_ptrs();
100
101 private:
102   ios_base::openmode _M_mode;
103   mutable basic_string<_CharT, _Traits, _Alloc> _M_str;
104
105   enum _JustName { _S_BufSiz = 8 };
106   _CharT _M_Buf[ 8 /* _S_BufSiz */];
107 };
108
109 #if defined (_STLP_USE_TEMPLATE_EXPORT)
110 _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<char, char_traits<char>, allocator<char> >;
111 #  if !defined (_STLP_NO_WCHAR_T)
112 _STLP_EXPORT_TEMPLATE_CLASS basic_stringbuf<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
113 #  endif
114 #endif /* _STLP_USE_TEMPLATE_EXPORT */
115
116 //----------------------------------------------------------------------
117 // Class basic_istringstream, an input stream that uses a stringbuf.
118
119 template <class _CharT, class _Traits, class _Alloc>
120 class basic_istringstream : public basic_istream<_CharT, _Traits> {
121 public:                         // Typedefs
122   typedef typename _Traits::char_type   char_type;
123   typedef typename _Traits::int_type    int_type;
124   typedef typename _Traits::pos_type    pos_type;
125   typedef typename _Traits::off_type    off_type;
126   typedef _Traits traits_type;
127
128   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
129   typedef basic_istream<_CharT, _Traits>            _Base;
130   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
131   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
132
133 public:                         // Constructors, destructor.
134   basic_istringstream(ios_base::openmode __mode = ios_base::in);
135   basic_istringstream(const _String& __str,
136                       ios_base::openmode __mode = ios_base::in);
137   ~basic_istringstream();
138
139 public:                         // Member functions
140
141   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
142     { return __CONST_CAST(_Buf*,&_M_buf); }
143
144   _String str() const { return _M_buf.str(); }
145   void str(const _String& __s) { _M_buf.str(__s); }
146
147 private:
148   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
149
150 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
151   typedef basic_istringstream<_CharT, _Traits> _Self;
152   //explicitely defined as private to avoid warnings:
153   basic_istringstream(_Self const&);
154   _Self& operator = (_Self const&);
155 #endif
156 };
157
158
159 //----------------------------------------------------------------------
160 // Class basic_ostringstream, an output stream that uses a stringbuf.
161
162 template <class _CharT, class _Traits, class _Alloc>
163 class basic_ostringstream : public basic_ostream<_CharT, _Traits> {
164 public:                         // Typedefs
165   typedef typename _Traits::char_type   char_type;
166   typedef typename _Traits::int_type    int_type;
167   typedef typename _Traits::pos_type    pos_type;
168   typedef typename _Traits::off_type    off_type;
169   typedef _Traits traits_type;
170
171   typedef basic_ios<_CharT, _Traits>                _Basic_ios;
172   typedef basic_ostream<_CharT, _Traits>            _Base;
173   typedef basic_string<_CharT, _Traits, _Alloc>     _String;
174   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
175
176 public:                         // Constructors, destructor.
177   basic_ostringstream(ios_base::openmode __mode = ios_base::out);
178   basic_ostringstream(const _String& __str,
179                       ios_base::openmode __mode = ios_base::out);
180   ~basic_ostringstream();
181
182 public:                         // Member functions.
183
184   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
185     { return __CONST_CAST(_Buf*,&_M_buf); }
186
187   _String str() const { return _M_buf.str(); }
188     void str(const _String& __s) { _M_buf.str(__s); } // dwa 02/07/00 - BUG STOMPER DAVE
189
190
191 private:
192   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
193
194 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
195   typedef basic_ostringstream<_CharT, _Traits> _Self;
196   //explicitely defined as private to avoid warnings:
197   basic_ostringstream(_Self const&);
198   _Self& operator = (_Self const&);
199 #endif
200 };
201
202
203 //----------------------------------------------------------------------
204 // Class basic_stringstream, a bidirectional stream that uses a stringbuf.
205
206 template <class _CharT, class _Traits, class _Alloc>
207 class basic_stringstream : public basic_iostream<_CharT, _Traits> {
208 public:                         // Typedefs
209   typedef typename _Traits::char_type char_type;
210   typedef typename _Traits::int_type  int_type;
211   typedef typename _Traits::pos_type  pos_type;
212   typedef typename _Traits::off_type  off_type;
213   typedef _Traits  traits_type;
214
215   typedef basic_ios<_CharT, _Traits>                 _Basic_ios;
216   typedef basic_iostream<_CharT, _Traits>            _Base;
217   typedef basic_string<_CharT, _Traits, _Alloc>      _String;
218   typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;
219
220   typedef ios_base::openmode openmode;
221
222 public:                         // Constructors, destructor.
223   basic_stringstream(openmode __mod = ios_base::in | ios_base::out);
224   basic_stringstream(const _String& __str,
225                      openmode __mod = ios_base::in | ios_base::out);
226   ~basic_stringstream();
227
228 public:                         // Member functions.
229
230   basic_stringbuf<_CharT, _Traits, _Alloc>* rdbuf() const
231     { return __CONST_CAST(_Buf*,&_M_buf); }
232
233   _String str() const { return _M_buf.str(); }
234     void str(const _String& __s) { _M_buf.str(__s); }
235
236 private:
237   basic_stringbuf<_CharT, _Traits, _Alloc> _M_buf;
238
239 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
240   typedef basic_stringstream<_CharT, _Traits> _Self;
241   //explicitely defined as private to avoid warnings:
242   basic_stringstream(_Self const&);
243   _Self& operator = (_Self const&);
244 #endif
245 };
246
247
248 #if defined (_STLP_USE_TEMPLATE_EXPORT)
249 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<char, char_traits<char>, allocator<char> >;
250 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<char, char_traits<char>, allocator<char> >;
251 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<char, char_traits<char>, allocator<char> >;
252 #  if !defined (_STLP_NO_WCHAR_T)
253 _STLP_EXPORT_TEMPLATE_CLASS basic_istringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
254 _STLP_EXPORT_TEMPLATE_CLASS basic_ostringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
255 _STLP_EXPORT_TEMPLATE_CLASS basic_stringstream<wchar_t, char_traits<wchar_t>, allocator<wchar_t>  >;
256 #  endif
257 #endif /* _STLP_USE_TEMPLATE_EXPORT */
258
259 _STLP_END_NAMESPACE
260
261 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
262 #  include <stl/_sstream.c>
263 #endif
264
265 #endif /* _STLP_INTERNAL_SSTREAM */
266
267 // Local Variables:
268 // mode:C++
269 // End: