ReactOS 0.4.16-dev-122-g325d74c
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.
44const ios_base::fmtflags ios_base::left;
45const ios_base::fmtflags ios_base::right;
46const ios_base::fmtflags ios_base::internal;
47const ios_base::fmtflags ios_base::dec;
48const ios_base::fmtflags ios_base::hex;
49const ios_base::fmtflags ios_base::oct;
50const ios_base::fmtflags ios_base::fixed;
51const ios_base::fmtflags ios_base::scientific;
52const ios_base::fmtflags ios_base::boolalpha;
53const ios_base::fmtflags ios_base::showbase;
54const ios_base::fmtflags ios_base::showpoint;
55const ios_base::fmtflags ios_base::showpos;
56const ios_base::fmtflags ios_base::skipws;
57const ios_base::fmtflags ios_base::unitbuf;
58const ios_base::fmtflags ios_base::uppercase;
59const ios_base::fmtflags ios_base::adjustfield;
60const ios_base::fmtflags ios_base::basefield;
61const ios_base::fmtflags ios_base::floatfield;
62
63// Definitions of ios_base's state flags.
64const ios_base::iostate ios_base::goodbit;
65const ios_base::iostate ios_base::badbit;
66const ios_base::iostate ios_base::eofbit;
67const ios_base::iostate ios_base::failbit;
68
69// Definitions of ios_base's openmode flags.
70const ios_base::openmode ios_base::app;
71const ios_base::openmode ios_base::ate;
72const ios_base::openmode ios_base::binary;
73const ios_base::openmode ios_base::in;
74const ios_base::openmode ios_base::out;
75const ios_base::openmode ios_base::trunc;
76
77// Definitions of ios_base's seekdir flags.
78const ios_base::seekdir ios_base::beg;
79const ios_base::seekdir ios_base::cur;
80const ios_base::seekdir ios_base::end;
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
92template <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.
114template <class PODType>
115static 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
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
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
184 if (tmp.first) {
185 _M_callbacks = tmp.first;
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");
213 ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_iostate));
214 strcpy(ptr, " mask = 0x");
215 ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_exception_mask));
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) {
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) {
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.
288 _M_precision(0), _M_width(0),
289 _M_locale(),
292 _M_pwords(0),
294{}
295
296// ios's destructor.
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 N
Definition: crc32.c:57
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
#define _STLP_CALL
Definition: _bc.h:131
pair< _T1, _T2 > _STLP_CALL make_pair(_T1 __x, _T2 __y)
Definition: _pair.h:124
#define _STLP_ATOMIC_INCREMENT(__x)
Definition: _sparc_atomic.h:57
size_t __stl_atomic_t
Definition: _threads.h:232
#define _STLP_MUTEX_INITIALIZER
Definition: _threads.h:241
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define index(s, c)
Definition: various.h:29
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
Definition: _ios.h:48
virtual ~failure() _STLP_NOTHROW_INHERENTLY
Definition: ios.cpp:40
failure(const string &)
Definition: ios.cpp:36
void(* event_callback)(event, ios_base &, int __index)
Definition: _ios_base.h:155
size_t _M_num_pwords
Definition: _ios_base.h:231
void ** _M_pwords
Definition: _ios_base.h:230
iostate _M_iostate
Definition: _ios_base.h:212
long & iword(int __index)
Definition: ios.cpp:148
void register_callback(event_callback __fn, int __index)
Definition: ios.cpp:181
size_t _M_num_callbacks
Definition: _ios_base.h:223
locale _M_locale
Definition: _ios_base.h:220
virtual ~ios_base()
Definition: ios.cpp:297
pair< event_callback, int > * _M_callbacks
Definition: _ios_base.h:222
void _M_setstate_nothrow(iostate __state)
Definition: _ios_base.h:185
ios_base()
Definition: ios.cpp:285
int openmode
Definition: _ios_base.h:59
size_t _M_callback_index
Definition: _ios_base.h:224
streamsize _M_precision
Definition: _ios_base.h:217
void _M_check_exception_mask()
Definition: _ios_base.h:189
int iostate
Definition: _ios_base.h:58
int fmtflags
Definition: _ios_base.h:57
void _M_copy_state(const ios_base &__x)
Definition: ios.cpp:232
streamsize _M_width
Definition: _ios_base.h:218
size_t _M_num_iwords
Definition: _ios_base.h:228
fmtflags _M_fmtflags
Definition: _ios_base.h:211
int seekdir
Definition: _ios_base.h:60
locale imbue(const locale &)
Definition: ios.cpp:122
@ erase_event
Definition: _ios_base.h:154
@ imbue_event
Definition: _ios_base.h:154
openmode _M_openmode
Definition: _ios_base.h:213
void _M_invoke_callbacks(event)
Definition: ios.cpp:197
iostate _M_exception_mask
Definition: _ios_base.h:215
void *& pword(int __index)
Definition: ios.cpp:165
long * _M_iwords
Definition: _ios_base.h:227
static int _STLP_CALL xalloc()
Definition: ios.cpp:135
seekdir _M_seekdir
Definition: _ios_base.h:214
void _STLP_FUNCTION_THROWS _M_throw_failure()
Definition: ios.cpp:207
Definition: _locale.h:75
#define realloc
Definition: debug_ros.c:6
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
static const WCHAR E[]
Definition: oid.c:1253
#define _STLP_NOTHROW_INHERENTLY
Definition: features.h:858
#define _STLP_STATIC_MUTEX
Definition: features.h:267
#define __STATIC_CAST(__x, __y)
Definition: features.h:585
#define _STLP_CLASS_DECLSPEC
Definition: features.h:983
#define _STLP_BEGIN_NAMESPACE
Definition: features.h:501
#define _STLP_END_NAMESPACE
Definition: features.h:503
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
GLuint buffer
Definition: glext.h:5915
GLuint index
Definition: glext.h:6031
GLfloat f
Definition: glext.h:7540
GLuint64EXT * result
Definition: glext.h:11304
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
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
static pair< PODType *, size_t > _Stl_expand_array(PODType *__array, size_t N, int index)
Definition: ios.cpp:94
static PODType * _Stl_copy_array(const PODType *__array, size_t N)
Definition: ios.cpp:115
#define f
Definition: ke_i.h:83
static PVOID ptr
Definition: dispmode.c:27
char *_STLP_CALL __write_integer(char *buf, ios_base::fmtflags flags, long x)
Definition: num_put.cpp:125
Definition: _pair.h:47
_T2 second
Definition: _pair.h:52
_T1 first
Definition: _pair.h:51
#define max(a, b)
Definition: svc.c:63
void * arg
Definition: msvc.h:10