xref: /freebsd/contrib/llvm-project/libcxx/include/strstream (revision 67be1e195acfaec99ce4fffeb17111ce085755f7)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRSTREAM
11#define _LIBCPP_STRSTREAM
12
13/*
14    strstream synopsis
15
16class strstreambuf
17    : public basic_streambuf<char>
18{
19public:
20    explicit strstreambuf(streamsize alsize_arg = 0); // before C++20
21    strstreambuf() : strstreambuf(0) {}               // C++20
22    explicit strstreambuf(streamsize alsize_arg);     // C++20
23
24    strstreambuf(void* (*palloc_arg)(size_t), void (*pfree_arg)(void*));
25    strstreambuf(char* gnext_arg, streamsize n, char* pbeg_arg = nullptr);
26    strstreambuf(const char* gnext_arg, streamsize n);
27
28    strstreambuf(signed char* gnext_arg, streamsize n, signed char* pbeg_arg = nullptr);
29    strstreambuf(const signed char* gnext_arg, streamsize n);
30    strstreambuf(unsigned char* gnext_arg, streamsize n, unsigned char* pbeg_arg = nullptr);
31    strstreambuf(const unsigned char* gnext_arg, streamsize n);
32
33    strstreambuf(strstreambuf&& rhs);
34    strstreambuf& operator=(strstreambuf&& rhs);
35
36    virtual ~strstreambuf();
37
38    void swap(strstreambuf& rhs);
39
40    void freeze(bool freezefl = true);
41    char* str();
42    int pcount() const;
43
44protected:
45    virtual int_type overflow (int_type c = EOF);
46    virtual int_type pbackfail(int_type c = EOF);
47    virtual int_type underflow();
48    virtual pos_type seekoff(off_type off, ios_base::seekdir way,
49                             ios_base::openmode which = ios_base::in | ios_base::out);
50    virtual pos_type seekpos(pos_type sp,
51                             ios_base::openmode which = ios_base::in | ios_base::out);
52    virtual streambuf* setbuf(char* s, streamsize n);
53
54private:
55    typedef T1 strstate;                // exposition only
56    static const strstate allocated;    // exposition only
57    static const strstate constant;     // exposition only
58    static const strstate dynamic;      // exposition only
59    static const strstate frozen;       // exposition only
60    strstate strmode;                   // exposition only
61    streamsize alsize;                  // exposition only
62    void* (*palloc)(size_t);            // exposition only
63    void (*pfree)(void*);               // exposition only
64};
65
66class istrstream
67    : public basic_istream<char>
68{
69public:
70    explicit istrstream(const char* s);
71    explicit istrstream(char* s);
72    istrstream(const char* s, streamsize n);
73    istrstream(char* s, streamsize n);
74
75    virtual ~istrstream();
76
77    strstreambuf* rdbuf() const;
78    char *str();
79
80private:
81    strstreambuf sb; // exposition only
82};
83
84class ostrstream
85    : public basic_ostream<char>
86{
87public:
88    ostrstream();
89    ostrstream(char* s, int n, ios_base::openmode mode = ios_base::out);
90
91    virtual ~ostrstream();
92
93    strstreambuf* rdbuf() const;
94    void freeze(bool freezefl = true);
95    char* str();
96    int pcount() const;
97
98private:
99    strstreambuf sb; // exposition only
100};
101
102class strstream
103    : public basic_iostream<char>
104{
105public:
106    // Types
107    typedef char                        char_type;
108    typedef char_traits<char>::int_type int_type;
109    typedef char_traits<char>::pos_type pos_type;
110    typedef char_traits<char>::off_type off_type;
111
112    // constructors/destructor
113    strstream();
114    strstream(char* s, int n, ios_base::openmode mode = ios_base::in | ios_base::out);
115
116    virtual ~strstream();
117
118    // Members:
119    strstreambuf* rdbuf() const;
120    void freeze(bool freezefl = true);
121    int pcount() const;
122    char* str();
123
124private:
125    strstreambuf sb; // exposition only
126};
127
128}  // std
129
130*/
131
132#include <__assert> // all public C++ headers provide the assertion handler
133#include <__config>
134#include <istream>
135#include <ostream>
136#include <version>
137
138#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
139#  pragma GCC system_header
140#endif
141
142_LIBCPP_PUSH_MACROS
143#include <__undef_macros>
144
145_LIBCPP_BEGIN_NAMESPACE_STD
146
147class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI strstreambuf : public streambuf {
148public:
149#ifndef _LIBCPP_CXX03_LANG
150  _LIBCPP_HIDE_FROM_ABI strstreambuf() : strstreambuf(0) {}
151  explicit strstreambuf(streamsize __alsize);
152#else
153  explicit strstreambuf(streamsize __alsize = 0);
154#endif
155  strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
156  strstreambuf(char* __gnext, streamsize __n, char* __pbeg = nullptr);
157  strstreambuf(const char* __gnext, streamsize __n);
158
159  strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = nullptr);
160  strstreambuf(const signed char* __gnext, streamsize __n);
161  strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = nullptr);
162  strstreambuf(const unsigned char* __gnext, streamsize __n);
163
164#ifndef _LIBCPP_CXX03_LANG
165  _LIBCPP_HIDE_FROM_ABI strstreambuf(strstreambuf&& __rhs);
166  _LIBCPP_HIDE_FROM_ABI strstreambuf& operator=(strstreambuf&& __rhs);
167#endif // _LIBCPP_CXX03_LANG
168
169  ~strstreambuf() override;
170
171  void swap(strstreambuf& __rhs);
172
173  void freeze(bool __freezefl = true);
174  char* str();
175  int pcount() const;
176
177protected:
178  int_type overflow(int_type __c = EOF) override;
179  int_type pbackfail(int_type __c = EOF) override;
180  int_type underflow() override;
181  pos_type
182  seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out) override;
183  pos_type seekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out) override;
184
185private:
186  typedef unsigned __mode_type;
187  static const __mode_type __allocated     = 0x01;
188  static const __mode_type __constant      = 0x02;
189  static const __mode_type __dynamic       = 0x04;
190  static const __mode_type __frozen        = 0x08;
191  static const streamsize __default_alsize = 4096;
192
193  __mode_type __strmode_;
194  streamsize __alsize_;
195  void* (*__palloc_)(size_t);
196  void (*__pfree_)(void*);
197
198  void __init(char* __gnext, streamsize __n, char* __pbeg);
199};
200
201#ifndef _LIBCPP_CXX03_LANG
202
203inline _LIBCPP_HIDE_FROM_ABI strstreambuf::strstreambuf(strstreambuf&& __rhs)
204    : streambuf(__rhs),
205      __strmode_(__rhs.__strmode_),
206      __alsize_(__rhs.__alsize_),
207      __palloc_(__rhs.__palloc_),
208      __pfree_(__rhs.__pfree_) {
209  __rhs.setg(nullptr, nullptr, nullptr);
210  __rhs.setp(nullptr, nullptr);
211}
212
213inline _LIBCPP_HIDE_FROM_ABI strstreambuf& strstreambuf::operator=(strstreambuf&& __rhs) {
214  if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0) {
215    if (__pfree_)
216      __pfree_(eback());
217    else
218      delete[] eback();
219  }
220  streambuf::operator=(__rhs);
221  __strmode_ = __rhs.__strmode_;
222  __alsize_  = __rhs.__alsize_;
223  __palloc_  = __rhs.__palloc_;
224  __pfree_   = __rhs.__pfree_;
225  __rhs.setg(nullptr, nullptr, nullptr);
226  __rhs.setp(nullptr, nullptr);
227  return *this;
228}
229
230#endif // _LIBCPP_CXX03_LANG
231
232class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI istrstream : public istream {
233public:
234  _LIBCPP_HIDE_FROM_ABI explicit istrstream(const char* __s) : istream(&__sb_), __sb_(__s, 0) {}
235  _LIBCPP_HIDE_FROM_ABI explicit istrstream(char* __s) : istream(&__sb_), __sb_(__s, 0) {}
236  _LIBCPP_HIDE_FROM_ABI istrstream(const char* __s, streamsize __n) : istream(&__sb_), __sb_(__s, __n) {}
237  _LIBCPP_HIDE_FROM_ABI istrstream(char* __s, streamsize __n) : istream(&__sb_), __sb_(__s, __n) {}
238
239#ifndef _LIBCPP_CXX03_LANG
240  _LIBCPP_HIDE_FROM_ABI istrstream(istrstream&& __rhs) // extension
241      : istream(std::move(static_cast<istream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
242    istream::set_rdbuf(&__sb_);
243  }
244
245  _LIBCPP_HIDE_FROM_ABI istrstream& operator=(istrstream&& __rhs) {
246    __sb_ = std::move(__rhs.__sb_);
247    istream::operator=(std::move(__rhs));
248    return *this;
249  }
250#endif // _LIBCPP_CXX03_LANG
251
252  ~istrstream() override;
253
254  _LIBCPP_HIDE_FROM_ABI void swap(istrstream& __rhs) {
255    istream::swap(__rhs);
256    __sb_.swap(__rhs.__sb_);
257  }
258
259  _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
260  _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
261
262private:
263  strstreambuf __sb_;
264};
265
266class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI ostrstream : public ostream {
267public:
268  _LIBCPP_HIDE_FROM_ABI ostrstream() : ostream(&__sb_) {}
269  _LIBCPP_HIDE_FROM_ABI ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
270      : ostream(&__sb_), __sb_(__s, __n, __s + (__mode & ios::app ? std::strlen(__s) : 0)) {}
271
272#ifndef _LIBCPP_CXX03_LANG
273  _LIBCPP_HIDE_FROM_ABI ostrstream(ostrstream&& __rhs) // extension
274      : ostream(std::move(static_cast<ostream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
275    ostream::set_rdbuf(&__sb_);
276  }
277
278  _LIBCPP_HIDE_FROM_ABI ostrstream& operator=(ostrstream&& __rhs) {
279    __sb_ = std::move(__rhs.__sb_);
280    ostream::operator=(std::move(__rhs));
281    return *this;
282  }
283#endif // _LIBCPP_CXX03_LANG
284
285  ~ostrstream() override;
286
287  _LIBCPP_HIDE_FROM_ABI void swap(ostrstream& __rhs) {
288    ostream::swap(__rhs);
289    __sb_.swap(__rhs.__sb_);
290  }
291
292  _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
293  _LIBCPP_HIDE_FROM_ABI void freeze(bool __freezefl = true) { __sb_.freeze(__freezefl); }
294  _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
295  _LIBCPP_HIDE_FROM_ABI int pcount() const { return __sb_.pcount(); }
296
297private:
298  strstreambuf __sb_; // exposition only
299};
300
301class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI strstream : public iostream {
302public:
303  // Types
304  typedef char char_type;
305  typedef char_traits<char>::int_type int_type;
306  typedef char_traits<char>::pos_type pos_type;
307  typedef char_traits<char>::off_type off_type;
308
309  // constructors/destructor
310  _LIBCPP_HIDE_FROM_ABI strstream() : iostream(&__sb_) {}
311  _LIBCPP_HIDE_FROM_ABI strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
312      : iostream(&__sb_), __sb_(__s, __n, __s + (__mode & ios::app ? std::strlen(__s) : 0)) {}
313
314#ifndef _LIBCPP_CXX03_LANG
315  _LIBCPP_HIDE_FROM_ABI strstream(strstream&& __rhs) // extension
316      : iostream(std::move(static_cast<iostream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
317    iostream::set_rdbuf(&__sb_);
318  }
319
320  _LIBCPP_HIDE_FROM_ABI strstream& operator=(strstream&& __rhs) {
321    __sb_ = std::move(__rhs.__sb_);
322    iostream::operator=(std::move(__rhs));
323    return *this;
324  }
325#endif // _LIBCPP_CXX03_LANG
326
327  ~strstream() override;
328
329  _LIBCPP_HIDE_FROM_ABI void swap(strstream& __rhs) {
330    iostream::swap(__rhs);
331    __sb_.swap(__rhs.__sb_);
332  }
333
334  // Members:
335  _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
336  _LIBCPP_HIDE_FROM_ABI void freeze(bool __freezefl = true) { __sb_.freeze(__freezefl); }
337  _LIBCPP_HIDE_FROM_ABI int pcount() const { return __sb_.pcount(); }
338  _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
339
340private:
341  strstreambuf __sb_; // exposition only
342};
343
344_LIBCPP_END_NAMESPACE_STD
345
346_LIBCPP_POP_MACROS
347
348#endif // _LIBCPP_STRSTREAM
349