Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
ios.cpp
Go to the documentation of this file.
1
29#define _RWSTD_LIB_SRC
30
31#include <stdlib.h>
32#include <string.h>
33
34#include <iosfwd>
35
36#include <rw/_error.h>
37
38#include "iosdata.h"
39
40#include <rw/_defs.h>
41
42
44
45// array of pointers to the 8 standard iostream objects
46_RWSTD_EXPORT const void* __rw_std_streams [8];
47
48} // namespace __rw
49
50
52
53
54ios_base::ios_base ()
55 : _C_usr (0)
56{
57 // outlined to hide implementation details
58 // no-op
59}
60
61
62// std::cin and std::wcin _C_usr_data structures
63ios_base::_C_usr_data ios_base::_C_usr_data::_C_std_usr_data [2];
64
65
66ios_base::~ios_base()
67{
68 // verify that _C_usr_data is a POD (i.e., has no user-defined ctor)
69 // necessary in order for the C_std_usr_data array defined above to
70 // not be overwritten during dynamic initialization
71 union U {
72 ios_base::_C_usr_data dummy;
73 };
74
75 // MT guard or reentrancy not necessary in a dtor
76
77 if (_C_usr) {
78 // invoke through a member pointer to avoid a link-time dependency
79 // of this translation unit on the one that defines the member
80 // function ios_base::_C_fire_event()
81 if (_C_usr->_C_fire)
82 (this->*_C_usr->_C_fire)(erase_event, false /* not reentrant */);
83
84 _C_usr_data::_C_dealloc (_C_usr);
85 }
86}
87
88
89streamsize ios_base::precision (streamsize prec)
90{
91 // outlined to hide implementation details
92 return _RWSTD_ATOMIC_IO_SWAP (_C_prec, prec, _C_mutex);
93}
94
95
96ios_base::fmtflags ios_base::flags (fmtflags fl)
97{
98 const unsigned mask =
100
101 unsigned ifl = unsigned (fl);
102
103 switch (fl & basefield) {
104 // if basefield is set, clear the base mask and set
105 // the numeric base bits according to the basefield
106 case oct:
107 ifl = ifl & mask | 8U << _RWSTD_IOS_BASEOFF;
108 break;
109
110 case dec:
111 ifl = ifl & mask | 10U << _RWSTD_IOS_BASEOFF;
112 break;
113
114 case hex:
115 ifl = ifl & mask | 16U << _RWSTD_IOS_BASEOFF;
116 break;
117
118 case _RWSTD_IOS_BIN:
119 ifl = ifl & mask | 2U << _RWSTD_IOS_BASEOFF;
120 break;
121
122 case 0:
123 // if basefield is clear and the numeric base bits are set,
124 // set the basefield accordingly, making sure that when
125 // numeric base bits are set to 10, dec is left alone
126 // (necessary for autodected parsing to work correctly)
127 switch (ifl >> _RWSTD_IOS_BASEOFF) {
128 case 0: ifl |= 10U << _RWSTD_IOS_BASEOFF; break;
129 case 2: ifl |= _RWSTD_IOS_BIN; break;
130 case 8: ifl |= oct; break;
131 case 10: /* no-op */ break;
132 case 16: ifl |= hex; break;
133 }
134 break;
135
136 default:
137 ifl |= 10U << _RWSTD_IOS_BASEOFF;
138 }
139
140 return fmtflags (_RWSTD_ATOMIC_IO_SWAP (_C_fmtfl, ifl, _C_mutex));
141}
142
143
144locale ios_base::imbue (const locale &loc)
145{
146 // outlined to hide implementation details
147
148 _RWSTD_MT_GUARD (flags () & _RW::__rw_nolock ? 0 : &_C_mutex);
149
150 return _C_unsafe_imbue (loc);
151}
152
153
154/* static */ bool ios_base::_C_sync_with_stdio = true;
155
156/* static */ bool ios_base::sync_with_stdio (bool syncflag /* = true */)
157{
158 // outlined to hide implementation details
159
160 // always atomic in MT environments since _C_sync is static
161 return _RWSTD_ATOMIC_SWAP (_C_sync_with_stdio, syncflag, false);
162}
163
164
165void ios_base::_C_init (void *sb)
166{
167 // not guarded, assumed to be called only from ctors
168 // all members are assumed to be uninitialized
169
170 // NOTE: `sb' may point to a yet uninitialized object
171 // it is unsafe to reference any of its members
172
173 const unsigned fmtfl = (10U << _RWSTD_IOS_BASEOFF) | skipws | dec;
174
175 _C_state = (_C_rdbuf = sb) ? goodbit : badbit;
176 _C_fmtfl = fmtfl;
177 _C_wide = 0;
178 _C_prec = 6;
179 _C_except = 0;
180
181#if defined (_RWSTD_REENTRANT) \
182 && !defined (_RWSTD_NO_EXT_REENTRANT_IO) \
183 && !defined (_RWSTD_NO_REENTRANT_IO_DEFAULT)
184
185 // disable locking of iostream objects and their associated buffers
186 // standard iostream objects will override in ios_base::Init::Init()
187 _C_fmtfl |= _RW::__rw_nolock | _RW::__rw_nolockbuf;
188
189#endif // _RWSTD_REENTRANT && !_RWSTD_NO_REENTRANT_IO && ...
190}
191
192
193locale ios_base::_C_unsafe_imbue (const locale &loc)
194{
195 // outlined to hide implementation details
196
197 const locale tmp (_C_loc);
198
199 _C_loc = loc;
200
201 if (_C_usr) {
202 // invoke through a member pointer to avoid a link-time dependency
203 // of this translation unit on the one that defines the member
204 // function ios_base::_C_fire_event()
205 if (_C_usr->_C_fire)
206 (this->*_C_usr->_C_fire)(imbue_event, true /* reentrant */);
207 }
208
209 return tmp;
210}
211
212
213void* ios_base::
214_C_set (unsigned state, unsigned except, void *rdbuf)
215{
216 _RWSTD_MT_GUARD ((_C_fmtfl | except) & _RW::__rw_nolock ? 0 : &_C_mutex);
217
218 except &= ~_RW::__rw_nolock;
219
220 if (!rdbuf)
221 state |= badbit;
222
223 void* const rdbuf_save = _C_rdbuf;
224
225 _C_rdbuf = rdbuf;
226 _C_state = iostate (state & ~(_RW::__rw_nothrow | _RW::__rw_rethrow));
227 _C_except = except;
228
229#ifndef _RWSTD_NO_EXCEPTIONS
230
231 if (!(state & _RW::__rw_nothrow) && (state & except)) {
232
233 if (state & _RW::__rw_rethrow)
234 throw;
235
236 const char *bit =
237 "std::ios::badbit, std::ios::failbit, std::ios::eofbit";
238
239 static const char streams[][11] = {
240 "std::cin", "std::cout", "std::cerr", "std::clog",
241 "std::wcin", "std::wcout", "std::wcerr", "std::wclog"
242 };
243
244 // is `this' one of the standard iostreams or is it user-defined?
245 const char *strm = "user-defined";
246 for (const void * const *ps = _RW::__rw_std_streams; *ps; ++ps) {
247 if (this == *ps)
248 strm = streams [ps - _RW::__rw_std_streams];
249 }
250
251 switch (state & except) {
252
253#ifndef _RWSTD_NO_EXT_FAILURE
254
255 // casts below are necessary to preserve the static type
256 // of the exception being thrown (upcast by _C_format ())
257
259 _RW::__rw_throw (_RWSTD_ERROR_BADBIT_SET, strm);
260
262 _RW::__rw_throw (_RWSTD_ERROR_FAILBIT_SET, strm);
263
265 _RW::__rw_throw (_RWSTD_ERROR_EOFBIT_SET, strm);
266
267#else // if defined (_RWSTD_NO_EXT_FAILURE)
268
270 bit = "badbit";
271 break;
272
274 bit = "failbit";
275 break;
276
278 bit = "eofbit";
279 break;
280
281#endif // _RWSTD_NO_EXT_FAILURE
282
284 bit = "badbit, failbit";
285 break;
286
288 bit = "badbit, eofbit";
289 break;
290
292 bit = "failbit, eofbit";
293 break;
294 }
295
296 _RW::__rw_throw (_RWSTD_ERROR_IOSTATE_BIT_SET, strm, bit);
297 }
298
299#endif // !defined (_RWSTD_NO_EXCEPTIONS)
300
301 return rdbuf_save;
302}
303
304
305void* ios_base::_C_tie () const
306{
307 // outlined to hide implementation details
308
309 return _C_usr ? _C_usr->_C_tie : 0;
310}
311
312
313void* ios_base::_C_tie (void *pstrm)
314{
315 _RWSTD_MT_GUARD (flags () & _RW::__rw_nolock ? 0 : &_C_mutex);
316
317 void *save = 0;
318
319 if (_C_usr) {
320 save = _C_usr->_C_tie;
321
322 if ( !pstrm
323 && !_C_usr->_C_iarray
324 && !_C_usr->_C_parray
325 && !_C_usr->_C_cbarray) {
326 _C_usr_data::_C_dealloc (_C_usr);
327 _C_usr = 0;
328 }
329 }
330 else if (pstrm)
331 _C_usr = _C_usr_data::_C_alloc (0);
332
333 if (_C_usr)
334 _C_usr->_C_tie = pstrm;
335
336 return save;
337}
338
339
340} // namespace std
#define loc
Definition 17.names.cpp:492
#define sb
Definition 17.names.cpp:542
#define tmp
Definition 17.names.cpp:553
#define save
Definition 17.names.cpp:538
#define strm
Definition 17.names.cpp:549
#define U
Definition 17.names.cpp:164
#define badbit
#define goodbit
#define dec
#define skipws
#define oct
#define hex
#define basefield
#define flags
#define _RWSTD_IOS_BADBIT
Definition _defs.h:808
#define _RWSTD_EXPORT
Definition _defs.h:485
#define _RWSTD_ATOMIC_SWAP(x, y, ignore)
Definition _defs.h:701
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
#define _RWSTD_IOS_FAILBIT
Definition _defs.h:810
#define _RWSTD_MT_GUARD(ignore)
Definition _defs.h:695
#define _RWSTD_IOS_EOFBIT
Definition _defs.h:809
#define _RWSTD_IOS_BASEOFF
Definition _defs.h:805
#define _RWSTD_IOS_BIN
Definition _defs.h:781
#define _RWSTD_IOS_BASEMASK
Definition _defs.h:804
#define _RWSTD_ATOMIC_IO_SWAP(x, y, ignore)
Definition _defs.h:704
#define _RWSTD_ERROR_IOSTATE_BIT_SET
Definition _error.h:102
#define _RWSTD_ERROR_FAILBIT_SET
Definition _error.h:99
#define _RWSTD_ERROR_BADBIT_SET
Definition _error.h:100
#define _RWSTD_ERROR_EOFBIT_SET
Definition _error.h:101
Definition json.h:5363