aboutsummaryrefslogtreecommitdiff
path: root/system/include/libcxx/strstream
blob: 5eadefd12b90a54eba55717372f3c82dd20cd3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// -*- C++ -*-
//===--------------------------- strstream --------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_STRSTREAM
#define _LIBCPP_STRSTREAM

/*
    strstream synopsis

class strstreambuf
    : public basic_streambuf<char>
{
public:
    explicit strstreambuf(streamsize alsize_arg = 0);
    strstreambuf(void* (*palloc_arg)(size_t), void (*pfree_arg)(void*));
    strstreambuf(char* gnext_arg, streamsize n, char* pbeg_arg = 0);
    strstreambuf(const char* gnext_arg, streamsize n);

    strstreambuf(signed char* gnext_arg, streamsize n, signed char* pbeg_arg = 0);
    strstreambuf(const signed char* gnext_arg, streamsize n);
    strstreambuf(unsigned char* gnext_arg, streamsize n, unsigned char* pbeg_arg = 0);
    strstreambuf(const unsigned char* gnext_arg, streamsize n);

    strstreambuf(strstreambuf&& rhs);
    strstreambuf& operator=(strstreambuf&& rhs);

    virtual ~strstreambuf();

    void swap(strstreambuf& rhs);

    void freeze(bool freezefl = true);
    char* str();
    int pcount() const;

protected:
    virtual int_type overflow (int_type c = EOF);
    virtual int_type pbackfail(int_type c = EOF);
    virtual int_type underflow();
    virtual pos_type seekoff(off_type off, ios_base::seekdir way,
                             ios_base::openmode which = ios_base::in | ios_base::out);
    virtual pos_type seekpos(pos_type sp,
                             ios_base::openmode which = ios_base::in | ios_base::out);
    virtual streambuf* setbuf(char* s, streamsize n);

private:
    typedef T1 strstate;                // exposition only
    static const strstate allocated;    // exposition only
    static const strstate constant;     // exposition only
    static const strstate dynamic;      // exposition only
    static const strstate frozen;       // exposition only
    strstate strmode;                   // exposition only
    streamsize alsize;                  // exposition only
    void* (*palloc)(size_t);            // exposition only
    void (*pfree)(void*);               // exposition only
};

class istrstream
    : public basic_istream<char>
{
public:
    explicit istrstream(const char* s);
    explicit istrstream(char* s);
    istrstream(const char* s, streamsize n);
    istrstream(char* s, streamsize n);

    virtual ~istrstream();

    strstreambuf* rdbuf() const;
    char *str();

private:
    strstreambuf sb; // exposition only
};

class ostrstream
    : public basic_ostream<char>
{
public:
    ostrstream();
    ostrstream(char* s, int n, ios_base::openmode mode = ios_base::out);

    virtual ~ostrstream();

    strstreambuf* rdbuf() const;
    void freeze(bool freezefl = true);
    char* str();
    int pcount() const;

private:
    strstreambuf sb; // exposition only
};

class strstream
    : public basic_iostream<char>
{
public:
    // Types
    typedef char                        char_type;
    typedef char_traits<char>::int_type int_type;
    typedef char_traits<char>::pos_type pos_type;
    typedef char_traits<char>::off_type off_type;

    // constructors/destructor
    strstream();
    strstream(char* s, int n, ios_base::openmode mode = ios_base::in | ios_base::out);

    virtual ~strstream();

    // Members:
    strstreambuf* rdbuf() const;
    void freeze(bool freezefl = true);
    int pcount() const;
    char* str();

private:
    strstreambuf sb; // exposition only
};

}  // std

*/

#include <__config>
#include <ostream>
#include <istream>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

class _LIBCPP_VISIBLE strstreambuf
    : public streambuf
{
public:
    explicit strstreambuf(streamsize __alsize = 0);
    strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*));
    strstreambuf(char* __gnext, streamsize __n, char* __pbeg = 0);
    strstreambuf(const char* __gnext, streamsize __n);

    strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg = 0);
    strstreambuf(const signed char* __gnext, streamsize __n);
    strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg = 0);
    strstreambuf(const unsigned char* __gnext, streamsize __n);

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    strstreambuf(strstreambuf&& __rhs);
    _LIBCPP_INLINE_VISIBILITY
    strstreambuf& operator=(strstreambuf&& __rhs);
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    virtual ~strstreambuf();

    void swap(strstreambuf& __rhs);

    void freeze(bool __freezefl = true);
    char* str();
    int pcount() const;

protected:
    virtual int_type overflow (int_type __c = EOF);
    virtual int_type pbackfail(int_type __c = EOF);
    virtual int_type underflow();
    virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
                             ios_base::openmode __which = ios_base::in | ios_base::out);
    virtual pos_type seekpos(pos_type __sp,
                             ios_base::openmode __which = ios_base::in | ios_base::out);

private:
    typedef unsigned __mode_type;
    static const __mode_type __allocated = 0x01;
    static const __mode_type __constant  = 0x02;
    static const __mode_type __dynamic   = 0x04;
    static const __mode_type __frozen    = 0x08;
    static const streamsize    __default_alsize = 4096;

    __mode_type __strmode_;
    streamsize __alsize_;
    void* (*__palloc_)(size_t);
    void (*__pfree_)(void*);

    void __init(char* __gnext, streamsize __n, char* __pbeg);
};

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES

inline _LIBCPP_INLINE_VISIBILITY
strstreambuf::strstreambuf(strstreambuf&& __rhs)
    : streambuf(__rhs),
      __strmode_(__rhs.__strmode_),
      __alsize_(__rhs.__alsize_),
      __palloc_(__rhs.__palloc_),
      __pfree_(__rhs.__pfree_)
{
    __rhs.setg(nullptr, nullptr, nullptr);
    __rhs.setp(nullptr, nullptr);
}

inline _LIBCPP_INLINE_VISIBILITY
strstreambuf&
strstreambuf::operator=(strstreambuf&& __rhs)
{
    if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0)
    {
        if (__pfree_)
            __pfree_(eback());
        else
            delete [] eback();
    }
    streambuf::operator=(__rhs);
    __strmode_ = __rhs.__strmode_;
    __alsize_ = __rhs.__alsize_;
    __palloc_ = __rhs.__palloc_;
    __pfree_ = __rhs.__pfree_;
    __rhs.setg(nullptr, nullptr, nullptr);
    __rhs.setp(nullptr, nullptr);
    return *this;
}

#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

class _LIBCPP_VISIBLE istrstream
    : public istream
{
public:
    _LIBCPP_INLINE_VISIBILITY
    explicit istrstream(const char* __s)
        : istream(&__sb_), __sb_(__s, 0) {}
    _LIBCPP_INLINE_VISIBILITY
    explicit istrstream(char* __s)
        : istream(&__sb_), __sb_(__s, 0) {}
    _LIBCPP_INLINE_VISIBILITY
    istrstream(const char* __s, streamsize __n)
        : istream(&__sb_), __sb_(__s, __n) {}
    _LIBCPP_INLINE_VISIBILITY
    istrstream(char* __s, streamsize __n)
        : istream(&__sb_), __sb_(__s, __n) {}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    istrstream(istrstream&& __rhs)
        : istream(_VSTD::move(__rhs)),
          __sb_(_VSTD::move(__rhs.__sb_))
    {
        istream::set_rdbuf(&__sb_);
    }

    _LIBCPP_INLINE_VISIBILITY
    istrstream& operator=(istrstream&& __rhs)
    {
        istream::operator=(_VSTD::move(__rhs));
        __sb_ = _VSTD::move(__rhs.__sb_);
        return *this;
    }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    virtual ~istrstream();

    _LIBCPP_INLINE_VISIBILITY
    void swap(istrstream& __rhs)
    {
        istream::swap(__rhs);
        __sb_.swap(__rhs.__sb_);
    }

    _LIBCPP_INLINE_VISIBILITY
    strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
    _LIBCPP_INLINE_VISIBILITY
    char *str() {return __sb_.str();}

private:
    strstreambuf __sb_;
};

class _LIBCPP_VISIBLE ostrstream
    : public ostream
{
public:
    _LIBCPP_INLINE_VISIBILITY
    ostrstream()
        : ostream(&__sb_) {}
    _LIBCPP_INLINE_VISIBILITY
    ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out)
        : ostream(&__sb_),
          __sb_(__s, __n, __s + (__mode & ios::app ? strlen(__s) : 0))
        {}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    ostrstream(ostrstream&& __rhs)
        : ostream(_VSTD::move(__rhs)),
          __sb_(_VSTD::move(__rhs.__sb_))
    {
        ostream::set_rdbuf(&__sb_);
    }

    _LIBCPP_INLINE_VISIBILITY
    ostrstream& operator=(ostrstream&& __rhs)
    {
        ostream::operator=(_VSTD::move(__rhs));
        __sb_ = _VSTD::move(__rhs.__sb_);
        return *this;
    }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    virtual ~ostrstream();

    _LIBCPP_INLINE_VISIBILITY
    void swap(ostrstream& __rhs)
    {
        ostream::swap(__rhs);
        __sb_.swap(__rhs.__sb_);
    }

    _LIBCPP_INLINE_VISIBILITY
    strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
    _LIBCPP_INLINE_VISIBILITY
    void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
    _LIBCPP_INLINE_VISIBILITY
    char* str()         {return __sb_.str();}
    _LIBCPP_INLINE_VISIBILITY
    int pcount() const  {return __sb_.pcount();}

private:
    strstreambuf __sb_; // exposition only
};

class _LIBCPP_VISIBLE strstream
    : public iostream
{
public:
    // Types
    typedef char                        char_type;
    typedef char_traits<char>::int_type int_type;
    typedef char_traits<char>::pos_type pos_type;
    typedef char_traits<char>::off_type off_type;

    // constructors/destructor
    _LIBCPP_INLINE_VISIBILITY
    strstream()
        : iostream(&__sb_) {}
    _LIBCPP_INLINE_VISIBILITY
    strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out)
        : iostream(&__sb_),
          __sb_(__s, __n, __s + (__mode & ios::app ? strlen(__s) : 0))
        {}

#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    _LIBCPP_INLINE_VISIBILITY
    strstream(strstream&& __rhs)
        : iostream(_VSTD::move(__rhs)),
          __sb_(_VSTD::move(__rhs.__sb_))
    {
        iostream::set_rdbuf(&__sb_);
    }

    _LIBCPP_INLINE_VISIBILITY
    strstream& operator=(strstream&& __rhs)
    {
        iostream::operator=(_VSTD::move(__rhs));
        __sb_ = _VSTD::move(__rhs.__sb_);
        return *this;
    }
#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES

    virtual ~strstream();

    _LIBCPP_INLINE_VISIBILITY
    void swap(strstream& __rhs)
    {
        iostream::swap(__rhs);
        __sb_.swap(__rhs.__sb_);
    }

    // Members:
    _LIBCPP_INLINE_VISIBILITY
    strstreambuf* rdbuf() const {return const_cast<strstreambuf*>(&__sb_);}
    _LIBCPP_INLINE_VISIBILITY
    void freeze(bool __freezefl = true) {__sb_.freeze(__freezefl);}
    _LIBCPP_INLINE_VISIBILITY
    int pcount() const {return __sb_.pcount();}
    _LIBCPP_INLINE_VISIBILITY
    char* str()        {return __sb_.str();}

private:
    strstreambuf __sb_; // exposition only
};

_LIBCPP_END_NAMESPACE_STD

#endif  // _LIBCPP_STRSTREAM