master
  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                                    // Removed in C++26
 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                                      // Removed in C++26
 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                                      // Removed in C++26
 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                                       // Removed in C++26
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#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
133#  include <__cxx03/strstream>
134#else
135#  include <__config>
136
137#  if _LIBCPP_HAS_LOCALIZATION
138
139#    include <__ostream/basic_ostream.h>
140#    include <istream>
141#    include <streambuf>
142#    include <version>
143
144#    if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
145#      pragma GCC system_header
146#    endif
147
148#    if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_STRSTREAM) || defined(_LIBCPP_BUILDING_LIBRARY)
149
150_LIBCPP_PUSH_MACROS
151#      include <__undef_macros>
152
153_LIBCPP_BEGIN_NAMESPACE_STD
154
155class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI strstreambuf : public streambuf {
156public:
157#      ifndef _LIBCPP_CXX03_LANG
158  _LIBCPP_HIDE_FROM_ABI strstreambuf() : strstreambuf(0) {}
159  explicit strstreambuf(streamsize __alsize);
160#      else
161  explicit strstreambuf(streamsize __alsize = 0);
162#      endif
163  strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
164  strstreambuf(char* __gnext, streamsize __n, char* __pbeg = nullptr);
165  strstreambuf(const char* __gnext, streamsize __n);
166
167  strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = nullptr);
168  strstreambuf(const signed char* __gnext, streamsize __n);
169  strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = nullptr);
170  strstreambuf(const unsigned char* __gnext, streamsize __n);
171
172#      ifndef _LIBCPP_CXX03_LANG
173  _LIBCPP_HIDE_FROM_ABI strstreambuf(strstreambuf&& __rhs);
174  _LIBCPP_HIDE_FROM_ABI strstreambuf& operator=(strstreambuf&& __rhs);
175#      endif // _LIBCPP_CXX03_LANG
176
177  ~strstreambuf() override;
178
179  void swap(strstreambuf& __rhs);
180
181  void freeze(bool __freezefl = true);
182  char* str();
183  int pcount() const;
184
185protected:
186  int_type overflow(int_type __c = EOF) override;
187  int_type pbackfail(int_type __c = EOF) override;
188  int_type underflow() override;
189  pos_type
190  seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out) override;
191  pos_type seekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out) override;
192
193private:
194  typedef unsigned __mode_type;
195  static const __mode_type __allocated     = 0x01;
196  static const __mode_type __constant      = 0x02;
197  static const __mode_type __dynamic       = 0x04;
198  static const __mode_type __frozen        = 0x08;
199  static const streamsize __default_alsize = 4096;
200
201  __mode_type __strmode_;
202  streamsize __alsize_;
203  void* (*__palloc_)(size_t);
204  void (*__pfree_)(void*);
205
206  void __init(char* __gnext, streamsize __n, char* __pbeg);
207};
208
209#      ifndef _LIBCPP_CXX03_LANG
210
211inline _LIBCPP_HIDE_FROM_ABI strstreambuf::strstreambuf(strstreambuf&& __rhs)
212    : streambuf(__rhs),
213      __strmode_(__rhs.__strmode_),
214      __alsize_(__rhs.__alsize_),
215      __palloc_(__rhs.__palloc_),
216      __pfree_(__rhs.__pfree_) {
217  __rhs.setg(nullptr, nullptr, nullptr);
218  __rhs.setp(nullptr, nullptr);
219}
220
221inline _LIBCPP_HIDE_FROM_ABI strstreambuf& strstreambuf::operator=(strstreambuf&& __rhs) {
222  if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0) {
223    if (__pfree_)
224      __pfree_(eback());
225    else
226      delete[] eback();
227  }
228  streambuf::operator=(__rhs);
229  __strmode_ = __rhs.__strmode_;
230  __alsize_  = __rhs.__alsize_;
231  __palloc_  = __rhs.__palloc_;
232  __pfree_   = __rhs.__pfree_;
233  __rhs.setg(nullptr, nullptr, nullptr);
234  __rhs.setp(nullptr, nullptr);
235  return *this;
236}
237
238#      endif // _LIBCPP_CXX03_LANG
239
240class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI istrstream : public istream {
241public:
242  _LIBCPP_HIDE_FROM_ABI explicit istrstream(const char* __s) : istream(&__sb_), __sb_(__s, 0) {}
243  _LIBCPP_HIDE_FROM_ABI explicit istrstream(char* __s) : istream(&__sb_), __sb_(__s, 0) {}
244  _LIBCPP_HIDE_FROM_ABI istrstream(const char* __s, streamsize __n) : istream(&__sb_), __sb_(__s, __n) {}
245  _LIBCPP_HIDE_FROM_ABI istrstream(char* __s, streamsize __n) : istream(&__sb_), __sb_(__s, __n) {}
246
247#      ifndef _LIBCPP_CXX03_LANG
248  _LIBCPP_HIDE_FROM_ABI istrstream(istrstream&& __rhs) // extension
249      : istream(std::move(static_cast<istream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
250    istream::set_rdbuf(&__sb_);
251  }
252
253  _LIBCPP_HIDE_FROM_ABI istrstream& operator=(istrstream&& __rhs) {
254    __sb_ = std::move(__rhs.__sb_);
255    istream::operator=(std::move(__rhs));
256    return *this;
257  }
258#      endif // _LIBCPP_CXX03_LANG
259
260  ~istrstream() override;
261
262  _LIBCPP_HIDE_FROM_ABI void swap(istrstream& __rhs) {
263    istream::swap(__rhs);
264    __sb_.swap(__rhs.__sb_);
265  }
266
267  _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
268  _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
269
270private:
271  strstreambuf __sb_;
272};
273
274class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI ostrstream : public ostream {
275public:
276  _LIBCPP_HIDE_FROM_ABI ostrstream() : ostream(&__sb_) {}
277  _LIBCPP_HIDE_FROM_ABI ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
278      : ostream(&__sb_), __sb_(__s, __n, __s + (__mode & ios::app ? std::strlen(__s) : 0)) {}
279
280#      ifndef _LIBCPP_CXX03_LANG
281  _LIBCPP_HIDE_FROM_ABI ostrstream(ostrstream&& __rhs) // extension
282      : ostream(std::move(static_cast<ostream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
283    ostream::set_rdbuf(&__sb_);
284  }
285
286  _LIBCPP_HIDE_FROM_ABI ostrstream& operator=(ostrstream&& __rhs) {
287    __sb_ = std::move(__rhs.__sb_);
288    ostream::operator=(std::move(__rhs));
289    return *this;
290  }
291#      endif // _LIBCPP_CXX03_LANG
292
293  ~ostrstream() override;
294
295  _LIBCPP_HIDE_FROM_ABI void swap(ostrstream& __rhs) {
296    ostream::swap(__rhs);
297    __sb_.swap(__rhs.__sb_);
298  }
299
300  _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
301  _LIBCPP_HIDE_FROM_ABI void freeze(bool __freezefl = true) { __sb_.freeze(__freezefl); }
302  _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
303  _LIBCPP_HIDE_FROM_ABI int pcount() const { return __sb_.pcount(); }
304
305private:
306  strstreambuf __sb_; // exposition only
307};
308
309class _LIBCPP_DEPRECATED _LIBCPP_EXPORTED_FROM_ABI strstream : public iostream {
310public:
311  // Types
312  typedef char char_type;
313  typedef char_traits<char>::int_type int_type;
314  typedef char_traits<char>::pos_type pos_type;
315  typedef char_traits<char>::off_type off_type;
316
317  // constructors/destructor
318  _LIBCPP_HIDE_FROM_ABI strstream() : iostream(&__sb_) {}
319  _LIBCPP_HIDE_FROM_ABI strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
320      : iostream(&__sb_), __sb_(__s, __n, __s + (__mode & ios::app ? std::strlen(__s) : 0)) {}
321
322#      ifndef _LIBCPP_CXX03_LANG
323  _LIBCPP_HIDE_FROM_ABI strstream(strstream&& __rhs) // extension
324      : iostream(std::move(static_cast<iostream&>(__rhs))), __sb_(std::move(__rhs.__sb_)) {
325    iostream::set_rdbuf(&__sb_);
326  }
327
328  _LIBCPP_HIDE_FROM_ABI strstream& operator=(strstream&& __rhs) {
329    __sb_ = std::move(__rhs.__sb_);
330    iostream::operator=(std::move(__rhs));
331    return *this;
332  }
333#      endif // _LIBCPP_CXX03_LANG
334
335  ~strstream() override;
336
337  _LIBCPP_HIDE_FROM_ABI void swap(strstream& __rhs) {
338    iostream::swap(__rhs);
339    __sb_.swap(__rhs.__sb_);
340  }
341
342  // Members:
343  _LIBCPP_HIDE_FROM_ABI strstreambuf* rdbuf() const { return const_cast<strstreambuf*>(&__sb_); }
344  _LIBCPP_HIDE_FROM_ABI void freeze(bool __freezefl = true) { __sb_.freeze(__freezefl); }
345  _LIBCPP_HIDE_FROM_ABI int pcount() const { return __sb_.pcount(); }
346  _LIBCPP_HIDE_FROM_ABI char* str() { return __sb_.str(); }
347
348private:
349  strstreambuf __sb_; // exposition only
350};
351
352_LIBCPP_END_NAMESPACE_STD
353
354_LIBCPP_POP_MACROS
355
356#    endif // _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_STRSTREAM) ||
357           // defined(_LIBCPP_BUILDING_LIBRARY)
358
359#  endif // _LIBCPP_HAS_LOCALIZATION
360
361#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
362
363#endif // _LIBCPP_STRSTREAM