ReactOS  0.4.14-dev-317-g96040ec
ios.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 
19 #include "stlport_prefix.h"
20 
21 #include <algorithm>
22 #include <ios>
23 #include <locale>
24 #include <ostream> // for __get_ostreambuf definition
25 
26 #include "aligned_buffer.h"
27 
29 
30 //----------------------------------------------------------------------
31 // ios_base members
32 
33 // class ios_base::failure, a subclass of exception. It's used solely
34 // for reporting errors.
35 
38 {}
39 
41 
42 #if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION)
43 // Definitions of ios_base's formatting flags.
59 const ios_base::fmtflags ios_base::adjustfield;
60 const ios_base::fmtflags ios_base::basefield;
61 const ios_base::fmtflags ios_base::floatfield;
62 
63 // Definitions of ios_base's state flags.
64 const ios_base::iostate ios_base::goodbit;
65 const ios_base::iostate ios_base::badbit;
66 const ios_base::iostate ios_base::eofbit;
67 const ios_base::iostate ios_base::failbit;
68 
69 // Definitions of ios_base's openmode flags.
70 const ios_base::openmode ios_base::app;
71 const ios_base::openmode ios_base::ate;
76 
77 // Definitions of ios_base's seekdir flags.
78 const ios_base::seekdir ios_base::beg;
79 const ios_base::seekdir ios_base::cur;
81 
82 #endif
83 
84 // Internal functions used for managing exponentially-growing arrays of
85 // POD types.
86 
87 // array is a pointer to N elements of type PODType. Expands the array,
88 // if necessary, so that array[index] is meaningful. All new elements are
89 // initialized to zero. Returns a pointer to the new array, and the new
90 // size.
91 
92 template <class PODType>
94 _Stl_expand_array(PODType* __array, size_t N, int index) {
95  if ((int)N < index + 1) {
96  size_t new_N = (max)(2 * N, size_t(index + 1));
97  PODType* new_array
98  = __STATIC_CAST(PODType*,realloc(__array, new_N * sizeof(PODType)));
99  if (new_array) {
100  fill(new_array + N, new_array + new_N, PODType());
101  return pair<PODType*, size_t>(new_array, new_N);
102  }
103  else
104  return pair<PODType*, size_t>(__STATIC_CAST(PODType*,0), 0);
105  }
106  else
107  return pair<PODType*, size_t>(__array, N);
108 }
109 
110 // array is a pointer to N elements of type PODType. Allocate a new
111 // array of N elements, copying the values from the old array to the new.
112 // Return a pointer to the new array. It is assumed that array is non-null
113 // and N is nonzero.
114 template <class PODType>
115 static PODType* _Stl_copy_array(const PODType* __array, size_t N) {
116  PODType* result = __STATIC_CAST(PODType*,malloc(N * sizeof(PODType)));
117  if (result)
118  copy(__array, __array + N, result);
119  return result;
120 }
121 
123  if (loc != _M_locale) {
124  locale previous = _M_locale;
125  _M_locale = loc;
127  return previous;
128  }
129  else {
131  return _M_locale;
132  }
133 }
134 
136 #if defined (_STLP_THREADS) && \
137  defined (_STLP_WIN32THREADS) && defined (_STLP_NEW_PLATFORM_SDK)
138  static volatile __stl_atomic_t _S_index = 0;
139  return _STLP_ATOMIC_INCREMENT(&_S_index);
140 #else
141  static int _S_index = 0;
143  _STLP_auto_lock sentry(__lock);
144  return _S_index++;
145 #endif
146 }
147 
148 long& ios_base::iword(int index) {
149  static long dummy = 0;
150 
152  if (tmp.first) { // The allocation, if any, succeeded.
153  _M_iwords = tmp.first;
154  _M_num_iwords = tmp.second;
155  return _M_iwords[index];
156  }
157  else {
158  _M_setstate_nothrow(badbit);
160  return dummy;
161  }
162 }
163 
164 
165 void*& ios_base::pword(int index) {
166  static void* dummy = 0;
167 
169  if (tmp.first) { // The allocation, if any, succeeded.
170  _M_pwords = tmp.first;
171  _M_num_pwords = tmp.second;
172  return _M_pwords[index];
173  }
174  else {
175  _M_setstate_nothrow(badbit);
177  return dummy;
178  }
179 }
180 
182  pair<pair<event_callback, int>*, size_t> tmp
183  = _Stl_expand_array(_M_callbacks, _M_num_callbacks, (int)_M_callback_index /* fbp: index ??? */ );
184  if (tmp.first) {
185  _M_callbacks = tmp.first;
186  _M_num_callbacks = tmp.second;
188  }
189  else {
190  _M_setstate_nothrow(badbit);
192  }
193 }
194 
195 // Invokes all currently registered callbacks for a particular event.
196 // Behaves correctly even if one of the callbacks adds a new callback.
198  for (size_t i = _M_callback_index; i > 0; --i) {
200  int n = _M_callbacks[i-1].second;
201  f(E, *this, n);
202  }
203 }
204 
205 // This function is called if the state, rdstate(), has a bit set
206 // that is also set in the exception mask exceptions().
208  const char* arg ;
209 # if 0
210  char buffer[256];
211  char* ptr;
212  strcpy(buffer, "ios failure: rdstate = 0x");
214  strcpy(ptr, " mask = 0x");
216  *ptr = 0;
217  arg = buffer;
218 # else
219  arg = "ios failure";
220 # endif
221 
222 # ifndef _STLP_USE_EXCEPTIONS
223  fputs(arg, stderr);
224 # else
225  throw failure(arg);
226 # endif
227 }
228 
229 // Copy x's state to *this. This member function is used in the
230 // implementation of basic_ios::copyfmt. Does not copy _M_exception_mask
231 // or _M_iostate.
233  _M_fmtflags = x._M_fmtflags; // Copy the flags, except for _M_iostate
234  _M_openmode = x._M_openmode; // and _M_exception_mask.
235  _M_seekdir = x._M_seekdir;
236  _M_precision = x._M_precision;
237  _M_width = x._M_width;
238  _M_locale = x._M_locale;
239 
240  if (x._M_callbacks) {
241  pair<event_callback, int>* tmp = _Stl_copy_array(x._M_callbacks, x._M_callback_index);
242  if (tmp) {
244  _M_callbacks = tmp;
245  _M_num_callbacks = _M_callback_index = x._M_callback_index;
246  }
247  else {
248  _M_setstate_nothrow(badbit);
250  }
251  }
252 
253  if (x._M_iwords) {
254  long* tmp = _Stl_copy_array(x._M_iwords, x._M_num_iwords);
255  if (tmp) {
256  free(_M_iwords);
257  _M_iwords = tmp;
258  _M_num_iwords = x._M_num_iwords;
259  }
260  else {
261  _M_setstate_nothrow(badbit);
263  }
264  }
265 
266  if (x._M_pwords) {
267  void** tmp = _Stl_copy_array(x._M_pwords, x._M_num_pwords);
268  if (tmp) {
269  free(_M_pwords);
270  _M_pwords = tmp;
271  _M_num_pwords = x._M_num_pwords;
272  }
273  else {
274  _M_setstate_nothrow(badbit);
276  }
277  }
278 }
279 
280 // ios's (protected) default constructor. The standard says that all
281 // fields have indeterminate values; we initialize them to zero for
282 // simplicity. The only thing that really matters is that the arrays
283 // are all initially null pointers, and the array element counts are all
284 // initially zero.
286  : _M_fmtflags(0), _M_iostate(0), _M_openmode(0), _M_seekdir(0),
288  _M_precision(0), _M_width(0),
289  _M_locale(),
291  _M_iwords(0), _M_num_iwords(0),
292  _M_pwords(0),
293  _M_num_pwords(0)
294 {}
295 
296 // ios's destructor.
300  free(_M_iwords);
301  free(_M_pwords);
302 }
303 
304 //----------------------------------------------------------------------
305 // Force instantiation of basic_ios
306 // For DLL exports, they are already instantiated.
307 #if !defined(_STLP_NO_FORCE_INSTANTIATE)
309 # if !defined (_STLP_NO_WCHAR_T)
311 # endif /* _STLP_NO_WCHAR_T */
312 #endif
313 
315 
316 // Local Variables:
317 // mode:C++
318 // End:
#define realloc
Definition: debug_ros.c:6
long * _M_iwords
Definition: _ios_base.h:227
locale imbue(const locale &)
Definition: ios.cpp:122
void _STLP_FUNCTION_THROWS _M_throw_failure()
Definition: ios.cpp:207
#define max(a, b)
Definition: svc.c:63
static int _STLP_CALL xalloc()
Definition: ios.cpp:135
#define _STLP_STATIC_MUTEX
Definition: features.h:267
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static pair< PODType *, size_t > _Stl_expand_array(PODType *__array, size_t N, int index)
Definition: ios.cpp:94
openmode _M_openmode
Definition: _ios_base.h:213
ios_base &_STLP_CALL showbase(ios_base &__s)
Definition: _ios_base.h:273
ios_base &_STLP_CALL skipws(ios_base &__s)
Definition: _ios_base.h:291
#define __STATIC_CAST(__x, __y)
Definition: features.h:585
#define free
Definition: debug_ros.c:5
size_t _M_num_pwords
Definition: _ios_base.h:231
ios_base &_STLP_CALL showpoint(ios_base &__s)
Definition: _ios_base.h:279
char *_STLP_CALL __write_integer(char *buf, ios_base::fmtflags flags, long x)
Definition: num_put.cpp:125
GLdouble n
Definition: glext.h:7729
size_t _M_callback_index
Definition: _ios_base.h:224
GLuint buffer
Definition: glext.h:5915
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
pair< _T1, _T2 > _STLP_CALL make_pair(_T1 __x, _T2 __y)
Definition: _pair.h:124
ios_base &_STLP_CALL dec(ios_base &__s)
Definition: _ios_base.h:321
void * arg
Definition: msvc.h:12
GLuint GLuint end
Definition: gl.h:1545
static const char hex[16]
Definition: profile.c:123
int seekdir
Definition: _ios_base.h:60
void _M_invoke_callbacks(event)
Definition: ios.cpp:197
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
ios_base &_STLP_CALL fixed(ios_base &__s)
Definition: _ios_base.h:332
long & iword(int __index)
Definition: ios.cpp:148
Definition: _locale.h:75
ios_base &_STLP_CALL boolalpha(ios_base &__s)
Definition: _ios_base.h:267
static PVOID ptr
Definition: dispmode.c:27
double __cdecl trunc(double)
#define _STLP_NOTHROW_INHERENTLY
Definition: features.h:858
streamsize _M_precision
Definition: _ios_base.h:217
ios_base &_STLP_CALL scientific(ios_base &__s)
Definition: _ios_base.h:335
void _M_copy_state(const ios_base &__x)
Definition: ios.cpp:232
seekdir _M_seekdir
Definition: _ios_base.h:214
GLuint index
Definition: glext.h:6031
Definition: _ios.h:48
void register_callback(event_callback __fn, int __index)
Definition: ios.cpp:181
_T1 first
Definition: _pair.h:51
ios_base()
Definition: ios.cpp:285
GLfloat f
Definition: glext.h:7540
pair< event_callback, int > * _M_callbacks
Definition: _ios_base.h:222
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
int openmode
Definition: _ios_base.h:59
ios_base &_STLP_CALL uppercase(ios_base &__s)
Definition: _ios_base.h:297
static FILE * out
Definition: regtests2xml.c:44
GLint left
Definition: glext.h:7726
const GLuint GLenum const GLvoid * binary
Definition: glext.h:7538
GLdouble GLdouble right
Definition: glext.h:10859
fmtflags _M_fmtflags
Definition: _ios_base.h:211
void _M_check_exception_mask()
Definition: _ios_base.h:189
void ** _M_pwords
Definition: _ios_base.h:230
iostate _M_exception_mask
Definition: _ios_base.h:215
int iostate
Definition: _ios_base.h:58
#define index(s, c)
Definition: various.h:29
failure(const string &)
Definition: ios.cpp:36
size_t _M_num_callbacks
Definition: _ios_base.h:223
void(* event_callback)(event, ios_base &, int __index)
Definition: _ios_base.h:155
GLdouble s
Definition: gl.h:2039
#define _STLP_CLASS_DECLSPEC
Definition: features.h:983
#define _STLP_MUTEX_INITIALIZER
Definition: _threads.h:241
static const WCHAR E[]
Definition: oid.c:1253
virtual ~failure() _STLP_NOTHROW_INHERENTLY
Definition: ios.cpp:40
int fmtflags
Definition: _ios_base.h:57
ios_base &_STLP_CALL showpos(ios_base &__s)
Definition: _ios_base.h:285
GLuint in
Definition: glext.h:9616
unsigned char dummy
Definition: maze.c:118
#define f
Definition: ke_i.h:83
#define _STLP_END_NAMESPACE
Definition: features.h:503
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
void *& pword(int __index)
Definition: ios.cpp:165
Definition: _pair.h:47
ios_base &_STLP_CALL oct(ios_base &__s)
Definition: _ios_base.h:327
ios_base &_STLP_CALL internal(ios_base &__s)
Definition: _ios_base.h:311
_T2 second
Definition: _pair.h:52
static PODType * _Stl_copy_array(const PODType *__array, size_t N)
Definition: ios.cpp:115
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
size_t _M_num_iwords
Definition: _ios_base.h:228
FILE * stderr
#define malloc
Definition: debug_ros.c:4
#define _STLP_BEGIN_NAMESPACE
Definition: features.h:501
size_t __stl_atomic_t
Definition: _threads.h:232
streamsize _M_width
Definition: _ios_base.h:218
iostate _M_iostate
Definition: _ios_base.h:212
GLuint64EXT * result
Definition: glext.h:11304
#define _STLP_CALL
Definition: _bc.h:131
virtual ~ios_base()
Definition: ios.cpp:297
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
ios_base &_STLP_CALL unitbuf(ios_base &__s)
Definition: _ios_base.h:303
void _M_setstate_nothrow(iostate __state)
Definition: _ios_base.h:185
locale _M_locale
Definition: _ios_base.h:220
#define _STLP_ATOMIC_INCREMENT(__x)
Definition: _sparc_atomic.h:57