1*58b9f456SAndroid Build Coastguard Worker //===-------------------------- ios.cpp -----------------------------------===//
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*58b9f456SAndroid Build Coastguard Worker //
5*58b9f456SAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*58b9f456SAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*58b9f456SAndroid Build Coastguard Worker //
8*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*58b9f456SAndroid Build Coastguard Worker
10*58b9f456SAndroid Build Coastguard Worker #include "__config"
11*58b9f456SAndroid Build Coastguard Worker
12*58b9f456SAndroid Build Coastguard Worker #include "ios"
13*58b9f456SAndroid Build Coastguard Worker
14*58b9f456SAndroid Build Coastguard Worker #include <stdlib.h>
15*58b9f456SAndroid Build Coastguard Worker
16*58b9f456SAndroid Build Coastguard Worker #include "__locale"
17*58b9f456SAndroid Build Coastguard Worker #include "algorithm"
18*58b9f456SAndroid Build Coastguard Worker #include "include/config_elast.h"
19*58b9f456SAndroid Build Coastguard Worker #include "istream"
20*58b9f456SAndroid Build Coastguard Worker #include "limits"
21*58b9f456SAndroid Build Coastguard Worker #include "memory"
22*58b9f456SAndroid Build Coastguard Worker #include "new"
23*58b9f456SAndroid Build Coastguard Worker #include "streambuf"
24*58b9f456SAndroid Build Coastguard Worker #include "string"
25*58b9f456SAndroid Build Coastguard Worker #include "__undef_macros"
26*58b9f456SAndroid Build Coastguard Worker
27*58b9f456SAndroid Build Coastguard Worker _LIBCPP_BEGIN_NAMESPACE_STD
28*58b9f456SAndroid Build Coastguard Worker
29*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios<char>;
30*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ios<wchar_t>;
31*58b9f456SAndroid Build Coastguard Worker
32*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_streambuf<char>;
33*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_streambuf<wchar_t>;
34*58b9f456SAndroid Build Coastguard Worker
35*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_istream<char>;
36*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_istream<wchar_t>;
37*58b9f456SAndroid Build Coastguard Worker
38*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ostream<char>;
39*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ostream<wchar_t>;
40*58b9f456SAndroid Build Coastguard Worker
41*58b9f456SAndroid Build Coastguard Worker template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_iostream<char>;
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard Worker class _LIBCPP_HIDDEN __iostream_category
44*58b9f456SAndroid Build Coastguard Worker : public __do_message
45*58b9f456SAndroid Build Coastguard Worker {
46*58b9f456SAndroid Build Coastguard Worker public:
47*58b9f456SAndroid Build Coastguard Worker virtual const char* name() const _NOEXCEPT;
48*58b9f456SAndroid Build Coastguard Worker virtual string message(int ev) const;
49*58b9f456SAndroid Build Coastguard Worker };
50*58b9f456SAndroid Build Coastguard Worker
51*58b9f456SAndroid Build Coastguard Worker const char*
name() const52*58b9f456SAndroid Build Coastguard Worker __iostream_category::name() const _NOEXCEPT
53*58b9f456SAndroid Build Coastguard Worker {
54*58b9f456SAndroid Build Coastguard Worker return "iostream";
55*58b9f456SAndroid Build Coastguard Worker }
56*58b9f456SAndroid Build Coastguard Worker
57*58b9f456SAndroid Build Coastguard Worker string
message(int ev) const58*58b9f456SAndroid Build Coastguard Worker __iostream_category::message(int ev) const
59*58b9f456SAndroid Build Coastguard Worker {
60*58b9f456SAndroid Build Coastguard Worker if (ev != static_cast<int>(io_errc::stream)
61*58b9f456SAndroid Build Coastguard Worker #ifdef _LIBCPP_ELAST
62*58b9f456SAndroid Build Coastguard Worker && ev <= _LIBCPP_ELAST
63*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_ELAST
64*58b9f456SAndroid Build Coastguard Worker )
65*58b9f456SAndroid Build Coastguard Worker return __do_message::message(ev);
66*58b9f456SAndroid Build Coastguard Worker return string("unspecified iostream_category error");
67*58b9f456SAndroid Build Coastguard Worker }
68*58b9f456SAndroid Build Coastguard Worker
69*58b9f456SAndroid Build Coastguard Worker const error_category&
iostream_category()70*58b9f456SAndroid Build Coastguard Worker iostream_category() _NOEXCEPT
71*58b9f456SAndroid Build Coastguard Worker {
72*58b9f456SAndroid Build Coastguard Worker static __iostream_category s;
73*58b9f456SAndroid Build Coastguard Worker return s;
74*58b9f456SAndroid Build Coastguard Worker }
75*58b9f456SAndroid Build Coastguard Worker
76*58b9f456SAndroid Build Coastguard Worker // ios_base::failure
77*58b9f456SAndroid Build Coastguard Worker
failure(const string & msg,const error_code & ec)78*58b9f456SAndroid Build Coastguard Worker ios_base::failure::failure(const string& msg, const error_code& ec)
79*58b9f456SAndroid Build Coastguard Worker : system_error(ec, msg)
80*58b9f456SAndroid Build Coastguard Worker {
81*58b9f456SAndroid Build Coastguard Worker }
82*58b9f456SAndroid Build Coastguard Worker
failure(const char * msg,const error_code & ec)83*58b9f456SAndroid Build Coastguard Worker ios_base::failure::failure(const char* msg, const error_code& ec)
84*58b9f456SAndroid Build Coastguard Worker : system_error(ec, msg)
85*58b9f456SAndroid Build Coastguard Worker {
86*58b9f456SAndroid Build Coastguard Worker }
87*58b9f456SAndroid Build Coastguard Worker
~failure()88*58b9f456SAndroid Build Coastguard Worker ios_base::failure::~failure() throw()
89*58b9f456SAndroid Build Coastguard Worker {
90*58b9f456SAndroid Build Coastguard Worker }
91*58b9f456SAndroid Build Coastguard Worker
92*58b9f456SAndroid Build Coastguard Worker // ios_base locale
93*58b9f456SAndroid Build Coastguard Worker
94*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::boolalpha;
95*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::dec;
96*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::fixed;
97*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::hex;
98*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::internal;
99*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::left;
100*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::oct;
101*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::right;
102*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::scientific;
103*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::showbase;
104*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::showpoint;
105*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::showpos;
106*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::skipws;
107*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::unitbuf;
108*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::uppercase;
109*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::adjustfield;
110*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::basefield;
111*58b9f456SAndroid Build Coastguard Worker const ios_base::fmtflags ios_base::floatfield;
112*58b9f456SAndroid Build Coastguard Worker
113*58b9f456SAndroid Build Coastguard Worker const ios_base::iostate ios_base::badbit;
114*58b9f456SAndroid Build Coastguard Worker const ios_base::iostate ios_base::eofbit;
115*58b9f456SAndroid Build Coastguard Worker const ios_base::iostate ios_base::failbit;
116*58b9f456SAndroid Build Coastguard Worker const ios_base::iostate ios_base::goodbit;
117*58b9f456SAndroid Build Coastguard Worker
118*58b9f456SAndroid Build Coastguard Worker const ios_base::openmode ios_base::app;
119*58b9f456SAndroid Build Coastguard Worker const ios_base::openmode ios_base::ate;
120*58b9f456SAndroid Build Coastguard Worker const ios_base::openmode ios_base::binary;
121*58b9f456SAndroid Build Coastguard Worker const ios_base::openmode ios_base::in;
122*58b9f456SAndroid Build Coastguard Worker const ios_base::openmode ios_base::out;
123*58b9f456SAndroid Build Coastguard Worker const ios_base::openmode ios_base::trunc;
124*58b9f456SAndroid Build Coastguard Worker
125*58b9f456SAndroid Build Coastguard Worker void
__call_callbacks(event ev)126*58b9f456SAndroid Build Coastguard Worker ios_base::__call_callbacks(event ev)
127*58b9f456SAndroid Build Coastguard Worker {
128*58b9f456SAndroid Build Coastguard Worker for (size_t i = __event_size_; i;)
129*58b9f456SAndroid Build Coastguard Worker {
130*58b9f456SAndroid Build Coastguard Worker --i;
131*58b9f456SAndroid Build Coastguard Worker __fn_[i](ev, *this, __index_[i]);
132*58b9f456SAndroid Build Coastguard Worker }
133*58b9f456SAndroid Build Coastguard Worker }
134*58b9f456SAndroid Build Coastguard Worker
135*58b9f456SAndroid Build Coastguard Worker // locale
136*58b9f456SAndroid Build Coastguard Worker
137*58b9f456SAndroid Build Coastguard Worker locale
imbue(const locale & newloc)138*58b9f456SAndroid Build Coastguard Worker ios_base::imbue(const locale& newloc)
139*58b9f456SAndroid Build Coastguard Worker {
140*58b9f456SAndroid Build Coastguard Worker static_assert(sizeof(locale) == sizeof(__loc_), "");
141*58b9f456SAndroid Build Coastguard Worker locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
142*58b9f456SAndroid Build Coastguard Worker locale oldloc = loc_storage;
143*58b9f456SAndroid Build Coastguard Worker loc_storage = newloc;
144*58b9f456SAndroid Build Coastguard Worker __call_callbacks(imbue_event);
145*58b9f456SAndroid Build Coastguard Worker return oldloc;
146*58b9f456SAndroid Build Coastguard Worker }
147*58b9f456SAndroid Build Coastguard Worker
148*58b9f456SAndroid Build Coastguard Worker locale
getloc() const149*58b9f456SAndroid Build Coastguard Worker ios_base::getloc() const
150*58b9f456SAndroid Build Coastguard Worker {
151*58b9f456SAndroid Build Coastguard Worker const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
152*58b9f456SAndroid Build Coastguard Worker return loc_storage;
153*58b9f456SAndroid Build Coastguard Worker }
154*58b9f456SAndroid Build Coastguard Worker
155*58b9f456SAndroid Build Coastguard Worker // xalloc
156*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
157*58b9f456SAndroid Build Coastguard Worker atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
158*58b9f456SAndroid Build Coastguard Worker #else
159*58b9f456SAndroid Build Coastguard Worker int ios_base::__xindex_ = 0;
160*58b9f456SAndroid Build Coastguard Worker #endif
161*58b9f456SAndroid Build Coastguard Worker
162*58b9f456SAndroid Build Coastguard Worker template <typename _Tp>
__ios_new_cap(size_t __req_size,size_t __current_cap)163*58b9f456SAndroid Build Coastguard Worker static size_t __ios_new_cap(size_t __req_size, size_t __current_cap)
164*58b9f456SAndroid Build Coastguard Worker { // Precondition: __req_size > __current_cap
165*58b9f456SAndroid Build Coastguard Worker const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);
166*58b9f456SAndroid Build Coastguard Worker if (__req_size < mx/2)
167*58b9f456SAndroid Build Coastguard Worker return _VSTD::max(2 * __current_cap, __req_size);
168*58b9f456SAndroid Build Coastguard Worker else
169*58b9f456SAndroid Build Coastguard Worker return mx;
170*58b9f456SAndroid Build Coastguard Worker }
171*58b9f456SAndroid Build Coastguard Worker
172*58b9f456SAndroid Build Coastguard Worker int
xalloc()173*58b9f456SAndroid Build Coastguard Worker ios_base::xalloc()
174*58b9f456SAndroid Build Coastguard Worker {
175*58b9f456SAndroid Build Coastguard Worker return __xindex_++;
176*58b9f456SAndroid Build Coastguard Worker }
177*58b9f456SAndroid Build Coastguard Worker
178*58b9f456SAndroid Build Coastguard Worker long&
iword(int index)179*58b9f456SAndroid Build Coastguard Worker ios_base::iword(int index)
180*58b9f456SAndroid Build Coastguard Worker {
181*58b9f456SAndroid Build Coastguard Worker size_t req_size = static_cast<size_t>(index)+1;
182*58b9f456SAndroid Build Coastguard Worker if (req_size > __iarray_cap_)
183*58b9f456SAndroid Build Coastguard Worker {
184*58b9f456SAndroid Build Coastguard Worker size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);
185*58b9f456SAndroid Build Coastguard Worker long* iarray = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));
186*58b9f456SAndroid Build Coastguard Worker if (iarray == 0)
187*58b9f456SAndroid Build Coastguard Worker {
188*58b9f456SAndroid Build Coastguard Worker setstate(badbit);
189*58b9f456SAndroid Build Coastguard Worker static long error;
190*58b9f456SAndroid Build Coastguard Worker error = 0;
191*58b9f456SAndroid Build Coastguard Worker return error;
192*58b9f456SAndroid Build Coastguard Worker }
193*58b9f456SAndroid Build Coastguard Worker __iarray_ = iarray;
194*58b9f456SAndroid Build Coastguard Worker for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)
195*58b9f456SAndroid Build Coastguard Worker *p = 0;
196*58b9f456SAndroid Build Coastguard Worker __iarray_cap_ = newcap;
197*58b9f456SAndroid Build Coastguard Worker }
198*58b9f456SAndroid Build Coastguard Worker __iarray_size_ = max<size_t>(__iarray_size_, req_size);
199*58b9f456SAndroid Build Coastguard Worker return __iarray_[index];
200*58b9f456SAndroid Build Coastguard Worker }
201*58b9f456SAndroid Build Coastguard Worker
202*58b9f456SAndroid Build Coastguard Worker void*&
pword(int index)203*58b9f456SAndroid Build Coastguard Worker ios_base::pword(int index)
204*58b9f456SAndroid Build Coastguard Worker {
205*58b9f456SAndroid Build Coastguard Worker size_t req_size = static_cast<size_t>(index)+1;
206*58b9f456SAndroid Build Coastguard Worker if (req_size > __parray_cap_)
207*58b9f456SAndroid Build Coastguard Worker {
208*58b9f456SAndroid Build Coastguard Worker size_t newcap = __ios_new_cap<void *>(req_size, __iarray_cap_);
209*58b9f456SAndroid Build Coastguard Worker void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void *)));
210*58b9f456SAndroid Build Coastguard Worker if (parray == 0)
211*58b9f456SAndroid Build Coastguard Worker {
212*58b9f456SAndroid Build Coastguard Worker setstate(badbit);
213*58b9f456SAndroid Build Coastguard Worker static void* error;
214*58b9f456SAndroid Build Coastguard Worker error = 0;
215*58b9f456SAndroid Build Coastguard Worker return error;
216*58b9f456SAndroid Build Coastguard Worker }
217*58b9f456SAndroid Build Coastguard Worker __parray_ = parray;
218*58b9f456SAndroid Build Coastguard Worker for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)
219*58b9f456SAndroid Build Coastguard Worker *p = 0;
220*58b9f456SAndroid Build Coastguard Worker __parray_cap_ = newcap;
221*58b9f456SAndroid Build Coastguard Worker }
222*58b9f456SAndroid Build Coastguard Worker __parray_size_ = max<size_t>(__parray_size_, req_size);
223*58b9f456SAndroid Build Coastguard Worker return __parray_[index];
224*58b9f456SAndroid Build Coastguard Worker }
225*58b9f456SAndroid Build Coastguard Worker
226*58b9f456SAndroid Build Coastguard Worker // register_callback
227*58b9f456SAndroid Build Coastguard Worker
228*58b9f456SAndroid Build Coastguard Worker void
register_callback(event_callback fn,int index)229*58b9f456SAndroid Build Coastguard Worker ios_base::register_callback(event_callback fn, int index)
230*58b9f456SAndroid Build Coastguard Worker {
231*58b9f456SAndroid Build Coastguard Worker size_t req_size = __event_size_ + 1;
232*58b9f456SAndroid Build Coastguard Worker if (req_size > __event_cap_)
233*58b9f456SAndroid Build Coastguard Worker {
234*58b9f456SAndroid Build Coastguard Worker size_t newcap = __ios_new_cap<event_callback>(req_size, __event_cap_);
235*58b9f456SAndroid Build Coastguard Worker event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));
236*58b9f456SAndroid Build Coastguard Worker if (fns == 0)
237*58b9f456SAndroid Build Coastguard Worker setstate(badbit);
238*58b9f456SAndroid Build Coastguard Worker __fn_ = fns;
239*58b9f456SAndroid Build Coastguard Worker int* indxs = static_cast<int *>(realloc(__index_, newcap * sizeof(int)));
240*58b9f456SAndroid Build Coastguard Worker if (indxs == 0)
241*58b9f456SAndroid Build Coastguard Worker setstate(badbit);
242*58b9f456SAndroid Build Coastguard Worker __index_ = indxs;
243*58b9f456SAndroid Build Coastguard Worker __event_cap_ = newcap;
244*58b9f456SAndroid Build Coastguard Worker }
245*58b9f456SAndroid Build Coastguard Worker __fn_[__event_size_] = fn;
246*58b9f456SAndroid Build Coastguard Worker __index_[__event_size_] = index;
247*58b9f456SAndroid Build Coastguard Worker ++__event_size_;
248*58b9f456SAndroid Build Coastguard Worker }
249*58b9f456SAndroid Build Coastguard Worker
~ios_base()250*58b9f456SAndroid Build Coastguard Worker ios_base::~ios_base()
251*58b9f456SAndroid Build Coastguard Worker {
252*58b9f456SAndroid Build Coastguard Worker __call_callbacks(erase_event);
253*58b9f456SAndroid Build Coastguard Worker locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
254*58b9f456SAndroid Build Coastguard Worker loc_storage.~locale();
255*58b9f456SAndroid Build Coastguard Worker free(__fn_);
256*58b9f456SAndroid Build Coastguard Worker free(__index_);
257*58b9f456SAndroid Build Coastguard Worker free(__iarray_);
258*58b9f456SAndroid Build Coastguard Worker free(__parray_);
259*58b9f456SAndroid Build Coastguard Worker }
260*58b9f456SAndroid Build Coastguard Worker
261*58b9f456SAndroid Build Coastguard Worker // iostate
262*58b9f456SAndroid Build Coastguard Worker
263*58b9f456SAndroid Build Coastguard Worker void
clear(iostate state)264*58b9f456SAndroid Build Coastguard Worker ios_base::clear(iostate state)
265*58b9f456SAndroid Build Coastguard Worker {
266*58b9f456SAndroid Build Coastguard Worker if (__rdbuf_)
267*58b9f456SAndroid Build Coastguard Worker __rdstate_ = state;
268*58b9f456SAndroid Build Coastguard Worker else
269*58b9f456SAndroid Build Coastguard Worker __rdstate_ = state | badbit;
270*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_NO_EXCEPTIONS
271*58b9f456SAndroid Build Coastguard Worker if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
272*58b9f456SAndroid Build Coastguard Worker throw failure("ios_base::clear");
273*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_NO_EXCEPTIONS
274*58b9f456SAndroid Build Coastguard Worker }
275*58b9f456SAndroid Build Coastguard Worker
276*58b9f456SAndroid Build Coastguard Worker // init
277*58b9f456SAndroid Build Coastguard Worker
278*58b9f456SAndroid Build Coastguard Worker void
init(void * sb)279*58b9f456SAndroid Build Coastguard Worker ios_base::init(void* sb)
280*58b9f456SAndroid Build Coastguard Worker {
281*58b9f456SAndroid Build Coastguard Worker __rdbuf_ = sb;
282*58b9f456SAndroid Build Coastguard Worker __rdstate_ = __rdbuf_ ? goodbit : badbit;
283*58b9f456SAndroid Build Coastguard Worker __exceptions_ = goodbit;
284*58b9f456SAndroid Build Coastguard Worker __fmtflags_ = skipws | dec;
285*58b9f456SAndroid Build Coastguard Worker __width_ = 0;
286*58b9f456SAndroid Build Coastguard Worker __precision_ = 6;
287*58b9f456SAndroid Build Coastguard Worker __fn_ = 0;
288*58b9f456SAndroid Build Coastguard Worker __index_ = 0;
289*58b9f456SAndroid Build Coastguard Worker __event_size_ = 0;
290*58b9f456SAndroid Build Coastguard Worker __event_cap_ = 0;
291*58b9f456SAndroid Build Coastguard Worker __iarray_ = 0;
292*58b9f456SAndroid Build Coastguard Worker __iarray_size_ = 0;
293*58b9f456SAndroid Build Coastguard Worker __iarray_cap_ = 0;
294*58b9f456SAndroid Build Coastguard Worker __parray_ = 0;
295*58b9f456SAndroid Build Coastguard Worker __parray_size_ = 0;
296*58b9f456SAndroid Build Coastguard Worker __parray_cap_ = 0;
297*58b9f456SAndroid Build Coastguard Worker ::new(&__loc_) locale;
298*58b9f456SAndroid Build Coastguard Worker }
299*58b9f456SAndroid Build Coastguard Worker
300*58b9f456SAndroid Build Coastguard Worker void
copyfmt(const ios_base & rhs)301*58b9f456SAndroid Build Coastguard Worker ios_base::copyfmt(const ios_base& rhs)
302*58b9f456SAndroid Build Coastguard Worker {
303*58b9f456SAndroid Build Coastguard Worker // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
304*58b9f456SAndroid Build Coastguard Worker // Don't alter *this until all needed resources are acquired
305*58b9f456SAndroid Build Coastguard Worker unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
306*58b9f456SAndroid Build Coastguard Worker unique_ptr<int, void (*)(void*)> new_ints(0, free);
307*58b9f456SAndroid Build Coastguard Worker unique_ptr<long, void (*)(void*)> new_longs(0, free);
308*58b9f456SAndroid Build Coastguard Worker unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
309*58b9f456SAndroid Build Coastguard Worker if (__event_cap_ < rhs.__event_size_)
310*58b9f456SAndroid Build Coastguard Worker {
311*58b9f456SAndroid Build Coastguard Worker size_t newesize = sizeof(event_callback) * rhs.__event_size_;
312*58b9f456SAndroid Build Coastguard Worker new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
313*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_NO_EXCEPTIONS
314*58b9f456SAndroid Build Coastguard Worker if (!new_callbacks)
315*58b9f456SAndroid Build Coastguard Worker throw bad_alloc();
316*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_NO_EXCEPTIONS
317*58b9f456SAndroid Build Coastguard Worker
318*58b9f456SAndroid Build Coastguard Worker size_t newisize = sizeof(int) * rhs.__event_size_;
319*58b9f456SAndroid Build Coastguard Worker new_ints.reset(static_cast<int *>(malloc(newisize)));
320*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_NO_EXCEPTIONS
321*58b9f456SAndroid Build Coastguard Worker if (!new_ints)
322*58b9f456SAndroid Build Coastguard Worker throw bad_alloc();
323*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_NO_EXCEPTIONS
324*58b9f456SAndroid Build Coastguard Worker }
325*58b9f456SAndroid Build Coastguard Worker if (__iarray_cap_ < rhs.__iarray_size_)
326*58b9f456SAndroid Build Coastguard Worker {
327*58b9f456SAndroid Build Coastguard Worker size_t newsize = sizeof(long) * rhs.__iarray_size_;
328*58b9f456SAndroid Build Coastguard Worker new_longs.reset(static_cast<long*>(malloc(newsize)));
329*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_NO_EXCEPTIONS
330*58b9f456SAndroid Build Coastguard Worker if (!new_longs)
331*58b9f456SAndroid Build Coastguard Worker throw bad_alloc();
332*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_NO_EXCEPTIONS
333*58b9f456SAndroid Build Coastguard Worker }
334*58b9f456SAndroid Build Coastguard Worker if (__parray_cap_ < rhs.__parray_size_)
335*58b9f456SAndroid Build Coastguard Worker {
336*58b9f456SAndroid Build Coastguard Worker size_t newsize = sizeof(void*) * rhs.__parray_size_;
337*58b9f456SAndroid Build Coastguard Worker new_pointers.reset(static_cast<void**>(malloc(newsize)));
338*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_NO_EXCEPTIONS
339*58b9f456SAndroid Build Coastguard Worker if (!new_pointers)
340*58b9f456SAndroid Build Coastguard Worker throw bad_alloc();
341*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_NO_EXCEPTIONS
342*58b9f456SAndroid Build Coastguard Worker }
343*58b9f456SAndroid Build Coastguard Worker // Got everything we need. Copy everything but __rdstate_, __rdbuf_ and __exceptions_
344*58b9f456SAndroid Build Coastguard Worker __fmtflags_ = rhs.__fmtflags_;
345*58b9f456SAndroid Build Coastguard Worker __precision_ = rhs.__precision_;
346*58b9f456SAndroid Build Coastguard Worker __width_ = rhs.__width_;
347*58b9f456SAndroid Build Coastguard Worker locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
348*58b9f456SAndroid Build Coastguard Worker const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
349*58b9f456SAndroid Build Coastguard Worker lhs_loc = rhs_loc;
350*58b9f456SAndroid Build Coastguard Worker if (__event_cap_ < rhs.__event_size_)
351*58b9f456SAndroid Build Coastguard Worker {
352*58b9f456SAndroid Build Coastguard Worker free(__fn_);
353*58b9f456SAndroid Build Coastguard Worker __fn_ = new_callbacks.release();
354*58b9f456SAndroid Build Coastguard Worker free(__index_);
355*58b9f456SAndroid Build Coastguard Worker __index_ = new_ints.release();
356*58b9f456SAndroid Build Coastguard Worker __event_cap_ = rhs.__event_size_;
357*58b9f456SAndroid Build Coastguard Worker }
358*58b9f456SAndroid Build Coastguard Worker for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_)
359*58b9f456SAndroid Build Coastguard Worker {
360*58b9f456SAndroid Build Coastguard Worker __fn_[__event_size_] = rhs.__fn_[__event_size_];
361*58b9f456SAndroid Build Coastguard Worker __index_[__event_size_] = rhs.__index_[__event_size_];
362*58b9f456SAndroid Build Coastguard Worker }
363*58b9f456SAndroid Build Coastguard Worker if (__iarray_cap_ < rhs.__iarray_size_)
364*58b9f456SAndroid Build Coastguard Worker {
365*58b9f456SAndroid Build Coastguard Worker free(__iarray_);
366*58b9f456SAndroid Build Coastguard Worker __iarray_ = new_longs.release();
367*58b9f456SAndroid Build Coastguard Worker __iarray_cap_ = rhs.__iarray_size_;
368*58b9f456SAndroid Build Coastguard Worker }
369*58b9f456SAndroid Build Coastguard Worker for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
370*58b9f456SAndroid Build Coastguard Worker __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
371*58b9f456SAndroid Build Coastguard Worker if (__parray_cap_ < rhs.__parray_size_)
372*58b9f456SAndroid Build Coastguard Worker {
373*58b9f456SAndroid Build Coastguard Worker free(__parray_);
374*58b9f456SAndroid Build Coastguard Worker __parray_ = new_pointers.release();
375*58b9f456SAndroid Build Coastguard Worker __parray_cap_ = rhs.__parray_size_;
376*58b9f456SAndroid Build Coastguard Worker }
377*58b9f456SAndroid Build Coastguard Worker for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
378*58b9f456SAndroid Build Coastguard Worker __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
379*58b9f456SAndroid Build Coastguard Worker }
380*58b9f456SAndroid Build Coastguard Worker
381*58b9f456SAndroid Build Coastguard Worker void
move(ios_base & rhs)382*58b9f456SAndroid Build Coastguard Worker ios_base::move(ios_base& rhs)
383*58b9f456SAndroid Build Coastguard Worker {
384*58b9f456SAndroid Build Coastguard Worker // *this is uninitialized
385*58b9f456SAndroid Build Coastguard Worker __fmtflags_ = rhs.__fmtflags_;
386*58b9f456SAndroid Build Coastguard Worker __precision_ = rhs.__precision_;
387*58b9f456SAndroid Build Coastguard Worker __width_ = rhs.__width_;
388*58b9f456SAndroid Build Coastguard Worker __rdstate_ = rhs.__rdstate_;
389*58b9f456SAndroid Build Coastguard Worker __exceptions_ = rhs.__exceptions_;
390*58b9f456SAndroid Build Coastguard Worker __rdbuf_ = 0;
391*58b9f456SAndroid Build Coastguard Worker locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
392*58b9f456SAndroid Build Coastguard Worker ::new(&__loc_) locale(rhs_loc);
393*58b9f456SAndroid Build Coastguard Worker __fn_ = rhs.__fn_;
394*58b9f456SAndroid Build Coastguard Worker rhs.__fn_ = 0;
395*58b9f456SAndroid Build Coastguard Worker __index_ = rhs.__index_;
396*58b9f456SAndroid Build Coastguard Worker rhs.__index_ = 0;
397*58b9f456SAndroid Build Coastguard Worker __event_size_ = rhs.__event_size_;
398*58b9f456SAndroid Build Coastguard Worker rhs.__event_size_ = 0;
399*58b9f456SAndroid Build Coastguard Worker __event_cap_ = rhs.__event_cap_;
400*58b9f456SAndroid Build Coastguard Worker rhs.__event_cap_ = 0;
401*58b9f456SAndroid Build Coastguard Worker __iarray_ = rhs.__iarray_;
402*58b9f456SAndroid Build Coastguard Worker rhs.__iarray_ = 0;
403*58b9f456SAndroid Build Coastguard Worker __iarray_size_ = rhs.__iarray_size_;
404*58b9f456SAndroid Build Coastguard Worker rhs.__iarray_size_ = 0;
405*58b9f456SAndroid Build Coastguard Worker __iarray_cap_ = rhs.__iarray_cap_;
406*58b9f456SAndroid Build Coastguard Worker rhs.__iarray_cap_ = 0;
407*58b9f456SAndroid Build Coastguard Worker __parray_ = rhs.__parray_;
408*58b9f456SAndroid Build Coastguard Worker rhs.__parray_ = 0;
409*58b9f456SAndroid Build Coastguard Worker __parray_size_ = rhs.__parray_size_;
410*58b9f456SAndroid Build Coastguard Worker rhs.__parray_size_ = 0;
411*58b9f456SAndroid Build Coastguard Worker __parray_cap_ = rhs.__parray_cap_;
412*58b9f456SAndroid Build Coastguard Worker rhs.__parray_cap_ = 0;
413*58b9f456SAndroid Build Coastguard Worker }
414*58b9f456SAndroid Build Coastguard Worker
415*58b9f456SAndroid Build Coastguard Worker void
swap(ios_base & rhs)416*58b9f456SAndroid Build Coastguard Worker ios_base::swap(ios_base& rhs) _NOEXCEPT
417*58b9f456SAndroid Build Coastguard Worker {
418*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__fmtflags_, rhs.__fmtflags_);
419*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__precision_, rhs.__precision_);
420*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__width_, rhs.__width_);
421*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__rdstate_, rhs.__rdstate_);
422*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__exceptions_, rhs.__exceptions_);
423*58b9f456SAndroid Build Coastguard Worker locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
424*58b9f456SAndroid Build Coastguard Worker locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
425*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(lhs_loc, rhs_loc);
426*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__fn_, rhs.__fn_);
427*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__index_, rhs.__index_);
428*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__event_size_, rhs.__event_size_);
429*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__event_cap_, rhs.__event_cap_);
430*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__iarray_, rhs.__iarray_);
431*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__iarray_size_, rhs.__iarray_size_);
432*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__iarray_cap_, rhs.__iarray_cap_);
433*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__parray_, rhs.__parray_);
434*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__parray_size_, rhs.__parray_size_);
435*58b9f456SAndroid Build Coastguard Worker _VSTD::swap(__parray_cap_, rhs.__parray_cap_);
436*58b9f456SAndroid Build Coastguard Worker }
437*58b9f456SAndroid Build Coastguard Worker
438*58b9f456SAndroid Build Coastguard Worker void
__set_badbit_and_consider_rethrow()439*58b9f456SAndroid Build Coastguard Worker ios_base::__set_badbit_and_consider_rethrow()
440*58b9f456SAndroid Build Coastguard Worker {
441*58b9f456SAndroid Build Coastguard Worker __rdstate_ |= badbit;
442*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_NO_EXCEPTIONS
443*58b9f456SAndroid Build Coastguard Worker if (__exceptions_ & badbit)
444*58b9f456SAndroid Build Coastguard Worker throw;
445*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_NO_EXCEPTIONS
446*58b9f456SAndroid Build Coastguard Worker }
447*58b9f456SAndroid Build Coastguard Worker
448*58b9f456SAndroid Build Coastguard Worker void
__set_failbit_and_consider_rethrow()449*58b9f456SAndroid Build Coastguard Worker ios_base::__set_failbit_and_consider_rethrow()
450*58b9f456SAndroid Build Coastguard Worker {
451*58b9f456SAndroid Build Coastguard Worker __rdstate_ |= failbit;
452*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_NO_EXCEPTIONS
453*58b9f456SAndroid Build Coastguard Worker if (__exceptions_ & failbit)
454*58b9f456SAndroid Build Coastguard Worker throw;
455*58b9f456SAndroid Build Coastguard Worker #endif // _LIBCPP_NO_EXCEPTIONS
456*58b9f456SAndroid Build Coastguard Worker }
457*58b9f456SAndroid Build Coastguard Worker
458*58b9f456SAndroid Build Coastguard Worker bool
sync_with_stdio(bool sync)459*58b9f456SAndroid Build Coastguard Worker ios_base::sync_with_stdio(bool sync)
460*58b9f456SAndroid Build Coastguard Worker {
461*58b9f456SAndroid Build Coastguard Worker static bool previous_state = true;
462*58b9f456SAndroid Build Coastguard Worker bool r = previous_state;
463*58b9f456SAndroid Build Coastguard Worker previous_state = sync;
464*58b9f456SAndroid Build Coastguard Worker return r;
465*58b9f456SAndroid Build Coastguard Worker }
466*58b9f456SAndroid Build Coastguard Worker
467*58b9f456SAndroid Build Coastguard Worker _LIBCPP_END_NAMESPACE_STD
468