2 //===--------------------------- strstream --------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_STRSTREAM
11 #define _LIBCPP_STRSTREAM
17 : public basic_streambuf<char>
20 explicit strstreambuf(streamsize alsize_arg = 0);
21 strstreambuf(void* (*palloc_arg)(size_t), void (*pfree_arg)(void*));
22 strstreambuf(char* gnext_arg, streamsize n, char* pbeg_arg = 0);
23 strstreambuf(const char* gnext_arg, streamsize n);
25 strstreambuf(signed char* gnext_arg, streamsize n, signed char* pbeg_arg = 0);
26 strstreambuf(const signed char* gnext_arg, streamsize n);
27 strstreambuf(unsigned char* gnext_arg, streamsize n, unsigned char* pbeg_arg = 0);
28 strstreambuf(const unsigned char* gnext_arg, streamsize n);
30 strstreambuf(strstreambuf&& rhs);
31 strstreambuf& operator=(strstreambuf&& rhs);
33 virtual ~strstreambuf();
35 void swap(strstreambuf& rhs);
37 void freeze(bool freezefl = true);
42 virtual int_type overflow (int_type c = EOF);
43 virtual int_type pbackfail(int_type c = EOF);
44 virtual int_type underflow();
45 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
46 ios_base::openmode which = ios_base::in | ios_base::out);
47 virtual pos_type seekpos(pos_type sp,
48 ios_base::openmode which = ios_base::in | ios_base::out);
49 virtual streambuf* setbuf(char* s, streamsize n);
52 typedef T1 strstate; // exposition only
53 static const strstate allocated; // exposition only
54 static const strstate constant; // exposition only
55 static const strstate dynamic; // exposition only
56 static const strstate frozen; // exposition only
57 strstate strmode; // exposition only
58 streamsize alsize; // exposition only
59 void* (*palloc)(size_t); // exposition only
60 void (*pfree)(void*); // exposition only
64 : public basic_istream<char>
67 explicit istrstream(const char* s);
68 explicit istrstream(char* s);
69 istrstream(const char* s, streamsize n);
70 istrstream(char* s, streamsize n);
72 virtual ~istrstream();
74 strstreambuf* rdbuf() const;
78 strstreambuf sb; // exposition only
82 : public basic_ostream<char>
86 ostrstream(char* s, int n, ios_base::openmode mode = ios_base::out);
88 virtual ~ostrstream();
90 strstreambuf* rdbuf() const;
91 void freeze(bool freezefl = true);
96 strstreambuf sb; // exposition only
100 : public basic_iostream<char>
104 typedef char char_type;
105 typedef char_traits<char>::int_type int_type;
106 typedef char_traits<char>::pos_type pos_type;
107 typedef char_traits<char>::off_type off_type;
109 // constructors/destructor
111 strstream(char* s, int n, ios_base::openmode mode = ios_base::in | ios_base::out);
113 virtual ~strstream();
116 strstreambuf* rdbuf() const;
117 void freeze(bool freezefl = true);
122 strstreambuf sb; // exposition only
133 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
134 #pragma GCC system_header
137 _LIBCPP_BEGIN_NAMESPACE_STD
139 class _LIBCPP_TYPE_VIS strstreambuf
143 explicit strstreambuf(streamsize __alsize = 0);
144 strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
145 strstreambuf(char* __gnext, streamsize __n, char* __pbeg = 0);
146 strstreambuf(const char* __gnext, streamsize __n);
148 strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = 0);
149 strstreambuf(const signed char* __gnext, streamsize __n);
150 strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = 0);
151 strstreambuf(const unsigned char* __gnext, streamsize __n);
153 #ifndef _LIBCPP_CXX03_LANG
154 _LIBCPP_INLINE_VISIBILITY
155 strstreambuf(strstreambuf&& __rhs);
156 _LIBCPP_INLINE_VISIBILITY
157 strstreambuf& operator=(strstreambuf&& __rhs);
158 #endif // _LIBCPP_CXX03_LANG
160 virtual ~strstreambuf();
162 void swap(strstreambuf& __rhs);
164 void freeze(bool __freezefl = true);
169 virtual int_type overflow (int_type __c = EOF);
170 virtual int_type pbackfail(int_type __c = EOF);
171 virtual int_type underflow();
172 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
173 ios_base::openmode __which = ios_base::in | ios_base::out);
174 virtual pos_type seekpos(pos_type __sp,
175 ios_base::openmode __which = ios_base::in | ios_base::out);
178 typedef unsigned __mode_type;
179 static const __mode_type __allocated = 0x01;
180 static const __mode_type __constant = 0x02;
181 static const __mode_type __dynamic = 0x04;
182 static const __mode_type __frozen = 0x08;
183 static const streamsize __default_alsize = 4096;
185 __mode_type __strmode_;
186 streamsize __alsize_;
187 void* (*__palloc_)(size_t);
188 void (*__pfree_)(void*);
190 void __init(char* __gnext, streamsize __n, char* __pbeg);
193 #ifndef _LIBCPP_CXX03_LANG
195 inline _LIBCPP_INLINE_VISIBILITY
196 strstreambuf::strstreambuf(strstreambuf&& __rhs)
198 __strmode_(__rhs.__strmode_),
199 __alsize_(__rhs.__alsize_),
200 __palloc_(__rhs.__palloc_),
201 __pfree_(__rhs.__pfree_)
203 __rhs.setg(nullptr, nullptr, nullptr);
204 __rhs.setp(nullptr, nullptr);
207 inline _LIBCPP_INLINE_VISIBILITY
209 strstreambuf::operator=(strstreambuf&& __rhs)
211 if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0)
218 streambuf::operator=(__rhs);
219 __strmode_ = __rhs.__strmode_;
220 __alsize_ = __rhs.__alsize_;
221 __palloc_ = __rhs.__palloc_;
222 __pfree_ = __rhs.__pfree_;
223 __rhs.setg(nullptr, nullptr, nullptr);
224 __rhs.setp(nullptr, nullptr);
228 #endif // _LIBCPP_CXX03_LANG
230 class _LIBCPP_TYPE_VIS istrstream
234 _LIBCPP_INLINE_VISIBILITY
235 explicit istrstream(const char* __s)
236 : istream(&__sb_), __sb_(__s, 0) {}
237 _LIBCPP_INLINE_VISIBILITY
238 explicit istrstream(char* __s)
239 : istream(&__sb_), __sb_(__s, 0) {}
240 _LIBCPP_INLINE_VISIBILITY
241 istrstream(const char* __s, streamsize __n)
242 : istream(&__sb_), __sb_(__s, __n) {}
243 _LIBCPP_INLINE_VISIBILITY
244 istrstream(char* __s, streamsize __n)
245 : istream(&__sb_), __sb_(__s, __n) {}
247 #ifndef _LIBCPP_CXX03_LANG
248 _LIBCPP_INLINE_VISIBILITY
249 istrstream(istrstream&& __rhs)
250 : istream(_VSTD::move(__rhs)),
251 __sb_(_VSTD::move(__rhs.__sb_))
253 istream::set_rdbuf(&__sb_);
256 _LIBCPP_INLINE_VISIBILITY
257 istrstream& operator=(istrstream&& __rhs)
259 istream::operator=(_VSTD::move(__rhs));
260 __sb_ = _VSTD::move(__rhs.__sb_);
263 #endif // _LIBCPP_CXX03_LANG
265 virtual ~istrstream();
267 _LIBCPP_INLINE_VISIBILITY
268 void swap(istrstream& __rhs)
270 istream::swap(__rhs);
271 __sb_.swap(__rhs.__sb_);
274 _LIBCPP_INLINE_VISIBILITY
275 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
276 _LIBCPP_INLINE_VISIBILITY
277 char *str() {return __sb_.str();}
283 class _LIBCPP_TYPE_VIS ostrstream
287 _LIBCPP_INLINE_VISIBILITY
290 _LIBCPP_INLINE_VISIBILITY
291 ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
293 __sb_(__s, __n, __s + (__mode & ios::app ? strlen(__s) : 0))
296 #ifndef _LIBCPP_CXX03_LANG
297 _LIBCPP_INLINE_VISIBILITY
298 ostrstream(ostrstream&& __rhs)
299 : ostream(_VSTD::move(__rhs)),
300 __sb_(_VSTD::move(__rhs.__sb_))
302 ostream::set_rdbuf(&__sb_);
305 _LIBCPP_INLINE_VISIBILITY
306 ostrstream& operator=(ostrstream&& __rhs)
308 ostream::operator=(_VSTD::move(__rhs));
309 __sb_ = _VSTD::move(__rhs.__sb_);
312 #endif // _LIBCPP_CXX03_LANG
314 virtual ~ostrstream();
316 _LIBCPP_INLINE_VISIBILITY
317 void swap(ostrstream& __rhs)
319 ostream::swap(__rhs);
320 __sb_.swap(__rhs.__sb_);
323 _LIBCPP_INLINE_VISIBILITY
324 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
325 _LIBCPP_INLINE_VISIBILITY
326 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
327 _LIBCPP_INLINE_VISIBILITY
328 char* str() {return __sb_.str();}
329 _LIBCPP_INLINE_VISIBILITY
330 int pcount() const {return __sb_.pcount();}
333 strstreambuf __sb_; // exposition only
336 class _LIBCPP_TYPE_VIS strstream
341 typedef char char_type;
342 typedef char_traits<char>::int_type int_type;
343 typedef char_traits<char>::pos_type pos_type;
344 typedef char_traits<char>::off_type off_type;
346 // constructors/destructor
347 _LIBCPP_INLINE_VISIBILITY
349 : iostream(&__sb_) {}
350 _LIBCPP_INLINE_VISIBILITY
351 strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
353 __sb_(__s, __n, __s + (__mode & ios::app ? strlen(__s) : 0))
356 #ifndef _LIBCPP_CXX03_LANG
357 _LIBCPP_INLINE_VISIBILITY
358 strstream(strstream&& __rhs)
359 : iostream(_VSTD::move(__rhs)),
360 __sb_(_VSTD::move(__rhs.__sb_))
362 iostream::set_rdbuf(&__sb_);
365 _LIBCPP_INLINE_VISIBILITY
366 strstream& operator=(strstream&& __rhs)
368 iostream::operator=(_VSTD::move(__rhs));
369 __sb_ = _VSTD::move(__rhs.__sb_);
372 #endif // _LIBCPP_CXX03_LANG
374 virtual ~strstream();
376 _LIBCPP_INLINE_VISIBILITY
377 void swap(strstream& __rhs)
379 iostream::swap(__rhs);
380 __sb_.swap(__rhs.__sb_);
384 _LIBCPP_INLINE_VISIBILITY
385 strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
386 _LIBCPP_INLINE_VISIBILITY
387 void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
388 _LIBCPP_INLINE_VISIBILITY
389 int pcount() const {return __sb_.pcount();}
390 _LIBCPP_INLINE_VISIBILITY
391 char* str() {return __sb_.str();}
394 strstreambuf __sb_; // exposition only
397 _LIBCPP_END_NAMESPACE_STD
399 #endif // _LIBCPP_STRSTREAM