1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/stlport/src/ios.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,320 @@ 1.4 +/* 1.5 + * Copyright (c) 1999 1.6 + * Silicon Graphics Computer Systems, Inc. 1.7 + * 1.8 + * Copyright (c) 1999 1.9 + * Boris Fomitchev 1.10 + * 1.11 + * This material is provided "as is", with absolutely no warranty expressed 1.12 + * or implied. Any use is at your own risk. 1.13 + * 1.14 + * Permission to use or copy this software for any purpose is hereby granted 1.15 + * without fee, provided the above notices are retained on all copies. 1.16 + * Permission to modify the code and to distribute modified code is granted, 1.17 + * provided the above notices are retained, and a notice that the code was 1.18 + * modified is included with the above copyright notice. 1.19 + * 1.20 + */ 1.21 + 1.22 +#include "stlport_prefix.h" 1.23 + 1.24 +#include <algorithm> 1.25 +#include <ios> 1.26 +#include <locale> 1.27 +#include <ostream> // for __get_ostreambuf definition 1.28 + 1.29 +#include "aligned_buffer.h" 1.30 + 1.31 +_STLP_BEGIN_NAMESPACE 1.32 + 1.33 +//---------------------------------------------------------------------- 1.34 +// ios_base members 1.35 + 1.36 +#ifdef _STLP_USE_EXCEPTIONS 1.37 +// class ios_base::failure, a subclass of exception. It's used solely 1.38 +// for reporting errors. 1.39 + 1.40 +ios_base::failure::failure(const string& s) 1.41 + : __Named_exception(s) 1.42 +{} 1.43 + 1.44 +ios_base::failure::~failure() _STLP_NOTHROW_INHERENTLY {} 1.45 +#endif 1.46 + 1.47 +#if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION) 1.48 +// Definitions of ios_base's formatting flags. 1.49 +const ios_base::fmtflags ios_base::left; 1.50 +const ios_base::fmtflags ios_base::right; 1.51 +const ios_base::fmtflags ios_base::internal; 1.52 +const ios_base::fmtflags ios_base::dec; 1.53 +const ios_base::fmtflags ios_base::hex; 1.54 +const ios_base::fmtflags ios_base::oct; 1.55 +const ios_base::fmtflags ios_base::fixed; 1.56 +const ios_base::fmtflags ios_base::scientific; 1.57 +const ios_base::fmtflags ios_base::boolalpha; 1.58 +const ios_base::fmtflags ios_base::showbase; 1.59 +const ios_base::fmtflags ios_base::showpoint; 1.60 +const ios_base::fmtflags ios_base::showpos; 1.61 +const ios_base::fmtflags ios_base::skipws; 1.62 +const ios_base::fmtflags ios_base::unitbuf; 1.63 +const ios_base::fmtflags ios_base::uppercase; 1.64 +const ios_base::fmtflags ios_base::adjustfield; 1.65 +const ios_base::fmtflags ios_base::basefield; 1.66 +const ios_base::fmtflags ios_base::floatfield; 1.67 + 1.68 +// Definitions of ios_base's state flags. 1.69 +const ios_base::iostate ios_base::goodbit; 1.70 +const ios_base::iostate ios_base::badbit; 1.71 +const ios_base::iostate ios_base::eofbit; 1.72 +const ios_base::iostate ios_base::failbit; 1.73 + 1.74 +// Definitions of ios_base's openmode flags. 1.75 +const ios_base::openmode ios_base::app; 1.76 +const ios_base::openmode ios_base::ate; 1.77 +const ios_base::openmode ios_base::binary; 1.78 +const ios_base::openmode ios_base::in; 1.79 +const ios_base::openmode ios_base::out; 1.80 +const ios_base::openmode ios_base::trunc; 1.81 + 1.82 +// Definitions of ios_base's seekdir flags. 1.83 +const ios_base::seekdir ios_base::beg; 1.84 +const ios_base::seekdir ios_base::cur; 1.85 +const ios_base::seekdir ios_base::end; 1.86 + 1.87 +#endif 1.88 + 1.89 +// Internal functions used for managing exponentially-growing arrays of 1.90 +// POD types. 1.91 + 1.92 +// array is a pointer to N elements of type PODType. Expands the array, 1.93 +// if necessary, so that array[index] is meaningful. All new elements are 1.94 +// initialized to zero. Returns a pointer to the new array, and the new 1.95 +// size. 1.96 + 1.97 +template <class PODType> 1.98 +static pair<PODType*, size_t> 1.99 +_Stl_expand_array(PODType* __array, size_t N, int index) { 1.100 + if ((int)N < index + 1) { 1.101 + size_t new_N = (max)(2 * N, size_t(index + 1)); 1.102 + PODType* new_array 1.103 + = __STATIC_CAST(PODType*,realloc(__array, new_N * sizeof(PODType))); 1.104 + if (new_array) { 1.105 + fill(new_array + N, new_array + new_N, PODType()); 1.106 + return pair<PODType*, size_t>(new_array, new_N); 1.107 + } 1.108 + else 1.109 + return pair<PODType*, size_t>(__STATIC_CAST(PODType*,0), 0); 1.110 + } 1.111 + else 1.112 + return pair<PODType*, size_t>(__array, N); 1.113 +} 1.114 + 1.115 +// array is a pointer to N elements of type PODType. Allocate a new 1.116 +// array of N elements, copying the values from the old array to the new. 1.117 +// Return a pointer to the new array. It is assumed that array is non-null 1.118 +// and N is nonzero. 1.119 +template <class PODType> 1.120 +static PODType* _Stl_copy_array(const PODType* __array, size_t N) { 1.121 + PODType* result = __STATIC_CAST(PODType*,malloc(N * sizeof(PODType))); 1.122 + if (result) 1.123 + copy(__array, __array + N, result); 1.124 + return result; 1.125 +} 1.126 + 1.127 +locale ios_base::imbue(const locale& loc) { 1.128 + if (loc != _M_locale) { 1.129 + locale previous = _M_locale; 1.130 + _M_locale = loc; 1.131 + _M_invoke_callbacks(imbue_event); 1.132 + return previous; 1.133 + } 1.134 + else { 1.135 + _M_invoke_callbacks(imbue_event); 1.136 + return _M_locale; 1.137 + } 1.138 +} 1.139 + 1.140 +int _STLP_CALL ios_base::xalloc() { 1.141 +#if defined (_STLP_THREADS) && \ 1.142 + defined (_STLP_WIN32THREADS) && defined (_STLP_NEW_PLATFORM_SDK) 1.143 + static volatile __stl_atomic_t _S_index = 0; 1.144 + return _STLP_ATOMIC_INCREMENT(&_S_index); 1.145 +#else 1.146 + static int _S_index = 0; 1.147 + static _STLP_STATIC_MUTEX __lock _STLP_MUTEX_INITIALIZER; 1.148 + _STLP_auto_lock sentry(__lock); 1.149 + return _S_index++; 1.150 +#endif 1.151 +} 1.152 + 1.153 +long& ios_base::iword(int index) { 1.154 + static long dummy = 0; 1.155 + 1.156 + pair<long*, size_t> tmp = _Stl_expand_array(_M_iwords, _M_num_iwords, index); 1.157 + if (tmp.first) { // The allocation, if any, succeeded. 1.158 + _M_iwords = tmp.first; 1.159 + _M_num_iwords = tmp.second; 1.160 + return _M_iwords[index]; 1.161 + } 1.162 + else { 1.163 + _M_setstate_nothrow(badbit); 1.164 + _M_check_exception_mask(); 1.165 + return dummy; 1.166 + } 1.167 +} 1.168 + 1.169 + 1.170 +void*& ios_base::pword(int index) { 1.171 + static void* dummy = 0; 1.172 + 1.173 + pair<void**, size_t> tmp = _Stl_expand_array(_M_pwords, _M_num_pwords, index); 1.174 + if (tmp.first) { // The allocation, if any, succeeded. 1.175 + _M_pwords = tmp.first; 1.176 + _M_num_pwords = tmp.second; 1.177 + return _M_pwords[index]; 1.178 + } 1.179 + else { 1.180 + _M_setstate_nothrow(badbit); 1.181 + _M_check_exception_mask(); 1.182 + return dummy; 1.183 + } 1.184 +} 1.185 + 1.186 +void ios_base::register_callback(event_callback __fn, int index) { 1.187 + pair<pair<event_callback, int>*, size_t> tmp 1.188 + = _Stl_expand_array(_M_callbacks, _M_num_callbacks, (int)_M_callback_index /* fbp: index ??? */ ); 1.189 + if (tmp.first) { 1.190 + _M_callbacks = tmp.first; 1.191 + _M_num_callbacks = tmp.second; 1.192 + _M_callbacks[_M_callback_index++] = make_pair(__fn, index); 1.193 + } 1.194 + else { 1.195 + _M_setstate_nothrow(badbit); 1.196 + _M_check_exception_mask(); 1.197 + } 1.198 +} 1.199 + 1.200 +// Invokes all currently registered callbacks for a particular event. 1.201 +// Behaves correctly even if one of the callbacks adds a new callback. 1.202 +void ios_base::_M_invoke_callbacks(event E) { 1.203 + for (size_t i = _M_callback_index; i > 0; --i) { 1.204 + event_callback f = _M_callbacks[i-1].first; 1.205 + int n = _M_callbacks[i-1].second; 1.206 + f(E, *this, n); 1.207 + } 1.208 +} 1.209 + 1.210 +// This function is called if the state, rdstate(), has a bit set 1.211 +// that is also set in the exception mask exceptions(). 1.212 +void ios_base::_M_throw_failure() { 1.213 + const char* arg ; 1.214 +# if 0 1.215 + char buffer[256]; 1.216 + char* ptr; 1.217 + strcpy(buffer, "ios failure: rdstate = 0x"); 1.218 + ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_iostate)); 1.219 + strcpy(ptr, " mask = 0x"); 1.220 + ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_exception_mask)); 1.221 + *ptr = 0; 1.222 + arg = buffer; 1.223 +# else 1.224 + arg = "ios failure"; 1.225 +# endif 1.226 + 1.227 +# ifndef _STLP_USE_EXCEPTIONS 1.228 + fputs(arg, stderr); 1.229 +# else 1.230 + throw failure(arg); 1.231 +# endif 1.232 +} 1.233 + 1.234 +// Copy x's state to *this. This member function is used in the 1.235 +// implementation of basic_ios::copyfmt. Does not copy _M_exception_mask 1.236 +// or _M_iostate. 1.237 +void ios_base::_M_copy_state(const ios_base& x) { 1.238 + _M_fmtflags = x._M_fmtflags; // Copy the flags, except for _M_iostate 1.239 + _M_openmode = x._M_openmode; // and _M_exception_mask. 1.240 + _M_seekdir = x._M_seekdir; 1.241 + _M_precision = x._M_precision; 1.242 + _M_width = x._M_width; 1.243 + _M_locale = x._M_locale; 1.244 + 1.245 + if (x._M_callbacks) { 1.246 + pair<event_callback, int>* tmp = _Stl_copy_array(x._M_callbacks, x._M_callback_index); 1.247 + if (tmp) { 1.248 + free(_M_callbacks); 1.249 + _M_callbacks = tmp; 1.250 + _M_num_callbacks = _M_callback_index = x._M_callback_index; 1.251 + } 1.252 + else { 1.253 + _M_setstate_nothrow(badbit); 1.254 + _M_check_exception_mask(); 1.255 + } 1.256 + } 1.257 + 1.258 + if (x._M_iwords) { 1.259 + long* tmp = _Stl_copy_array(x._M_iwords, x._M_num_iwords); 1.260 + if (tmp) { 1.261 + free(_M_iwords); 1.262 + _M_iwords = tmp; 1.263 + _M_num_iwords = x._M_num_iwords; 1.264 + } 1.265 + else { 1.266 + _M_setstate_nothrow(badbit); 1.267 + _M_check_exception_mask(); 1.268 + } 1.269 + } 1.270 + 1.271 + if (x._M_pwords) { 1.272 + void** tmp = _Stl_copy_array(x._M_pwords, x._M_num_pwords); 1.273 + if (tmp) { 1.274 + free(_M_pwords); 1.275 + _M_pwords = tmp; 1.276 + _M_num_pwords = x._M_num_pwords; 1.277 + } 1.278 + else { 1.279 + _M_setstate_nothrow(badbit); 1.280 + _M_check_exception_mask(); 1.281 + } 1.282 + } 1.283 +} 1.284 + 1.285 +// ios's (protected) default constructor. The standard says that all 1.286 +// fields have indeterminate values; we initialize them to zero for 1.287 +// simplicity. The only thing that really matters is that the arrays 1.288 +// are all initially null pointers, and the array element counts are all 1.289 +// initially zero. 1.290 +ios_base::ios_base() 1.291 + : _M_fmtflags(0), _M_iostate(0), _M_openmode(0), _M_seekdir(0), 1.292 + _M_exception_mask(0), 1.293 + _M_precision(0), _M_width(0), 1.294 + _M_locale(), 1.295 + _M_callbacks(0), _M_num_callbacks(0), _M_callback_index(0), 1.296 + _M_iwords(0), _M_num_iwords(0), 1.297 + _M_pwords(0), 1.298 + _M_num_pwords(0) 1.299 +{} 1.300 + 1.301 +// ios's destructor. 1.302 +ios_base::~ios_base() { 1.303 + _M_invoke_callbacks(erase_event); 1.304 + free(_M_callbacks); 1.305 + free(_M_iwords); 1.306 + free(_M_pwords); 1.307 +} 1.308 + 1.309 +//---------------------------------------------------------------------- 1.310 +// Force instantiation of basic_ios 1.311 +// For DLL exports, they are already instantiated. 1.312 +#if !defined(_STLP_NO_FORCE_INSTANTIATE) 1.313 +template class _STLP_CLASS_DECLSPEC basic_ios<char, char_traits<char> >; 1.314 +# if !defined (_STLP_NO_WCHAR_T) 1.315 +template class _STLP_CLASS_DECLSPEC basic_ios<wchar_t, char_traits<wchar_t> >; 1.316 +# endif /* _STLP_NO_WCHAR_T */ 1.317 +#endif 1.318 + 1.319 +_STLP_END_NAMESPACE 1.320 + 1.321 +// Local Variables: 1.322 +// mode:C++ 1.323 +// End: