/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/strstream.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===----------------------------------------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include <__assert> |
10 | | #include <__utility/unreachable.h> |
11 | | #include <algorithm> |
12 | | #include <climits> |
13 | | #include <cstdlib> |
14 | | #include <cstring> |
15 | | #include <strstream> |
16 | | |
17 | | _LIBCPP_PUSH_MACROS |
18 | | #include <__undef_macros> |
19 | | |
20 | | _LIBCPP_BEGIN_NAMESPACE_STD |
21 | | |
22 | | strstreambuf::strstreambuf(streamsize __alsize) |
23 | | : __strmode_(__dynamic), |
24 | | __alsize_(__alsize), |
25 | | __palloc_(nullptr), |
26 | | __pfree_(nullptr) |
27 | 0 | { |
28 | 0 | } |
29 | | |
30 | | strstreambuf::strstreambuf(void* (*__palloc)(size_t), void (*__pfree)(void*)) |
31 | | : __strmode_(__dynamic), |
32 | | __alsize_(__default_alsize), |
33 | | __palloc_(__palloc), |
34 | | __pfree_(__pfree) |
35 | 0 | { |
36 | 0 | } |
37 | | |
38 | | void |
39 | | strstreambuf::__init(char* __gnext, streamsize __n, char* __pbeg) |
40 | 0 | { |
41 | 0 | if (__n == 0) |
42 | 0 | __n = static_cast<streamsize>(strlen(__gnext)); |
43 | 0 | else if (__n < 0) |
44 | 0 | __n = INT_MAX; |
45 | 0 | if (__pbeg == nullptr) |
46 | 0 | setg(__gnext, __gnext, __gnext + __n); |
47 | 0 | else |
48 | 0 | { |
49 | 0 | setg(__gnext, __gnext, __pbeg); |
50 | 0 | setp(__pbeg, __pbeg + __n); |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | strstreambuf::strstreambuf(char* __gnext, streamsize __n, char* __pbeg) |
55 | | : __strmode_(), |
56 | | __alsize_(__default_alsize), |
57 | | __palloc_(nullptr), |
58 | | __pfree_(nullptr) |
59 | 0 | { |
60 | 0 | __init(__gnext, __n, __pbeg); |
61 | 0 | } |
62 | | |
63 | | strstreambuf::strstreambuf(const char* __gnext, streamsize __n) |
64 | | : __strmode_(__constant), |
65 | | __alsize_(__default_alsize), |
66 | | __palloc_(nullptr), |
67 | | __pfree_(nullptr) |
68 | 0 | { |
69 | 0 | __init(const_cast<char *>(__gnext), __n, nullptr); |
70 | 0 | } |
71 | | |
72 | | strstreambuf::strstreambuf(signed char* __gnext, streamsize __n, signed char* __pbeg) |
73 | | : __strmode_(), |
74 | | __alsize_(__default_alsize), |
75 | | __palloc_(nullptr), |
76 | | __pfree_(nullptr) |
77 | 0 | { |
78 | 0 | __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, reinterpret_cast<char*>(__pbeg)); |
79 | 0 | } |
80 | | |
81 | | strstreambuf::strstreambuf(const signed char* __gnext, streamsize __n) |
82 | | : __strmode_(__constant), |
83 | | __alsize_(__default_alsize), |
84 | | __palloc_(nullptr), |
85 | | __pfree_(nullptr) |
86 | 0 | { |
87 | 0 | __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, nullptr); |
88 | 0 | } |
89 | | |
90 | | strstreambuf::strstreambuf(unsigned char* __gnext, streamsize __n, unsigned char* __pbeg) |
91 | | : __strmode_(), |
92 | | __alsize_(__default_alsize), |
93 | | __palloc_(nullptr), |
94 | | __pfree_(nullptr) |
95 | 0 | { |
96 | 0 | __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, reinterpret_cast<char*>(__pbeg)); |
97 | 0 | } |
98 | | |
99 | | strstreambuf::strstreambuf(const unsigned char* __gnext, streamsize __n) |
100 | | : __strmode_(__constant), |
101 | | __alsize_(__default_alsize), |
102 | | __palloc_(nullptr), |
103 | | __pfree_(nullptr) |
104 | 0 | { |
105 | 0 | __init(const_cast<char *>(reinterpret_cast<const char*>(__gnext)), __n, nullptr); |
106 | 0 | } |
107 | | |
108 | | strstreambuf::~strstreambuf() |
109 | 0 | { |
110 | 0 | if (eback() && (__strmode_ & __allocated) != 0 && (__strmode_ & __frozen) == 0) |
111 | 0 | { |
112 | 0 | if (__pfree_) |
113 | 0 | __pfree_(eback()); |
114 | 0 | else |
115 | 0 | delete [] eback(); |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | void |
120 | | strstreambuf::swap(strstreambuf& __rhs) |
121 | 0 | { |
122 | 0 | streambuf::swap(__rhs); |
123 | 0 | _VSTD::swap(__strmode_, __rhs.__strmode_); |
124 | 0 | _VSTD::swap(__alsize_, __rhs.__alsize_); |
125 | 0 | _VSTD::swap(__palloc_, __rhs.__palloc_); |
126 | 0 | _VSTD::swap(__pfree_, __rhs.__pfree_); |
127 | 0 | } |
128 | | |
129 | | void |
130 | | strstreambuf::freeze(bool __freezefl) |
131 | 0 | { |
132 | 0 | if (__strmode_ & __dynamic) |
133 | 0 | { |
134 | 0 | if (__freezefl) |
135 | 0 | __strmode_ |= __frozen; |
136 | 0 | else |
137 | 0 | __strmode_ &= ~__frozen; |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | | char* |
142 | | strstreambuf::str() |
143 | 0 | { |
144 | 0 | if (__strmode_ & __dynamic) |
145 | 0 | __strmode_ |= __frozen; |
146 | 0 | return eback(); |
147 | 0 | } |
148 | | |
149 | | int |
150 | | strstreambuf::pcount() const |
151 | 0 | { |
152 | 0 | return static_cast<int>(pptr() - pbase()); |
153 | 0 | } |
154 | | |
155 | | strstreambuf::int_type |
156 | | strstreambuf::overflow(int_type __c) |
157 | 0 | { |
158 | 0 | if (__c == EOF) |
159 | 0 | return int_type(0); |
160 | 0 | if (pptr() == epptr()) |
161 | 0 | { |
162 | 0 | if ((__strmode_ & __dynamic) == 0 || (__strmode_ & __frozen) != 0) |
163 | 0 | return int_type(EOF); |
164 | 0 | size_t old_size = static_cast<size_t> ((epptr() ? epptr() : egptr()) - eback()); |
165 | 0 | size_t new_size = max<size_t>(static_cast<size_t>(__alsize_), 2*old_size); |
166 | 0 | if (new_size == 0) |
167 | 0 | new_size = __default_alsize; |
168 | 0 | char* buf = nullptr; |
169 | 0 | if (__palloc_) |
170 | 0 | buf = static_cast<char*>(__palloc_(new_size)); |
171 | 0 | else |
172 | 0 | buf = new char[new_size]; |
173 | 0 | if (buf == nullptr) |
174 | 0 | return int_type(EOF); |
175 | 0 | if (old_size != 0) { |
176 | 0 | _LIBCPP_ASSERT(eback(), "overflow copying from NULL"); |
177 | 0 | memcpy(buf, eback(), static_cast<size_t>(old_size)); |
178 | 0 | } |
179 | 0 | ptrdiff_t ninp = gptr() - eback(); |
180 | 0 | ptrdiff_t einp = egptr() - eback(); |
181 | 0 | ptrdiff_t nout = pptr() - pbase(); |
182 | 0 | if (__strmode_ & __allocated) |
183 | 0 | { |
184 | 0 | if (__pfree_) |
185 | 0 | __pfree_(eback()); |
186 | 0 | else |
187 | 0 | delete [] eback(); |
188 | 0 | } |
189 | 0 | setg(buf, buf + ninp, buf + einp); |
190 | 0 | setp(buf + einp, buf + new_size); |
191 | 0 | __pbump(nout); |
192 | 0 | __strmode_ |= __allocated; |
193 | 0 | } |
194 | 0 | *pptr() = static_cast<char>(__c); |
195 | 0 | pbump(1); |
196 | 0 | return int_type(static_cast<unsigned char>(__c)); |
197 | 0 | } |
198 | | |
199 | | strstreambuf::int_type |
200 | | strstreambuf::pbackfail(int_type __c) |
201 | 0 | { |
202 | 0 | if (eback() == gptr()) |
203 | 0 | return EOF; |
204 | 0 | if (__c == EOF) |
205 | 0 | { |
206 | 0 | gbump(-1); |
207 | 0 | return int_type(0); |
208 | 0 | } |
209 | 0 | if (__strmode_ & __constant) |
210 | 0 | { |
211 | 0 | if (gptr()[-1] == static_cast<char>(__c)) |
212 | 0 | { |
213 | 0 | gbump(-1); |
214 | 0 | return __c; |
215 | 0 | } |
216 | 0 | return EOF; |
217 | 0 | } |
218 | 0 | gbump(-1); |
219 | 0 | *gptr() = static_cast<char>(__c); |
220 | 0 | return __c; |
221 | 0 | } |
222 | | |
223 | | strstreambuf::int_type |
224 | | strstreambuf::underflow() |
225 | 0 | { |
226 | 0 | if (gptr() == egptr()) |
227 | 0 | { |
228 | 0 | if (egptr() >= pptr()) |
229 | 0 | return EOF; |
230 | 0 | setg(eback(), gptr(), pptr()); |
231 | 0 | } |
232 | 0 | return int_type(static_cast<unsigned char>(*gptr())); |
233 | 0 | } |
234 | | |
235 | | strstreambuf::pos_type |
236 | | strstreambuf::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which) |
237 | 0 | { |
238 | 0 | off_type __p(-1); |
239 | 0 | bool pos_in = (__which & ios::in) != 0; |
240 | 0 | bool pos_out = (__which & ios::out) != 0; |
241 | 0 | bool legal = false; |
242 | 0 | switch (__way) |
243 | 0 | { |
244 | 0 | case ios::beg: |
245 | 0 | case ios::end: |
246 | 0 | if (pos_in || pos_out) |
247 | 0 | legal = true; |
248 | 0 | break; |
249 | 0 | case ios::cur: |
250 | 0 | if (pos_in != pos_out) |
251 | 0 | legal = true; |
252 | 0 | break; |
253 | 0 | } |
254 | 0 | if (pos_in && gptr() == nullptr) |
255 | 0 | legal = false; |
256 | 0 | if (pos_out && pptr() == nullptr) |
257 | 0 | legal = false; |
258 | 0 | if (legal) |
259 | 0 | { |
260 | 0 | off_type newoff; |
261 | 0 | char* seekhigh = epptr() ? epptr() : egptr(); |
262 | 0 | switch (__way) |
263 | 0 | { |
264 | 0 | case ios::beg: |
265 | 0 | newoff = 0; |
266 | 0 | break; |
267 | 0 | case ios::cur: |
268 | 0 | newoff = (pos_in ? gptr() : pptr()) - eback(); |
269 | 0 | break; |
270 | 0 | case ios::end: |
271 | 0 | newoff = seekhigh - eback(); |
272 | 0 | break; |
273 | 0 | default: |
274 | 0 | __libcpp_unreachable(); |
275 | 0 | } |
276 | 0 | newoff += __off; |
277 | 0 | if (0 <= newoff && newoff <= seekhigh - eback()) |
278 | 0 | { |
279 | 0 | char* newpos = eback() + newoff; |
280 | 0 | if (pos_in) |
281 | 0 | setg(eback(), newpos, _VSTD::max(newpos, egptr())); |
282 | 0 | if (pos_out) |
283 | 0 | { |
284 | | // min(pbase, newpos), newpos, epptr() |
285 | 0 | __off = epptr() - newpos; |
286 | 0 | setp(min(pbase(), newpos), epptr()); |
287 | 0 | __pbump((epptr() - pbase()) - __off); |
288 | 0 | } |
289 | 0 | __p = newoff; |
290 | 0 | } |
291 | 0 | } |
292 | 0 | return pos_type(__p); |
293 | 0 | } |
294 | | |
295 | | strstreambuf::pos_type |
296 | | strstreambuf::seekpos(pos_type __sp, ios_base::openmode __which) |
297 | 0 | { |
298 | 0 | off_type __p(-1); |
299 | 0 | bool pos_in = (__which & ios::in) != 0; |
300 | 0 | bool pos_out = (__which & ios::out) != 0; |
301 | 0 | if (pos_in || pos_out) |
302 | 0 | { |
303 | 0 | if (!((pos_in && gptr() == nullptr) || (pos_out && pptr() == nullptr))) |
304 | 0 | { |
305 | 0 | off_type newoff = __sp; |
306 | 0 | char* seekhigh = epptr() ? epptr() : egptr(); |
307 | 0 | if (0 <= newoff && newoff <= seekhigh - eback()) |
308 | 0 | { |
309 | 0 | char* newpos = eback() + newoff; |
310 | 0 | if (pos_in) |
311 | 0 | setg(eback(), newpos, _VSTD::max(newpos, egptr())); |
312 | 0 | if (pos_out) |
313 | 0 | { |
314 | | // min(pbase, newpos), newpos, epptr() |
315 | 0 | off_type temp = epptr() - newpos; |
316 | 0 | setp(min(pbase(), newpos), epptr()); |
317 | 0 | __pbump((epptr() - pbase()) - temp); |
318 | 0 | } |
319 | 0 | __p = newoff; |
320 | 0 | } |
321 | 0 | } |
322 | 0 | } |
323 | 0 | return pos_type(__p); |
324 | 0 | } |
325 | | |
326 | | istrstream::~istrstream() |
327 | 0 | { |
328 | 0 | } |
329 | | |
330 | | ostrstream::~ostrstream() |
331 | 0 | { |
332 | 0 | } |
333 | | |
334 | | strstream::~strstream() |
335 | 0 | { |
336 | 0 | } |
337 | | |
338 | | _LIBCPP_END_NAMESPACE_STD |
339 | | |
340 | | _LIBCPP_POP_MACROS |