libstdc++: strstream Source File

libstdc++

strstream
Go to the documentation of this file.
1// Backward-compat support -*- C++ -*-
2
3// Copyright (C) 2001-2026 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 3, 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// 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.
19
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/>.
24
25/*
26 * Copyright (c) 1998
27 * Silicon Graphics Computer Systems, Inc.
28 *
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
36 */
37
38// WARNING: The classes defined in this header are DEPRECATED. This
39// header is defined in section D.7.1 of the C++ standard, and it
40// MAY BE REMOVED in a future standard revision. One should use the
41// header <sstream> instead.
42
43/** @file strstream
44 * This is a Standard C++ Library header.
45 */
46
47#ifndef _BACKWARD_STRSTREAM
48#define _BACKWARD_STRSTREAM
49
50#include <backward/backward_warning.h>
51#include <iosfwd>
52#include <ios>
53#include <istream>
54#include <ostream>
55
56namespace std _GLIBCXX_VISIBILITY(default)
57{
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
59
60#if __glibcxx_spanstream
61# define _GLIBCXX_STRSTREAM_DEPR(A, B) _GLIBCXX_DEPRECATED_SUGGEST(A "' or '" B)
62#else
63# define _GLIBCXX_STRSTREAM_DEPR(A, B) _GLIBCXX_DEPRECATED_SUGGEST(A)
64#endif
65
66 // Class strstreambuf, a streambuf class that manages an array of char.
67 // Note that this class is not a template.
68 class strstreambuf : public basic_streambuf<char, char_traits<char> >
69 {
70 public:
71 // Types.
72 typedef char_traits<char> _Traits;
73 typedef basic_streambuf<char, _Traits> _Base;
74
75 public:
76 // Constructor, destructor
77#if __cplusplus >= 201103L
78 strstreambuf() : strstreambuf(0) { }
79 explicit strstreambuf(streamsize __initial_capacity);
80#else
81 explicit strstreambuf(streamsize __initial_capacity = 0);
82#endif
83 strstreambuf(void* (*__alloc)(size_t), void (*__free)(void*));
84
85 strstreambuf(char* __get, streamsize __n, char* __put = 0) throw ();
86 strstreambuf(signed char* __get, streamsize __n, signed char* __put = 0) throw ();
87 strstreambuf(unsigned char* __get, streamsize __n, unsigned char* __put=0) throw ();
88
89 strstreambuf(const char* __get, streamsize __n) throw ();
90 strstreambuf(const signed char* __get, streamsize __n) throw ();
91 strstreambuf(const unsigned char* __get, streamsize __n) throw ();
92
93 virtual ~strstreambuf();
94
95#if __cplusplus >= 201103L
96 strstreambuf(strstreambuf&& __rhs) noexcept
97 : _Base(__rhs), _M_alloc_fun(__rhs._M_alloc_fun),
98 _M_free_fun(__rhs._M_free_fun), _M_dynamic(__rhs._M_dynamic),
99 _M_frozen(__rhs._M_frozen), _M_constant(__rhs._M_constant)
100 {
101 __rhs.setg(nullptr, nullptr, nullptr);
102 __rhs.setp(nullptr, nullptr);
103 }
104
105 strstreambuf&
106 operator=(strstreambuf&& __rhs) noexcept
107 {
108 if (_M_dynamic && !_M_frozen)
109 _M_free(eback());
110 _Base::operator=(static_cast<const _Base&>(__rhs));
111 _M_alloc_fun = __rhs._M_alloc_fun;
112 _M_free_fun = __rhs._M_free_fun;
113 _M_dynamic = __rhs._M_dynamic;
114 _M_frozen = __rhs._M_frozen;
115 _M_constant = __rhs._M_constant;
116 __rhs.setg(nullptr, nullptr, nullptr);
117 __rhs.setp(nullptr, nullptr);
118 return *this;
119 }
120#endif
121
122 public:
123 void freeze(bool = true) throw ();
124 char* str() throw ();
125 _GLIBCXX_PURE int pcount() const throw ();
126
127 protected:
128 virtual int_type overflow(int_type __c = _Traits::eof());
129 virtual int_type pbackfail(int_type __c = _Traits::eof());
130 virtual int_type underflow();
131 virtual _Base* setbuf(char* __buf, streamsize __n);
132 virtual pos_type seekoff(off_type __off, ios_base::seekdir __dir,
133 ios_base::openmode __mode
134 = ios_base::in | ios_base::out);
135 virtual pos_type seekpos(pos_type __pos, ios_base::openmode __mode
136 = ios_base::in | ios_base::out);
137
138 private:
139#if __cplusplus < 201103L
140 strstreambuf&
141 operator=(const strstreambuf&);
142
143 strstreambuf(const strstreambuf&);
144#endif
145
146 // Dynamic allocation, possibly using _M_alloc_fun and _M_free_fun.
147 char* _M_alloc(size_t);
148 void _M_free(char*);
149
150 // Helper function used in constructors.
151 void _M_setup(char* __get, char* __put, streamsize __n) throw ();
152
153 // Data members.
154 void* (*_M_alloc_fun)(size_t);
155 void (*_M_free_fun)(void*);
156
157 bool _M_dynamic : 1;
158 bool _M_frozen : 1;
159 bool _M_constant : 1;
160 } _GLIBCXX_STRSTREAM_DEPR("std::stringbuf", "std::spanbuf");
161
162#pragma GCC diagnostic push
163#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
164
165 // Class istrstream, an istream that manages a strstreambuf.
166 class istrstream : public basic_istream<char>
167 {
168 public:
169 explicit istrstream(char*);
170 explicit istrstream(const char*);
171 istrstream(char* , streamsize);
172 istrstream(const char*, streamsize);
173 virtual ~istrstream();
174
175#if __cplusplus >= 201103L
176 istrstream(istrstream&& __rhs)
177 : istream(std::move(__rhs)), _M_buf(std::move(__rhs._M_buf))
178 { set_rdbuf(&_M_buf); }
179
180 istrstream& operator=(istrstream&&) = default;
181#endif
182
183 _GLIBCXX_CONST strstreambuf* rdbuf() const throw ();
184 char* str() throw ();
185
186 private:
187 strstreambuf _M_buf;
188 } _GLIBCXX_STRSTREAM_DEPR("std::istringstream", "std::ispanstream");
189
190 // Class ostrstream
191 class ostrstream : public basic_ostream<char>
192 {
193 public:
194 ostrstream();
195 ostrstream(char*, int, ios_base::openmode = ios_base::out);
196 virtual ~ostrstream();
197
198#if __cplusplus >= 201103L
199 ostrstream(ostrstream&& __rhs)
200 : ostream(std::move(__rhs)), _M_buf(std::move(__rhs._M_buf))
201 { set_rdbuf(&_M_buf); }
202
203 ostrstream& operator=(ostrstream&&) = default;
204#endif
205
206 _GLIBCXX_CONST strstreambuf* rdbuf() const throw ();
207 void freeze(bool = true) throw();
208 char* str() throw ();
209 _GLIBCXX_PURE int pcount() const throw ();
210
211 private:
212 strstreambuf _M_buf;
213 } _GLIBCXX_STRSTREAM_DEPR("std::ostringstream", "std::ospanstream");
214
215 // Class strstream
216 class strstream : public basic_iostream<char>
217 {
218 public:
219 typedef char char_type;
220 typedef char_traits<char>::int_type int_type;
221 typedef char_traits<char>::pos_type pos_type;
222 typedef char_traits<char>::off_type off_type;
223
224 strstream();
225 strstream(char*, int, ios_base::openmode = ios_base::in | ios_base::out);
226 virtual ~strstream();
227
228#if __cplusplus >= 201103L
229 strstream(strstream&& __rhs)
230 : iostream(std::move(__rhs)), _M_buf(std::move(__rhs._M_buf))
231 { set_rdbuf(&_M_buf); }
232
233 strstream& operator=(strstream&&) = default;
234#endif
235
236 _GLIBCXX_CONST strstreambuf* rdbuf() const throw ();
237 void freeze(bool = true) throw ();
238 _GLIBCXX_PURE int pcount() const throw ();
239 char* str() throw ();
240
241 private:
242 strstreambuf _M_buf;
243 } _GLIBCXX_STRSTREAM_DEPR("std::stringstream", "std::spanstream");
244
245#undef _GLIBCXX_STRSTREAM_DEPR
246#pragma GCC diagnostic pop
247
248_GLIBCXX_END_NAMESPACE_VERSION
249} // namespace
250
251#endif
ios
iosfwd
istream
ostream
backward_warning.h
std::ostream
basic_ostream< char > ostream
Base class for char output streams.
Definition iosfwd.h:84
std::iostream
basic_iostream< char > iostream
Base class for char mixed input and output streams.
Definition iosfwd.h:87
std::istream
basic_istream< char > istream
Base class for char input streams.
Definition iosfwd.h:81
std
ISO C++ entities toplevel namespace is std.
std::streamsize
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
std::basic_istream
Template class basic_istream.
Definition istream:73
std::basic_streambuf
The actual work of input and output (interface).
Definition streambuf:127
std::basic_streambuf< char, char_traits< char > >::int_type
traits_type::int_type int_type
Definition streambuf:137
std::basic_streambuf< char, char_traits< char > >::eback
char_type * eback() const
Definition streambuf:491
std::basic_streambuf< char, char_traits< char > >::pos_type
traits_type::pos_type pos_type
Definition streambuf:138
std::basic_streambuf< char, char_traits< char > >::off_type
traits_type::off_type off_type
Definition streambuf:139
std::basic_streambuf< char, char_traits< char > >::basic_streambuf
basic_streambuf()
Definition streambuf:472
std::basic_ios::pos_type
_Traits::pos_type pos_type
Definition basic_ios.h:85
std::basic_ios::off_type
_Traits::off_type off_type
Definition basic_ios.h:86
std::basic_istream< char >::rdbuf
basic_streambuf< char, _Traits > * rdbuf() const
Definition basic_ios.h:338
std::basic_ios::char_type
_CharT char_type
Definition basic_ios.h:83
std::basic_ios::int_type
_Traits::int_type int_type
Definition basic_ios.h:84
std::ios_base::in
static const openmode in
Open for input. Default for ifstream and fstream.
Definition ios_base.h:498
std::ios_base::out
static const openmode out
Open for output. Default for ofstream and fstream.
Definition ios_base.h:501
std::ios_base::openmode
_Ios_Openmode openmode
This is a bitmask type.
Definition ios_base.h:484