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