epoc32/include/tools/stlport/stl/_ostream.c
branchSymbian3
changeset 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/tools/stlport/stl/_ostream.c	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -0,0 +1,446 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999
     1.6 + * Silicon Graphics Computer Systems, Inc.
     1.7 + *
     1.8 + * Copyright (c) 1999
     1.9 + * Boris Fomitchev
    1.10 + *
    1.11 + * This material is provided "as is", with absolutely no warranty expressed
    1.12 + * or implied. Any use is at your own risk.
    1.13 + *
    1.14 + * Permission to use or copy this software for any purpose is hereby granted
    1.15 + * without fee, provided the above notices are retained on all copies.
    1.16 + * Permission to modify the code and to distribute modified code is granted,
    1.17 + * provided the above notices are retained, and a notice that the code was
    1.18 + * modified is included with the above copyright notice.
    1.19 + *
    1.20 + */
    1.21 +#ifndef _STLP_OSTREAM_C
    1.22 +#define _STLP_OSTREAM_C
    1.23 +
    1.24 +#ifndef _STLP_INTERNAL_OSTREAM_H
    1.25 +#  include <stl/_ostream.h>
    1.26 +#endif
    1.27 +
    1.28 +#if !defined (_STLP_INTERNAL_NUM_PUT_H)
    1.29 +#  include <stl/_num_put.h>            // For basic_streambuf and iterators
    1.30 +#endif
    1.31 +
    1.32 +_STLP_BEGIN_NAMESPACE
    1.33 +
    1.34 +//----------------------------------------------------------------------
    1.35 +// Definitions of non-inline member functions.
    1.36 +
    1.37 +// Constructor, destructor
    1.38 +
    1.39 +template <class _CharT, class _Traits>
    1.40 +basic_ostream<_CharT, _Traits>::basic_ostream(basic_streambuf<_CharT, _Traits>* __buf)
    1.41 +    : basic_ios<_CharT, _Traits>() {
    1.42 +  this->init(__buf);
    1.43 +}
    1.44 +
    1.45 +template <class _CharT, class _Traits>
    1.46 +basic_ostream<_CharT, _Traits>::~basic_ostream()
    1.47 +{}
    1.48 +
    1.49 +// Output directly from a streambuf.
    1.50 +template <class _CharT, class _Traits>
    1.51 +basic_ostream<_CharT, _Traits>&
    1.52 +basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<_CharT, _Traits>* __from) {
    1.53 +  sentry __sentry(*this);
    1.54 +  if (__sentry) {
    1.55 +    if (__from) {
    1.56 +      bool __any_inserted = __from->gptr() != __from->egptr()
    1.57 +        ? this->_M_copy_buffered(__from, this->rdbuf())
    1.58 +        : this->_M_copy_unbuffered(__from, this->rdbuf());
    1.59 +      if (!__any_inserted)
    1.60 +        this->setstate(ios_base::failbit);
    1.61 +    }
    1.62 +    else
    1.63 +      this->setstate(ios_base::badbit);
    1.64 +  }
    1.65 +
    1.66 +  return *this;
    1.67 +}
    1.68 +
    1.69 +// Helper functions for the streambuf version of operator<<.  The
    1.70 +// exception-handling code is complicated because exceptions thrown
    1.71 +// while extracting characters are treated differently than exceptions
    1.72 +// thrown while inserting characters.
    1.73 +
    1.74 +template <class _CharT, class _Traits>
    1.75 +bool basic_ostream<_CharT, _Traits>
    1.76 +  ::_M_copy_buffered(basic_streambuf<_CharT, _Traits>* __from,
    1.77 +                     basic_streambuf<_CharT, _Traits>* __to) {
    1.78 +  bool __any_inserted = false;
    1.79 +
    1.80 +  while (__from->egptr() != __from->gptr()) {
    1.81 +    const ptrdiff_t __avail = __from->egptr() - __from->gptr();
    1.82 +
    1.83 +    streamsize __nwritten;
    1.84 +    _STLP_TRY {
    1.85 +      __nwritten = __to->sputn(__from->gptr(), __avail);
    1.86 +      __from->gbump((int)__nwritten);
    1.87 +    }
    1.88 +    _STLP_CATCH_ALL {
    1.89 +      this->_M_handle_exception(ios_base::badbit);
    1.90 +      return __any_inserted;
    1.91 +    }
    1.92 +
    1.93 +    if (__nwritten == __avail) {
    1.94 +      _STLP_TRY {
    1.95 +        if (this->_S_eof(__from->sgetc()))
    1.96 +          return true;
    1.97 +        else
    1.98 +          __any_inserted = true;
    1.99 +      }
   1.100 +      _STLP_CATCH_ALL {
   1.101 +        this->_M_handle_exception(ios_base::failbit);
   1.102 +        return false;
   1.103 +      }
   1.104 +    }
   1.105 +    else if (__nwritten != 0)
   1.106 +      return true;
   1.107 +    else
   1.108 +      return __any_inserted;
   1.109 +  }
   1.110 +
   1.111 +  // No characters are in the buffer, but we aren't at EOF.  Switch to
   1.112 +  // unbuffered mode.
   1.113 +  return __any_inserted || this->_M_copy_unbuffered(__from, __to);
   1.114 +}
   1.115 +
   1.116 +/*
   1.117 + * Helper struct (guard) to put back a character in a streambuf
   1.118 + * whenever an exception or an eof occur.
   1.119 + */
   1.120 +template <class _CharT, class _Traits>
   1.121 +struct _SPutBackC {
   1.122 +  typedef basic_streambuf<_CharT, _Traits> _StreamBuf;
   1.123 +  typedef typename _StreamBuf::int_type int_type;
   1.124 +  _SPutBackC(_StreamBuf *pfrom)
   1.125 +    : __pfrom(pfrom), __c(0), __do_guard(false) {}
   1.126 +  ~_SPutBackC() {
   1.127 +    if (__do_guard) {
   1.128 +      __pfrom->sputbackc(_Traits::to_char_type(__c));
   1.129 +    }
   1.130 +  }
   1.131 +
   1.132 +  void guard(int_type c) {
   1.133 +    __c = c;
   1.134 +    __do_guard = true;
   1.135 +  }
   1.136 +  void release() {
   1.137 +    __do_guard = false;
   1.138 +  }
   1.139 +
   1.140 +private:
   1.141 +  _StreamBuf *__pfrom;
   1.142 +  int_type __c;
   1.143 +  bool __do_guard;
   1.144 +};
   1.145 +
   1.146 +template <class _CharT, class _Traits>
   1.147 +bool basic_ostream<_CharT, _Traits>
   1.148 +  ::_M_copy_unbuffered(basic_streambuf<_CharT, _Traits>* __from,
   1.149 +                       basic_streambuf<_CharT, _Traits>* __to) {
   1.150 +  typedef _SPutBackC<_CharT, _Traits> _SPutBackCGuard;
   1.151 +  bool __any_inserted = false;
   1.152 +  int_type __c;
   1.153 +
   1.154 +  _STLP_TRY {
   1.155 +    _SPutBackCGuard __cguard(__from);
   1.156 +    for (;;) {
   1.157 +      _STLP_TRY {
   1.158 +        __c = __from->sbumpc();
   1.159 +      }
   1.160 +      _STLP_CATCH_ALL {
   1.161 +        this->_M_handle_exception(ios_base::failbit);
   1.162 +        return __any_inserted;
   1.163 +      }
   1.164 +
   1.165 +      if ( this->_S_eof(__c) )
   1.166 +        return __any_inserted;
   1.167 +
   1.168 +      __cguard.guard(__c);
   1.169 +      if ( this->_S_eof( __to->sputc(_Traits::to_char_type(__c)) ) ) {
   1.170 +        return __any_inserted;
   1.171 +      }
   1.172 +
   1.173 +      __cguard.release();
   1.174 +      __any_inserted = true;
   1.175 +    }
   1.176 +  }
   1.177 +  _STLP_CATCH_ALL {
   1.178 +    this->_M_handle_exception(ios_base::badbit);
   1.179 +    return __any_inserted;
   1.180 +  }
   1.181 +}
   1.182 +
   1.183 +_STLP_MOVE_TO_PRIV_NAMESPACE
   1.184 +
   1.185 +// Helper function for numeric output.
   1.186 +template <class _CharT, class _Traits, class _Number>
   1.187 +basic_ostream<_CharT, _Traits>&  _STLP_CALL
   1.188 +__put_num(basic_ostream<_CharT, _Traits>& __os, _Number __x) {
   1.189 +  typedef typename basic_ostream<_CharT, _Traits>::sentry _Sentry;
   1.190 +  _Sentry __sentry(__os);
   1.191 +  bool __failed = true;
   1.192 +
   1.193 +  if (__sentry) {
   1.194 +    _STLP_TRY {
   1.195 +      typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> > _NumPut;
   1.196 +      __failed = (use_facet<_NumPut>(__os.getloc())).put(ostreambuf_iterator<_CharT, _Traits>(__os.rdbuf()),
   1.197 +                                                         __os, __os.fill(),
   1.198 +                                                         __x).failed();
   1.199 +    }
   1.200 +    _STLP_CATCH_ALL {
   1.201 +      __os._M_handle_exception(ios_base::badbit);
   1.202 +    }
   1.203 +  }
   1.204 +  if (__failed)
   1.205 +    __os.setstate(ios_base::badbit);
   1.206 +  return __os;
   1.207 +}
   1.208 +
   1.209 +_STLP_MOVE_TO_STD_NAMESPACE
   1.210 +
   1.211 +/*
   1.212 + * In the following operators we try to limit code bloat by limiting the
   1.213 + * number of __put_num instanciations.
   1.214 + */
   1.215 +template <class _CharT, class _Traits>
   1.216 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __x) {
   1.217 +  _STLP_STATIC_ASSERT( sizeof(short) <= sizeof(long) )
   1.218 +  long __tmp = ((this->flags() & _Basic_ios::basefield) != ios_base::dec) ?
   1.219 +                  __STATIC_CAST(long, __STATIC_CAST(unsigned short, __x)): __x;
   1.220 +  return _STLP_PRIV __put_num(*this, __tmp);
   1.221 +}
   1.222 +
   1.223 +template <class _CharT, class _Traits>
   1.224 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __x) {
   1.225 +  _STLP_STATIC_ASSERT( sizeof(unsigned short) <= sizeof(unsigned long) )
   1.226 +  return _STLP_PRIV __put_num(*this, __STATIC_CAST(unsigned long,__x));
   1.227 +}
   1.228 +
   1.229 +template <class _CharT, class _Traits>
   1.230 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __x) {
   1.231 +  _STLP_STATIC_ASSERT( sizeof(int) <= sizeof(long) )
   1.232 +  long __tmp = ((this->flags() & _Basic_ios::basefield) != ios_base::dec) ?
   1.233 +                  __STATIC_CAST(long, __STATIC_CAST(unsigned int, __x)): __x;
   1.234 +  return _STLP_PRIV __put_num(*this, __tmp);
   1.235 +}
   1.236 +
   1.237 +template <class _CharT, class _Traits>
   1.238 +#if defined (_WIN64) || !defined (_STLP_MSVC) || (_STLP_MSVC < 1300)
   1.239 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __x) {
   1.240 +  _STLP_STATIC_ASSERT( sizeof(unsigned int) <= sizeof(unsigned long) )
   1.241 +#else
   1.242 +/* We define this operator with size_t rather than unsigned int to avoid
   1.243 + * 64 bits warning.
   1.244 + */
   1.245 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(size_t __x) {
   1.246 +  _STLP_STATIC_ASSERT( sizeof(size_t) <= sizeof(unsigned long) )
   1.247 +#endif
   1.248 +  return _STLP_PRIV __put_num(*this,  __STATIC_CAST(unsigned long,__x));
   1.249 +}
   1.250 +
   1.251 +template <class _CharT, class _Traits>
   1.252 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __x)
   1.253 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.254 +
   1.255 +template <class _CharT, class _Traits>
   1.256 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __x)
   1.257 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.258 +
   1.259 +#ifdef _STLP_LONG_LONG
   1.260 +template <class _CharT, class _Traits>
   1.261 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<< (_STLP_LONG_LONG __x)
   1.262 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.263 +
   1.264 +template <class _CharT, class _Traits>
   1.265 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<< (unsigned _STLP_LONG_LONG __x)
   1.266 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.267 +#endif
   1.268 +
   1.269 +template <class _CharT, class _Traits>
   1.270 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __x)
   1.271 +{ return _STLP_PRIV __put_num(*this,  __STATIC_CAST(double,__x)); }
   1.272 +
   1.273 +template <class _CharT, class _Traits>
   1.274 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __x)
   1.275 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.276 +
   1.277 +#ifndef _STLP_NO_LONG_DOUBLE
   1.278 +template <class _CharT, class _Traits>
   1.279 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __x)
   1.280 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.281 +#endif
   1.282 +
   1.283 +template <class _CharT, class _Traits>
   1.284 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __x)
   1.285 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.286 +
   1.287 +#ifndef _STLP_NO_BOOL
   1.288 +template <class _CharT, class _Traits>
   1.289 +basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __x)
   1.290 +{ return _STLP_PRIV __put_num(*this,  __x); }
   1.291 +#endif
   1.292 +
   1.293 +template <class _CharT, class _Traits>
   1.294 +void basic_ostream<_CharT, _Traits>::_M_put_char(_CharT __c) {
   1.295 +  sentry __sentry(*this);
   1.296 +  if (__sentry) {
   1.297 +    bool __failed = true;
   1.298 +    _STLP_TRY {
   1.299 +      streamsize __npad = this->width() > 0 ? this->width() - 1 : 0;
   1.300 +      //      if (__npad <= 1)
   1.301 +      if (__npad == 0)
   1.302 +        __failed = this->_S_eof(this->rdbuf()->sputc(__c));
   1.303 +      else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
   1.304 +        __failed = this->_S_eof(this->rdbuf()->sputc(__c));
   1.305 +        __failed = __failed ||
   1.306 +                   this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
   1.307 +      }
   1.308 +      else {
   1.309 +        __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
   1.310 +        __failed = __failed || this->_S_eof(this->rdbuf()->sputc(__c));
   1.311 +      }
   1.312 +
   1.313 +      this->width(0);
   1.314 +    }
   1.315 +    _STLP_CATCH_ALL {
   1.316 +      this->_M_handle_exception(ios_base::badbit);
   1.317 +    }
   1.318 +
   1.319 +    if (__failed)
   1.320 +      this->setstate(ios_base::badbit);
   1.321 +  }
   1.322 +}
   1.323 +
   1.324 +template <class _CharT, class _Traits>
   1.325 +void basic_ostream<_CharT, _Traits>::_M_put_nowiden(const _CharT* __s) {
   1.326 +  sentry __sentry(*this);
   1.327 +  if (__sentry) {
   1.328 +    bool __failed = true;
   1.329 +    streamsize __n = _Traits::length(__s);
   1.330 +    streamsize __npad = this->width() > __n ? this->width() - __n : 0;
   1.331 +
   1.332 +    _STLP_TRY {
   1.333 +      if (__npad == 0)
   1.334 +        __failed = this->rdbuf()->sputn(__s, __n) != __n;
   1.335 +      else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
   1.336 +        __failed = this->rdbuf()->sputn(__s, __n) != __n;
   1.337 +        __failed = __failed ||
   1.338 +                   this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
   1.339 +      }
   1.340 +      else {
   1.341 +        __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
   1.342 +        __failed = __failed || this->rdbuf()->sputn(__s, __n) != __n;
   1.343 +      }
   1.344 +
   1.345 +      this->width(0);
   1.346 +    }
   1.347 +    _STLP_CATCH_ALL {
   1.348 +      this->_M_handle_exception(ios_base::badbit);
   1.349 +    }
   1.350 +
   1.351 +    if (__failed)
   1.352 +      this->setstate(ios_base::failbit);
   1.353 +  }
   1.354 +}
   1.355 +
   1.356 +template <class _CharT, class _Traits>
   1.357 +void basic_ostream<_CharT, _Traits>::_M_put_widen(const char* __s) {
   1.358 +  sentry __sentry(*this);
   1.359 +  if (__sentry) {
   1.360 +    bool __failed = true;
   1.361 +    streamsize __n = char_traits<char>::length(__s);
   1.362 +    streamsize __npad = this->width() > __n ? this->width() - __n : 0;
   1.363 +
   1.364 +    _STLP_TRY {
   1.365 +      if (__npad == 0)
   1.366 +        __failed = !this->_M_put_widen_aux(__s, __n);
   1.367 +      else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
   1.368 +        __failed = !this->_M_put_widen_aux(__s, __n);
   1.369 +        __failed = __failed ||
   1.370 +                   this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
   1.371 +      }
   1.372 +      else {
   1.373 +        __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
   1.374 +        __failed = __failed || !this->_M_put_widen_aux(__s, __n);
   1.375 +      }
   1.376 +
   1.377 +      this->width(0);
   1.378 +    }
   1.379 +    _STLP_CATCH_ALL {
   1.380 +      this->_M_handle_exception(ios_base::badbit);
   1.381 +    }
   1.382 +
   1.383 +    if (__failed)
   1.384 +      this->setstate(ios_base::failbit);
   1.385 +  }
   1.386 +}
   1.387 +
   1.388 +template <class _CharT, class _Traits>
   1.389 +bool basic_ostream<_CharT, _Traits>::_M_put_widen_aux(const char* __s,
   1.390 +                                                      streamsize __n) {
   1.391 +  basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
   1.392 +
   1.393 +  for ( ; __n > 0 ; --__n)
   1.394 +    if (this->_S_eof(__buf->sputc(this->widen(*__s++))))
   1.395 +      return false;
   1.396 +  return true;
   1.397 +}
   1.398 +
   1.399 +// Unformatted output of a single character.
   1.400 +template <class _CharT, class _Traits>
   1.401 +basic_ostream<_CharT, _Traits>&
   1.402 +basic_ostream<_CharT, _Traits>::put(char_type __c) {
   1.403 +  sentry __sentry(*this);
   1.404 +  bool __failed = true;
   1.405 +
   1.406 +  if (__sentry) {
   1.407 +    _STLP_TRY {
   1.408 +      __failed = this->_S_eof(this->rdbuf()->sputc(__c));
   1.409 +    }
   1.410 +    _STLP_CATCH_ALL {
   1.411 +      this->_M_handle_exception(ios_base::badbit);
   1.412 +    }
   1.413 +  }
   1.414 +
   1.415 +  if (__failed)
   1.416 +    this->setstate(ios_base::badbit);
   1.417 +
   1.418 +  return *this;
   1.419 +}
   1.420 +
   1.421 +// Unformatted output of a single character.
   1.422 +template <class _CharT, class _Traits>
   1.423 +basic_ostream<_CharT, _Traits>&
   1.424 +basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) {
   1.425 +  sentry __sentry(*this);
   1.426 +  bool __failed = true;
   1.427 +
   1.428 +  if (__sentry) {
   1.429 +    _STLP_TRY {
   1.430 +      __failed = this->rdbuf()->sputn(__s, __n) != __n;
   1.431 +    }
   1.432 +    _STLP_CATCH_ALL {
   1.433 +      this->_M_handle_exception(ios_base::badbit);
   1.434 +    }
   1.435 +  }
   1.436 +
   1.437 +  if (__failed)
   1.438 +    this->setstate(ios_base::badbit);
   1.439 +
   1.440 +  return *this;
   1.441 +}
   1.442 +
   1.443 +_STLP_END_NAMESPACE
   1.444 +
   1.445 +#endif /* _STLP_OSTREAM_C */
   1.446 +
   1.447 +// Local Variables:
   1.448 +// mode:C++
   1.449 +// End: