blob: 2dc84be82873939d12cab579b3d6dd520a0071ce [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===-------------------------- ios.cpp -----------------------------------===//
2//
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 "istream"
19#include "limits"
Howard Hinnantc51e1022010-05-11 19:42:16 +000020#include "memory"
21#include "new"
Dan Albertf0abdfa2015-01-06 17:34:51 +000022#include "streambuf"
23#include "string"
Eric Fiselierf4433a32017-05-31 22:07:49 +000024#include "__undef_macros"
Howard Hinnantc51e1022010-05-11 19:42:16 +000025
26_LIBCPP_BEGIN_NAMESPACE_STD
27
Shoaib Meenai5ac10672016-09-19 18:29:07 +000028template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios<char>;
29template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios<wchar_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000030
Shoaib Meenai5ac10672016-09-19 18:29:07 +000031template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_streambuf<char>;
32template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_streambuf<wchar_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000033
Shoaib Meenai5ac10672016-09-19 18:29:07 +000034template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_istream<char>;
35template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_istream<wchar_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000036
Shoaib Meenai5ac10672016-09-19 18:29:07 +000037template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ostream<char>;
38template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ostream<wchar_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000039
Shoaib Meenai5ac10672016-09-19 18:29:07 +000040template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_iostream<char>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000041
42class _LIBCPP_HIDDEN __iostream_category
43 : public __do_message
44{
45public:
Howard Hinnant1f098bc2011-05-26 19:48:01 +000046 virtual const char* name() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 virtual string message(int ev) const;
48};
49
50const char*
Howard Hinnant1f098bc2011-05-26 19:48:01 +000051__iostream_category::name() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000052{
53 return "iostream";
54}
55
56string
57__iostream_category::message(int ev) const
58{
Howard Hinnant155c2af2010-05-24 17:49:41 +000059 if (ev != static_cast<int>(io_errc::stream)
Jonathan Roelofs6552b1e2014-09-02 20:34:23 +000060#ifdef _LIBCPP_ELAST
61 && ev <= _LIBCPP_ELAST
62#endif // _LIBCPP_ELAST
Howard Hinnant155c2af2010-05-24 17:49:41 +000063 )
Howard Hinnantc51e1022010-05-11 19:42:16 +000064 return __do_message::message(ev);
65 return string("unspecified iostream_category error");
66}
67
68const error_category&
Marshall Clow3f138dc2013-10-12 22:49:56 +000069iostream_category() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000070{
71 static __iostream_category s;
72 return s;
73}
74
75// ios_base::failure
76
77ios_base::failure::failure(const string& msg, const error_code& ec)
78 : system_error(ec, msg)
79{
80}
81
82ios_base::failure::failure(const char* msg, const error_code& ec)
83 : system_error(ec, msg)
84{
85}
86
87ios_base::failure::~failure() throw()
88{
89}
90
91// ios_base locale
92
93const ios_base::fmtflags ios_base::boolalpha;
94const ios_base::fmtflags ios_base::dec;
95const ios_base::fmtflags ios_base::fixed;
96const ios_base::fmtflags ios_base::hex;
97const ios_base::fmtflags ios_base::internal;
98const ios_base::fmtflags ios_base::left;
99const ios_base::fmtflags ios_base::oct;
100const ios_base::fmtflags ios_base::right;
101const ios_base::fmtflags ios_base::scientific;
102const ios_base::fmtflags ios_base::showbase;
103const ios_base::fmtflags ios_base::showpoint;
104const ios_base::fmtflags ios_base::showpos;
105const ios_base::fmtflags ios_base::skipws;
106const ios_base::fmtflags ios_base::unitbuf;
107const ios_base::fmtflags ios_base::uppercase;
108const ios_base::fmtflags ios_base::adjustfield;
109const ios_base::fmtflags ios_base::basefield;
110const ios_base::fmtflags ios_base::floatfield;
111
112const ios_base::iostate ios_base::badbit;
113const ios_base::iostate ios_base::eofbit;
114const ios_base::iostate ios_base::failbit;
115const ios_base::iostate ios_base::goodbit;
116
117const ios_base::openmode ios_base::app;
118const ios_base::openmode ios_base::ate;
119const ios_base::openmode ios_base::binary;
120const ios_base::openmode ios_base::in;
121const ios_base::openmode ios_base::out;
122const ios_base::openmode ios_base::trunc;
123
124void
125ios_base::__call_callbacks(event ev)
126{
127 for (size_t i = __event_size_; i;)
128 {
129 --i;
130 __fn_[i](ev, *this, __index_[i]);
131 }
132}
133
134// locale
135
136locale
137ios_base::imbue(const locale& newloc)
138{
139 static_assert(sizeof(locale) == sizeof(__loc_), "");
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000140 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141 locale oldloc = loc_storage;
142 loc_storage = newloc;
143 __call_callbacks(imbue_event);
144 return oldloc;
145}
146
147locale
148ios_base::getloc() const
149{
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000150 const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151 return loc_storage;
152}
153
154// xalloc
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000155#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Marshall Clowf9698ab2013-10-12 19:13:52 +0000156atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
157#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158int ios_base::__xindex_ = 0;
Marshall Clowf9698ab2013-10-12 19:13:52 +0000159#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000161template <typename _Tp>
162static size_t __ios_new_cap(size_t __req_size, size_t __current_cap)
163{ // Precondition: __req_size > __current_cap
164 const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);
165 if (__req_size < mx/2)
166 return _VSTD::max(2 * __current_cap, __req_size);
167 else
168 return mx;
169}
170
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171int
172ios_base::xalloc()
173{
174 return __xindex_++;
175}
176
177long&
178ios_base::iword(int index)
179{
180 size_t req_size = static_cast<size_t>(index)+1;
181 if (req_size > __iarray_cap_)
182 {
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000183 size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);
184 long* iarray = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185 if (iarray == 0)
186 {
187 setstate(badbit);
188 static long error;
189 error = 0;
190 return error;
191 }
192 __iarray_ = iarray;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000193 for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 *p = 0;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000195 __iarray_cap_ = newcap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196 }
197 __iarray_size_ = max<size_t>(__iarray_size_, req_size);
198 return __iarray_[index];
199}
200
201void*&
202ios_base::pword(int index)
203{
204 size_t req_size = static_cast<size_t>(index)+1;
205 if (req_size > __parray_cap_)
206 {
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000207 size_t newcap = __ios_new_cap<void *>(req_size, __iarray_cap_);
208 void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void *)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209 if (parray == 0)
210 {
211 setstate(badbit);
212 static void* error;
213 error = 0;
214 return error;
215 }
216 __parray_ = parray;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000217 for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218 *p = 0;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000219 __parray_cap_ = newcap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220 }
221 __parray_size_ = max<size_t>(__parray_size_, req_size);
222 return __parray_[index];
223}
224
225// register_callback
226
227void
228ios_base::register_callback(event_callback fn, int index)
229{
230 size_t req_size = __event_size_ + 1;
231 if (req_size > __event_cap_)
232 {
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000233 size_t newcap = __ios_new_cap<event_callback>(req_size, __event_cap_);
234 event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 if (fns == 0)
236 setstate(badbit);
237 __fn_ = fns;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000238 int* indxs = static_cast<int *>(realloc(__index_, newcap * sizeof(int)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 if (indxs == 0)
240 setstate(badbit);
241 __index_ = indxs;
Marshall Clow6c9ddc22014-10-27 19:08:10 +0000242 __event_cap_ = newcap;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243 }
244 __fn_[__event_size_] = fn;
245 __index_[__event_size_] = index;
246 ++__event_size_;
247}
248
249ios_base::~ios_base()
250{
251 __call_callbacks(erase_event);
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000252 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000253 loc_storage.~locale();
254 free(__fn_);
255 free(__index_);
256 free(__iarray_);
257 free(__parray_);
258}
259
260// iostate
261
262void
263ios_base::clear(iostate state)
264{
265 if (__rdbuf_)
266 __rdstate_ = state;
267 else
268 __rdstate_ = state | badbit;
Louis Dionne2b239162019-02-12 16:06:02 +0000269
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270 if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
Louis Dionne2b239162019-02-12 16:06:02 +0000271 __throw_failure("ios_base::clear");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272}
273
274// init
275
276void
277ios_base::init(void* sb)
278{
279 __rdbuf_ = sb;
280 __rdstate_ = __rdbuf_ ? goodbit : badbit;
281 __exceptions_ = goodbit;
282 __fmtflags_ = skipws | dec;
283 __width_ = 0;
284 __precision_ = 6;
285 __fn_ = 0;
286 __index_ = 0;
287 __event_size_ = 0;
288 __event_cap_ = 0;
289 __iarray_ = 0;
290 __iarray_size_ = 0;
291 __iarray_cap_ = 0;
292 __parray_ = 0;
293 __parray_size_ = 0;
294 __parray_cap_ = 0;
295 ::new(&__loc_) locale;
296}
297
298void
299ios_base::copyfmt(const ios_base& rhs)
300{
301 // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
Alp Tokerb8a95f52014-05-15 11:27:39 +0000302 // Don't alter *this until all needed resources are acquired
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
304 unique_ptr<int, void (*)(void*)> new_ints(0, free);
305 unique_ptr<long, void (*)(void*)> new_longs(0, free);
306 unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
307 if (__event_cap_ < rhs.__event_size_)
308 {
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000309 size_t newesize = sizeof(event_callback) * rhs.__event_size_;
310 new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311 if (!new_callbacks)
Louis Dionne2b239162019-02-12 16:06:02 +0000312 __throw_bad_alloc();
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000313
314 size_t newisize = sizeof(int) * rhs.__event_size_;
315 new_ints.reset(static_cast<int *>(malloc(newisize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 if (!new_ints)
Louis Dionne2b239162019-02-12 16:06:02 +0000317 __throw_bad_alloc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318 }
319 if (__iarray_cap_ < rhs.__iarray_size_)
320 {
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000321 size_t newsize = sizeof(long) * rhs.__iarray_size_;
322 new_longs.reset(static_cast<long*>(malloc(newsize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323 if (!new_longs)
Louis Dionne2b239162019-02-12 16:06:02 +0000324 __throw_bad_alloc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325 }
326 if (__parray_cap_ < rhs.__parray_size_)
327 {
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000328 size_t newsize = sizeof(void*) * rhs.__parray_size_;
329 new_pointers.reset(static_cast<void**>(malloc(newsize)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330 if (!new_pointers)
Louis Dionne2b239162019-02-12 16:06:02 +0000331 __throw_bad_alloc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332 }
333 // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_
334 __fmtflags_ = rhs.__fmtflags_;
335 __precision_ = rhs.__precision_;
336 __width_ = rhs.__width_;
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000337 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
338 const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 lhs_loc = rhs_loc;
340 if (__event_cap_ < rhs.__event_size_)
341 {
342 free(__fn_);
343 __fn_ = new_callbacks.release();
344 free(__index_);
345 __index_ = new_ints.release();
346 __event_cap_ = rhs.__event_size_;
347 }
348 for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_)
349 {
350 __fn_[__event_size_] = rhs.__fn_[__event_size_];
351 __index_[__event_size_] = rhs.__index_[__event_size_];
352 }
353 if (__iarray_cap_ < rhs.__iarray_size_)
354 {
355 free(__iarray_);
356 __iarray_ = new_longs.release();
357 __iarray_cap_ = rhs.__iarray_size_;
358 }
359 for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
360 __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
361 if (__parray_cap_ < rhs.__parray_size_)
362 {
363 free(__parray_);
364 __parray_ = new_pointers.release();
365 __parray_cap_ = rhs.__parray_size_;
366 }
367 for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
368 __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
369}
370
371void
372ios_base::move(ios_base& rhs)
373{
374 // *this is uninitialized
375 __fmtflags_ = rhs.__fmtflags_;
376 __precision_ = rhs.__precision_;
377 __width_ = rhs.__width_;
378 __rdstate_ = rhs.__rdstate_;
379 __exceptions_ = rhs.__exceptions_;
380 __rdbuf_ = 0;
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000381 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382 ::new(&__loc_) locale(rhs_loc);
383 __fn_ = rhs.__fn_;
384 rhs.__fn_ = 0;
385 __index_ = rhs.__index_;
386 rhs.__index_ = 0;
387 __event_size_ = rhs.__event_size_;
388 rhs.__event_size_ = 0;
389 __event_cap_ = rhs.__event_cap_;
390 rhs.__event_cap_ = 0;
391 __iarray_ = rhs.__iarray_;
392 rhs.__iarray_ = 0;
393 __iarray_size_ = rhs.__iarray_size_;
394 rhs.__iarray_size_ = 0;
395 __iarray_cap_ = rhs.__iarray_cap_;
396 rhs.__iarray_cap_ = 0;
397 __parray_ = rhs.__parray_;
398 rhs.__parray_ = 0;
399 __parray_size_ = rhs.__parray_size_;
400 rhs.__parray_size_ = 0;
401 __parray_cap_ = rhs.__parray_cap_;
402 rhs.__parray_cap_ = 0;
403}
404
405void
Howard Hinnant770a3662012-07-21 01:03:40 +0000406ios_base::swap(ios_base& rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000408 _VSTD::swap(__fmtflags_, rhs.__fmtflags_);
409 _VSTD::swap(__precision_, rhs.__precision_);
410 _VSTD::swap(__width_, rhs.__width_);
411 _VSTD::swap(__rdstate_, rhs.__rdstate_);
412 _VSTD::swap(__exceptions_, rhs.__exceptions_);
Joerg Sonnenbergerc7655a22014-01-04 17:43:00 +0000413 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
414 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000415 _VSTD::swap(lhs_loc, rhs_loc);
416 _VSTD::swap(__fn_, rhs.__fn_);
417 _VSTD::swap(__index_, rhs.__index_);
418 _VSTD::swap(__event_size_, rhs.__event_size_);
419 _VSTD::swap(__event_cap_, rhs.__event_cap_);
420 _VSTD::swap(__iarray_, rhs.__iarray_);
421 _VSTD::swap(__iarray_size_, rhs.__iarray_size_);
422 _VSTD::swap(__iarray_cap_, rhs.__iarray_cap_);
423 _VSTD::swap(__parray_, rhs.__parray_);
424 _VSTD::swap(__parray_size_, rhs.__parray_size_);
425 _VSTD::swap(__parray_cap_, rhs.__parray_cap_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426}
427
428void
429ios_base::__set_badbit_and_consider_rethrow()
430{
431 __rdstate_ |= badbit;
Howard Hinnant72f73582010-08-11 17:04:31 +0000432#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 if (__exceptions_ & badbit)
434 throw;
Howard Hinnantffb308e2010-08-22 00:03:27 +0000435#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436}
437
438void
439ios_base::__set_failbit_and_consider_rethrow()
440{
441 __rdstate_ |= failbit;
Howard Hinnant72f73582010-08-11 17:04:31 +0000442#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 if (__exceptions_ & failbit)
444 throw;
Howard Hinnantffb308e2010-08-22 00:03:27 +0000445#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446}
447
448bool
449ios_base::sync_with_stdio(bool sync)
450{
451 static bool previous_state = true;
452 bool r = previous_state;
453 previous_state = sync;
454 return r;
455}
456
457_LIBCPP_END_NAMESPACE_STD