Branch data Line data Source code
1 : : // String based streams -*- C++ -*-
2 : :
3 : : // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 : : // 2006, 2008 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
18 : : // along with this library; see the file COPYING. If not, write to
19 : : // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 : : // Boston, MA 02110-1301, 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 : : /** @file sstream
32 : : * This is a Standard C++ Library header.
33 : : */
34 : :
35 : : //
36 : : // ISO C++ 14882: 27.7 String-based streams
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 : : _GLIBCXX_BEGIN_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 : : /// Place to stash in || out || in | out settings for current stringbuf.
81 : : ios_base::openmode _M_mode;
82 : :
83 : : // Data Members:
84 : : __string_type _M_string;
85 : :
86 : : public:
87 : : // Constructors:
88 : : /**
89 : : * @brief Starts with an empty string buffer.
90 : : * @param mode Whether the buffer can read, or write, or both.
91 : : *
92 : : * The default constructor initializes the parent class using its
93 : : * own default ctor.
94 : : */
95 : : explicit
96 : 260209 : basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
97 : 260209 : : __streambuf_type(), _M_mode(__mode), _M_string()
98 : 260209 : { }
99 : :
100 : : /**
101 : : * @brief Starts with an existing string buffer.
102 : : * @param str A string to copy as a starting buffer.
103 : : * @param mode Whether the buffer can read, or write, or both.
104 : : *
105 : : * This constructor initializes the parent class using its
106 : : * own default ctor.
107 : : */
108 : : explicit
109 : : basic_stringbuf(const __string_type& __str,
110 : : ios_base::openmode __mode = ios_base::in | ios_base::out)
111 : : : __streambuf_type(), _M_mode(), _M_string(__str.data(), __str.size())
112 : : { _M_stringbuf_init(__mode); }
113 : :
114 : : // Get and set:
115 : : /**
116 : : * @brief Copying out the string buffer.
117 : : * @return A copy of one of the underlying sequences.
118 : : *
119 : : * "If the buffer is only created in input mode, the underlying
120 : : * character sequence is equal to the input sequence; otherwise, it
121 : : * is equal to the output sequence." [27.7.1.2]/1
122 : : */
123 : : __string_type
124 : 250383 : str() const
125 : : {
126 : 250383 : __string_type __ret;
127 [ + - ]: 250383 : if (this->pptr())
128 : : {
129 : : // The current egptr() may not be the actual string end.
130 [ + + ]: 250383 : if (this->pptr() > this->egptr())
131 : 475444 : __ret = __string_type(this->pbase(), this->pptr());
132 : : else
133 : 25322 : __ret = __string_type(this->pbase(), this->egptr());
134 : : }
135 : : else
136 : 0 : __ret = _M_string;
137 : 250383 : return __ret;
138 : : }
139 : :
140 : : /**
141 : : * @brief Setting a new buffer.
142 : : * @param s The string to use as a new sequence.
143 : : *
144 : : * Deallocates any previous stored sequence, then copies @a s to
145 : : * use as a new one.
146 : : */
147 : : void
148 : 0 : str(const __string_type& __s)
149 : : {
150 : : // Cannot use _M_string = __s, since v3 strings are COW.
151 : 0 : _M_string.assign(__s.data(), __s.size());
152 : 0 : _M_stringbuf_init(_M_mode);
153 : 0 : }
154 : :
155 : : protected:
156 : : // Common initialization code goes here.
157 : : void
158 : 0 : _M_stringbuf_init(ios_base::openmode __mode)
159 : : {
160 : 0 : _M_mode = __mode;
161 : 0 : __size_type __len = 0;
162 [ # # ]: 0 : if (_M_mode & (ios_base::ate | ios_base::app))
163 : 0 : __len = _M_string.size();
164 : 0 : _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
165 : 0 : }
166 : :
167 : : virtual streamsize
168 : : showmanyc()
169 : : {
170 : : streamsize __ret = -1;
171 : : if (_M_mode & ios_base::in)
172 : : {
173 : : _M_update_egptr();
174 : : __ret = this->egptr() - this->gptr();
175 : : }
176 : : return __ret;
177 : : }
178 : :
179 : : virtual int_type
180 : : underflow();
181 : :
182 : : virtual int_type
183 : : pbackfail(int_type __c = traits_type::eof());
184 : :
185 : : virtual int_type
186 : : overflow(int_type __c = traits_type::eof());
187 : :
188 : : /**
189 : : * @brief Manipulates the buffer.
190 : : * @param s Pointer to a buffer area.
191 : : * @param n Size of @a s.
192 : : * @return @c this
193 : : *
194 : : * If no buffer has already been created, and both @a s and @a n are
195 : : * non-zero, then @c s is used as a buffer; see
196 : : * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
197 : : * for more.
198 : : */
199 : : virtual __streambuf_type*
200 : : setbuf(char_type* __s, streamsize __n)
201 : : {
202 : : if (__s && __n >= 0)
203 : : {
204 : : // This is implementation-defined behavior, and assumes
205 : : // that an external char_type array of length __n exists
206 : : // and has been pre-allocated. If this is not the case,
207 : : // things will quickly blow up.
208 : :
209 : : // Step 1: Destroy the current internal array.
210 : : _M_string.clear();
211 : :
212 : : // Step 2: Use the external array.
213 : : _M_sync(__s, __n, 0);
214 : : }
215 : : return this;
216 : : }
217 : :
218 : : virtual pos_type
219 : : seekoff(off_type __off, ios_base::seekdir __way,
220 : : ios_base::openmode __mode = ios_base::in | ios_base::out);
221 : :
222 : : virtual pos_type
223 : : seekpos(pos_type __sp,
224 : : ios_base::openmode __mode = ios_base::in | ios_base::out);
225 : :
226 : : // Internal function for correctly updating the internal buffer
227 : : // for a particular _M_string, due to initialization or re-sizing
228 : : // of an existing _M_string.
229 : : void
230 : : _M_sync(char_type* __base, __size_type __i, __size_type __o);
231 : :
232 : : // Internal function for correctly updating egptr() to the actual
233 : : // string end.
234 : : void
235 : : _M_update_egptr()
236 : : {
237 : : const bool __testin = _M_mode & ios_base::in;
238 : : if (this->pptr() && this->pptr() > this->egptr())
239 : : if (__testin)
240 : : this->setg(this->eback(), this->gptr(), this->pptr());
241 : : else
242 : : this->setg(this->pptr(), this->pptr(), this->pptr());
243 : : }
244 : : };
245 : :
246 : :
247 : : // [27.7.2] Template class basic_istringstream
248 : : /**
249 : : * @brief Controlling input for std::string.
250 : : *
251 : : * This class supports reading from objects of type std::basic_string,
252 : : * using the inherited functions from std::basic_istream. To control
253 : : * the associated sequence, an instance of std::basic_stringbuf is used,
254 : : * which this page refers to as @c sb.
255 : : */
256 : : template<typename _CharT, typename _Traits, typename _Alloc>
257 : : class basic_istringstream : public basic_istream<_CharT, _Traits>
258 : : {
259 : : public:
260 : : // Types:
261 : : typedef _CharT char_type;
262 : : typedef _Traits traits_type;
263 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
264 : : // 251. basic_stringbuf missing allocator_type
265 : : typedef _Alloc allocator_type;
266 : : typedef typename traits_type::int_type int_type;
267 : : typedef typename traits_type::pos_type pos_type;
268 : : typedef typename traits_type::off_type off_type;
269 : :
270 : : // Non-standard types:
271 : : typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
272 : : typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
273 : : typedef basic_istream<char_type, traits_type> __istream_type;
274 : :
275 : : private:
276 : : __stringbuf_type _M_stringbuf;
277 : :
278 : : public:
279 : : // Constructors:
280 : : /**
281 : : * @brief Default constructor starts with an empty string buffer.
282 : : * @param mode Whether the buffer can read, or write, or both.
283 : : *
284 : : * @c ios_base::in is automatically included in @a mode.
285 : : *
286 : : * Initializes @c sb using @c mode|in, and passes @c &sb to the base
287 : : * class initializer. Does not allocate any buffer.
288 : : *
289 : : * That's a lie. We initialize the base class with NULL, because the
290 : : * string class does its own memory management.
291 : : */
292 : : explicit
293 : : basic_istringstream(ios_base::openmode __mode = ios_base::in)
294 : : : __istream_type(), _M_stringbuf(__mode | ios_base::in)
295 : : { this->init(&_M_stringbuf); }
296 : :
297 : : /**
298 : : * @brief Starts with an existing string buffer.
299 : : * @param str A string to copy as a starting buffer.
300 : : * @param mode Whether the buffer can read, or write, or both.
301 : : *
302 : : * @c ios_base::in is automatically included in @a mode.
303 : : *
304 : : * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
305 : : * to the base class initializer.
306 : : *
307 : : * That's a lie. We initialize the base class with NULL, because the
308 : : * string class does its own memory management.
309 : : */
310 : : explicit
311 : : basic_istringstream(const __string_type& __str,
312 : : ios_base::openmode __mode = ios_base::in)
313 : : : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
314 : : { this->init(&_M_stringbuf); }
315 : :
316 : : /**
317 : : * @brief The destructor does nothing.
318 : : *
319 : : * The buffer is deallocated by the stringbuf object, not the
320 : : * formatting stream.
321 : : */
322 : : ~basic_istringstream()
323 : : { }
324 : :
325 : : // Members:
326 : : /**
327 : : * @brief Accessing the underlying buffer.
328 : : * @return The current basic_stringbuf buffer.
329 : : *
330 : : * This hides both signatures of std::basic_ios::rdbuf().
331 : : */
332 : : __stringbuf_type*
333 : : rdbuf() const
334 : : { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
335 : :
336 : : /**
337 : : * @brief Copying out the string buffer.
338 : : * @return @c rdbuf()->str()
339 : : */
340 : : __string_type
341 : : str() const
342 : : { return _M_stringbuf.str(); }
343 : :
344 : : /**
345 : : * @brief Setting a new buffer.
346 : : * @param s The string to use as a new sequence.
347 : : *
348 : : * Calls @c rdbuf()->str(s).
349 : : */
350 : : void
351 : : str(const __string_type& __s)
352 : : { _M_stringbuf.str(__s); }
353 : : };
354 : :
355 : :
356 : : // [27.7.3] Template class basic_ostringstream
357 : : /**
358 : : * @brief Controlling output for std::string.
359 : : *
360 : : * This class supports writing to objects of type std::basic_string,
361 : : * using the inherited functions from std::basic_ostream. To control
362 : : * the associated sequence, an instance of std::basic_stringbuf is used,
363 : : * which this page refers to as @c sb.
364 : : */
365 : : template <typename _CharT, typename _Traits, typename _Alloc>
366 : : class basic_ostringstream : public basic_ostream<_CharT, _Traits>
367 : : {
368 : : public:
369 : : // Types:
370 : : typedef _CharT char_type;
371 : : typedef _Traits traits_type;
372 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
373 : : // 251. basic_stringbuf missing allocator_type
374 : : typedef _Alloc allocator_type;
375 : : typedef typename traits_type::int_type int_type;
376 : : typedef typename traits_type::pos_type pos_type;
377 : : typedef typename traits_type::off_type off_type;
378 : :
379 : : // Non-standard types:
380 : : typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
381 : : typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
382 : : typedef basic_ostream<char_type, traits_type> __ostream_type;
383 : :
384 : : private:
385 : : __stringbuf_type _M_stringbuf;
386 : :
387 : : public:
388 : : // Constructors/destructor:
389 : : /**
390 : : * @brief Default constructor starts with an empty string buffer.
391 : : * @param mode Whether the buffer can read, or write, or both.
392 : : *
393 : : * @c ios_base::out is automatically included in @a mode.
394 : : *
395 : : * Initializes @c sb using @c mode|out, and passes @c &sb to the base
396 : : * class initializer. Does not allocate any buffer.
397 : : *
398 : : * That's a lie. We initialize the base class with NULL, because the
399 : : * string class does its own memory management.
400 : : */
401 : : explicit
402 : 255292 : basic_ostringstream(ios_base::openmode __mode = ios_base::out)
403 : 255292 : : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
404 : 255292 : { this->init(&_M_stringbuf); }
405 : :
406 : : /**
407 : : * @brief Starts with an existing string buffer.
408 : : * @param str A string to copy as a starting buffer.
409 : : * @param mode Whether the buffer can read, or write, or both.
410 : : *
411 : : * @c ios_base::out is automatically included in @a mode.
412 : : *
413 : : * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
414 : : * to the base class initializer.
415 : : *
416 : : * That's a lie. We initialize the base class with NULL, because the
417 : : * string class does its own memory management.
418 : : */
419 : : explicit
420 : : basic_ostringstream(const __string_type& __str,
421 : : ios_base::openmode __mode = ios_base::out)
422 : : : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
423 : : { this->init(&_M_stringbuf); }
424 : :
425 : : /**
426 : : * @brief The destructor does nothing.
427 : : *
428 : : * The buffer is deallocated by the stringbuf object, not the
429 : : * formatting stream.
430 : : */
431 : 255292 : ~basic_ostringstream()
432 [ # # ][ + - ]: 255292 : { }
[ - + ]
433 : :
434 : : // Members:
435 : : /**
436 : : * @brief Accessing the underlying buffer.
437 : : * @return The current basic_stringbuf buffer.
438 : : *
439 : : * This hides both signatures of std::basic_ios::rdbuf().
440 : : */
441 : : __stringbuf_type*
442 : : rdbuf() const
443 : : { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
444 : :
445 : : /**
446 : : * @brief Copying out the string buffer.
447 : : * @return @c rdbuf()->str()
448 : : */
449 : : __string_type
450 : 255683 : str() const
451 : 255683 : { return _M_stringbuf.str(); }
452 : :
453 : : /**
454 : : * @brief Setting a new buffer.
455 : : * @param s The string to use as a new sequence.
456 : : *
457 : : * Calls @c rdbuf()->str(s).
458 : : */
459 : : void
460 : : str(const __string_type& __s)
461 : 0 : { _M_stringbuf.str(__s); }
462 : : };
463 : :
464 : :
465 : : // [27.7.4] Template class basic_stringstream
466 : : /**
467 : : * @brief Controlling input and output for std::string.
468 : : *
469 : : * This class supports reading from and writing to objects of type
470 : : * std::basic_string, using the inherited functions from
471 : : * std::basic_iostream. To control the associated sequence, an instance
472 : : * of std::basic_stringbuf is used, which this page refers to as @c sb.
473 : : */
474 : : template <typename _CharT, typename _Traits, typename _Alloc>
475 : : class basic_stringstream : public basic_iostream<_CharT, _Traits>
476 : : {
477 : : public:
478 : : // Types:
479 : : typedef _CharT char_type;
480 : : typedef _Traits traits_type;
481 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
482 : : // 251. basic_stringbuf missing allocator_type
483 : : typedef _Alloc allocator_type;
484 : : typedef typename traits_type::int_type int_type;
485 : : typedef typename traits_type::pos_type pos_type;
486 : : typedef typename traits_type::off_type off_type;
487 : :
488 : : // Non-standard Types:
489 : : typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
490 : : typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
491 : : typedef basic_iostream<char_type, traits_type> __iostream_type;
492 : :
493 : : private:
494 : : __stringbuf_type _M_stringbuf;
495 : :
496 : : public:
497 : : // Constructors/destructors
498 : : /**
499 : : * @brief Default constructor starts with an empty string buffer.
500 : : * @param mode Whether the buffer can read, or write, or both.
501 : : *
502 : : * Initializes @c sb using @c mode, and passes @c &sb to the base
503 : : * class initializer. Does not allocate any buffer.
504 : : *
505 : : * That's a lie. We initialize the base class with NULL, because the
506 : : * string class does its own memory management.
507 : : */
508 : : explicit
509 : 4917 : basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
510 : 4917 : : __iostream_type(), _M_stringbuf(__m)
511 : 4917 : { this->init(&_M_stringbuf); }
512 : :
513 : : /**
514 : : * @brief Starts with an existing string buffer.
515 : : * @param str A string to copy as a starting buffer.
516 : : * @param mode Whether the buffer can read, or write, or both.
517 : : *
518 : : * Initializes @c sb using @a str and @c mode, and passes @c &sb
519 : : * to the base class initializer.
520 : : *
521 : : * That's a lie. We initialize the base class with NULL, because the
522 : : * string class does its own memory management.
523 : : */
524 : : explicit
525 : : basic_stringstream(const __string_type& __str,
526 : : ios_base::openmode __m = ios_base::out | ios_base::in)
527 : : : __iostream_type(), _M_stringbuf(__str, __m)
528 : : { this->init(&_M_stringbuf); }
529 : :
530 : : /**
531 : : * @brief The destructor does nothing.
532 : : *
533 : : * The buffer is deallocated by the stringbuf object, not the
534 : : * formatting stream.
535 : : */
536 : 4917 : ~basic_stringstream()
537 [ # # ][ + - ]: 4917 : { }
[ - + ]
538 : :
539 : : // Members:
540 : : /**
541 : : * @brief Accessing the underlying buffer.
542 : : * @return The current basic_stringbuf buffer.
543 : : *
544 : : * This hides both signatures of std::basic_ios::rdbuf().
545 : : */
546 : : __stringbuf_type*
547 : : rdbuf() const
548 : : { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
549 : :
550 : : /**
551 : : * @brief Copying out the string buffer.
552 : : * @return @c rdbuf()->str()
553 : : */
554 : : __string_type
555 : 463 : str() const
556 : 463 : { return _M_stringbuf.str(); }
557 : :
558 : : /**
559 : : * @brief Setting a new buffer.
560 : : * @param s The string to use as a new sequence.
561 : : *
562 : : * Calls @c rdbuf()->str(s).
563 : : */
564 : : void
565 : : str(const __string_type& __s)
566 : : { _M_stringbuf.str(__s); }
567 : : };
568 : :
569 : : _GLIBCXX_END_NAMESPACE
570 : :
571 : : #ifndef _GLIBCXX_EXPORT_TEMPLATE
572 : : # include <bits/sstream.tcc>
573 : : #endif
574 : :
575 : : #endif /* _GLIBCXX_SSTREAM */
|