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