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
|
//===------------------------ strstream.cpp -------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
#include "strstream"
#include "algorithm"
#include "climits"
#include "cstring"
_LIBCPP_BEGIN_NAMESPACE_STD
strstreambuf::strstreambuf(streamsize __alsize)
: __strmode_(__dynamic),
__alsize_(__alsize),
__palloc_(nullptr),
__pfree_(nullptr)
{
}
strstreambuf::strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*))
: __strmode_(__dynamic),
__alsize_(__default_alsize),
__palloc_(__palloc),
__pfree_(__pfree)
{
}
void
strstreambuf::__init(char* __gnext, streamsize __n, char* __pbeg)
{
if (__n == 0)
__n = static_cast<streamsize>(strlen(__gnext));
else if (__n < 0)
__n = INT_MAX;
if (__pbeg == nullptr)
setg(__gnext, __gnext, __gnext + __n);
else
{
setg(__gnext, __gnext, __pbeg);
setp(__pbeg, __pbeg + __n);
}
}
strstreambuf::strstreambuf(char* __gnext, streamsize __n, char* __pbeg)
: __strmode_(),
__alsize_(__default_alsize),
__palloc_(nullptr),
__pfree_(nullptr)
{
__init(__gnext, __n, __pbeg);
}
strstreambuf::strstreambuf(const char* __gnext, streamsize __n)
: __strmode_(__constant),
__alsize_(__default_alsize),
__palloc_(nullptr),
__pfree_(nullptr)
{
__init((char*)__gnext, __n, nullptr);
}
strstreambuf::strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg)
: __strmode_(),
__alsize_(__default_alsize),
__palloc_(nullptr),
__pfree_(nullptr)
{
__init((char*)__gnext, __n, (char*)__pbeg);
}
strstreambuf::strstreambuf(const signed char* __gnext, streamsize __n)
: __strmode_(__constant),
__alsize_(__default_alsize),
__palloc_(nullptr),
__pfree_(nullptr)
{
__init((char*)__gnext, __n, nullptr);
}
strstreambuf::strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg)
: __strmode_(),
__alsize_(__default_alsize),
__palloc_(nullptr),
__pfree_(nullptr)
{
__init((char*)__gnext, __n, (char*)__pbeg);
}
strstreambuf::strstreambuf(const unsigned char* __gnext, streamsize __n)
: __strmode_(__constant),
__alsize_(__default_alsize),
__palloc_(nullptr),
__pfree_(nullptr)
{
__init((char*)__gnext, __n, nullptr);
}
strstreambuf::~strstreambuf()
{
if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0)
{
if (__pfree_)
__pfree_(eback());
else
delete [] eback();
}
}
void
strstreambuf::swap(strstreambuf& __rhs)
{
streambuf::swap(__rhs);
_VSTD::swap(__strmode_, __rhs.__strmode_);
_VSTD::swap(__alsize_, __rhs.__alsize_);
_VSTD::swap(__palloc_, __rhs.__palloc_);
_VSTD::swap(__pfree_, __rhs.__pfree_);
}
void
strstreambuf::freeze(bool __freezefl)
{
if (__strmode_ & __dynamic)
{
if (__freezefl)
__strmode_ |= __frozen;
else
__strmode_ &= ~__frozen;
}
}
char*
strstreambuf::str()
{
if (__strmode_ & __dynamic)
__strmode_ |= __frozen;
return eback();
}
int
strstreambuf::pcount() const
{
return static_cast<int>(pptr() - pbase());
}
strstreambuf::int_type
strstreambuf::overflow(int_type __c)
{
if (__c == EOF)
return int_type(0);
if (pptr() == epptr())
{
if ((__strmode_ & __dynamic) == 0 || (__strmode_ & __frozen) != 0)
return int_type(EOF);
size_t old_size = static_cast<size_t> ((epptr() ? epptr() : egptr()) - eback());
size_t new_size = max<size_t>(static_cast<size_t>(__alsize_), 2*old_size);
if (new_size == 0)
new_size = __default_alsize;
char* buf = nullptr;
if (__palloc_)
buf = static_cast<char*>(__palloc_(new_size));
else
buf = new char[new_size];
if (buf == nullptr)
return int_type(EOF);
memcpy(buf, eback(), static_cast<size_t>(old_size));
ptrdiff_t ninp = gptr() - eback();
ptrdiff_t einp = egptr() - eback();
ptrdiff_t nout = pptr() - pbase();
ptrdiff_t eout = epptr() - pbase();
if (__strmode_ & __allocated)
{
if (__pfree_)
__pfree_(eback());
else
delete [] eback();
}
setg(buf, buf + ninp, buf + einp);
setp(buf + einp, buf + einp + eout);
pbump(static_cast<int>(nout));
__strmode_ |= __allocated;
}
*pptr() = static_cast<char>(__c);
pbump(1);
return int_type((unsigned char)__c);
}
strstreambuf::int_type
strstreambuf::pbackfail(int_type __c)
{
if (eback() == gptr())
return EOF;
if (__c == EOF)
{
gbump(-1);
return int_type(0);
}
if (__strmode_ & __constant)
{
if (gptr()[-1] == static_cast<char>(__c))
{
gbump(-1);
return __c;
}
return EOF;
}
gbump(-1);
*gptr() = static_cast<char>(__c);
return __c;
}
strstreambuf::int_type
strstreambuf::underflow()
{
if (gptr() == egptr())
{
if (egptr() >= pptr())
return EOF;
setg(eback(), gptr(), pptr());
}
return int_type((unsigned char)*gptr());
}
strstreambuf::pos_type
strstreambuf::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which)
{
off_type __p(-1);
bool pos_in = (__which & ios::in) != 0;
bool pos_out = (__which & ios::out) != 0;
bool legal = false;
switch (__way)
{
case ios::beg:
case ios::end:
if (pos_in || pos_out)
legal = true;
break;
case ios::cur:
if (pos_in != pos_out)
legal = true;
break;
}
if (pos_in && gptr() == nullptr)
legal = false;
if (pos_out && pptr() == nullptr)
legal = false;
if (legal)
{
off_type newoff;
char* seekhigh = epptr() ? epptr() : egptr();
switch (__way)
{
case ios::beg:
newoff = 0;
break;
case ios::cur:
newoff = (pos_in ? gptr() : pptr()) - eback();
break;
case ios::end:
newoff = seekhigh - eback();
break;
}
newoff += __off;
if (0 <= newoff && newoff <= seekhigh - eback())
{
char* newpos = eback() + newoff;
if (pos_in)
setg(eback(), newpos, _VSTD::max(newpos, egptr()));
if (pos_out)
{
// min(pbase, newpos), newpos, epptr()
__off = epptr() - newpos;
setp(min(pbase(), newpos), epptr());
pbump(static_cast<int>((epptr() - pbase()) - __off));
}
__p = newoff;
}
}
return pos_type(__p);
}
strstreambuf::pos_type
strstreambuf::seekpos(pos_type __sp, ios_base::openmode __which)
{
off_type __p(-1);
bool pos_in = (__which & ios::in) != 0;
bool pos_out = (__which & ios::out) != 0;
if (pos_in || pos_out)
{
if (!((pos_in && gptr() == nullptr) || (pos_out && pptr() == nullptr)))
{
off_type newoff = __sp;
char* seekhigh = epptr() ? epptr() : egptr();
if (0 <= newoff && newoff <= seekhigh - eback())
{
char* newpos = eback() + newoff;
if (pos_in)
setg(eback(), newpos, _VSTD::max(newpos, egptr()));
if (pos_out)
{
// min(pbase, newpos), newpos, epptr()
off_type temp = epptr() - newpos;
setp(min(pbase(), newpos), epptr());
pbump(static_cast<int>((epptr() - pbase()) - temp));
}
__p = newoff;
}
}
}
return pos_type(__p);
}
istrstream::~istrstream()
{
}
ostrstream::~ostrstream()
{
}
strstream::~strstream()
{
}
_LIBCPP_END_NAMESPACE_STD
|