]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/std_sstream.h
include: New directory.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / std_sstream.h
1 // String based streams -*- C++ -*-
2
3 // Copyright (C) 1997-1999 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.
10
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.
15
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.
20
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.
29
30 //
31 // ISO C++ 14882: 27.7 String-based streams
32 //
33
34 #ifndef _CPP_SSTREAM
35 #define _CPP_SSTREAM 1
36
37 #include <bits/std_istream.h>
38 #include <bits/std_ostream.h>
39
40 namespace std {
41
42 template<typename _CharT, typename _Traits, typename _Alloc>
43 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
44 {
45 public:
46 // Types:
47 typedef _CharT char_type;
48 typedef _Traits traits_type;
49 typedef typename traits_type::int_type int_type;
50 typedef typename traits_type::pos_type pos_type;
51 typedef typename traits_type::off_type off_type;
52
53 // Non-standard Types:
54 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
55 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
56 typedef typename __string_type::size_type __size_type;
57
58 private:
59 // Data Members:
60 __string_type _M_string;
61
62 public:
63 // Constructors:
64 explicit
65 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
66 : __streambuf_type(), _M_string()
67 { _M_stringbuf_init(__mode); }
68
69 explicit
70 basic_stringbuf(const __string_type& __str,
71 ios_base::openmode __mode = ios_base::in | ios_base::out)
72 : __streambuf_type(), _M_string(__str)
73 { _M_stringbuf_init(__mode); }
74
75 // Get and set:
76 __string_type
77 str() const
78 {
79 if (_M_mode & ios_base::in && !(_M_mode & ios_base::out))
80 return _M_string;
81 else
82 {
83 // This is the deal: _M_string.size() is value that
84 // represents the size of the intial string that makes
85 // _M_string, and may not be the correct size of the
86 // current stringbuf internal buffer.
87 __size_type __len = _M_string.size();
88 if (_M_out_cur > _M_out_beg)
89 __len = max(__size_type(_M_out_end - _M_out_beg), __len);
90 return __string_type(_M_out_beg, _M_out_beg + __len);
91 }
92 }
93
94 void
95 str(const __string_type& __s)
96 {
97 _M_string = __s;
98 _M_stringbuf_init(_M_mode);
99 }
100
101 protected:
102 // Common initialization code for both ctors goes here.
103 void
104 _M_stringbuf_init(ios_base::openmode __mode)
105 {
106 // _M_buf_size is a convenient alias for "what the streambuf
107 // thinks the allocated size of the string really is." This is
108 // necessary as ostringstreams are implemented with the
109 // streambufs having control of the allocation and
110 // re-allocation of the internal string object, _M_string.
111 _M_buf_size = _M_string.size();
112
113 // NB: Start ostringstream buffers at 1024 bytes. This is an
114 // experimental value (pronounced "arbitrary" in some of the
115 // hipper english-speaking countries), and can be changed to
116 // suite particular needs.
117 _M_buf_size_opt = 512;
118 _M_mode = __mode;
119 if (_M_mode & ios_base::ate)
120 _M_really_sync(0, _M_buf_size);
121 else
122 _M_really_sync(0, 0);
123 }
124
125 // Overridden virtual functions:
126 virtual int_type
127 underflow()
128 {
129 if (_M_in_cur && _M_in_cur < _M_in_end)
130 return traits_type::to_int_type(*gptr());
131 else
132 return traits_type::eof();
133 }
134
135 virtual int_type
136 pbackfail(int_type __c = traits_type::eof());
137
138 virtual int_type
139 overflow(int_type __c = traits_type::eof());
140
141 virtual __streambuf_type*
142 setbuf(char_type* __s, streamsize __n)
143 {
144 if (__n)
145 {
146 _M_string = __string_type(__s, __n);
147 _M_really_sync(0, 0);
148 }
149 return this;
150 }
151
152 virtual pos_type
153 seekoff(off_type __off, ios_base::seekdir __way,
154 ios_base::openmode __mode = ios_base::in | ios_base::out);
155
156 virtual pos_type
157 seekpos(pos_type __sp,
158 ios_base::openmode __mode = ios_base::in | ios_base::out);
159
160 // Internal function for correctly updating the internal buffer
161 // for a particular _M_string, due to initialization or
162 // re-sizing of an existing _M_string.
163 // Assumes: contents of _M_string and internal buffer match exactly.
164 // __i == _M_in_cur - _M_in_beg
165 // __o == _M_out_cur - _M_out_beg
166 virtual int
167 _M_really_sync(__size_type __i, __size_type __o)
168 {
169 char_type* __base = const_cast<char_type*>(_M_string.data());
170 bool __testin = _M_mode & ios_base::in;
171 bool __testout = _M_mode & ios_base::out;
172 __size_type __len = _M_string.size();
173
174 _M_buf = __base;
175 if (__testin)
176 this->setg(__base, __base + __i, __base + __len);
177 if (__testout)
178 {
179 this->setp(__base, __base + __len);
180 _M_out_cur += __o;
181 }
182 return 0;
183 }
184 };
185
186
187 // 27.7.2 Template class basic_istringstream
188 template<typename _CharT, typename _Traits, typename _Alloc>
189 class basic_istringstream : public basic_istream<_CharT, _Traits>
190 {
191 public:
192 // Types:
193 typedef _CharT char_type;
194 typedef _Traits traits_type;
195 typedef typename traits_type::int_type int_type;
196 typedef typename traits_type::pos_type pos_type;
197 typedef typename traits_type::off_type off_type;
198
199 // Non-standard types:
200 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
201 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
202 typedef basic_istream<char_type, traits_type> __istream_type;
203
204 // Constructors:
205 explicit
206 basic_istringstream(ios_base::openmode __mode = ios_base::in)
207 : __istream_type(new __stringbuf_type(__mode | ios_base::in))
208 { }
209
210 explicit
211 basic_istringstream(const __string_type& __str,
212 ios_base::openmode __mode = ios_base::in)
213 : __istream_type(new __stringbuf_type(__str, __mode | ios_base::in))
214 { }
215
216 ~basic_istringstream()
217 {
218 delete _M_streambuf;
219 _M_streambuf = NULL;
220 }
221
222 // Members:
223 __stringbuf_type*
224 rdbuf() const
225 { return static_cast<__stringbuf_type*>(_M_streambuf); }
226
227 __string_type
228 str() const
229 { return this->rdbuf()->str(); }
230
231 void
232 str(const __string_type& __s)
233 { rdbuf()->str(__s); }
234
235 };
236
237
238 // 27.7.3 Template class basic_ostringstream
239 template <typename _CharT, typename _Traits, typename _Alloc>
240 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
241 {
242 public:
243 // Types:
244 typedef _CharT char_type;
245 typedef _Traits traits_type;
246 typedef typename traits_type::int_type int_type;
247 typedef typename traits_type::pos_type pos_type;
248 typedef typename traits_type::off_type off_type;
249
250 // Non-standard types:
251 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
252 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
253 typedef basic_ostream<char_type, traits_type> __ostream_type;
254
255 // Constructors/destructor:
256 explicit
257 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
258 : __ostream_type(new __stringbuf_type(__mode | ios_base::out))
259 { }
260
261 explicit
262 basic_ostringstream(const __string_type __str,
263 ios_base::openmode __mode = ios_base::out)
264 : __ostream_type(new __stringbuf_type(__str, __mode | ios_base::out))
265 { }
266
267 ~basic_ostringstream()
268 {
269 delete _M_streambuf;
270 _M_streambuf = NULL;
271 }
272
273 // Members:
274 __stringbuf_type*
275 rdbuf() const
276 { return static_cast<__stringbuf_type*>(_M_streambuf); }
277
278 __string_type
279 str() const
280 { return this->rdbuf()->str(); }
281
282 void
283 str(const __string_type& __s)
284 { rdbuf()->str(__s); }
285
286 };
287
288
289 // 27.7.4 Template class basic_stringstream
290 template <typename _CharT, typename _Traits, typename _Alloc>
291 class basic_stringstream : public basic_iostream<_CharT, _Traits>
292 {
293 public:
294 // Types:
295 typedef _CharT char_type;
296 typedef _Traits traits_type;
297 typedef typename traits_type::int_type int_type;
298 typedef typename traits_type::pos_type pos_type;
299 typedef typename traits_type::off_type off_type;
300
301 // Non-standard Types:
302 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
303 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
304 typedef basic_iostream<char_type, traits_type> __iostream_type;
305
306 // Constructors/destructors
307 explicit
308 basic_stringstream(ios_base::openmode __mode =
309 ios_base::out | ios_base::in)
310 : __iostream_type(new __stringbuf_type(__mode))
311 { }
312
313 explicit
314 basic_stringstream(const __string_type& __str,
315 ios_base::openmode __mode =
316 ios_base::out | ios_base::in)
317 : __iostream_type(new __stringbuf_type(__str, __mode))
318 { }
319
320 ~basic_stringstream()
321 {
322 delete _M_streambuf;
323 _M_streambuf = NULL;
324 }
325
326 // Members:
327 __stringbuf_type*
328 rdbuf() const
329 { return static_cast<__stringbuf_type*>(_M_streambuf); }
330
331 __string_type
332 str() const
333 { return rdbuf()->str(); }
334
335 void
336 str(const __string_type& __s)
337 { rdbuf()->str(__s); }
338 };
339
340 } // namespace std
341
342
343
344 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
345 # define export
346 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
347 # include <bits/sstream.tcc>
348 #endif
349 #endif
350
351
352 #endif /* _CPP_SSTREAM */
353
354
355
356
357
358
359
360
361
362
363
364
365
366