build/stlport/src/ios.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 1999
michael@0 3 * Silicon Graphics Computer Systems, Inc.
michael@0 4 *
michael@0 5 * Copyright (c) 1999
michael@0 6 * Boris Fomitchev
michael@0 7 *
michael@0 8 * This material is provided "as is", with absolutely no warranty expressed
michael@0 9 * or implied. Any use is at your own risk.
michael@0 10 *
michael@0 11 * Permission to use or copy this software for any purpose is hereby granted
michael@0 12 * without fee, provided the above notices are retained on all copies.
michael@0 13 * Permission to modify the code and to distribute modified code is granted,
michael@0 14 * provided the above notices are retained, and a notice that the code was
michael@0 15 * modified is included with the above copyright notice.
michael@0 16 *
michael@0 17 */
michael@0 18
michael@0 19 #include "stlport_prefix.h"
michael@0 20
michael@0 21 #include <algorithm>
michael@0 22 #include <ios>
michael@0 23 #include <locale>
michael@0 24 #include <ostream> // for __get_ostreambuf definition
michael@0 25
michael@0 26 #include "aligned_buffer.h"
michael@0 27
michael@0 28 _STLP_BEGIN_NAMESPACE
michael@0 29
michael@0 30 //----------------------------------------------------------------------
michael@0 31 // ios_base members
michael@0 32
michael@0 33 #ifdef _STLP_USE_EXCEPTIONS
michael@0 34 // class ios_base::failure, a subclass of exception. It's used solely
michael@0 35 // for reporting errors.
michael@0 36
michael@0 37 ios_base::failure::failure(const string& s)
michael@0 38 : __Named_exception(s)
michael@0 39 {}
michael@0 40
michael@0 41 ios_base::failure::~failure() _STLP_NOTHROW_INHERENTLY {}
michael@0 42 #endif
michael@0 43
michael@0 44 #if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION)
michael@0 45 // Definitions of ios_base's formatting flags.
michael@0 46 const ios_base::fmtflags ios_base::left;
michael@0 47 const ios_base::fmtflags ios_base::right;
michael@0 48 const ios_base::fmtflags ios_base::internal;
michael@0 49 const ios_base::fmtflags ios_base::dec;
michael@0 50 const ios_base::fmtflags ios_base::hex;
michael@0 51 const ios_base::fmtflags ios_base::oct;
michael@0 52 const ios_base::fmtflags ios_base::fixed;
michael@0 53 const ios_base::fmtflags ios_base::scientific;
michael@0 54 const ios_base::fmtflags ios_base::boolalpha;
michael@0 55 const ios_base::fmtflags ios_base::showbase;
michael@0 56 const ios_base::fmtflags ios_base::showpoint;
michael@0 57 const ios_base::fmtflags ios_base::showpos;
michael@0 58 const ios_base::fmtflags ios_base::skipws;
michael@0 59 const ios_base::fmtflags ios_base::unitbuf;
michael@0 60 const ios_base::fmtflags ios_base::uppercase;
michael@0 61 const ios_base::fmtflags ios_base::adjustfield;
michael@0 62 const ios_base::fmtflags ios_base::basefield;
michael@0 63 const ios_base::fmtflags ios_base::floatfield;
michael@0 64
michael@0 65 // Definitions of ios_base's state flags.
michael@0 66 const ios_base::iostate ios_base::goodbit;
michael@0 67 const ios_base::iostate ios_base::badbit;
michael@0 68 const ios_base::iostate ios_base::eofbit;
michael@0 69 const ios_base::iostate ios_base::failbit;
michael@0 70
michael@0 71 // Definitions of ios_base's openmode flags.
michael@0 72 const ios_base::openmode ios_base::app;
michael@0 73 const ios_base::openmode ios_base::ate;
michael@0 74 const ios_base::openmode ios_base::binary;
michael@0 75 const ios_base::openmode ios_base::in;
michael@0 76 const ios_base::openmode ios_base::out;
michael@0 77 const ios_base::openmode ios_base::trunc;
michael@0 78
michael@0 79 // Definitions of ios_base's seekdir flags.
michael@0 80 const ios_base::seekdir ios_base::beg;
michael@0 81 const ios_base::seekdir ios_base::cur;
michael@0 82 const ios_base::seekdir ios_base::end;
michael@0 83
michael@0 84 #endif
michael@0 85
michael@0 86 // Internal functions used for managing exponentially-growing arrays of
michael@0 87 // POD types.
michael@0 88
michael@0 89 // array is a pointer to N elements of type PODType. Expands the array,
michael@0 90 // if necessary, so that array[index] is meaningful. All new elements are
michael@0 91 // initialized to zero. Returns a pointer to the new array, and the new
michael@0 92 // size.
michael@0 93
michael@0 94 template <class PODType>
michael@0 95 static pair<PODType*, size_t>
michael@0 96 _Stl_expand_array(PODType* __array, size_t N, int index) {
michael@0 97 if ((int)N < index + 1) {
michael@0 98 size_t new_N = (max)(2 * N, size_t(index + 1));
michael@0 99 PODType* new_array
michael@0 100 = __STATIC_CAST(PODType*,realloc(__array, new_N * sizeof(PODType)));
michael@0 101 if (new_array) {
michael@0 102 fill(new_array + N, new_array + new_N, PODType());
michael@0 103 return pair<PODType*, size_t>(new_array, new_N);
michael@0 104 }
michael@0 105 else
michael@0 106 return pair<PODType*, size_t>(__STATIC_CAST(PODType*,0), 0);
michael@0 107 }
michael@0 108 else
michael@0 109 return pair<PODType*, size_t>(__array, N);
michael@0 110 }
michael@0 111
michael@0 112 // array is a pointer to N elements of type PODType. Allocate a new
michael@0 113 // array of N elements, copying the values from the old array to the new.
michael@0 114 // Return a pointer to the new array. It is assumed that array is non-null
michael@0 115 // and N is nonzero.
michael@0 116 template <class PODType>
michael@0 117 static PODType* _Stl_copy_array(const PODType* __array, size_t N) {
michael@0 118 PODType* result = __STATIC_CAST(PODType*,malloc(N * sizeof(PODType)));
michael@0 119 if (result)
michael@0 120 copy(__array, __array + N, result);
michael@0 121 return result;
michael@0 122 }
michael@0 123
michael@0 124 locale ios_base::imbue(const locale& loc) {
michael@0 125 if (loc != _M_locale) {
michael@0 126 locale previous = _M_locale;
michael@0 127 _M_locale = loc;
michael@0 128 _M_invoke_callbacks(imbue_event);
michael@0 129 return previous;
michael@0 130 }
michael@0 131 else {
michael@0 132 _M_invoke_callbacks(imbue_event);
michael@0 133 return _M_locale;
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 int _STLP_CALL ios_base::xalloc() {
michael@0 138 #if defined (_STLP_THREADS) && \
michael@0 139 defined (_STLP_WIN32THREADS) && defined (_STLP_NEW_PLATFORM_SDK)
michael@0 140 static volatile __stl_atomic_t _S_index = 0;
michael@0 141 return _STLP_ATOMIC_INCREMENT(&_S_index);
michael@0 142 #else
michael@0 143 static int _S_index = 0;
michael@0 144 static _STLP_STATIC_MUTEX __lock _STLP_MUTEX_INITIALIZER;
michael@0 145 _STLP_auto_lock sentry(__lock);
michael@0 146 return _S_index++;
michael@0 147 #endif
michael@0 148 }
michael@0 149
michael@0 150 long& ios_base::iword(int index) {
michael@0 151 static long dummy = 0;
michael@0 152
michael@0 153 pair<long*, size_t> tmp = _Stl_expand_array(_M_iwords, _M_num_iwords, index);
michael@0 154 if (tmp.first) { // The allocation, if any, succeeded.
michael@0 155 _M_iwords = tmp.first;
michael@0 156 _M_num_iwords = tmp.second;
michael@0 157 return _M_iwords[index];
michael@0 158 }
michael@0 159 else {
michael@0 160 _M_setstate_nothrow(badbit);
michael@0 161 _M_check_exception_mask();
michael@0 162 return dummy;
michael@0 163 }
michael@0 164 }
michael@0 165
michael@0 166
michael@0 167 void*& ios_base::pword(int index) {
michael@0 168 static void* dummy = 0;
michael@0 169
michael@0 170 pair<void**, size_t> tmp = _Stl_expand_array(_M_pwords, _M_num_pwords, index);
michael@0 171 if (tmp.first) { // The allocation, if any, succeeded.
michael@0 172 _M_pwords = tmp.first;
michael@0 173 _M_num_pwords = tmp.second;
michael@0 174 return _M_pwords[index];
michael@0 175 }
michael@0 176 else {
michael@0 177 _M_setstate_nothrow(badbit);
michael@0 178 _M_check_exception_mask();
michael@0 179 return dummy;
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 void ios_base::register_callback(event_callback __fn, int index) {
michael@0 184 pair<pair<event_callback, int>*, size_t> tmp
michael@0 185 = _Stl_expand_array(_M_callbacks, _M_num_callbacks, (int)_M_callback_index /* fbp: index ??? */ );
michael@0 186 if (tmp.first) {
michael@0 187 _M_callbacks = tmp.first;
michael@0 188 _M_num_callbacks = tmp.second;
michael@0 189 _M_callbacks[_M_callback_index++] = make_pair(__fn, index);
michael@0 190 }
michael@0 191 else {
michael@0 192 _M_setstate_nothrow(badbit);
michael@0 193 _M_check_exception_mask();
michael@0 194 }
michael@0 195 }
michael@0 196
michael@0 197 // Invokes all currently registered callbacks for a particular event.
michael@0 198 // Behaves correctly even if one of the callbacks adds a new callback.
michael@0 199 void ios_base::_M_invoke_callbacks(event E) {
michael@0 200 for (size_t i = _M_callback_index; i > 0; --i) {
michael@0 201 event_callback f = _M_callbacks[i-1].first;
michael@0 202 int n = _M_callbacks[i-1].second;
michael@0 203 f(E, *this, n);
michael@0 204 }
michael@0 205 }
michael@0 206
michael@0 207 // This function is called if the state, rdstate(), has a bit set
michael@0 208 // that is also set in the exception mask exceptions().
michael@0 209 void ios_base::_M_throw_failure() {
michael@0 210 const char* arg ;
michael@0 211 # if 0
michael@0 212 char buffer[256];
michael@0 213 char* ptr;
michael@0 214 strcpy(buffer, "ios failure: rdstate = 0x");
michael@0 215 ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_iostate));
michael@0 216 strcpy(ptr, " mask = 0x");
michael@0 217 ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_exception_mask));
michael@0 218 *ptr = 0;
michael@0 219 arg = buffer;
michael@0 220 # else
michael@0 221 arg = "ios failure";
michael@0 222 # endif
michael@0 223
michael@0 224 # ifndef _STLP_USE_EXCEPTIONS
michael@0 225 fputs(arg, stderr);
michael@0 226 # else
michael@0 227 throw failure(arg);
michael@0 228 # endif
michael@0 229 }
michael@0 230
michael@0 231 // Copy x's state to *this. This member function is used in the
michael@0 232 // implementation of basic_ios::copyfmt. Does not copy _M_exception_mask
michael@0 233 // or _M_iostate.
michael@0 234 void ios_base::_M_copy_state(const ios_base& x) {
michael@0 235 _M_fmtflags = x._M_fmtflags; // Copy the flags, except for _M_iostate
michael@0 236 _M_openmode = x._M_openmode; // and _M_exception_mask.
michael@0 237 _M_seekdir = x._M_seekdir;
michael@0 238 _M_precision = x._M_precision;
michael@0 239 _M_width = x._M_width;
michael@0 240 _M_locale = x._M_locale;
michael@0 241
michael@0 242 if (x._M_callbacks) {
michael@0 243 pair<event_callback, int>* tmp = _Stl_copy_array(x._M_callbacks, x._M_callback_index);
michael@0 244 if (tmp) {
michael@0 245 free(_M_callbacks);
michael@0 246 _M_callbacks = tmp;
michael@0 247 _M_num_callbacks = _M_callback_index = x._M_callback_index;
michael@0 248 }
michael@0 249 else {
michael@0 250 _M_setstate_nothrow(badbit);
michael@0 251 _M_check_exception_mask();
michael@0 252 }
michael@0 253 }
michael@0 254
michael@0 255 if (x._M_iwords) {
michael@0 256 long* tmp = _Stl_copy_array(x._M_iwords, x._M_num_iwords);
michael@0 257 if (tmp) {
michael@0 258 free(_M_iwords);
michael@0 259 _M_iwords = tmp;
michael@0 260 _M_num_iwords = x._M_num_iwords;
michael@0 261 }
michael@0 262 else {
michael@0 263 _M_setstate_nothrow(badbit);
michael@0 264 _M_check_exception_mask();
michael@0 265 }
michael@0 266 }
michael@0 267
michael@0 268 if (x._M_pwords) {
michael@0 269 void** tmp = _Stl_copy_array(x._M_pwords, x._M_num_pwords);
michael@0 270 if (tmp) {
michael@0 271 free(_M_pwords);
michael@0 272 _M_pwords = tmp;
michael@0 273 _M_num_pwords = x._M_num_pwords;
michael@0 274 }
michael@0 275 else {
michael@0 276 _M_setstate_nothrow(badbit);
michael@0 277 _M_check_exception_mask();
michael@0 278 }
michael@0 279 }
michael@0 280 }
michael@0 281
michael@0 282 // ios's (protected) default constructor. The standard says that all
michael@0 283 // fields have indeterminate values; we initialize them to zero for
michael@0 284 // simplicity. The only thing that really matters is that the arrays
michael@0 285 // are all initially null pointers, and the array element counts are all
michael@0 286 // initially zero.
michael@0 287 ios_base::ios_base()
michael@0 288 : _M_fmtflags(0), _M_iostate(0), _M_openmode(0), _M_seekdir(0),
michael@0 289 _M_exception_mask(0),
michael@0 290 _M_precision(0), _M_width(0),
michael@0 291 _M_locale(),
michael@0 292 _M_callbacks(0), _M_num_callbacks(0), _M_callback_index(0),
michael@0 293 _M_iwords(0), _M_num_iwords(0),
michael@0 294 _M_pwords(0),
michael@0 295 _M_num_pwords(0)
michael@0 296 {}
michael@0 297
michael@0 298 // ios's destructor.
michael@0 299 ios_base::~ios_base() {
michael@0 300 _M_invoke_callbacks(erase_event);
michael@0 301 free(_M_callbacks);
michael@0 302 free(_M_iwords);
michael@0 303 free(_M_pwords);
michael@0 304 }
michael@0 305
michael@0 306 //----------------------------------------------------------------------
michael@0 307 // Force instantiation of basic_ios
michael@0 308 // For DLL exports, they are already instantiated.
michael@0 309 #if !defined(_STLP_NO_FORCE_INSTANTIATE)
michael@0 310 template class _STLP_CLASS_DECLSPEC basic_ios<char, char_traits<char> >;
michael@0 311 # if !defined (_STLP_NO_WCHAR_T)
michael@0 312 template class _STLP_CLASS_DECLSPEC basic_ios<wchar_t, char_traits<wchar_t> >;
michael@0 313 # endif /* _STLP_NO_WCHAR_T */
michael@0 314 #endif
michael@0 315
michael@0 316 _STLP_END_NAMESPACE
michael@0 317
michael@0 318 // Local Variables:
michael@0 319 // mode:C++
michael@0 320 // End:

mercurial