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