1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | # include "stlport_prefix.h" |
20 | # include <algorithm> |
21 | # include <stl/_ios.h> |
22 | |
23 | _STLP_BEGIN_NAMESPACEnamespace _STL { |
24 | |
25 | char* _STLP_CALL |
26 | __write_integer(char* buf, ios_base::fmtflags flags, long x); |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | ios_base::failure::failure(const string& s) |
35 | : __Named_exception(s) |
36 | {} |
37 | |
38 | ios_base::failure::~failure() _STLP_NOTHROW_INHERENTLYthrow() {} |
39 | |
40 | #if !defined (_STLP_STATIC_CONST_INIT_BUG) |
41 | |
42 | |
43 | const ios_base::fmtflags ios_base::left; |
44 | const ios_base::fmtflags ios_base::right; |
45 | const ios_base::fmtflags ios_base::internal; |
46 | const ios_base::fmtflags ios_base::dec; |
47 | const ios_base::fmtflags ios_base::hex; |
48 | const ios_base::fmtflags ios_base::oct; |
49 | const ios_base::fmtflags ios_base::fixed; |
50 | const ios_base::fmtflags ios_base::scientific; |
51 | const ios_base::fmtflags ios_base::boolalpha; |
52 | const ios_base::fmtflags ios_base::showbase; |
53 | const ios_base::fmtflags ios_base::showpoint; |
54 | const ios_base::fmtflags ios_base::showpos; |
55 | const ios_base::fmtflags ios_base::skipws; |
56 | const ios_base::fmtflags ios_base::unitbuf; |
57 | const ios_base::fmtflags ios_base::uppercase; |
58 | const ios_base::fmtflags ios_base::adjustfield; |
59 | const ios_base::fmtflags ios_base::basefield; |
60 | const ios_base::fmtflags ios_base::floatfield; |
61 | |
62 | |
63 | const ios_base::iostate ios_base::goodbit; |
64 | const ios_base::iostate ios_base::badbit; |
65 | const ios_base::iostate ios_base::eofbit; |
66 | const ios_base::iostate ios_base::failbit; |
67 | |
68 | |
69 | const ios_base::openmode ios_base::app; |
70 | const ios_base::openmode ios_base::ate; |
71 | const ios_base::openmode ios_base::binary; |
72 | const ios_base::openmode ios_base::in; |
73 | const ios_base::openmode ios_base::out; |
74 | const ios_base::openmode ios_base::trunc; |
75 | |
76 | |
77 | const ios_base::seekdir ios_base::beg; |
78 | const ios_base::seekdir ios_base::cur; |
79 | const ios_base::seekdir ios_base::end; |
80 | |
81 | # endif /* _STLP_STATIC_CONST_INIT_BUG */ |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | template <class PODType> |
91 | pair<PODType*, size_t> |
92 | _Stl_expand_array(PODType* array, size_t N, int index) |
93 | { |
94 | if ((int)N < index + 1) { |
| |
95 | size_t new_N = (max)(2 * N, size_t(index + 1)); |
96 | PODType* new_array |
97 | = __STATIC_CAST(PODType*,realloc(array, new_N * sizeof(PODType)))static_cast<PODType*>(realloc(array, new_N * sizeof(PODType ))); |
| 3 | | Within the expansion of the macro '__STATIC_CAST':
| |
a | Attempt to reallocate memory |
|
98 | if (new_array) { |
| |
| |
99 | fill(new_array + N, new_array + new_N, PODType()); |
100 | return pair<PODType*, size_t>(new_array, new_N); |
101 | } |
102 | else |
103 | return pair<PODType*, size_t>(__STATIC_CAST(PODType*,0)static_cast<PODType*>(0), 0); |
104 | } |
105 | else |
106 | return pair<PODType*, size_t>(array, N); |
107 | } |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | template <class PODType> |
114 | PODType* _Stl_copy_array(const PODType* array, size_t N) { |
115 | PODType* result = __STATIC_CAST(PODType*,malloc(N * sizeof(PODType)))static_cast<PODType*>(malloc(N * sizeof(PODType))); |
116 | if (result) |
117 | copy(array, array + N, result); |
118 | return result; |
119 | } |
120 | |
121 | locale ios_base::imbue(const locale& loc) { |
122 | locale previous = _M_locale; |
123 | _M_locale = loc; |
124 | _M_invoke_callbacks(imbue_event); |
125 | return previous; |
126 | } |
127 | |
128 | int ios_base::_S_index = 0; |
129 | |
130 | int _STLP_CALL ios_base::xalloc() |
131 | { |
132 | static _STLP_STATIC_MUTEX_STLP_mutex_base L _STLP_MUTEX_INITIALIZER= { { { 0, 0, 0, 0, 0, { 0 } } } }; |
133 | _STLP_auto_lock sentry(L); |
134 | return _S_index++; |
135 | } |
136 | |
137 | long& ios_base::iword(int index) { |
138 | static long dummy = 0; |
139 | |
140 | pair<long*, size_t> tmp = _Stl_expand_array(_M_iwords, _M_num_iwords, index); |
141 | if (tmp.first) { |
142 | _M_iwords = tmp.first; |
143 | _M_num_iwords = tmp.second; |
144 | return _M_iwords[index]; |
145 | } |
146 | else { |
147 | _M_setstate_nothrow(badbit); |
148 | _M_check_exception_mask(); |
149 | return dummy; |
150 | } |
151 | } |
152 | |
153 | |
154 | void*& ios_base::pword(int index) { |
155 | static void* dummy = 0; |
156 | |
157 | pair<void**, size_t> tmp = _Stl_expand_array(_M_pwords, _M_num_pwords, index); |
158 | if (tmp.first) { |
159 | _M_pwords = tmp.first; |
160 | _M_num_pwords = tmp.second; |
161 | return _M_pwords[index]; |
162 | } |
163 | else { |
164 | _M_setstate_nothrow(badbit); |
165 | _M_check_exception_mask(); |
166 | return dummy; |
167 | } |
168 | } |
169 | |
170 | void ios_base::register_callback(event_callback __fn, int index) { |
171 | pair<pair<event_callback, int>*, size_t> tmp |
172 | = _Stl_expand_array(_M_callbacks, _M_num_callbacks, (int)_M_callback_index ); |
| 1 | Calling '_Stl_expand_array' | |
|
| 6 | | Reallocation of 1st parameter failed | |
|
173 | if (tmp.first) { |
| |
174 | _M_callbacks = tmp.first; |
175 | _M_num_callbacks = tmp.second; |
176 | _M_callbacks[_M_callback_index++] = make_pair(__fn, index); |
177 | } |
178 | else { |
179 | _M_setstate_nothrow(badbit); |
180 | _M_check_exception_mask(); |
181 | } |
182 | } |
| 8 | | Memory is never released; potential leak |
|
183 | |
184 | |
185 | |
186 | void ios_base::_M_invoke_callbacks(event E) { |
187 | for (size_t i = _M_callback_index; i > 0; --i) { |
188 | event_callback f = _M_callbacks[i-1].first; |
189 | int n = _M_callbacks[i-1].second; |
190 | f(E, *this, n); |
191 | } |
192 | } |
193 | |
194 | |
195 | |
196 | void ios_base::_M_throw_failure() { |
197 | const char* arg ; |
198 | # if 0 |
199 | char buffer[256]; |
200 | char* ptr; |
201 | strcpy(buffer, "ios failure: rdstate = 0x"); |
202 | ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_iostate)static_cast<unsigned long>(_M_iostate)); |
203 | strcpy(ptr, " mask = 0x"); |
204 | ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_exception_mask)static_cast<unsigned long>(_M_exception_mask)); |
205 | *ptr = 0; |
206 | arg = buffer; |
207 | # else |
208 | arg = "ios failure"; |
209 | # endif |
210 | |
211 | # ifndef _STLP_USE_EXCEPTIONS |
212 | fputs(arg, stderrstderr); |
213 | # else |
214 | throw failure(arg); |
215 | # endif |
216 | } |
217 | |
218 | |
219 | |
220 | |
221 | void ios_base::_M_copy_state(const ios_base& x) { |
222 | _M_fmtflags = x._M_fmtflags; |
223 | _M_openmode = x._M_openmode; |
224 | _M_seekdir = x._M_seekdir; |
225 | _M_precision = x._M_precision; |
226 | _M_width = x._M_width; |
227 | |
228 | if (_M_locale != x._M_locale) { |
229 | _M_locale = x._M_locale; |
230 | _M_cached_ctype = x._M_cached_ctype; |
231 | _M_cached_numpunct = x._M_cached_numpunct; |
232 | } |
233 | |
234 | if (x._M_callbacks) { |
235 | pair<event_callback, int>* tmp = _Stl_copy_array(x._M_callbacks, x._M_callback_index); |
236 | if (tmp) { |
237 | free(_M_callbacks); |
238 | _M_callbacks = tmp; |
239 | _M_num_callbacks = _M_callback_index = x._M_callback_index; |
240 | } |
241 | else { |
242 | _M_setstate_nothrow(badbit); |
243 | _M_check_exception_mask(); |
244 | } |
245 | } |
246 | |
247 | if (x._M_iwords) { |
248 | long* tmp = _Stl_copy_array(x._M_iwords, x._M_num_iwords); |
249 | if (tmp) { |
250 | free(_M_iwords); |
251 | _M_iwords = tmp; |
252 | _M_num_iwords = x._M_num_iwords; |
253 | } |
254 | else { |
255 | _M_setstate_nothrow(badbit); |
256 | _M_check_exception_mask(); |
257 | } |
258 | } |
259 | |
260 | if (x._M_pwords) { |
261 | void** tmp = _Stl_copy_array(x._M_pwords, x._M_num_pwords); |
262 | if (tmp) { |
263 | free(_M_pwords); |
264 | _M_pwords = tmp; |
265 | _M_num_pwords = x._M_num_pwords; |
266 | } |
267 | else { |
268 | _M_setstate_nothrow(badbit); |
269 | _M_check_exception_mask(); |
270 | } |
271 | } |
272 | } |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | ios_base::ios_base() |
281 | : _M_fmtflags(0), _M_iostate(0), _M_openmode(0), _M_seekdir(0), |
282 | _M_exception_mask(0), |
283 | _M_precision(0), _M_width(0), |
284 | _M_locale(), |
285 | _M_callbacks(0), _M_num_callbacks(0), _M_callback_index(0), |
286 | _M_iwords(0), _M_num_iwords(0), |
287 | _M_pwords(0), |
288 | _M_num_pwords(0) , _M_cached_ctype(0), _M_cached_numpunct(0) |
289 | { } |
290 | |
291 | |
292 | ios_base::~ios_base() { |
293 | _M_invoke_callbacks(erase_event); |
294 | free(_M_callbacks); |
295 | free(_M_iwords); |
296 | free(_M_pwords); |
297 | } |
298 | |
299 | |
300 | |
301 | |
302 | # if !defined(_STLP_NO_FORCE_INSTANTIATE) |
303 | template class _STLP_CLASS_DECLSPEC basic_ios<char, char_traits<char> >; |
304 | # ifndef _STLP_NO_WCHAR_T |
305 | template class _STLP_CLASS_DECLSPEC basic_ios<wchar_t, char_traits<wchar_t> >; |
306 | # endif /* _STLP_NO_WCHAR_T */ |
307 | # endif |
308 | |
309 | _STLP_END_NAMESPACE} |
310 | |
311 | |
312 | |
313 | |