1 //===-------------------------- ios.cpp -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
21 _LIBCPP_BEGIN_NAMESPACE_STD
23 template class basic_ios<char>;
24 template class basic_ios<wchar_t>;
26 template class basic_streambuf<char>;
27 template class basic_streambuf<wchar_t>;
29 template class basic_istream<char>;
30 template class basic_istream<wchar_t>;
32 template class basic_ostream<char>;
33 template class basic_ostream<wchar_t>;
35 template class basic_iostream<char>;
37 class _LIBCPP_HIDDEN __iostream_category
41 virtual const char* name() const _NOEXCEPT;
42 virtual string message(int ev) const;
46 __iostream_category::name() const _NOEXCEPT
52 __iostream_category::message(int ev) const
54 if (ev != static_cast<int>(io_errc::stream)
57 #elif defined(__linux__)
61 return __do_message::message(ev);
62 return string("unspecified iostream_category error");
66 iostream_category() _NOEXCEPT
68 static __iostream_category s;
74 ios_base::failure::failure(const string& msg, const error_code& ec)
75 : system_error(ec, msg)
79 ios_base::failure::failure(const char* msg, const error_code& ec)
80 : system_error(ec, msg)
84 ios_base::failure::~failure() throw()
90 const ios_base::fmtflags ios_base::boolalpha;
91 const ios_base::fmtflags ios_base::dec;
92 const ios_base::fmtflags ios_base::fixed;
93 const ios_base::fmtflags ios_base::hex;
94 const ios_base::fmtflags ios_base::internal;
95 const ios_base::fmtflags ios_base::left;
96 const ios_base::fmtflags ios_base::oct;
97 const ios_base::fmtflags ios_base::right;
98 const ios_base::fmtflags ios_base::scientific;
99 const ios_base::fmtflags ios_base::showbase;
100 const ios_base::fmtflags ios_base::showpoint;
101 const ios_base::fmtflags ios_base::showpos;
102 const ios_base::fmtflags ios_base::skipws;
103 const ios_base::fmtflags ios_base::unitbuf;
104 const ios_base::fmtflags ios_base::uppercase;
105 const ios_base::fmtflags ios_base::adjustfield;
106 const ios_base::fmtflags ios_base::basefield;
107 const ios_base::fmtflags ios_base::floatfield;
109 const ios_base::iostate ios_base::badbit;
110 const ios_base::iostate ios_base::eofbit;
111 const ios_base::iostate ios_base::failbit;
112 const ios_base::iostate ios_base::goodbit;
114 const ios_base::openmode ios_base::app;
115 const ios_base::openmode ios_base::ate;
116 const ios_base::openmode ios_base::binary;
117 const ios_base::openmode ios_base::in;
118 const ios_base::openmode ios_base::out;
119 const ios_base::openmode ios_base::trunc;
122 ios_base::__call_callbacks(event ev)
124 for (size_t i = __event_size_; i;)
127 __fn_[i](ev, *this, __index_[i]);
134 ios_base::imbue(const locale& newloc)
136 static_assert(sizeof(locale) == sizeof(__loc_), "");
137 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
138 locale oldloc = loc_storage;
139 loc_storage = newloc;
140 __call_callbacks(imbue_event);
145 ios_base::getloc() const
147 const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
152 #if __has_feature(cxx_atomic)
153 atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
155 int ios_base::__xindex_ = 0;
165 ios_base::iword(int index)
167 size_t req_size = static_cast<size_t>(index)+1;
168 if (req_size > __iarray_cap_)
171 const size_t mx = std::numeric_limits<size_t>::max();
173 newcap = _VSTD::max(2 * __iarray_cap_, req_size);
176 size_t newsize = newcap * sizeof(long);
177 long* iarray = static_cast<long*>(realloc(__iarray_, newsize));
186 for (long* p = __iarray_ + __iarray_size_; __iarray_cap_ < newcap; ++__iarray_cap_, ++p)
189 __iarray_size_ = max<size_t>(__iarray_size_, req_size);
190 return __iarray_[index];
194 ios_base::pword(int index)
196 size_t req_size = static_cast<size_t>(index)+1;
197 if (req_size > __parray_cap_)
200 const size_t mx = std::numeric_limits<size_t>::max();
202 newcap = _VSTD::max(2 * __parray_cap_, req_size);
205 size_t newsize = newcap * sizeof(void*);
206 void** parray = static_cast<void**>(realloc(__parray_, newsize));
215 for (void** p = __parray_ + __parray_size_; __parray_cap_ < newcap; ++__parray_cap_, ++p)
218 __parray_size_ = max<size_t>(__parray_size_, req_size);
219 return __parray_[index];
225 ios_base::register_callback(event_callback fn, int index)
227 size_t req_size = __event_size_ + 1;
228 if (req_size > __event_cap_)
231 const size_t mx = std::numeric_limits<size_t>::max();
233 newcap = _VSTD::max(2 * __event_cap_, req_size);
236 size_t newesize = newcap * sizeof(event_callback);
237 event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newesize));
241 size_t newisize = newcap * sizeof(int);
242 int* indxs = static_cast<int *>(realloc(__index_, newisize));
247 __fn_[__event_size_] = fn;
248 __index_[__event_size_] = index;
252 ios_base::~ios_base()
254 __call_callbacks(erase_event);
255 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
256 loc_storage.~locale();
266 ios_base::clear(iostate state)
271 __rdstate_ = state | badbit;
272 #ifndef _LIBCPP_NO_EXCEPTIONS
273 if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
274 throw failure("ios_base::clear");
275 #endif // _LIBCPP_NO_EXCEPTIONS
281 ios_base::init(void* sb)
284 __rdstate_ = __rdbuf_ ? goodbit : badbit;
285 __exceptions_ = goodbit;
286 __fmtflags_ = skipws | dec;
299 ::new(&__loc_) locale;
303 ios_base::copyfmt(const ios_base& rhs)
305 // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
306 // Don't alter *this until all needed resources are acquired
307 unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
308 unique_ptr<int, void (*)(void*)> new_ints(0, free);
309 unique_ptr<long, void (*)(void*)> new_longs(0, free);
310 unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
311 if (__event_cap_ < rhs.__event_size_)
313 size_t newesize = sizeof(event_callback) * rhs.__event_size_;
314 new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
315 #ifndef _LIBCPP_NO_EXCEPTIONS
318 #endif // _LIBCPP_NO_EXCEPTIONS
320 size_t newisize = sizeof(int) * rhs.__event_size_;
321 new_ints.reset(static_cast<int *>(malloc(newisize)));
322 #ifndef _LIBCPP_NO_EXCEPTIONS
325 #endif // _LIBCPP_NO_EXCEPTIONS
327 if (__iarray_cap_ < rhs.__iarray_size_)
329 size_t newsize = sizeof(long) * rhs.__iarray_size_;
330 new_longs.reset(static_cast<long*>(malloc(newsize)));
331 #ifndef _LIBCPP_NO_EXCEPTIONS
334 #endif // _LIBCPP_NO_EXCEPTIONS
336 if (__parray_cap_ < rhs.__parray_size_)
338 size_t newsize = sizeof(void*) * rhs.__parray_size_;
339 new_pointers.reset(static_cast<void**>(malloc(newsize)));
340 #ifndef _LIBCPP_NO_EXCEPTIONS
343 #endif // _LIBCPP_NO_EXCEPTIONS
345 // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_
346 __fmtflags_ = rhs.__fmtflags_;
347 __precision_ = rhs.__precision_;
348 __width_ = rhs.__width_;
349 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
350 const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
352 if (__event_cap_ < rhs.__event_size_)
355 __fn_ = new_callbacks.release();
357 __index_ = new_ints.release();
358 __event_cap_ = rhs.__event_size_;
360 for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_)
362 __fn_[__event_size_] = rhs.__fn_[__event_size_];
363 __index_[__event_size_] = rhs.__index_[__event_size_];
365 if (__iarray_cap_ < rhs.__iarray_size_)
368 __iarray_ = new_longs.release();
369 __iarray_cap_ = rhs.__iarray_size_;
371 for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
372 __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
373 if (__parray_cap_ < rhs.__parray_size_)
376 __parray_ = new_pointers.release();
377 __parray_cap_ = rhs.__parray_size_;
379 for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
380 __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
384 ios_base::move(ios_base& rhs)
386 // *this is uninitialized
387 __fmtflags_ = rhs.__fmtflags_;
388 __precision_ = rhs.__precision_;
389 __width_ = rhs.__width_;
390 __rdstate_ = rhs.__rdstate_;
391 __exceptions_ = rhs.__exceptions_;
393 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
394 ::new(&__loc_) locale(rhs_loc);
397 __index_ = rhs.__index_;
399 __event_size_ = rhs.__event_size_;
400 rhs.__event_size_ = 0;
401 __event_cap_ = rhs.__event_cap_;
402 rhs.__event_cap_ = 0;
403 __iarray_ = rhs.__iarray_;
405 __iarray_size_ = rhs.__iarray_size_;
406 rhs.__iarray_size_ = 0;
407 __iarray_cap_ = rhs.__iarray_cap_;
408 rhs.__iarray_cap_ = 0;
409 __parray_ = rhs.__parray_;
411 __parray_size_ = rhs.__parray_size_;
412 rhs.__parray_size_ = 0;
413 __parray_cap_ = rhs.__parray_cap_;
414 rhs.__parray_cap_ = 0;
418 ios_base::swap(ios_base& rhs) _NOEXCEPT
420 _VSTD::swap(__fmtflags_, rhs.__fmtflags_);
421 _VSTD::swap(__precision_, rhs.__precision_);
422 _VSTD::swap(__width_, rhs.__width_);
423 _VSTD::swap(__rdstate_, rhs.__rdstate_);
424 _VSTD::swap(__exceptions_, rhs.__exceptions_);
425 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
426 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
427 _VSTD::swap(lhs_loc, rhs_loc);
428 _VSTD::swap(__fn_, rhs.__fn_);
429 _VSTD::swap(__index_, rhs.__index_);
430 _VSTD::swap(__event_size_, rhs.__event_size_);
431 _VSTD::swap(__event_cap_, rhs.__event_cap_);
432 _VSTD::swap(__iarray_, rhs.__iarray_);
433 _VSTD::swap(__iarray_size_, rhs.__iarray_size_);
434 _VSTD::swap(__iarray_cap_, rhs.__iarray_cap_);
435 _VSTD::swap(__parray_, rhs.__parray_);
436 _VSTD::swap(__parray_size_, rhs.__parray_size_);
437 _VSTD::swap(__parray_cap_, rhs.__parray_cap_);
441 ios_base::__set_badbit_and_consider_rethrow()
443 __rdstate_ |= badbit;
444 #ifndef _LIBCPP_NO_EXCEPTIONS
445 if (__exceptions_ & badbit)
447 #endif // _LIBCPP_NO_EXCEPTIONS
451 ios_base::__set_failbit_and_consider_rethrow()
453 __rdstate_ |= failbit;
454 #ifndef _LIBCPP_NO_EXCEPTIONS
455 if (__exceptions_ & failbit)
457 #endif // _LIBCPP_NO_EXCEPTIONS
461 ios_base::sync_with_stdio(bool sync)
463 static bool previous_state = true;
464 bool r = previous_state;
465 previous_state = sync;
469 _LIBCPP_END_NAMESPACE_STD