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