blob: 35cb030203c74fdb63a97e04ab4b6d8a7ffc7b81 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===-------------------------- ios.cpp -----------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Howard Hinnant04a2c712013-08-29 20:56:53 +000010#define _LIBCPP_EXTERN_TEMPLATE(...) extern template __VA_ARGS__;
11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000012#include "ios"
13#include "streambuf"
14#include "istream"
15#include "string"
16#include "__locale"
17#include "algorithm"
18#include "memory"
19#include "new"
20#include "limits"
21#include <stdlib.h>
22
23_LIBCPP_BEGIN_NAMESPACE_STD
24
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025template class basic_ios<char>;
26template class basic_ios<wchar_t>;
27
28template class basic_streambuf<char>;
29template class basic_streambuf<wchar_t>;
30
31template class basic_istream<char>;
32template class basic_istream<wchar_t>;
33
34template class basic_ostream<char>;
35template class basic_ostream<wchar_t>;
36
37template class basic_iostream<char>;
38
39class _LIBCPP_HIDDEN __iostream_category
40 : public __do_message
41{
42public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000043 virtual const char* name() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044 virtual string message(int ev) const;
45};
46
47const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +000048__iostream_category::name() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049{
50 return "iostream";
51}
52
53string
54__iostream_category::message(int ev) const
55{
Howard Hinnantadff4892010-05-24 17:49:41 +000056 if (ev != static_cast<int>(io_errc::stream)
57#ifdef ELAST
58 && ev <= ELAST
David Majnemer58ff10e2014-05-29 05:02:22 +000059#elif defined(__linux__)
60 && ev <= 4095
61#endif // ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +000062 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 return __do_message::message(ev);
64 return string("unspecified iostream_category error");
65}
66
67const error_category&
Marshall Clow61a84222013-10-12 22:49:56 +000068iostream_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069{
70 static __iostream_category s;
71 return s;
72}
73
74// ios_base::failure
75
76ios_base::failure::failure(const string& msg, const error_code& ec)
77 : system_error(ec, msg)
78{
79}
80
81ios_base::failure::failure(const char* msg, const error_code& ec)
82 : system_error(ec, msg)
83{
84}
85
86ios_base::failure::~failure() throw()
87{
88}
89
90// ios_base locale
91
92const ios_base::fmtflags ios_base::boolalpha;
93const ios_base::fmtflags ios_base::dec;
94const ios_base::fmtflags ios_base::fixed;
95const ios_base::fmtflags ios_base::hex;
96const ios_base::fmtflags ios_base::internal;
97const ios_base::fmtflags ios_base::left;
98const ios_base::fmtflags ios_base::oct;
99const ios_base::fmtflags ios_base::right;
100const ios_base::fmtflags ios_base::scientific;
101const ios_base::fmtflags ios_base::showbase;
102const ios_base::fmtflags ios_base::showpoint;
103const ios_base::fmtflags ios_base::showpos;
104const ios_base::fmtflags ios_base::skipws;
105const ios_base::fmtflags ios_base::unitbuf;
106const ios_base::fmtflags ios_base::uppercase;
107const ios_base::fmtflags ios_base::adjustfield;
108const ios_base::fmtflags ios_base::basefield;
109const ios_base::fmtflags ios_base::floatfield;
110
111const ios_base::iostate ios_base::badbit;
112const ios_base::iostate ios_base::eofbit;
113const ios_base::iostate ios_base::failbit;
114const ios_base::iostate ios_base::goodbit;
115
116const ios_base::openmode ios_base::app;
117const ios_base::openmode ios_base::ate;
118const ios_base::openmode ios_base::binary;
119const ios_base::openmode ios_base::in;
120const ios_base::openmode ios_base::out;
121const ios_base::openmode ios_base::trunc;
122
123void
124ios_base::__call_callbacks(event ev)
125{
126 for (size_t i = __event_size_; i;)
127 {
128 --i;
129 __fn_[i](ev, *this, __index_[i]);
130 }
131}
132
133// locale
134
135locale
136ios_base::imbue(const locale& newloc)
137{
138 static_assert(sizeof(locale) == sizeof(__loc_), "");
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000139 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000140 locale oldloc = loc_storage;
141 loc_storage = newloc;
142 __call_callbacks(imbue_event);
143 return oldloc;
144}
145
146locale
147ios_base::getloc() const
148{
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000149 const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150 return loc_storage;
151}
152
153// xalloc
Marshall Clow206ce1f2013-10-12 19:13:52 +0000154#if __has_feature(cxx_atomic)
155atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
156#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157int ios_base::__xindex_ = 0;
Marshall Clow206ce1f2013-10-12 19:13:52 +0000158#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159
160int
161ios_base::xalloc()
162{
163 return __xindex_++;
164}
165
166long&
167ios_base::iword(int index)
168{
169 size_t req_size = static_cast<size_t>(index)+1;
170 if (req_size > __iarray_cap_)
171 {
172 size_t newcap;
173 const size_t mx = std::numeric_limits<size_t>::max();
174 if (req_size < mx/2)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000175 newcap = _VSTD::max(2 * __iarray_cap_, req_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176 else
177 newcap = mx;
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000178 size_t newsize = newcap * sizeof(long);
179 long* iarray = static_cast<long*>(realloc(__iarray_, newsize));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 if (iarray == 0)
181 {
182 setstate(badbit);
183 static long error;
184 error = 0;
185 return error;
186 }
187 __iarray_ = iarray;
188 for (long* p = __iarray_ + __iarray_size_; __iarray_cap_ < newcap; ++__iarray_cap_, ++p)
189 *p = 0;
190 }
191 __iarray_size_ = max<size_t>(__iarray_size_, req_size);
192 return __iarray_[index];
193}
194
195void*&
196ios_base::pword(int index)
197{
198 size_t req_size = static_cast<size_t>(index)+1;
199 if (req_size > __parray_cap_)
200 {
201 size_t newcap;
202 const size_t mx = std::numeric_limits<size_t>::max();
203 if (req_size < mx/2)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000204 newcap = _VSTD::max(2 * __parray_cap_, req_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205 else
206 newcap = mx;
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000207 size_t newsize = newcap * sizeof(void*);
208 void** parray = static_cast<void**>(realloc(__parray_, newsize));
Howard Hinnantbc8d3f92010-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;
217 for (void** p = __parray_ + __parray_size_; __parray_cap_ < newcap; ++__parray_cap_, ++p)
218 *p = 0;
219 }
220 __parray_size_ = max<size_t>(__parray_size_, req_size);
221 return __parray_[index];
222}
223
224// register_callback
225
226void
227ios_base::register_callback(event_callback fn, int index)
228{
229 size_t req_size = __event_size_ + 1;
230 if (req_size > __event_cap_)
231 {
232 size_t newcap;
233 const size_t mx = std::numeric_limits<size_t>::max();
234 if (req_size < mx/2)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000235 newcap = _VSTD::max(2 * __event_cap_, req_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236 else
237 newcap = mx;
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000238 size_t newesize = newcap * sizeof(event_callback);
239 event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newesize));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240 if (fns == 0)
241 setstate(badbit);
242 __fn_ = fns;
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000243 size_t newisize = newcap * sizeof(int);
244 int* indxs = static_cast<int *>(realloc(__index_, newisize));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245 if (indxs == 0)
246 setstate(badbit);
247 __index_ = indxs;
248 }
249 __fn_[__event_size_] = fn;
250 __index_[__event_size_] = index;
251 ++__event_size_;
252}
253
254ios_base::~ios_base()
255{
256 __call_callbacks(erase_event);
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000257 locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258 loc_storage.~locale();
259 free(__fn_);
260 free(__index_);
261 free(__iarray_);
262 free(__parray_);
263}
264
265// iostate
266
267void
268ios_base::clear(iostate state)
269{
270 if (__rdbuf_)
271 __rdstate_ = state;
272 else
273 __rdstate_ = state | badbit;
Howard Hinnantd4444702010-08-11 17:04:31 +0000274#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275 if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
276 throw failure("ios_base::clear");
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000277#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278}
279
280// init
281
282void
283ios_base::init(void* sb)
284{
285 __rdbuf_ = sb;
286 __rdstate_ = __rdbuf_ ? goodbit : badbit;
287 __exceptions_ = goodbit;
288 __fmtflags_ = skipws | dec;
289 __width_ = 0;
290 __precision_ = 6;
291 __fn_ = 0;
292 __index_ = 0;
293 __event_size_ = 0;
294 __event_cap_ = 0;
295 __iarray_ = 0;
296 __iarray_size_ = 0;
297 __iarray_cap_ = 0;
298 __parray_ = 0;
299 __parray_size_ = 0;
300 __parray_cap_ = 0;
301 ::new(&__loc_) locale;
302}
303
304void
305ios_base::copyfmt(const ios_base& rhs)
306{
307 // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
308 // Don't alter *this until all needed resources are aquired
309 unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
310 unique_ptr<int, void (*)(void*)> new_ints(0, free);
311 unique_ptr<long, void (*)(void*)> new_longs(0, free);
312 unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
313 if (__event_cap_ < rhs.__event_size_)
314 {
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000315 size_t newesize = sizeof(event_callback) * rhs.__event_size_;
316 new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
Howard Hinnantd4444702010-08-11 17:04:31 +0000317#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000318 if (!new_callbacks)
319 throw bad_alloc();
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000320#endif // _LIBCPP_NO_EXCEPTIONS
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000321
322 size_t newisize = sizeof(int) * rhs.__event_size_;
323 new_ints.reset(static_cast<int *>(malloc(newisize)));
Howard Hinnantd4444702010-08-11 17:04:31 +0000324#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 if (!new_ints)
326 throw bad_alloc();
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000327#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328 }
329 if (__iarray_cap_ < rhs.__iarray_size_)
330 {
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000331 size_t newsize = sizeof(long) * rhs.__iarray_size_;
332 new_longs.reset(static_cast<long*>(malloc(newsize)));
Howard Hinnantd4444702010-08-11 17:04:31 +0000333#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 if (!new_longs)
335 throw bad_alloc();
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000336#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 }
338 if (__parray_cap_ < rhs.__parray_size_)
339 {
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000340 size_t newsize = sizeof(void*) * rhs.__parray_size_;
341 new_pointers.reset(static_cast<void**>(malloc(newsize)));
Howard Hinnantd4444702010-08-11 17:04:31 +0000342#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343 if (!new_pointers)
344 throw bad_alloc();
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000345#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346 }
347 // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_
348 __fmtflags_ = rhs.__fmtflags_;
349 __precision_ = rhs.__precision_;
350 __width_ = rhs.__width_;
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000351 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
352 const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353 lhs_loc = rhs_loc;
354 if (__event_cap_ < rhs.__event_size_)
355 {
356 free(__fn_);
357 __fn_ = new_callbacks.release();
358 free(__index_);
359 __index_ = new_ints.release();
360 __event_cap_ = rhs.__event_size_;
361 }
362 for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_)
363 {
364 __fn_[__event_size_] = rhs.__fn_[__event_size_];
365 __index_[__event_size_] = rhs.__index_[__event_size_];
366 }
367 if (__iarray_cap_ < rhs.__iarray_size_)
368 {
369 free(__iarray_);
370 __iarray_ = new_longs.release();
371 __iarray_cap_ = rhs.__iarray_size_;
372 }
373 for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
374 __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
375 if (__parray_cap_ < rhs.__parray_size_)
376 {
377 free(__parray_);
378 __parray_ = new_pointers.release();
379 __parray_cap_ = rhs.__parray_size_;
380 }
381 for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
382 __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
383}
384
385void
386ios_base::move(ios_base& rhs)
387{
388 // *this is uninitialized
389 __fmtflags_ = rhs.__fmtflags_;
390 __precision_ = rhs.__precision_;
391 __width_ = rhs.__width_;
392 __rdstate_ = rhs.__rdstate_;
393 __exceptions_ = rhs.__exceptions_;
394 __rdbuf_ = 0;
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000395 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396 ::new(&__loc_) locale(rhs_loc);
397 __fn_ = rhs.__fn_;
398 rhs.__fn_ = 0;
399 __index_ = rhs.__index_;
400 rhs.__index_ = 0;
401 __event_size_ = rhs.__event_size_;
402 rhs.__event_size_ = 0;
403 __event_cap_ = rhs.__event_cap_;
404 rhs.__event_cap_ = 0;
405 __iarray_ = rhs.__iarray_;
406 rhs.__iarray_ = 0;
407 __iarray_size_ = rhs.__iarray_size_;
408 rhs.__iarray_size_ = 0;
409 __iarray_cap_ = rhs.__iarray_cap_;
410 rhs.__iarray_cap_ = 0;
411 __parray_ = rhs.__parray_;
412 rhs.__parray_ = 0;
413 __parray_size_ = rhs.__parray_size_;
414 rhs.__parray_size_ = 0;
415 __parray_cap_ = rhs.__parray_cap_;
416 rhs.__parray_cap_ = 0;
417}
418
419void
Howard Hinnantf57bd562012-07-21 01:03:40 +0000420ios_base::swap(ios_base& rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000422 _VSTD::swap(__fmtflags_, rhs.__fmtflags_);
423 _VSTD::swap(__precision_, rhs.__precision_);
424 _VSTD::swap(__width_, rhs.__width_);
425 _VSTD::swap(__rdstate_, rhs.__rdstate_);
426 _VSTD::swap(__exceptions_, rhs.__exceptions_);
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000427 locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
428 locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000429 _VSTD::swap(lhs_loc, rhs_loc);
430 _VSTD::swap(__fn_, rhs.__fn_);
431 _VSTD::swap(__index_, rhs.__index_);
432 _VSTD::swap(__event_size_, rhs.__event_size_);
433 _VSTD::swap(__event_cap_, rhs.__event_cap_);
434 _VSTD::swap(__iarray_, rhs.__iarray_);
435 _VSTD::swap(__iarray_size_, rhs.__iarray_size_);
436 _VSTD::swap(__iarray_cap_, rhs.__iarray_cap_);
437 _VSTD::swap(__parray_, rhs.__parray_);
438 _VSTD::swap(__parray_size_, rhs.__parray_size_);
439 _VSTD::swap(__parray_cap_, rhs.__parray_cap_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440}
441
442void
443ios_base::__set_badbit_and_consider_rethrow()
444{
445 __rdstate_ |= badbit;
Howard Hinnantd4444702010-08-11 17:04:31 +0000446#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447 if (__exceptions_ & badbit)
448 throw;
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000449#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450}
451
452void
453ios_base::__set_failbit_and_consider_rethrow()
454{
455 __rdstate_ |= failbit;
Howard Hinnantd4444702010-08-11 17:04:31 +0000456#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 if (__exceptions_ & failbit)
458 throw;
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000459#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460}
461
462bool
463ios_base::sync_with_stdio(bool sync)
464{
465 static bool previous_state = true;
466 bool r = previous_state;
467 previous_state = sync;
468 return r;
469}
470
471_LIBCPP_END_NAMESPACE_STD