ReactOS  0.4.13-dev-99-g7e18b6d
_ostream.c
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 #ifndef _STLP_OSTREAM_C
19 #define _STLP_OSTREAM_C
20 
21 #ifndef _STLP_INTERNAL_OSTREAM_H
22 # include <stl/_ostream.h>
23 #endif
24 
25 #if !defined (_STLP_INTERNAL_NUM_PUT_H)
26 # include <stl/_num_put.h> // For basic_streambuf and iterators
27 #endif
28 
30 
31 //----------------------------------------------------------------------
32 // Definitions of non-inline member functions.
33 
34 // Constructor, destructor
35 
36 template <class _CharT, class _Traits>
38  : basic_ios<_CharT, _Traits>() {
39  this->init(__buf);
40 }
41 
42 template <class _CharT, class _Traits>
44 {}
45 
46 // Output directly from a streambuf.
47 template <class _CharT, class _Traits>
50  sentry __sentry(*this);
51  if (__sentry) {
52  if (__from) {
53  bool __any_inserted = __from->gptr() != __from->egptr()
54  ? this->_M_copy_buffered(__from, this->rdbuf())
55  : this->_M_copy_unbuffered(__from, this->rdbuf());
56  if (!__any_inserted)
57  this->setstate(ios_base::failbit);
58  }
59  else
60  this->setstate(ios_base::badbit);
61  }
62 
63  return *this;
64 }
65 
66 // Helper functions for the streambuf version of operator<<. The
67 // exception-handling code is complicated because exceptions thrown
68 // while extracting characters are treated differently than exceptions
69 // thrown while inserting characters.
70 
71 template <class _CharT, class _Traits>
75  bool __any_inserted = false;
76 
77  while (__from->egptr() != __from->gptr()) {
78  const ptrdiff_t __avail = __from->egptr() - __from->gptr();
79 
80  streamsize __nwritten;
81  _STLP_TRY {
82  __nwritten = __to->sputn(__from->gptr(), __avail);
83  __from->gbump((int)__nwritten);
84  }
86  this->_M_handle_exception(ios_base::badbit);
87  return __any_inserted;
88  }
89 
90  if (__nwritten == __avail) {
91  _STLP_TRY {
92  if (this->_S_eof(__from->sgetc()))
93  return true;
94  else
95  __any_inserted = true;
96  }
98  this->_M_handle_exception(ios_base::failbit);
99  return false;
100  }
101  }
102  else if (__nwritten != 0)
103  return true;
104  else
105  return __any_inserted;
106  }
107 
108  // No characters are in the buffer, but we aren't at EOF. Switch to
109  // unbuffered mode.
110  return __any_inserted || this->_M_copy_unbuffered(__from, __to);
111 }
112 
113 /*
114  * Helper struct (guard) to put back a character in a streambuf
115  * whenever an exception or an eof occur.
116  */
117 template <class _CharT, class _Traits>
118 struct _SPutBackC {
120  typedef typename _StreamBuf::int_type int_type;
122  : __pfrom(pfrom), __c(0), __do_guard(false) {}
124  if (__do_guard) {
125  __pfrom->sputbackc(_Traits::to_char_type(__c));
126  }
127  }
128 
129  void guard(int_type c) {
130  __c = c;
131  __do_guard = true;
132  }
133  void release() {
134  __do_guard = false;
135  }
136 
137 private:
141 };
142 
143 template <class _CharT, class _Traits>
147  typedef _SPutBackC<_CharT, _Traits> _SPutBackCGuard;
148  bool __any_inserted = false;
149  int_type __c;
150 
151  _STLP_TRY {
152  _SPutBackCGuard __cguard(__from);
153  for (;;) {
154  _STLP_TRY {
155  __c = __from->sbumpc();
156  }
158  this->_M_handle_exception(ios_base::failbit);
159  break;
160  }
161 
162  if (this->_S_eof(__c))
163  break;
164 
165  __cguard.guard(__c);
166 #if defined (__DMC__)
167  _STLP_TRY {
168 #endif
169  if (this->_S_eof(__to->sputc(_Traits::to_char_type(__c))))
170  break;
171 
172 #if defined (__DMC__)
173  }
175  this->_M_handle_exception(ios_base::badbit);
176  break;
177  }
178 #endif
179  __cguard.release();
180  __any_inserted = true;
181  }
182  }
184  this->_M_handle_exception(ios_base::badbit);
185  }
186  return __any_inserted;
187 }
188 
190 
191 // Helper function for numeric output.
192 template <class _CharT, class _Traits, class _Number>
195  typedef typename basic_ostream<_CharT, _Traits>::sentry _Sentry;
196  _Sentry __sentry(__os);
197  bool __failed = true;
198 
199  if (__sentry) {
200  _STLP_TRY {
202  __failed = (use_facet<_NumPut>(__os.getloc())).put(ostreambuf_iterator<_CharT, _Traits>(__os.rdbuf()),
203  __os, __os.fill(),
204  __x).failed();
205  }
207  __os._M_handle_exception(ios_base::badbit);
208  }
209  }
210  if (__failed)
211  __os.setstate(ios_base::badbit);
212  return __os;
213 }
214 
216 
217 /*
218  * In the following operators we try to limit code bloat by limiting the
219  * number of __put_num instanciations.
220  */
221 template <class _CharT, class _Traits>
223  _STLP_STATIC_ASSERT( sizeof(short) <= sizeof(long) )
224  long __tmp = ((this->flags() & _Basic_ios::basefield) != ios_base::dec) ?
225  __STATIC_CAST(long, __STATIC_CAST(unsigned short, __x)): __x;
226  return _STLP_PRIV __put_num(*this, __tmp);
227 }
228 
229 template <class _CharT, class _Traits>
231  _STLP_STATIC_ASSERT( sizeof(unsigned short) <= sizeof(unsigned long) )
232  return _STLP_PRIV __put_num(*this, __STATIC_CAST(unsigned long,__x));
233 }
234 
235 template <class _CharT, class _Traits>
237  _STLP_STATIC_ASSERT( sizeof(int) <= sizeof(long) )
238  long __tmp = ((this->flags() & _Basic_ios::basefield) != ios_base::dec) ?
239  __STATIC_CAST(long, __STATIC_CAST(unsigned int, __x)): __x;
240  return _STLP_PRIV __put_num(*this, __tmp);
241 }
242 
243 template <class _CharT, class _Traits>
244 #if defined (_WIN64) || !defined (_STLP_MSVC) || (_STLP_MSVC < 1300)
246  _STLP_STATIC_ASSERT( sizeof(unsigned int) <= sizeof(unsigned long) )
247 #else
248 /* We define this operator with size_t rather than unsigned int to avoid
249  * 64 bits warning.
250  */
252  _STLP_STATIC_ASSERT( sizeof(size_t) <= sizeof(unsigned long) )
253 #endif
254  return _STLP_PRIV __put_num(*this, __STATIC_CAST(unsigned long,__x));
255 }
256 
257 template <class _CharT, class _Traits>
259 { return _STLP_PRIV __put_num(*this, __x); }
260 
261 template <class _CharT, class _Traits>
263 { return _STLP_PRIV __put_num(*this, __x); }
264 
265 #ifdef _STLP_LONG_LONG
266 template <class _CharT, class _Traits>
268 { return _STLP_PRIV __put_num(*this, __x); }
269 
270 template <class _CharT, class _Traits>
272 { return _STLP_PRIV __put_num(*this, __x); }
273 #endif
274 
275 template <class _CharT, class _Traits>
277 { return _STLP_PRIV __put_num(*this, __STATIC_CAST(double,__x)); }
278 
279 template <class _CharT, class _Traits>
281 { return _STLP_PRIV __put_num(*this, __x); }
282 
283 #ifndef _STLP_NO_LONG_DOUBLE
284 template <class _CharT, class _Traits>
286 { return _STLP_PRIV __put_num(*this, __x); }
287 #endif
288 
289 template <class _CharT, class _Traits>
291 { return _STLP_PRIV __put_num(*this, __x); }
292 
293 #ifndef _STLP_NO_BOOL
294 template <class _CharT, class _Traits>
296 { return _STLP_PRIV __put_num(*this, __x); }
297 #endif
298 
299 template <class _CharT, class _Traits>
301  sentry __sentry(*this);
302  if (__sentry) {
303  bool __failed = true;
304  _STLP_TRY {
305  streamsize __npad = this->width() > 0 ? this->width() - 1 : 0;
306  // if (__npad <= 1)
307  if (__npad == 0)
308  __failed = this->_S_eof(this->rdbuf()->sputc(__c));
309  else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
310  __failed = this->_S_eof(this->rdbuf()->sputc(__c));
311  __failed = __failed ||
312  this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
313  }
314  else {
315  __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
316  __failed = __failed || this->_S_eof(this->rdbuf()->sputc(__c));
317  }
318 
319  this->width(0);
320  }
322  this->_M_handle_exception(ios_base::badbit);
323  }
324 
325  if (__failed)
326  this->setstate(ios_base::badbit);
327  }
328 }
329 
330 template <class _CharT, class _Traits>
332  sentry __sentry(*this);
333  if (__sentry) {
334  bool __failed = true;
336  streamsize __npad = this->width() > __n ? this->width() - __n : 0;
337 
338  _STLP_TRY {
339  if (__npad == 0)
340  __failed = this->rdbuf()->sputn(__s, __n) != __n;
341  else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
342  __failed = this->rdbuf()->sputn(__s, __n) != __n;
343  __failed = __failed ||
344  this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
345  }
346  else {
347  __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
348  __failed = __failed || this->rdbuf()->sputn(__s, __n) != __n;
349  }
350 
351  this->width(0);
352  }
354  this->_M_handle_exception(ios_base::badbit);
355  }
356 
357  if (__failed)
358  this->setstate(ios_base::failbit);
359  }
360 }
361 
362 template <class _CharT, class _Traits>
364  sentry __sentry(*this);
365  if (__sentry) {
366  bool __failed = true;
368  streamsize __npad = this->width() > __n ? this->width() - __n : 0;
369 
370  _STLP_TRY {
371  if (__npad == 0)
372  __failed = !this->_M_put_widen_aux(__s, __n);
373  else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
374  __failed = !this->_M_put_widen_aux(__s, __n);
375  __failed = __failed ||
376  this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
377  }
378  else {
379  __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
380  __failed = __failed || !this->_M_put_widen_aux(__s, __n);
381  }
382 
383  this->width(0);
384  }
386  this->_M_handle_exception(ios_base::badbit);
387  }
388 
389  if (__failed)
390  this->setstate(ios_base::failbit);
391  }
392 }
393 
394 template <class _CharT, class _Traits>
396  streamsize __n) {
397  basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
398 
399  for ( ; __n > 0 ; --__n)
400  if (this->_S_eof(__buf->sputc(this->widen(*__s++))))
401  return false;
402  return true;
403 }
404 
405 // Unformatted output of a single character.
406 template <class _CharT, class _Traits>
409  sentry __sentry(*this);
410  bool __failed = true;
411 
412  if (__sentry) {
413  _STLP_TRY {
414  __failed = this->_S_eof(this->rdbuf()->sputc(__c));
415  }
417  this->_M_handle_exception(ios_base::badbit);
418  }
419  }
420 
421  if (__failed)
422  this->setstate(ios_base::badbit);
423 
424  return *this;
425 }
426 
427 // Unformatted output of a single character.
428 template <class _CharT, class _Traits>
431  sentry __sentry(*this);
432  bool __failed = true;
433 
434  if (__sentry) {
435  _STLP_TRY {
436  __failed = this->rdbuf()->sputn(__s, __n) != __n;
437  }
439  this->_M_handle_exception(ios_base::badbit);
440  }
441  }
442 
443  if (__failed)
444  this->setstate(ios_base::badbit);
445 
446  return *this;
447 }
448 
450 
451 #endif /* _STLP_OSTREAM_C */
452 
453 // Local Variables:
454 // mode:C++
455 // End:
_Traits::int_type int_type
Definition: _streambuf.h:54
GLint GLint GLsizei width
Definition: gl.h:1546
void gbump(int __n)
Definition: _streambuf.h:91
return __n
Definition: _algo.h:75
char_type * egptr() const
Definition: _streambuf.h:89
_Self & put(char_type __c)
Definition: _ostream.c:408
basic_streambuf< _CharT, _Traits > * rdbuf() const
Definition: _ios.h:72
char_type fill() const
Definition: _ios.h:81
bool __do_guard
Definition: _ostream.c:140
#define __STATIC_CAST(__x, __y)
Definition: features.h:585
void _M_handle_exception(ios_base::iostate __flag)
Definition: _ios.c:114
_StreamBuf * __pfrom
Definition: _ostream.c:138
locale getloc() const
Definition: _ios_base.h:143
ios_base &_STLP_CALL dec(ios_base &__s)
Definition: _ios_base.h:321
bool _M_put_widen_aux(const char *__s, streamsize __n)
Definition: _ostream.c:395
int_type sputc(char_type __c)
Definition: _streambuf.h:198
void _M_put_widen(const char *__s)
Definition: _ostream.c:363
void _M_put_char(_CharT __c)
Definition: _ostream.c:300
#define __c
Definition: schilyio.h:209
#define _STLP_MOVE_TO_PRIV_NAMESPACE
Definition: features.h:524
bool _M_copy_unbuffered(basic_streambuf< _CharT, _Traits > *__from, basic_streambuf< _CharT, _Traits > *__to)
Definition: _ostream.c:145
void release()
Definition: _ostream.c:133
_SPutBackC(_StreamBuf *pfrom)
Definition: _ostream.c:121
bool _M_copy_buffered(basic_streambuf< _CharT, _Traits > *__from, basic_streambuf< _CharT, _Traits > *__to)
Definition: _ostream.c:73
int_type sbumpc()
Definition: _streambuf.h:227
#define _STLP_LONG_LONG
Definition: _apcc.h:12
#define _STLP_CATCH_ALL
Definition: features.h:818
Definition: _ios.h:48
#define _STLP_MOVE_TO_STD_NAMESPACE
Definition: features.h:525
_Self & write(const char_type *__s, streamsize __n)
Definition: _ostream.c:430
_STLP_STATIC_ASSERT(sizeof(nl_catd_type)<=sizeof(int)) class _STLP_CLASS_DECLSPEC _Catalog_nl_catd_map
void guard(int_type c)
Definition: _ostream.c:129
basic_ostream(basic_streambuf< _CharT, _Traits > *__buf)
Definition: _ostream.c:37
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
char_traits< char > ::int_type int_type
Definition: _ios.h:52
_STLP_MOVE_TO_PRIV_NAMESPACE basic_ostream< _CharT, _Traits > &_STLP_CALL __put_num(basic_ostream< _CharT, _Traits > &__os, _Number __x)
Definition: _ostream.c:194
static size_t _STLP_CALL length(const char_type *__s)
Definition: char_traits.h:153
const GLubyte * c
Definition: glext.h:8905
GLint left
Definition: glext.h:7726
basic_streambuf< _CharT, _Traits > _StreamBuf
Definition: _ostream.c:119
#define false
Definition: stdbool.h:38
GLbitfield flags
Definition: glext.h:7161
void init(basic_streambuf< _CharT, _Traits > *__streambuf)
Definition: _ios.c:97
#define _STLP_PRIV
Definition: _dm.h:70
void _M_put_nowiden(const _CharT *__s)
Definition: _ostream.c:331
char_type * gptr() const
Definition: _streambuf.h:88
void setstate(iostate __state)
Definition: _ios.h:95
iter_type put(iter_type __s, ios_base &__f, char_type __fill, bool __val) const
Definition: _num_put.h:60
streamsize sputn(const char_type *__s, streamsize __n)
Definition: _streambuf.h:204
#define _STLP_END_NAMESPACE
Definition: features.h:503
#define _STLP_TRY
Definition: features.h:817
__kernel_ptrdiff_t ptrdiff_t
Definition: linux.h:247
int_type sputbackc(char_type __c)
Definition: _streambuf.h:241
_Self & operator<<(__ostream_fn __f)
Definition: _ostream.h:78
~basic_ostream()
Definition: _ostream.c:43
ptrdiff_t streamsize
Definition: char_traits.h:81
#define c
Definition: ke_i.h:80
#define _STLP_BEGIN_NAMESPACE
Definition: features.h:501
int_type __c
Definition: _ostream.c:139
~_SPutBackC()
Definition: _ostream.c:123
#define _STLP_CALL
Definition: _bc.h:131
_StreamBuf::int_type int_type
Definition: _ostream.c:120
int_type sgetc()
Definition: _streambuf.h:233