1 // String based streams -*- C++ -*-
3 // Copyright (C) 1997-2015 Free Software Foundation, Inc.
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 3, or (at your option)
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.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/sstream
26 * This is a Standard C++ Library header.
30 // ISO C++ 14882: 27.7 String-based streams
33 #ifndef _GLIBCXX_SSTREAM
34 #define _GLIBCXX_SSTREAM 1
36 #pragma GCC system_header
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 _GLIBCXX_BEGIN_NAMESPACE_CXX11
46 // [27.7.1] template class basic_stringbuf
48 * @brief The actual work of input and output (for std::string).
51 * @tparam _CharT Type of character stream.
52 * @tparam _Traits Traits for character type, defaults to
53 * char_traits<_CharT>.
54 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
56 * This class associates either or both of its input and output sequences
57 * with a sequence of characters, which can be initialized from, or made
58 * available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
60 * For this class, open modes (of type @c ios_base::openmode) have
61 * @c in set if the input sequence can be read, and @c out set if the
62 * output sequence can be written.
64 template<typename _CharT, typename _Traits, typename _Alloc>
65 class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
67 struct __xfer_bufptrs;
70 typedef _CharT char_type;
71 typedef _Traits traits_type;
72 // _GLIBCXX_RESOLVE_LIB_DEFECTS
73 // 251. basic_stringbuf missing allocator_type
74 typedef _Alloc allocator_type;
75 typedef typename traits_type::int_type int_type;
76 typedef typename traits_type::pos_type pos_type;
77 typedef typename traits_type::off_type off_type;
79 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
80 typedef basic_string<char_type, _Traits, _Alloc> __string_type;
81 typedef typename __string_type::size_type __size_type;
84 /// Place to stash in || out || in | out settings for current stringbuf.
85 ios_base::openmode _M_mode;
88 __string_type _M_string;
93 * @brief Starts with an empty string buffer.
94 * @param __mode Whether the buffer can read, or write, or both.
96 * The default constructor initializes the parent class using its
100 basic_stringbuf(ios_base::openmode __mode = ios_base::in | ios_base::out)
101 : __streambuf_type(), _M_mode(__mode), _M_string()
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.
109 * This constructor initializes the parent class using its
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); }
118 #if __cplusplus >= 201103L
119 basic_stringbuf(const basic_stringbuf&) = delete;
121 basic_stringbuf(basic_stringbuf&& __rhs)
122 : basic_stringbuf(std::move(__rhs), __xfer_bufptrs(__rhs, this))
123 { __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0); }
125 // 27.8.2.2 Assign and swap:
128 operator=(const basic_stringbuf&) = delete;
131 operator=(basic_stringbuf&& __rhs)
133 __xfer_bufptrs __st{__rhs, this};
134 const __streambuf_type& __base = __rhs;
135 __streambuf_type::operator=(__base);
136 this->pubimbue(__rhs.getloc());
137 _M_mode = __rhs._M_mode;
138 _M_string = std::move(__rhs._M_string);
139 __rhs._M_sync(const_cast<char_type*>(__rhs._M_string.data()), 0, 0);
144 swap(basic_stringbuf& __rhs)
146 __xfer_bufptrs __l_st{*this, std::__addressof(__rhs)};
147 __xfer_bufptrs __r_st{__rhs, this};
148 __streambuf_type& __base = __rhs;
149 __streambuf_type::swap(__base);
150 __rhs.pubimbue(this->pubimbue(__rhs.getloc()));
151 std::swap(_M_mode, __rhs._M_mode);
152 std::swap(_M_string, __rhs._M_string);
158 * @brief Copying out the string buffer.
159 * @return A copy of one of the underlying sequences.
161 * <em>If the buffer is only created in input mode, the underlying
162 * character sequence is equal to the input sequence; otherwise, it
163 * is equal to the output sequence.</em> [27.7.1.2]/1
171 // The current egptr() may not be the actual string end.
172 if (this->pptr() > this->egptr())
173 __ret = __string_type(this->pbase(), this->pptr());
175 __ret = __string_type(this->pbase(), this->egptr());
183 * @brief Setting a new buffer.
184 * @param __s The string to use as a new sequence.
186 * Deallocates any previous stored sequence, then copies @a s to
190 str(const __string_type& __s)
192 // Cannot use _M_string = __s, since v3 strings are COW
193 // (not always true now but assign() always works).
194 _M_string.assign(__s.data(), __s.size());
195 _M_stringbuf_init(_M_mode);
199 // Common initialization code goes here.
201 _M_stringbuf_init(ios_base::openmode __mode)
204 __size_type __len = 0;
205 if (_M_mode & (ios_base::ate | ios_base::app))
206 __len = _M_string.size();
207 _M_sync(const_cast<char_type*>(_M_string.data()), 0, __len);
213 streamsize __ret = -1;
214 if (_M_mode & ios_base::in)
217 __ret = this->egptr() - this->gptr();
226 pbackfail(int_type __c = traits_type::eof());
229 overflow(int_type __c = traits_type::eof());
232 * @brief Manipulates the buffer.
233 * @param __s Pointer to a buffer area.
234 * @param __n Size of @a __s.
237 * If no buffer has already been created, and both @a __s and @a __n are
238 * non-zero, then @c __s is used as a buffer; see
239 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
242 virtual __streambuf_type*
243 setbuf(char_type* __s, streamsize __n)
247 // This is implementation-defined behavior, and assumes
248 // that an external char_type array of length __n exists
249 // and has been pre-allocated. If this is not the case,
250 // things will quickly blow up.
252 // Step 1: Destroy the current internal array.
255 // Step 2: Use the external array.
256 _M_sync(__s, __n, 0);
262 seekoff(off_type __off, ios_base::seekdir __way,
263 ios_base::openmode __mode = ios_base::in | ios_base::out);
266 seekpos(pos_type __sp,
267 ios_base::openmode __mode = ios_base::in | ios_base::out);
269 // Internal function for correctly updating the internal buffer
270 // for a particular _M_string, due to initialization or re-sizing
271 // of an existing _M_string.
273 _M_sync(char_type* __base, __size_type __i, __size_type __o);
275 // Internal function for correctly updating egptr() to the actual
280 const bool __testin = _M_mode & ios_base::in;
281 if (this->pptr() && this->pptr() > this->egptr())
284 this->setg(this->eback(), this->gptr(), this->pptr());
286 this->setg(this->pptr(), this->pptr(), this->pptr());
290 // Works around the issue with pbump, part of the protected
291 // interface of basic_streambuf, taking just an int.
293 _M_pbump(char_type* __pbeg, char_type* __pend, off_type __off);
296 #if __cplusplus >= 201103L
297 #if _GLIBCXX_USE_CXX11_ABI
298 // This type captures the state of the gptr / pptr pointers as offsets
299 // so they can be restored in another object after moving the string.
300 struct __xfer_bufptrs
302 __xfer_bufptrs(const basic_stringbuf& __from, basic_stringbuf* __to)
303 : _M_to{__to}, _M_goff{-1, -1, -1}, _M_poff{-1, -1, -1}
305 const _CharT* __str = __from._M_string.data();
308 _M_goff[0] = __from.eback() - __str;
309 _M_goff[1] = __from.gptr() - __str;
310 _M_goff[2] = __from.egptr() - __str;
314 _M_poff[0] = __from.pbase() - __str;
315 _M_poff[1] = __from.pptr() - __from.pbase();
316 _M_poff[2] = __from.epptr() - __str;
322 char_type* __str = const_cast<char_type*>(_M_to->_M_string.data());
323 if (_M_goff[0] != -1)
324 _M_to->setg(__str+_M_goff[0], __str+_M_goff[1], __str+_M_goff[2]);
325 if (_M_poff[0] != -1)
326 _M_to->_M_pbump(__str+_M_poff[0], __str+_M_poff[2], _M_poff[1]);
329 basic_stringbuf* _M_to;
334 // This type does nothing when using Copy-On-Write strings.
335 struct __xfer_bufptrs
337 __xfer_bufptrs(const basic_stringbuf&, basic_stringbuf*) { }
341 // The move constructor initializes an __xfer_bufptrs temporary then
342 // delegates to this constructor to performs moves during its lifetime.
343 basic_stringbuf(basic_stringbuf&& __rhs, __xfer_bufptrs&&)
344 : __streambuf_type(static_cast<const __streambuf_type&>(__rhs)),
345 _M_mode(__rhs._M_mode), _M_string(std::move(__rhs._M_string))
351 // [27.7.2] Template class basic_istringstream
353 * @brief Controlling input for std::string.
356 * @tparam _CharT Type of character stream.
357 * @tparam _Traits Traits for character type, defaults to
358 * char_traits<_CharT>.
359 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
361 * This class supports reading from objects of type std::basic_string,
362 * using the inherited functions from std::basic_istream. To control
363 * the associated sequence, an instance of std::basic_stringbuf is used,
364 * which this page refers to as @c sb.
366 template<typename _CharT, typename _Traits, typename _Alloc>
367 class basic_istringstream : public basic_istream<_CharT, _Traits>
371 typedef _CharT char_type;
372 typedef _Traits traits_type;
373 // _GLIBCXX_RESOLVE_LIB_DEFECTS
374 // 251. basic_stringbuf missing allocator_type
375 typedef _Alloc allocator_type;
376 typedef typename traits_type::int_type int_type;
377 typedef typename traits_type::pos_type pos_type;
378 typedef typename traits_type::off_type off_type;
380 // Non-standard types:
381 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
382 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
383 typedef basic_istream<char_type, traits_type> __istream_type;
386 __stringbuf_type _M_stringbuf;
391 * @brief Default constructor starts with an empty string buffer.
392 * @param __mode Whether the buffer can read, or write, or both.
394 * @c ios_base::in is automatically included in @a __mode.
396 * Initializes @c sb using @c __mode|in, and passes @c &sb to the base
397 * class initializer. Does not allocate any buffer.
399 * That's a lie. We initialize the base class with NULL, because the
400 * string class does its own memory management.
403 basic_istringstream(ios_base::openmode __mode = ios_base::in)
404 : __istream_type(), _M_stringbuf(__mode | ios_base::in)
405 { this->init(&_M_stringbuf); }
408 * @brief Starts with an existing string buffer.
409 * @param __str A string to copy as a starting buffer.
410 * @param __mode Whether the buffer can read, or write, or both.
412 * @c ios_base::in is automatically included in @a mode.
414 * Initializes @c sb using @a str and @c mode|in, and passes @c &sb
415 * to the base class initializer.
417 * That's a lie. We initialize the base class with NULL, because the
418 * string class does its own memory management.
421 basic_istringstream(const __string_type& __str,
422 ios_base::openmode __mode = ios_base::in)
423 : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
424 { this->init(&_M_stringbuf); }
427 * @brief The destructor does nothing.
429 * The buffer is deallocated by the stringbuf object, not the
432 ~basic_istringstream()
435 #if __cplusplus >= 201103L
436 basic_istringstream(const basic_istringstream&) = delete;
438 basic_istringstream(basic_istringstream&& __rhs)
439 : __istream_type(std::move(__rhs)),
440 _M_stringbuf(std::move(__rhs._M_stringbuf))
441 { __istream_type::set_rdbuf(&_M_stringbuf); }
443 // 27.8.3.2 Assign and swap:
446 operator=(const basic_istringstream&) = delete;
449 operator=(basic_istringstream&& __rhs)
451 __istream_type::operator=(std::move(__rhs));
452 _M_stringbuf = std::move(__rhs._M_stringbuf);
457 swap(basic_istringstream& __rhs)
459 __istream_type::swap(__rhs);
460 _M_stringbuf.swap(__rhs._M_stringbuf);
466 * @brief Accessing the underlying buffer.
467 * @return The current basic_stringbuf buffer.
469 * This hides both signatures of std::basic_ios::rdbuf().
473 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
476 * @brief Copying out the string buffer.
477 * @return @c rdbuf()->str()
481 { return _M_stringbuf.str(); }
484 * @brief Setting a new buffer.
485 * @param __s The string to use as a new sequence.
487 * Calls @c rdbuf()->str(s).
490 str(const __string_type& __s)
491 { _M_stringbuf.str(__s); }
495 // [27.7.3] Template class basic_ostringstream
497 * @brief Controlling output for std::string.
500 * @tparam _CharT Type of character stream.
501 * @tparam _Traits Traits for character type, defaults to
502 * char_traits<_CharT>.
503 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
505 * This class supports writing to objects of type std::basic_string,
506 * using the inherited functions from std::basic_ostream. To control
507 * the associated sequence, an instance of std::basic_stringbuf is used,
508 * which this page refers to as @c sb.
510 template <typename _CharT, typename _Traits, typename _Alloc>
511 class basic_ostringstream : public basic_ostream<_CharT, _Traits>
515 typedef _CharT char_type;
516 typedef _Traits traits_type;
517 // _GLIBCXX_RESOLVE_LIB_DEFECTS
518 // 251. basic_stringbuf missing allocator_type
519 typedef _Alloc allocator_type;
520 typedef typename traits_type::int_type int_type;
521 typedef typename traits_type::pos_type pos_type;
522 typedef typename traits_type::off_type off_type;
524 // Non-standard types:
525 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
526 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
527 typedef basic_ostream<char_type, traits_type> __ostream_type;
530 __stringbuf_type _M_stringbuf;
533 // Constructors/destructor:
535 * @brief Default constructor starts with an empty string buffer.
536 * @param __mode Whether the buffer can read, or write, or both.
538 * @c ios_base::out is automatically included in @a mode.
540 * Initializes @c sb using @c mode|out, and passes @c &sb to the base
541 * class initializer. Does not allocate any buffer.
543 * That's a lie. We initialize the base class with NULL, because the
544 * string class does its own memory management.
547 basic_ostringstream(ios_base::openmode __mode = ios_base::out)
548 : __ostream_type(), _M_stringbuf(__mode | ios_base::out)
549 { this->init(&_M_stringbuf); }
552 * @brief Starts with an existing string buffer.
553 * @param __str A string to copy as a starting buffer.
554 * @param __mode Whether the buffer can read, or write, or both.
556 * @c ios_base::out is automatically included in @a mode.
558 * Initializes @c sb using @a str and @c mode|out, and passes @c &sb
559 * to the base class initializer.
561 * That's a lie. We initialize the base class with NULL, because the
562 * string class does its own memory management.
565 basic_ostringstream(const __string_type& __str,
566 ios_base::openmode __mode = ios_base::out)
567 : __ostream_type(), _M_stringbuf(__str, __mode | ios_base::out)
568 { this->init(&_M_stringbuf); }
571 * @brief The destructor does nothing.
573 * The buffer is deallocated by the stringbuf object, not the
576 ~basic_ostringstream()
579 #if __cplusplus >= 201103L
580 basic_ostringstream(const basic_ostringstream&) = delete;
582 basic_ostringstream(basic_ostringstream&& __rhs)
583 : __ostream_type(std::move(__rhs)),
584 _M_stringbuf(std::move(__rhs._M_stringbuf))
585 { __ostream_type::set_rdbuf(&_M_stringbuf); }
587 // 27.8.3.2 Assign and swap:
590 operator=(const basic_ostringstream&) = delete;
593 operator=(basic_ostringstream&& __rhs)
595 __ostream_type::operator=(std::move(__rhs));
596 _M_stringbuf = std::move(__rhs._M_stringbuf);
601 swap(basic_ostringstream& __rhs)
603 __ostream_type::swap(__rhs);
604 _M_stringbuf.swap(__rhs._M_stringbuf);
610 * @brief Accessing the underlying buffer.
611 * @return The current basic_stringbuf buffer.
613 * This hides both signatures of std::basic_ios::rdbuf().
617 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
620 * @brief Copying out the string buffer.
621 * @return @c rdbuf()->str()
625 { return _M_stringbuf.str(); }
628 * @brief Setting a new buffer.
629 * @param __s The string to use as a new sequence.
631 * Calls @c rdbuf()->str(s).
634 str(const __string_type& __s)
635 { _M_stringbuf.str(__s); }
639 // [27.7.4] Template class basic_stringstream
641 * @brief Controlling input and output for std::string.
644 * @tparam _CharT Type of character stream.
645 * @tparam _Traits Traits for character type, defaults to
646 * char_traits<_CharT>.
647 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
649 * This class supports reading from and writing to objects of type
650 * std::basic_string, using the inherited functions from
651 * std::basic_iostream. To control the associated sequence, an instance
652 * of std::basic_stringbuf is used, which this page refers to as @c sb.
654 template <typename _CharT, typename _Traits, typename _Alloc>
655 class basic_stringstream : public basic_iostream<_CharT, _Traits>
659 typedef _CharT char_type;
660 typedef _Traits traits_type;
661 // _GLIBCXX_RESOLVE_LIB_DEFECTS
662 // 251. basic_stringbuf missing allocator_type
663 typedef _Alloc allocator_type;
664 typedef typename traits_type::int_type int_type;
665 typedef typename traits_type::pos_type pos_type;
666 typedef typename traits_type::off_type off_type;
668 // Non-standard Types:
669 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
670 typedef basic_stringbuf<_CharT, _Traits, _Alloc> __stringbuf_type;
671 typedef basic_iostream<char_type, traits_type> __iostream_type;
674 __stringbuf_type _M_stringbuf;
677 // Constructors/destructors
679 * @brief Default constructor starts with an empty string buffer.
680 * @param __m Whether the buffer can read, or write, or both.
682 * Initializes @c sb using the mode from @c __m, and passes @c
683 * &sb to the base class initializer. Does not allocate any
686 * That's a lie. We initialize the base class with NULL, because the
687 * string class does its own memory management.
690 basic_stringstream(ios_base::openmode __m = ios_base::out | ios_base::in)
691 : __iostream_type(), _M_stringbuf(__m)
692 { this->init(&_M_stringbuf); }
695 * @brief Starts with an existing string buffer.
696 * @param __str A string to copy as a starting buffer.
697 * @param __m Whether the buffer can read, or write, or both.
699 * Initializes @c sb using @a __str and @c __m, and passes @c &sb
700 * to the base class initializer.
702 * That's a lie. We initialize the base class with NULL, because the
703 * string class does its own memory management.
706 basic_stringstream(const __string_type& __str,
707 ios_base::openmode __m = ios_base::out | ios_base::in)
708 : __iostream_type(), _M_stringbuf(__str, __m)
709 { this->init(&_M_stringbuf); }
712 * @brief The destructor does nothing.
714 * The buffer is deallocated by the stringbuf object, not the
717 ~basic_stringstream()
720 #if __cplusplus >= 201103L
721 basic_stringstream(const basic_stringstream&) = delete;
723 basic_stringstream(basic_stringstream&& __rhs)
724 : __iostream_type(std::move(__rhs)),
725 _M_stringbuf(std::move(__rhs._M_stringbuf))
726 { __iostream_type::set_rdbuf(&_M_stringbuf); }
728 // 27.8.3.2 Assign and swap:
731 operator=(const basic_stringstream&) = delete;
734 operator=(basic_stringstream&& __rhs)
736 __iostream_type::operator=(std::move(__rhs));
737 _M_stringbuf = std::move(__rhs._M_stringbuf);
742 swap(basic_stringstream& __rhs)
744 __iostream_type::swap(__rhs);
745 _M_stringbuf.swap(__rhs._M_stringbuf);
751 * @brief Accessing the underlying buffer.
752 * @return The current basic_stringbuf buffer.
754 * This hides both signatures of std::basic_ios::rdbuf().
758 { return const_cast<__stringbuf_type*>(&_M_stringbuf); }
761 * @brief Copying out the string buffer.
762 * @return @c rdbuf()->str()
766 { return _M_stringbuf.str(); }
769 * @brief Setting a new buffer.
770 * @param __s The string to use as a new sequence.
772 * Calls @c rdbuf()->str(s).
775 str(const __string_type& __s)
776 { _M_stringbuf.str(__s); }
779 #if __cplusplus >= 201103L
780 /// Swap specialization for stringbufs.
781 template <class _CharT, class _Traits, class _Allocator>
783 swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
784 basic_stringbuf<_CharT, _Traits, _Allocator>& __y)
787 /// Swap specialization for istringstreams.
788 template <class _CharT, class _Traits, class _Allocator>
790 swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x,
791 basic_istringstream<_CharT, _Traits, _Allocator>& __y)
794 /// Swap specialization for ostringstreams.
795 template <class _CharT, class _Traits, class _Allocator>
797 swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x,
798 basic_ostringstream<_CharT, _Traits, _Allocator>& __y)
801 /// Swap specialization for stringstreams.
802 template <class _CharT, class _Traits, class _Allocator>
804 swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x,
805 basic_stringstream<_CharT, _Traits, _Allocator>& __y)
809 _GLIBCXX_END_NAMESPACE_CXX11
810 _GLIBCXX_END_NAMESPACE_VERSION
813 #include <bits/sstream.tcc>
815 #endif /* _GLIBCXX_SSTREAM */