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