epoc32/include/stdapis/stlport/stl/_ostream.c
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@4
     1
/*
williamr@4
     2
 * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
williamr@4
     3
 *
williamr@4
     4
 * Copyright (c) 1999
williamr@4
     5
 * Silicon Graphics Computer Systems, Inc.
williamr@4
     6
 *
williamr@4
     7
 * Copyright (c) 1999 
williamr@4
     8
 * Boris Fomitchev
williamr@4
     9
 *
williamr@4
    10
 * This material is provided "as is", with absolutely no warranty expressed
williamr@4
    11
 * or implied. Any use is at your own risk.
williamr@4
    12
 *
williamr@4
    13
 * Permission to use or copy this software for any purpose is hereby granted 
williamr@4
    14
 * without fee, provided the above notices are retained on all copies.
williamr@4
    15
 * Permission to modify the code and to distribute modified code is granted,
williamr@4
    16
 * provided the above notices are retained, and a notice that the code was
williamr@4
    17
 * modified is included with the above copyright notice.
williamr@4
    18
 *
williamr@4
    19
 */ 
williamr@4
    20
#ifndef _STLP_OSTREAM_C
williamr@4
    21
#define _STLP_OSTREAM_C
williamr@4
    22
williamr@4
    23
williamr@4
    24
#ifndef _STLP_INTERNAL_OSTREAM_H
williamr@4
    25
# include <stl/_ostream.h>
williamr@4
    26
#endif
williamr@4
    27
williamr@4
    28
#if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION)
williamr@4
    29
williamr@4
    30
#if !defined (_STLP_INTERNAL_NUM_PUT_H)
williamr@4
    31
# include <stl/_num_put.h>            // For basic_streambuf and iterators
williamr@4
    32
#endif
williamr@4
    33
williamr@4
    34
_STLP_BEGIN_NAMESPACE
williamr@4
    35
williamr@4
    36
// Helper functions for istream<>::sentry constructor.
williamr@4
    37
template <class _CharT, class _Traits>
williamr@4
    38
bool
williamr@4
    39
_M_init(basic_ostream<_CharT, _Traits>& __str) {
williamr@4
    40
  if (__str.good()) {
williamr@4
    41
    // boris : check if this is needed !
williamr@4
    42
    if (!__str.rdbuf())
williamr@4
    43
      __str.setstate(ios_base::badbit);
williamr@4
    44
    if (__str.tie())
williamr@4
    45
      __str.tie()->flush();
williamr@4
    46
    return __str.good();
williamr@4
    47
  } else
williamr@4
    48
    return false;
williamr@4
    49
}
williamr@4
    50
williamr@4
    51
//----------------------------------------------------------------------
williamr@4
    52
// Definitions of non-inline member functions.
williamr@4
    53
williamr@4
    54
// Constructor, destructor
williamr@4
    55
williamr@4
    56
template <class _CharT, class _Traits>
williamr@4
    57
_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>
williamr@4
    58
  ::basic_ostream(basic_streambuf<_CharT, _Traits>* __buf)
williamr@4
    59
    : basic_ios<_CharT, _Traits>() 
williamr@4
    60
{
williamr@4
    61
  this->init(__buf);
williamr@4
    62
}
williamr@4
    63
williamr@4
    64
template <class _CharT, class _Traits>
williamr@4
    65
_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>::~basic_ostream()
williamr@4
    66
{}
williamr@4
    67
williamr@4
    68
// Output directly from a streambuf.
williamr@4
    69
template <class _CharT, class _Traits>
williamr@4
    70
_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>& 
williamr@4
    71
basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<_CharT, _Traits>* __from)
williamr@4
    72
{
williamr@4
    73
  sentry __sentry(*this);
williamr@4
    74
  if (__sentry) {
williamr@4
    75
    if (__from) {
williamr@4
    76
      bool __any_inserted = __from->gptr() != __from->egptr()
williamr@4
    77
        ? this->_M_copy_buffered(__from, this->rdbuf())
williamr@4
    78
        : this->_M_copy_unbuffered(__from, this->rdbuf());
williamr@4
    79
      if (!__any_inserted)
williamr@4
    80
        this->setstate(ios_base::failbit);
williamr@4
    81
    }
williamr@4
    82
    else
williamr@4
    83
      this->setstate(ios_base::badbit);
williamr@4
    84
  }
williamr@4
    85
williamr@4
    86
  return *this;
williamr@4
    87
}
williamr@4
    88
williamr@4
    89
// Helper functions for the streambuf version of operator<<.  The
williamr@4
    90
// exception-handling code is complicated because exceptions thrown
williamr@4
    91
// while extracting characters are treated differently than exceptions
williamr@4
    92
// thrown while inserting characters.
williamr@4
    93
williamr@4
    94
template <class _CharT, class _Traits>
williamr@4
    95
bool basic_ostream<_CharT, _Traits>
williamr@4
    96
  ::_M_copy_buffered(basic_streambuf<_CharT, _Traits>* __from,
williamr@4
    97
                     basic_streambuf<_CharT, _Traits>* __to)
williamr@4
    98
{
williamr@4
    99
  bool __any_inserted = false;
williamr@4
   100
williamr@4
   101
  while (__from->egptr() != __from->gptr()) {
williamr@4
   102
    const ptrdiff_t __avail = __from->egptr() - __from->gptr();
williamr@4
   103
williamr@4
   104
    streamsize __nwritten;
williamr@4
   105
    _STLP_TRY {
williamr@4
   106
      __nwritten = __to->sputn(__from->gptr(), __avail);
williamr@4
   107
      __from->gbump((int)__nwritten);
williamr@4
   108
    }
williamr@4
   109
    _STLP_CATCH_ALL {
williamr@4
   110
      this->_M_handle_exception(ios_base::badbit);
williamr@4
   111
      return __any_inserted;
williamr@4
   112
    }
williamr@4
   113
williamr@4
   114
    if (__nwritten == __avail) {
williamr@4
   115
      _STLP_TRY {
williamr@4
   116
        if (this->_S_eof(__from->sgetc()))
williamr@4
   117
          return true;
williamr@4
   118
        else
williamr@4
   119
          __any_inserted = true;
williamr@4
   120
      }
williamr@4
   121
      _STLP_CATCH_ALL {
williamr@4
   122
        this->_M_handle_exception(ios_base::failbit);
williamr@4
   123
        return false;
williamr@4
   124
      }
williamr@4
   125
    }
williamr@4
   126
williamr@4
   127
    else if (__nwritten != 0)
williamr@4
   128
      return true;
williamr@4
   129
williamr@4
   130
    else
williamr@4
   131
      return __any_inserted;
williamr@4
   132
  }
williamr@4
   133
williamr@4
   134
  // No characters are in the buffer, but we aren't at EOF.  Switch to
williamr@4
   135
  // unbuffered mode.
williamr@4
   136
  return __any_inserted || this->_M_copy_unbuffered(__from, __to);
williamr@4
   137
}
williamr@4
   138
williamr@4
   139
template <class _CharT, class _Traits>
williamr@4
   140
bool basic_ostream<_CharT, _Traits>
williamr@4
   141
  ::_M_copy_unbuffered(basic_streambuf<_CharT, _Traits>* __from,
williamr@4
   142
                       basic_streambuf<_CharT, _Traits>* __to)
williamr@4
   143
{
williamr@4
   144
  bool __any_inserted = false;
williamr@4
   145
williamr@4
   146
#ifdef __SYMBIAN32__
williamr@4
   147
  int_type __c;
williamr@4
   148
    _STLP_TRY {
williamr@4
   149
  __c = __from->sgetc();;
williamr@4
   150
    }
williamr@4
   151
    _STLP_CATCH_ALL {
williamr@4
   152
      this->_M_handle_exception(ios_base::failbit);
williamr@4
   153
      return __any_inserted;
williamr@4
   154
    }
williamr@4
   155
  for(;;){
williamr@4
   156
williamr@4
   157
    if (this->_S_eof(__c))
williamr@4
   158
      return __any_inserted;
williamr@4
   159
williamr@4
   160
    else {
williamr@4
   161
      int_type __tmp;
williamr@4
   162
      _STLP_TRY {
williamr@4
   163
        __tmp = __to->sputc(__c);
williamr@4
   164
      }
williamr@4
   165
      _STLP_CATCH_ALL {
williamr@4
   166
        this->_M_handle_exception(ios_base::badbit);
williamr@4
   167
        return __any_inserted;
williamr@4
   168
      }
williamr@4
   169
williamr@4
   170
      if (this->_S_eof(__tmp)) {
williamr@4
   171
        break;
williamr@4
   172
      }
williamr@4
   173
      else
williamr@4
   174
        __any_inserted = true;
williamr@4
   175
    }
williamr@4
   176
    _STLP_TRY {
williamr@4
   177
      __c = __from->snextc();
williamr@4
   178
    }
williamr@4
   179
    _STLP_CATCH_ALL {
williamr@4
   180
      this->_M_handle_exception(ios_base::failbit);
williamr@4
   181
      return __any_inserted;
williamr@4
   182
    }
williamr@4
   183
  }
williamr@4
   184
#else
williamr@4
   185
  while (true) {
williamr@4
   186
    int_type __c;
williamr@4
   187
    _STLP_TRY {
williamr@4
   188
      __c = __from->sbumpc();
williamr@4
   189
    }
williamr@4
   190
    _STLP_CATCH_ALL {
williamr@4
   191
      this->_M_handle_exception(ios_base::failbit);
williamr@4
   192
      return __any_inserted;
williamr@4
   193
    }
williamr@4
   194
williamr@4
   195
    if (this->_S_eof(__c))
williamr@4
   196
      return __any_inserted;
williamr@4
   197
williamr@4
   198
    else {
williamr@4
   199
      int_type __tmp;
williamr@4
   200
      _STLP_TRY {
williamr@4
   201
        __tmp = __to->sputc(__c);
williamr@4
   202
      }
williamr@4
   203
      _STLP_CATCH_ALL {
williamr@4
   204
        this->_M_handle_exception(ios_base::badbit);
williamr@4
   205
        return __any_inserted;
williamr@4
   206
      }
williamr@4
   207
williamr@4
   208
      if (this->_S_eof(__tmp)) {
williamr@4
   209
        _STLP_TRY {
williamr@4
   210
          /* __tmp = */ __from->sputbackc(__c);
williamr@4
   211
        }
williamr@4
   212
        _STLP_CATCH_ALL {
williamr@4
   213
          this->_M_handle_exception(ios_base::badbit);
williamr@4
   214
          return __any_inserted;
williamr@4
   215
        }
williamr@4
   216
      }
williamr@4
   217
      else
williamr@4
   218
        __any_inserted = true;
williamr@4
   219
    }
williamr@4
   220
  }
williamr@4
   221
#endif
williamr@4
   222
  return __any_inserted;
williamr@4
   223
}
williamr@4
   224
williamr@4
   225
// Helper function for numeric output.
williamr@4
   226
williamr@4
   227
template <class _CharT, class _Traits, class _Number>
williamr@4
   228
basic_ostream<_CharT, _Traits>&  _STLP_CALL
williamr@4
   229
_M_put_num(basic_ostream<_CharT, _Traits>& __os, _Number __x)
williamr@4
   230
{
williamr@4
   231
  typedef typename basic_ostream<_CharT, _Traits>::sentry _Sentry;
williamr@4
   232
  _Sentry __sentry(__os);
williamr@4
   233
  bool __failed = true;
williamr@4
   234
williamr@4
   235
  if (__sentry) {
williamr@4
   236
    _STLP_TRY {
williamr@4
   237
      typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> > _NumPut;      
williamr@4
   238
      __failed = (use_facet<_NumPut>(__os.getloc())).put(
williamr@4
   239
                                                         ostreambuf_iterator<_CharT, _Traits>(__os.rdbuf()), 
williamr@4
   240
                                                         __os, __os.fill(),
williamr@4
   241
                                                         __x).failed();
williamr@4
   242
    }
williamr@4
   243
    _STLP_CATCH_ALL {
williamr@4
   244
      __os._M_handle_exception(ios_base::badbit);
williamr@4
   245
    }
williamr@4
   246
  }
williamr@4
   247
  if (__failed)
williamr@4
   248
    __os.setstate(ios_base::badbit); 
williamr@4
   249
  return __os;
williamr@4
   250
}
williamr@4
   251
williamr@4
   252
# if defined (_STLP_USE_TEMPLATE_EXPORT)  && defined (__BUILDING_STLPORT)
williamr@4
   253
_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream<char, char_traits<char> >& _STLP_CALL
williamr@4
   254
_M_put_num(basic_ostream<char, char_traits<char> >&, unsigned long);
williamr@4
   255
_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream<char, char_traits<char> >&  _STLP_CALL
williamr@4
   256
_M_put_num(basic_ostream<char, char_traits<char> >&, long);
williamr@4
   257
#  if defined (_STLP_LONG_LONG)
williamr@4
   258
_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream<char, char_traits<char> >&  _STLP_CALL
williamr@4
   259
_M_put_num(basic_ostream<char, char_traits<char> >&, unsigned _STLP_LONG_LONG);
williamr@4
   260
_STLP_EXPORT_TEMPLATE _STLP_EXP_DECLSPEC basic_ostream<char, char_traits<char> >&  _STLP_CALL
williamr@4
   261
_M_put_num(basic_ostream<char, char_traits<char> >&, _STLP_LONG_LONG );
williamr@4
   262
#  endif
williamr@4
   263
# endif
williamr@4
   264
williamr@4
   265
template <class _CharT, class _Traits>
williamr@4
   266
void basic_ostream<_CharT, _Traits>::_M_put_char(_CharT __c)
williamr@4
   267
{
williamr@4
   268
  sentry __sentry(*this);
williamr@4
   269
  if (__sentry) {
williamr@4
   270
    bool __failed = true;
williamr@4
   271
    _STLP_TRY {
williamr@4
   272
      streamsize __npad = this->width() > 0 ? this->width() - 1 : 0;
williamr@4
   273
      //      if (__npad <= 1)
williamr@4
   274
      if (__npad == 0)
williamr@4
   275
        __failed = this->_S_eof(this->rdbuf()->sputc(__c));
williamr@4
   276
      else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
williamr@4
   277
        __failed = this->_S_eof(this->rdbuf()->sputc(__c));
williamr@4
   278
        __failed = __failed || 
williamr@4
   279
                   this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
williamr@4
   280
      }
williamr@4
   281
      else {
williamr@4
   282
        __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
williamr@4
   283
        __failed = __failed || this->_S_eof(this->rdbuf()->sputc(__c));
williamr@4
   284
      }
williamr@4
   285
williamr@4
   286
      this->width(0);
williamr@4
   287
    }
williamr@4
   288
    _STLP_CATCH_ALL {
williamr@4
   289
      this->_M_handle_exception(ios_base::badbit);
williamr@4
   290
    }
williamr@4
   291
williamr@4
   292
    if (__failed)
williamr@4
   293
      this->setstate(ios_base::badbit);
williamr@4
   294
  }
williamr@4
   295
}
williamr@4
   296
williamr@4
   297
template <class _CharT, class _Traits>
williamr@4
   298
void basic_ostream<_CharT, _Traits>::_M_put_nowiden(const _CharT* __s)
williamr@4
   299
{
williamr@4
   300
  sentry __sentry(*this);
williamr@4
   301
  if (__sentry) {
williamr@4
   302
    bool __failed = true;
williamr@4
   303
    streamsize __n = _Traits::length(__s);
williamr@4
   304
    streamsize __npad = this->width() > __n ? this->width() - __n : 0;
williamr@4
   305
williamr@4
   306
    _STLP_TRY {
williamr@4
   307
      if (__npad == 0)
williamr@4
   308
        __failed = this->rdbuf()->sputn(__s, __n) != __n;
williamr@4
   309
      else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
williamr@4
   310
        __failed = this->rdbuf()->sputn(__s, __n) != __n;
williamr@4
   311
        __failed = __failed || 
williamr@4
   312
                   this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
williamr@4
   313
      }
williamr@4
   314
      else {
williamr@4
   315
        __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
williamr@4
   316
        __failed = __failed || this->rdbuf()->sputn(__s, __n) != __n;
williamr@4
   317
      }
williamr@4
   318
williamr@4
   319
      this->width(0);
williamr@4
   320
    }
williamr@4
   321
    _STLP_CATCH_ALL {
williamr@4
   322
      this->_M_handle_exception(ios_base::badbit);
williamr@4
   323
    }
williamr@4
   324
williamr@4
   325
    if (__failed)
williamr@4
   326
      this->setstate(ios_base::failbit);
williamr@4
   327
  }
williamr@4
   328
}
williamr@4
   329
williamr@4
   330
template <class _CharT, class _Traits>
williamr@4
   331
void basic_ostream<_CharT, _Traits>::_M_put_widen(const char* __s)
williamr@4
   332
{
williamr@4
   333
  sentry __sentry(*this);
williamr@4
   334
  if (__sentry) {
williamr@4
   335
    bool __failed = true;
williamr@4
   336
    streamsize __n = char_traits<char>::length(__s);
williamr@4
   337
    streamsize __npad = this->width() > __n ? this->width() - __n : 0;
williamr@4
   338
williamr@4
   339
    _STLP_TRY {
williamr@4
   340
      if (__npad == 0)
williamr@4
   341
        __failed = !this->_M_put_widen_aux(__s, __n);
williamr@4
   342
      else if ((this->flags() & ios_base::adjustfield) == ios_base::left) {
williamr@4
   343
        __failed = !this->_M_put_widen_aux(__s, __n);
williamr@4
   344
        __failed = __failed || 
williamr@4
   345
                   this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
williamr@4
   346
      }
williamr@4
   347
      else {
williamr@4
   348
        __failed = this->rdbuf()->_M_sputnc(this->fill(), __npad) != __npad;
williamr@4
   349
        __failed = __failed || !this->_M_put_widen_aux(__s, __n);
williamr@4
   350
      }
williamr@4
   351
williamr@4
   352
      this->width(0);
williamr@4
   353
    }
williamr@4
   354
    _STLP_CATCH_ALL {
williamr@4
   355
      this->_M_handle_exception(ios_base::badbit);
williamr@4
   356
    }
williamr@4
   357
williamr@4
   358
    if (__failed)
williamr@4
   359
      this->setstate(ios_base::failbit);
williamr@4
   360
  }
williamr@4
   361
}
williamr@4
   362
williamr@4
   363
template <class _CharT, class _Traits>
williamr@4
   364
bool basic_ostream<_CharT, _Traits>::_M_put_widen_aux(const char* __s,
williamr@4
   365
                                                      streamsize __n)
williamr@4
   366
{
williamr@4
   367
  basic_streambuf<_CharT, _Traits>* __buf = this->rdbuf();
williamr@4
   368
williamr@4
   369
  for ( ; __n > 0 ; --__n)
williamr@4
   370
    if (this->_S_eof(__buf->sputc(this->widen(*__s++))))
williamr@4
   371
      return false;
williamr@4
   372
  return true;
williamr@4
   373
}
williamr@4
   374
williamr@4
   375
// Unformatted output of a single character.
williamr@4
   376
template <class _CharT, class _Traits>
williamr@4
   377
_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>&
williamr@4
   378
basic_ostream<_CharT, _Traits>::put(char_type __c)
williamr@4
   379
{
williamr@4
   380
  sentry __sentry(*this);
williamr@4
   381
  bool __failed = true;
williamr@4
   382
williamr@4
   383
  if (__sentry) {
williamr@4
   384
    _STLP_TRY {
williamr@4
   385
      __failed = this->_S_eof(this->rdbuf()->sputc(__c));
williamr@4
   386
    }
williamr@4
   387
    _STLP_CATCH_ALL {
williamr@4
   388
      this->_M_handle_exception(ios_base::badbit);
williamr@4
   389
    }
williamr@4
   390
  }
williamr@4
   391
williamr@4
   392
  if (__failed)
williamr@4
   393
    this->setstate(ios_base::badbit);
williamr@4
   394
williamr@4
   395
  return *this;
williamr@4
   396
}
williamr@4
   397
williamr@4
   398
// Unformatted output of a single character.
williamr@4
   399
template <class _CharT, class _Traits>
williamr@4
   400
_STLP_EXP_DECLSPEC basic_ostream<_CharT, _Traits>&
williamr@4
   401
basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
williamr@4
   402
{
williamr@4
   403
  sentry __sentry(*this);
williamr@4
   404
  bool __failed = true;
williamr@4
   405
williamr@4
   406
  if (__sentry) {
williamr@4
   407
    _STLP_TRY {
williamr@4
   408
      __failed = this->rdbuf()->sputn(__s, __n) != __n;
williamr@4
   409
    }
williamr@4
   410
    _STLP_CATCH_ALL {
williamr@4
   411
      this->_M_handle_exception(ios_base::badbit);
williamr@4
   412
    }
williamr@4
   413
  }
williamr@4
   414
williamr@4
   415
  if (__failed)
williamr@4
   416
    this->setstate(ios_base::badbit);
williamr@4
   417
williamr@4
   418
  return *this;
williamr@4
   419
}
williamr@4
   420
williamr@4
   421
_STLP_END_NAMESPACE
williamr@4
   422
williamr@4
   423
#endif /* defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) */
williamr@4
   424
williamr@4
   425
#endif /* _STLP_OSTREAM_C */