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