blob: a9bd1dc3239790cf80282d9a446e8321f663ef8a [file] [log] [blame]
Louis Dionne9bd93882021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00002//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
Jonathan Roelofs6552b1e2014-09-02 20:34:23 +00009#include "__config"
Dan Albertf0abdfa2015-01-06 17:34:51 +000010
Howard Hinnantc51e1022010-05-11 19:42:16 +000011#include "ios"
Dan Albertf0abdfa2015-01-06 17:34:51 +000012
13#include <stdlib.h>
14
Howard Hinnantc51e1022010-05-11 19:42:16 +000015#include "__locale"
16#include "algorithm"
Eric Fiselier1366d262015-08-18 21:08:54 +000017#include "include/config_elast.h"
Dan Albertf0abdfa2015-01-06 17:34:51 +000018#include "limits"
Howard Hinnantc51e1022010-05-11 19:42:16 +000019#include "memory"
20#include "new"
Dan Albertf0abdfa2015-01-06 17:34:51 +000021#include "string"
Eric Fiselierf4433a32017-05-31 22:07:49 +000022#include "__undef_macros"
Howard Hinnantc51e1022010-05-11 19:42:16 +000023
24_LIBCPP_BEGIN_NAMESPACE_STD
25
Howard Hinnantc51e1022010-05-11 19:42:16 +000026class _LIBCPP_HIDDEN __iostream_category
27 : public __do_message
28{
29public:
Louis Dionne65358e12021-03-01 12:09:45 -050030 virtual const char* name() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 virtual string message(int ev) const;
32};
33
34const char*
Louis Dionne65358e12021-03-01 12:09:45 -050035__iostream_category::name() const noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000036{
37 return "iostream";
38}
39
40string
41__iostream_category::message(int ev) const
42{
Howard Hinnant155c2af2010-05-24 17:49:41 +000043 if (ev != static_cast<int>(io_errc::stream)
Jonathan Roelofs6552b1e2014-09-02 20:34:23 +000044#ifdef _LIBCPP_ELAST
45 && ev <= _LIBCPP_ELAST
Louis Dionne2b1ceaa2021-04-20 12:03:32 -040046#endif // _LIBCPP_ELAST
Howard Hinnant155c2af2010-05-24 17:49:41 +000047 )
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 return __do_message::message(ev);
49 return string("unspecified iostream_category error");
50}
51
52const error_category&
Louis Dionne65358e12021-03-01 12:09:45 -050053iostream_category() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000054{
55 static __iostream_category s;
56 return s;
57}
58
59// ios_base::failure
60
61ios_base::failure::failure(const string& msg, const error_code& ec)
62 : system_error(ec, msg)
63{
64}
65
66ios_base::failure::failure(const char* msg, const error_code& ec)
67 : system_error(ec, msg)
68{
69}
70
71ios_base::failure::~failure() throw()
72{
73}
74
75// ios_base locale
76
77const ios_base::fmtflags ios_base::boolalpha;
78const ios_base::fmtflags ios_base::dec;
79const ios_base::fmtflags ios_base::fixed;
80const ios_base::fmtflags ios_base::hex;
81const ios_base::fmtflags ios_base::internal;
82const ios_base::fmtflags ios_base::left;
83const ios_base::fmtflags ios_base::oct;
84const ios_base::fmtflags ios_base::right;
85const ios_base::fmtflags ios_base::scientific;
86const ios_base::fmtflags ios_base::showbase;
87const ios_base::fmtflags ios_base::showpoint;
88const ios_base::fmtflags ios_base::showpos;
89const ios_base::fmtflags ios_base::skipws;
90const ios_base::fmtflags ios_base::unitbuf;
91const ios_base::fmtflags ios_base::uppercase;
92const ios_base::fmtflags ios_base::adjustfield;
93const ios_base::fmtflags ios_base::basefield;
94const ios_base::fmtflags ios_base::floatfield;
95
96const ios_base::iostate ios_base::badbit;
97const ios_base::iostate ios_base::eofbit;
98const ios_base::iostate ios_base::failbit;
99const ios_base::iostate ios_base::goodbit;
100
101const ios_base::openmode ios_base::app;
102const ios_base::openmode ios_base::ate;
103const ios_base::openmode ios_base::binary;
104const ios_base::openmode ios_base::in;
105const ios_base::openmode ios_base::out;
106const ios_base::openmode ios_base::trunc;
107
108void
109ios_base::__call_callbacks(event ev)
110{
111 for (size_t i = __event_size_; i;)
112 {
113 --i;
114 __fn_[i](ev, *this, __index_[i]);
115 }
116}
117
118// locale
119
120locale
121ios_base::imbue(const locale& newloc)
122{
123 static_assert(sizeof(locale) == sizeof(__loc_), "");
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000124 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125 locale oldloc = loc_storage;
126 loc_storage = newloc;
127 __call_callbacks(imbue_event);
128 return oldloc;
129}
130
131locale
132ios_base::getloc() const
133{
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000134 const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 return loc_storage;
136}
137
138// xalloc
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000139#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000140atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
141#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142int ios_base::__xindex_ = 0;
Marshall Clowf9698ab2013-10-12 19:13:52 +0000143#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000145template <typename _Tp>
146static size_t __ios_new_cap(size_t __req_size, size_t __current_cap)
147{ // Precondition: __req_size > __current_cap
148 const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);
149 if (__req_size < mx/2)
150 return _VSTD::max(2 * __current_cap, __req_size);
151 else
152 return mx;
153}
154
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155int
156ios_base::xalloc()
157{
158 return __xindex_++;
159}
160
161long&
162ios_base::iword(int index)
163{
164 size_t req_size = static_cast<size_t>(index)+1;
165 if (req_size > __iarray_cap_)
166 {
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000167 size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);
168 long* iarray = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169 if (iarray == 0)
170 {
171 setstate(badbit);
172 static long error;
173 error = 0;
174 return error;
175 }
176 __iarray_ = iarray;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000177 for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178 *p = 0;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000179 __iarray_cap_ = newcap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 }
181 __iarray_size_ = max<size_t>(__iarray_size_, req_size);
182 return __iarray_[index];
183}
184
185void*&
186ios_base::pword(int index)
187{
188 size_t req_size = static_cast<size_t>(index)+1;
189 if (req_size > __parray_cap_)
190 {
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000191 size_t newcap = __ios_new_cap<void *>(req_size, __iarray_cap_);
192 void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void *)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193 if (parray == 0)
194 {
195 setstate(badbit);
196 static void* error;
197 error = 0;
198 return error;
199 }
200 __parray_ = parray;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000201 for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202 *p = 0;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000203 __parray_cap_ = newcap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204 }
205 __parray_size_ = max<size_t>(__parray_size_, req_size);
206 return __parray_[index];
207}
208
209// register_callback
210
211void
212ios_base::register_callback(event_callback fn, int index)
213{
214 size_t req_size = __event_size_ + 1;
215 if (req_size > __event_cap_)
216 {
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000217 size_t newcap = __ios_new_cap<event_callback>(req_size, __event_cap_);
218 event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219 if (fns == 0)
220 setstate(badbit);
221 __fn_ = fns;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000222 int* indxs = static_cast<int *>(realloc(__index_, newcap * sizeof(int)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 if (indxs == 0)
224 setstate(badbit);
225 __index_ = indxs;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000226 __event_cap_ = newcap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 }
228 __fn_[__event_size_] = fn;
229 __index_[__event_size_] = index;
230 ++__event_size_;
231}
232
233ios_base::~ios_base()
234{
235 __call_callbacks(erase_event);
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000236 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 loc_storage.~locale();
238 free(__fn_);
239 free(__index_);
240 free(__iarray_);
241 free(__parray_);
242}
243
244// iostate
245
246void
247ios_base::clear(iostate state)
248{
249 if (__rdbuf_)
250 __rdstate_ = state;
251 else
252 __rdstate_ = state | badbit;
Louis Dionne2b239162019-02-12 16:06:02 +0000253
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
Louis Dionne2b239162019-02-12 16:06:02 +0000255 __throw_failure("ios_base::clear");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256}
257
258// init
259
260void
261ios_base::init(void* sb)
262{
263 __rdbuf_ = sb;
264 __rdstate_ = __rdbuf_ ? goodbit : badbit;
265 __exceptions_ = goodbit;
266 __fmtflags_ = skipws | dec;
267 __width_ = 0;
268 __precision_ = 6;
269 __fn_ = 0;
270 __index_ = 0;
271 __event_size_ = 0;
272 __event_cap_ = 0;
273 __iarray_ = 0;
274 __iarray_size_ = 0;
275 __iarray_cap_ = 0;
276 __parray_ = 0;
277 __parray_size_ = 0;
278 __parray_cap_ = 0;
279 ::new(&__loc_) locale;
280}
281
282void
283ios_base::copyfmt(const ios_base& rhs)
284{
285 // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
Alp Tokerb8a95f52014-05-15 11:27:39 +0000286 // Don't alter *this until all needed resources are acquired
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287 unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
288 unique_ptr<int, void (*)(void*)> new_ints(0, free);
289 unique_ptr<long, void (*)(void*)> new_longs(0, free);
290 unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
291 if (__event_cap_ < rhs.__event_size_)
292 {
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000293 size_t newesize = sizeof(event_callback) * rhs.__event_size_;
294 new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295 if (!new_callbacks)
Louis Dionne2b239162019-02-12 16:06:02 +0000296 __throw_bad_alloc();
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000297
298 size_t newisize = sizeof(int) * rhs.__event_size_;
299 new_ints.reset(static_cast<int *>(malloc(newisize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 if (!new_ints)
Louis Dionne2b239162019-02-12 16:06:02 +0000301 __throw_bad_alloc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 }
303 if (__iarray_cap_ < rhs.__iarray_size_)
304 {
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000305 size_t newsize = sizeof(long) * rhs.__iarray_size_;
306 new_longs.reset(static_cast<long*>(malloc(newsize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 if (!new_longs)
Louis Dionne2b239162019-02-12 16:06:02 +0000308 __throw_bad_alloc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309 }
310 if (__parray_cap_ < rhs.__parray_size_)
311 {
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000312 size_t newsize = sizeof(void*) * rhs.__parray_size_;
313 new_pointers.reset(static_cast<void**>(malloc(newsize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314 if (!new_pointers)
Louis Dionne2b239162019-02-12 16:06:02 +0000315 __throw_bad_alloc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 }
317 // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_
318 __fmtflags_ = rhs.__fmtflags_;
319 __precision_ = rhs.__precision_;
320 __width_ = rhs.__width_;
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000321 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
322 const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323 lhs_loc = rhs_loc;
324 if (__event_cap_ < rhs.__event_size_)
325 {
326 free(__fn_);
327 __fn_ = new_callbacks.release();
328 free(__index_);
329 __index_ = new_ints.release();
330 __event_cap_ = rhs.__event_size_;
331 }
332 for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_)
333 {
334 __fn_[__event_size_] = rhs.__fn_[__event_size_];
335 __index_[__event_size_] = rhs.__index_[__event_size_];
336 }
337 if (__iarray_cap_ < rhs.__iarray_size_)
338 {
339 free(__iarray_);
340 __iarray_ = new_longs.release();
341 __iarray_cap_ = rhs.__iarray_size_;
342 }
343 for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
344 __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
345 if (__parray_cap_ < rhs.__parray_size_)
346 {
347 free(__parray_);
348 __parray_ = new_pointers.release();
349 __parray_cap_ = rhs.__parray_size_;
350 }
351 for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
352 __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
353}
354
355void
356ios_base::move(ios_base& rhs)
357{
358 // *this is uninitialized
359 __fmtflags_ = rhs.__fmtflags_;
360 __precision_ = rhs.__precision_;
361 __width_ = rhs.__width_;
362 __rdstate_ = rhs.__rdstate_;
363 __exceptions_ = rhs.__exceptions_;
364 __rdbuf_ = 0;
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000365 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366 ::new(&__loc_) locale(rhs_loc);
367 __fn_ = rhs.__fn_;
368 rhs.__fn_ = 0;
369 __index_ = rhs.__index_;
370 rhs.__index_ = 0;
371 __event_size_ = rhs.__event_size_;
372 rhs.__event_size_ = 0;
373 __event_cap_ = rhs.__event_cap_;
374 rhs.__event_cap_ = 0;
375 __iarray_ = rhs.__iarray_;
376 rhs.__iarray_ = 0;
377 __iarray_size_ = rhs.__iarray_size_;
378 rhs.__iarray_size_ = 0;
379 __iarray_cap_ = rhs.__iarray_cap_;
380 rhs.__iarray_cap_ = 0;
381 __parray_ = rhs.__parray_;
382 rhs.__parray_ = 0;
383 __parray_size_ = rhs.__parray_size_;
384 rhs.__parray_size_ = 0;
385 __parray_cap_ = rhs.__parray_cap_;
386 rhs.__parray_cap_ = 0;
387}
388
389void
Louis Dionne65358e12021-03-01 12:09:45 -0500390ios_base::swap(ios_base& rhs) noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000392 _VSTD::swap(__fmtflags_, rhs.__fmtflags_);
393 _VSTD::swap(__precision_, rhs.__precision_);
394 _VSTD::swap(__width_, rhs.__width_);
395 _VSTD::swap(__rdstate_, rhs.__rdstate_);
396 _VSTD::swap(__exceptions_, rhs.__exceptions_);
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000397 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
398 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000399 _VSTD::swap(lhs_loc, rhs_loc);
400 _VSTD::swap(__fn_, rhs.__fn_);
401 _VSTD::swap(__index_, rhs.__index_);
402 _VSTD::swap(__event_size_, rhs.__event_size_);
403 _VSTD::swap(__event_cap_, rhs.__event_cap_);
404 _VSTD::swap(__iarray_, rhs.__iarray_);
405 _VSTD::swap(__iarray_size_, rhs.__iarray_size_);
406 _VSTD::swap(__iarray_cap_, rhs.__iarray_cap_);
407 _VSTD::swap(__parray_, rhs.__parray_);
408 _VSTD::swap(__parray_size_, rhs.__parray_size_);
409 _VSTD::swap(__parray_cap_, rhs.__parray_cap_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410}
411
412void
413ios_base::__set_badbit_and_consider_rethrow()
414{
415 __rdstate_ |= badbit;
Howard Hinnant72f73582010-08-11 17:04:31 +0000416#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417 if (__exceptions_ & badbit)
418 throw;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400419#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420}
421
422void
423ios_base::__set_failbit_and_consider_rethrow()
424{
425 __rdstate_ |= failbit;
Howard Hinnant72f73582010-08-11 17:04:31 +0000426#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427 if (__exceptions_ & failbit)
428 throw;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400429#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430}
431
432bool
433ios_base::sync_with_stdio(bool sync)
434{
435 static bool previous_state = true;
436 bool r = previous_state;
437 previous_state = sync;
438 return r;
439}
440
441_LIBCPP_END_NAMESPACE_STD