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