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