epoc32/include/tools/stlport/stl/_fstream.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
 * Copyright (c) 1996,1997
williamr@4
     3
 * Silicon Graphics Computer Systems, Inc.
williamr@4
     4
 *
williamr@4
     5
 * Copyright (c) 1999
williamr@4
     6
 * Boris Fomitchev
williamr@4
     7
 *
williamr@4
     8
 * This material is provided "as is", with absolutely no warranty expressed
williamr@4
     9
 * or implied. Any use is at your own risk.
williamr@4
    10
 *
williamr@4
    11
 * Permission to use or copy this software for any purpose is hereby granted
williamr@4
    12
 * without fee, provided the above notices are retained on all copies.
williamr@4
    13
 * Permission to modify the code and to distribute modified code is granted,
williamr@4
    14
 * provided the above notices are retained, and a notice that the code was
williamr@4
    15
 * modified is included with the above copyright notice.
williamr@4
    16
 *
williamr@4
    17
 */
williamr@4
    18
#ifndef _STLP_FSTREAM_C
williamr@4
    19
#define _STLP_FSTREAM_C
williamr@4
    20
williamr@4
    21
#ifndef _STLP_INTERNAL_FSTREAM_H
williamr@4
    22
#  include <stl/_fstream.h>
williamr@4
    23
#endif
williamr@4
    24
williamr@4
    25
#ifndef _STLP_INTERNAL_LIMITS
williamr@4
    26
#  include <stl/_limits.h>
williamr@4
    27
#endif
williamr@4
    28
williamr@4
    29
_STLP_BEGIN_NAMESPACE
williamr@4
    30
williamr@4
    31
# if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
williamr@4
    32
// no wchar_t is supported for this mode
williamr@4
    33
# define __BF_int_type__ int
williamr@4
    34
# define __BF_pos_type__ streampos
williamr@4
    35
# define __BF_off_type__ streamoff
williamr@4
    36
# else
williamr@4
    37
# define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
williamr@4
    38
# define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type
williamr@4
    39
# define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type
williamr@4
    40
# endif
williamr@4
    41
williamr@4
    42
williamr@4
    43
//----------------------------------------------------------------------
williamr@4
    44
// Public basic_filebuf<> member functions
williamr@4
    45
williamr@4
    46
template <class _CharT, class _Traits>
williamr@4
    47
basic_filebuf<_CharT, _Traits>::basic_filebuf()
williamr@4
    48
     :  basic_streambuf<_CharT, _Traits>(), _M_base(),
williamr@4
    49
    _M_constant_width(false), _M_always_noconv(false),
williamr@4
    50
    _M_int_buf_dynamic(false),
williamr@4
    51
    _M_in_input_mode(false), _M_in_output_mode(false),
williamr@4
    52
    _M_in_error_mode(false), _M_in_putback_mode(false),
williamr@4
    53
    _M_int_buf(0), _M_int_buf_EOS(0),
williamr@4
    54
    _M_ext_buf(0), _M_ext_buf_EOS(0),
williamr@4
    55
    _M_ext_buf_converted(0), _M_ext_buf_end(0),
williamr@4
    56
    _M_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
williamr@4
    57
    _M_end_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
williamr@4
    58
    _M_mmap_base(0), _M_mmap_len(0),
williamr@4
    59
    _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0),
williamr@4
    60
    _M_codecvt(0),
williamr@4
    61
    _M_width(1), _M_max_width(1)
williamr@4
    62
{
williamr@4
    63
  this->_M_setup_codecvt(locale(), false);
williamr@4
    64
}
williamr@4
    65
williamr@4
    66
template <class _CharT, class _Traits>
williamr@4
    67
basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
williamr@4
    68
  this->close();
williamr@4
    69
  _M_deallocate_buffers();
williamr@4
    70
}
williamr@4
    71
williamr@4
    72
williamr@4
    73
template <class _CharT, class _Traits>
williamr@4
    74
_STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
williamr@4
    75
basic_filebuf<_CharT, _Traits>::underflow() {
williamr@4
    76
  return _Underflow<_CharT, _Traits>::_M_doit(this);
williamr@4
    77
}
williamr@4
    78
williamr@4
    79
template <class _CharT, class _Traits>
williamr@4
    80
basic_filebuf<_CharT, _Traits>*
williamr@4
    81
basic_filebuf<_CharT, _Traits>::close() {
williamr@4
    82
  bool __ok = this->is_open();
williamr@4
    83
williamr@4
    84
  if (_M_in_output_mode) {
williamr@4
    85
    __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()),
williamr@4
    86
                                         traits_type::eof());
williamr@4
    87
    __ok == __ok && this->_M_unshift();
williamr@4
    88
  }
williamr@4
    89
  else if (_M_in_input_mode)
williamr@4
    90
      this->_M_exit_input_mode();
williamr@4
    91
williamr@4
    92
  // Note order of arguments.  We close the file even if __ok is false.
williamr@4
    93
  __ok = _M_base._M_close() && __ok;
williamr@4
    94
williamr@4
    95
  // Restore the initial state, except that we don't deallocate the buffer
williamr@4
    96
  // or mess with the cached codecvt information.
williamr@4
    97
  _M_state = _M_end_state = _State_type();
williamr@4
    98
  _M_ext_buf_converted = _M_ext_buf_end = 0;
williamr@4
    99
williamr@4
   100
  _M_mmap_base = 0;
williamr@4
   101
  _M_mmap_len = 0;
williamr@4
   102
williamr@4
   103
  this->setg(0, 0, 0);
williamr@4
   104
  this->setp(0, 0);
williamr@4
   105
williamr@4
   106
  _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0;
williamr@4
   107
williamr@4
   108
  _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode
williamr@4
   109
    = false;
williamr@4
   110
williamr@4
   111
  return __ok ? this : 0;
williamr@4
   112
}
williamr@4
   113
williamr@4
   114
// This member function is called whenever we exit input mode.
williamr@4
   115
// It unmaps the memory-mapped file, if any, and sets
williamr@4
   116
// _M_in_input_mode to false.
williamr@4
   117
template <class _CharT, class _Traits>
williamr@4
   118
void basic_filebuf<_CharT, _Traits>::_M_exit_input_mode() {
williamr@4
   119
   if (_M_mmap_base != 0)
williamr@4
   120
     _M_base._M_unmap(_M_mmap_base, _M_mmap_len);
williamr@4
   121
   _M_in_input_mode = false;
williamr@4
   122
   _M_mmap_base = 0;
williamr@4
   123
}
williamr@4
   124
williamr@4
   125
williamr@4
   126
//----------------------------------------------------------------------
williamr@4
   127
// basic_filebuf<> overridden protected virtual member functions
williamr@4
   128
williamr@4
   129
template <class _CharT, class _Traits>
williamr@4
   130
streamsize basic_filebuf<_CharT, _Traits>::showmanyc() {
williamr@4
   131
  // Is there any possibility that reads can succeed?
williamr@4
   132
  if (!this->is_open() || _M_in_output_mode || _M_in_error_mode)
williamr@4
   133
    return -1;
williamr@4
   134
  else if (_M_in_putback_mode)
williamr@4
   135
    return this->egptr() - this->gptr();
williamr@4
   136
  else if (_M_constant_width) {
williamr@4
   137
    streamoff __pos  = _M_base._M_seek(0, ios_base::cur);
williamr@4
   138
    streamoff __size = _M_base._M_file_size();
williamr@4
   139
    return __pos >= 0 && __size > __pos ? __size - __pos : 0;
williamr@4
   140
  }
williamr@4
   141
  else
williamr@4
   142
    return 0;
williamr@4
   143
}
williamr@4
   144
williamr@4
   145
williamr@4
   146
// Make a putback position available, if necessary, by switching to a
williamr@4
   147
// special internal buffer used only for putback.  The buffer is
williamr@4
   148
// [_M_pback_buf, _M_pback_buf + _S_pback_buf_size), but the base
williamr@4
   149
// class only sees a piece of it at a time.  (We want to make sure
williamr@4
   150
// that we don't try to read a character that hasn't been initialized.)
williamr@4
   151
// The end of the putback buffer is always _M_pback_buf + _S_pback_buf_size,
williamr@4
   152
// but the beginning is usually not _M_pback_buf.
williamr@4
   153
template <class _CharT, class _Traits>
williamr@4
   154
__BF_int_type__
williamr@4
   155
basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
williamr@4
   156
  const int_type __eof = traits_type::eof();
williamr@4
   157
williamr@4
   158
  // If we aren't already in input mode, pushback is impossible.
williamr@4
   159
  if (!_M_in_input_mode)
williamr@4
   160
    return __eof;
williamr@4
   161
williamr@4
   162
  // We can use the ordinary get buffer if there's enough space, and
williamr@4
   163
  // if it's a buffer that we're allowed to write to.
williamr@4
   164
  if (this->gptr() != this->eback() &&
williamr@4
   165
      (traits_type::eq_int_type(__c, __eof) ||
williamr@4
   166
       traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]) ||
williamr@4
   167
       !_M_mmap_base)) {
williamr@4
   168
    this->gbump(-1);
williamr@4
   169
    if (traits_type::eq_int_type(__c, __eof) ||
williamr@4
   170
        traits_type::eq(traits_type::to_char_type(__c), *this->gptr()))
williamr@4
   171
      return traits_type::to_int_type(*this->gptr());
williamr@4
   172
  }
williamr@4
   173
  else if (!traits_type::eq_int_type(__c, __eof)) {
williamr@4
   174
    // Are we in the putback buffer already?
williamr@4
   175
    _CharT* __pback_end = _M_pback_buf + __STATIC_CAST(int,_S_pback_buf_size);
williamr@4
   176
    if (_M_in_putback_mode) {
williamr@4
   177
      // Do we have more room in the putback buffer?
williamr@4
   178
      if (this->eback() != _M_pback_buf)
williamr@4
   179
        this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end);
williamr@4
   180
      else
williamr@4
   181
        return __eof;           // No more room in the buffer, so fail.
williamr@4
   182
    }
williamr@4
   183
    else {                      // We're not yet in the putback buffer.
williamr@4
   184
      _M_saved_eback = this->eback();
williamr@4
   185
      _M_saved_gptr  = this->gptr();
williamr@4
   186
      _M_saved_egptr = this->egptr();
williamr@4
   187
      this->setg(__pback_end - 1, __pback_end - 1, __pback_end);
williamr@4
   188
      _M_in_putback_mode = true;
williamr@4
   189
    }
williamr@4
   190
  }
williamr@4
   191
  else
williamr@4
   192
    return __eof;
williamr@4
   193
williamr@4
   194
  // We have made a putback position available.  Assign to it, and return.
williamr@4
   195
  *this->gptr() = traits_type::to_char_type(__c);
williamr@4
   196
  return __c;
williamr@4
   197
}
williamr@4
   198
williamr@4
   199
// This member function flushes the put area, and also outputs the
williamr@4
   200
// character __c (unless __c is eof).  Invariant: we always leave room
williamr@4
   201
// in the internal buffer for one character more than the base class knows
williamr@4
   202
// about.  We see the internal buffer as [_M_int_buf, _M_int_buf_EOS), but
williamr@4
   203
// the base class only sees [_M_int_buf, _M_int_buf_EOS - 1).
williamr@4
   204
template <class _CharT, class _Traits>
williamr@4
   205
__BF_int_type__
williamr@4
   206
basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
williamr@4
   207
  // Switch to output mode, if necessary.
williamr@4
   208
  if (!_M_in_output_mode)
williamr@4
   209
    if (!_M_switch_to_output_mode())
williamr@4
   210
      return traits_type::eof();
williamr@4
   211
williamr@4
   212
  _CharT* __ibegin = this->_M_int_buf;
williamr@4
   213
  _CharT* __iend   = this->pptr();
williamr@4
   214
  this->setp(_M_int_buf, _M_int_buf_EOS - 1);
williamr@4
   215
williamr@4
   216
  // Put __c at the end of the internal buffer.
williamr@4
   217
  if (!traits_type::eq_int_type(__c, traits_type::eof()))
williamr@4
   218
    *__iend++ = _Traits::to_char_type(__c);
williamr@4
   219
williamr@4
   220
  // For variable-width encodings, output may take more than one pass.
williamr@4
   221
  while (__ibegin != __iend) {
williamr@4
   222
    const _CharT* __inext = __ibegin;
williamr@4
   223
    char* __enext         = _M_ext_buf;
williamr@4
   224
    typename _Codecvt::result __status
williamr@4
   225
      = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
williamr@4
   226
                        _M_ext_buf, _M_ext_buf_EOS, __enext);
williamr@4
   227
    if (__status == _Codecvt::noconv) {
williamr@4
   228
      return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
williamr@4
   229
        ? traits_type::not_eof(__c)
williamr@4
   230
        : _M_output_error();
williamr@4
   231
    }
williamr@4
   232
williamr@4
   233
    // For a constant-width encoding we know that the external buffer
williamr@4
   234
    // is large enough, so failure to consume the entire internal buffer
williamr@4
   235
    // or to produce the correct number of external characters, is an error.
williamr@4
   236
    // For a variable-width encoding, however, we require only that we
williamr@4
   237
    // consume at least one internal character
williamr@4
   238
    else if (__status != _Codecvt::error &&
williamr@4
   239
             (((__inext == __iend) &&
williamr@4
   240
               (__enext - _M_ext_buf == _M_width * (__iend - __ibegin))) ||
williamr@4
   241
              (!_M_constant_width && __inext != __ibegin))) {
williamr@4
   242
        // We successfully converted part or all of the internal buffer.
williamr@4
   243
      ptrdiff_t __n = __enext - _M_ext_buf;
williamr@4
   244
      if (_M_write(_M_ext_buf, __n))
williamr@4
   245
        __ibegin += __inext - __ibegin;
williamr@4
   246
      else
williamr@4
   247
        return _M_output_error();
williamr@4
   248
    }
williamr@4
   249
    else
williamr@4
   250
      return _M_output_error();
williamr@4
   251
  }
williamr@4
   252
williamr@4
   253
  return traits_type::not_eof(__c);
williamr@4
   254
}
williamr@4
   255
williamr@4
   256
// This member function must be called before any I/O has been
williamr@4
   257
// performed on the stream, otherwise it has no effect.
williamr@4
   258
//
williamr@4
   259
// __buf == 0 && __n == 0 means to make this stream unbuffered.
williamr@4
   260
// __buf != 0 && __n > 0 means to use __buf as the stream's internal
williamr@4
   261
// buffer, rather than the buffer that would otherwise be allocated
williamr@4
   262
// automatically.  __buf must be a pointer to an array of _CharT whose
williamr@4
   263
// size is at least __n.
williamr@4
   264
template <class _CharT, class _Traits>
williamr@4
   265
basic_streambuf<_CharT, _Traits>*
williamr@4
   266
basic_filebuf<_CharT, _Traits>::setbuf(_CharT* __buf, streamsize __n) {
williamr@4
   267
  if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode &&
williamr@4
   268
      _M_int_buf == 0) {
williamr@4
   269
    if (__buf == 0 && __n == 0)
williamr@4
   270
      _M_allocate_buffers(0, 1);
williamr@4
   271
    else if (__buf != 0 && __n > 0)
williamr@4
   272
      _M_allocate_buffers(__buf, __n);
williamr@4
   273
  }
williamr@4
   274
  return this;
williamr@4
   275
}
williamr@4
   276
williamr@4
   277
template <class _CharT, class _Traits>
williamr@4
   278
__BF_pos_type__
williamr@4
   279
basic_filebuf<_CharT, _Traits>::seekoff(off_type __off,
williamr@4
   280
                                        ios_base::seekdir __whence,
williamr@4
   281
                                        ios_base::openmode /* dummy */) {
williamr@4
   282
  if (this->is_open() &&
williamr@4
   283
      (__off == 0 || (_M_constant_width && this->_M_base._M_in_binary_mode()))) {
williamr@4
   284
williamr@4
   285
    if (!_M_seek_init(__off != 0 || __whence != ios_base::cur))
williamr@4
   286
      return pos_type(-1);
williamr@4
   287
williamr@4
   288
    // Seek to beginning or end, regardless of whether we're in input mode.
williamr@4
   289
    if (__whence == ios_base::beg || __whence == ios_base::end)
williamr@4
   290
      return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
williamr@4
   291
                            _State_type());
williamr@4
   292
williamr@4
   293
    // Seek relative to current position. Complicated if we're in input mode.
williamr@4
   294
    else if (__whence == ios_base::cur) {
williamr@4
   295
      if (!_M_in_input_mode)
williamr@4
   296
        return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
williamr@4
   297
                              _State_type());
williamr@4
   298
      else if (_M_mmap_base != 0) {
williamr@4
   299
        // __off is relative to gptr().  We need to do a bit of arithmetic
williamr@4
   300
        // to get an offset relative to the external file pointer.
williamr@4
   301
        streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base);
williamr@4
   302
williamr@4
   303
        // if __off == 0, we do not need to exit input mode and to shift file pointer
williamr@4
   304
        return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust)
williamr@4
   305
                          : _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur), _State_type());
williamr@4
   306
      }
williamr@4
   307
      else if (_M_constant_width) { // Get or set the position.
williamr@4
   308
        streamoff __iadj = _M_width * (this->gptr() - this->eback());
williamr@4
   309
williamr@4
   310
        // Compensate for offset relative to gptr versus offset relative
williamr@4
   311
        // to external pointer.  For a text-oriented stream, where the
williamr@4
   312
        // compensation is more than just pointer arithmetic, we may get
williamr@4
   313
        // but not set the current position.
williamr@4
   314
williamr@4
   315
        if (__iadj <= _M_ext_buf_end - _M_ext_buf) {
williamr@4
   316
          streamoff __eadj =  _M_base._M_get_offset(_M_ext_buf + __STATIC_CAST(ptrdiff_t, __iadj), _M_ext_buf_end);
williamr@4
   317
williamr@4
   318
          return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj)
williamr@4
   319
                            : _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur), _State_type());
williamr@4
   320
        }
williamr@4
   321
      } else {                    // Get the position.  Encoding is var width.
williamr@4
   322
        // Get position in internal buffer.
williamr@4
   323
        ptrdiff_t __ipos = this->gptr() - this->eback();
williamr@4
   324
williamr@4
   325
        // Get corresponding position in external buffer.
williamr@4
   326
        _State_type __state = _M_state;
williamr@4
   327
        int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_end,
williamr@4
   328
                                        __ipos);
williamr@4
   329
williamr@4
   330
        if (__epos >= 0) {
williamr@4
   331
          // Sanity check (expensive): make sure __epos is the right answer.
williamr@4
   332
          _State_type __tmp_state = _M_state;
williamr@4
   333
          _Filebuf_Tmp_Buf<_CharT> __buf(__ipos);
williamr@4
   334
          _CharT* __ibegin = __buf._M_ptr;
williamr@4
   335
          _CharT* __inext  = __ibegin;
williamr@4
   336
williamr@4
   337
          const char* __dummy;
williamr@4
   338
          typename _Codecvt::result __status
williamr@4
   339
            = _M_codecvt->in(__tmp_state,
williamr@4
   340
                             _M_ext_buf, _M_ext_buf + __epos, __dummy,
williamr@4
   341
                             __ibegin, __ibegin + __ipos, __inext);
williamr@4
   342
          if (__status != _Codecvt::error &&
williamr@4
   343
              (__status == _Codecvt::noconv ||
williamr@4
   344
               (__inext == __ibegin + __ipos &&
williamr@4
   345
                equal(this->eback(), this->gptr(), __ibegin, _STLP_PRIV _Eq_traits<traits_type>())))) {
williamr@4
   346
            // Get the current position (at the end of the external buffer),
williamr@4
   347
            // then adjust it.  Again, it might be a text-oriented stream.
williamr@4
   348
            streamoff __cur = _M_base._M_seek(0, ios_base::cur);
williamr@4
   349
            streamoff __adj =
williamr@4
   350
              _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) -
williamr@4
   351
              _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end);
williamr@4
   352
            if (__cur != -1 && __cur + __adj >= 0)
williamr@4
   353
              return __off == 0 ? pos_type(__cur + __adj)
williamr@4
   354
                                : _M_seek_return(__cur + __adj, __state);
williamr@4
   355
              //return _M_seek_return(__cur + __adj, __state);
williamr@4
   356
          }
williamr@4
   357
          // We failed the sanity check here.
williamr@4
   358
        }
williamr@4
   359
      }
williamr@4
   360
    }
williamr@4
   361
    // Unrecognized value for __whence here.
williamr@4
   362
  }
williamr@4
   363
williamr@4
   364
  return pos_type(-1);
williamr@4
   365
}
williamr@4
   366
williamr@4
   367
williamr@4
   368
template <class _CharT, class _Traits>
williamr@4
   369
__BF_pos_type__
williamr@4
   370
basic_filebuf<_CharT, _Traits>::seekpos(pos_type __pos,
williamr@4
   371
                                        ios_base::openmode /* dummy */) {
williamr@4
   372
  if (this->is_open()) {
williamr@4
   373
    if (!_M_seek_init(true))
williamr@4
   374
      return pos_type(-1);
williamr@4
   375
williamr@4
   376
    streamoff __off = off_type(__pos);
williamr@4
   377
    if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) {
williamr@4
   378
      _M_state = __pos.state();
williamr@4
   379
      return _M_seek_return(__off, __pos.state());
williamr@4
   380
    }
williamr@4
   381
  }
williamr@4
   382
williamr@4
   383
  return pos_type(-1);
williamr@4
   384
}
williamr@4
   385
williamr@4
   386
williamr@4
   387
template <class _CharT, class _Traits>
williamr@4
   388
int basic_filebuf<_CharT, _Traits>::sync() {
williamr@4
   389
  if (_M_in_output_mode)
williamr@4
   390
    return traits_type::eq_int_type(this->overflow(traits_type::eof()),
williamr@4
   391
                                    traits_type::eof()) ? -1 : 0;
williamr@4
   392
  return 0;
williamr@4
   393
}
williamr@4
   394
williamr@4
   395
williamr@4
   396
// Change the filebuf's locale.  This member function has no effect
williamr@4
   397
// unless it is called before any I/O is performed on the stream.
williamr@4
   398
template <class _CharT, class _Traits>
williamr@4
   399
void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
williamr@4
   400
  if (!_M_in_input_mode && !_M_in_output_mode && !_M_in_error_mode) {
williamr@4
   401
    this->_M_setup_codecvt(__loc);
williamr@4
   402
  }
williamr@4
   403
}
williamr@4
   404
williamr@4
   405
//----------------------------------------------------------------------
williamr@4
   406
// basic_filebuf<> helper functions.
williamr@4
   407
williamr@4
   408
//----------------------------------------
williamr@4
   409
// Helper functions for switching between modes.
williamr@4
   410
williamr@4
   411
// This member function is called if we're performing the first I/O
williamr@4
   412
// operation on a filebuf, or if we're performing an input operation
williamr@4
   413
// immediately after a seek.
williamr@4
   414
template <class _CharT, class _Traits>
williamr@4
   415
bool basic_filebuf<_CharT, _Traits>::_M_switch_to_input_mode() {
williamr@4
   416
  if (this->is_open() && (((int)_M_base.__o_mode() & (int)ios_base::in) != 0)
williamr@4
   417
      && (_M_in_output_mode == 0) && (_M_in_error_mode == 0)) {
williamr@4
   418
    if (!_M_int_buf && !_M_allocate_buffers())
williamr@4
   419
      return false;
williamr@4
   420
williamr@4
   421
    _M_ext_buf_converted = _M_ext_buf;
williamr@4
   422
    _M_ext_buf_end       = _M_ext_buf;
williamr@4
   423
williamr@4
   424
    _M_end_state    = _M_state;
williamr@4
   425
williamr@4
   426
    _M_in_input_mode = true;
williamr@4
   427
    return true;
williamr@4
   428
  }
williamr@4
   429
williamr@4
   430
  return false;
williamr@4
   431
}
williamr@4
   432
williamr@4
   433
williamr@4
   434
// This member function is called if we're performing the first I/O
williamr@4
   435
// operation on a filebuf, or if we're performing an output operation
williamr@4
   436
// immediately after a seek.
williamr@4
   437
template <class _CharT, class _Traits>
williamr@4
   438
bool basic_filebuf<_CharT, _Traits>::_M_switch_to_output_mode() {
williamr@4
   439
  if (this->is_open() && (_M_base.__o_mode() & (int)ios_base::out) &&
williamr@4
   440
      _M_in_input_mode == 0 && _M_in_error_mode == 0) {
williamr@4
   441
williamr@4
   442
    if (!_M_int_buf && !_M_allocate_buffers())
williamr@4
   443
      return false;
williamr@4
   444
williamr@4
   445
    // In append mode, every write does an implicit seek to the end
williamr@4
   446
    // of the file.  Whenever leaving output mode, the end of file
williamr@4
   447
    // get put in the initial shift state.
williamr@4
   448
    if (_M_base.__o_mode() & ios_base::app)
williamr@4
   449
      _M_state = _State_type();
williamr@4
   450
williamr@4
   451
    this->setp(_M_int_buf, _M_int_buf_EOS - 1);
williamr@4
   452
    _M_in_output_mode = true;
williamr@4
   453
    return true;
williamr@4
   454
  }
williamr@4
   455
williamr@4
   456
  return false;
williamr@4
   457
}
williamr@4
   458
williamr@4
   459
williamr@4
   460
//----------------------------------------
williamr@4
   461
// Helper functions for input
williamr@4
   462
williamr@4
   463
// This member function is called if there is an error during input.
williamr@4
   464
// It puts the filebuf in error mode, clear the get area buffer, and
williamr@4
   465
// returns eof.
williamr@4
   466
// returns eof.  Error mode is sticky; it is cleared only by close or
williamr@4
   467
// seek.
williamr@4
   468
williamr@4
   469
template <class _CharT, class _Traits>
williamr@4
   470
__BF_int_type__
williamr@4
   471
basic_filebuf<_CharT, _Traits>::_M_input_error() {
williamr@4
   472
   this->_M_exit_input_mode();
williamr@4
   473
  _M_in_output_mode = false;
williamr@4
   474
  _M_in_error_mode = true;
williamr@4
   475
  this->setg(0, 0, 0);
williamr@4
   476
  return traits_type::eof();
williamr@4
   477
}
williamr@4
   478
williamr@4
   479
template <class _CharT, class _Traits>
williamr@4
   480
__BF_int_type__
williamr@4
   481
basic_filebuf<_CharT, _Traits>::_M_underflow_aux() {
williamr@4
   482
  // We have the state and file position from the end of the internal
williamr@4
   483
  // buffer.  This round, they become the beginning of the internal buffer.
williamr@4
   484
  _M_state    = _M_end_state;
williamr@4
   485
williamr@4
   486
  // Fill the external buffer.  Start with any leftover characters that
williamr@4
   487
  // didn't get converted last time.
williamr@4
   488
  if (_M_ext_buf_end > _M_ext_buf_converted)
williamr@4
   489
williamr@4
   490
    _M_ext_buf_end = copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf);
williamr@4
   491
    // boris : copy_backward did not work
williamr@4
   492
    //_M_ext_buf_end = copy_backward(_M_ext_buf_converted, _M_ext_buf_end,
williamr@4
   493
    //_M_ext_buf+ (_M_ext_buf_end - _M_ext_buf_converted));
williamr@4
   494
  else
williamr@4
   495
    _M_ext_buf_end = _M_ext_buf;
williamr@4
   496
williamr@4
   497
  // Now fill the external buffer with characters from the file.  This is
williamr@4
   498
  // a loop because occasionally we don't get enough external characters
williamr@4
   499
  // to make progress.
williamr@4
   500
  for (;;) {
williamr@4
   501
    ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end);
williamr@4
   502
williamr@4
   503
    // Don't enter error mode for a failed read.  Error mode is sticky,
williamr@4
   504
    // and we might succeed if we try again.
williamr@4
   505
    if (__n <= 0)
williamr@4
   506
      return traits_type::eof();
williamr@4
   507
williamr@4
   508
    // Convert the external buffer to internal characters.
williamr@4
   509
    _M_ext_buf_end += __n;
williamr@4
   510
    const char*   __enext;
williamr@4
   511
    _CharT* __inext;
williamr@4
   512
williamr@4
   513
    typename _Codecvt::result __status
williamr@4
   514
      = _M_codecvt->in(_M_end_state,
williamr@4
   515
                       _M_ext_buf, _M_ext_buf_end, __enext,
williamr@4
   516
                       _M_int_buf, _M_int_buf_EOS, __inext);
williamr@4
   517
williamr@4
   518
    // Error conditions: (1) Return value of error.  (2) Producing internal
williamr@4
   519
    // characters without consuming external characters.  (3) In fixed-width
williamr@4
   520
    // encodings, producing an internal sequence whose length is inconsistent
williamr@4
   521
    // with that of the internal sequence.  (4) Failure to produce any
williamr@4
   522
    // characters if we have enough characters in the external buffer, where
williamr@4
   523
    // "enough" means the largest possible width of a single character.
williamr@4
   524
    if (__status == _Codecvt::noconv)
williamr@4
   525
      return _Noconv_input<_Traits>::_M_doit(this);
williamr@4
   526
    else if (__status == _Codecvt::error ||
williamr@4
   527
             (__inext != _M_int_buf && __enext == _M_ext_buf) ||
williamr@4
   528
             (_M_constant_width &&
williamr@4
   529
              //         __inext - _M_int_buf != _M_width * (__enext - _M_ext_buf)) ||
williamr@4
   530
              (__inext - _M_int_buf) *  _M_width != (__enext - _M_ext_buf)) ||
williamr@4
   531
             (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width))
williamr@4
   532
      return _M_input_error();
williamr@4
   533
    else if (__inext != _M_int_buf) {
williamr@4
   534
      _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf);
williamr@4
   535
      this->setg(_M_int_buf, _M_int_buf, __inext);
williamr@4
   536
      return traits_type::to_int_type(*_M_int_buf);
williamr@4
   537
    }
williamr@4
   538
    // We need to go around the loop again to get more external characters.
williamr@4
   539
  }
williamr@4
   540
}
williamr@4
   541
williamr@4
   542
//----------------------------------------
williamr@4
   543
// Helper functions for output
williamr@4
   544
williamr@4
   545
// This member function is called if there is an error during output.
williamr@4
   546
// It puts the filebuf in error mode, clear the put area buffer, and
williamr@4
   547
// returns eof.  Error mode is sticky; it is cleared only by close or
williamr@4
   548
// seek.
williamr@4
   549
template <class _CharT, class _Traits>
williamr@4
   550
__BF_int_type__
williamr@4
   551
basic_filebuf<_CharT, _Traits>::_M_output_error() {
williamr@4
   552
  _M_in_output_mode = false;
williamr@4
   553
  _M_in_input_mode = false;
williamr@4
   554
  _M_in_error_mode = true;
williamr@4
   555
  this->setp(0, 0);
williamr@4
   556
  return traits_type::eof();
williamr@4
   557
}
williamr@4
   558
williamr@4
   559
williamr@4
   560
// Write whatever sequence of characters is necessary to get back to
williamr@4
   561
// the initial shift state.  This function overwrites the external
williamr@4
   562
// buffer, changes the external file position, and changes the state.
williamr@4
   563
// Precondition: the internal buffer is empty.
williamr@4
   564
template <class _CharT, class _Traits>
williamr@4
   565
bool basic_filebuf<_CharT, _Traits>::_M_unshift() {
williamr@4
   566
  if (_M_in_output_mode && !_M_constant_width) {
williamr@4
   567
    typename _Codecvt::result __status;
williamr@4
   568
    do {
williamr@4
   569
      char* __enext = _M_ext_buf;
williamr@4
   570
      __status = _M_codecvt->unshift(_M_state,
williamr@4
   571
                                     _M_ext_buf, _M_ext_buf_EOS, __enext);
williamr@4
   572
      if (__status == _Codecvt::noconv ||
williamr@4
   573
          (__enext == _M_ext_buf && __status == _Codecvt::ok))
williamr@4
   574
        return true;
williamr@4
   575
      else if (__status == _Codecvt::error)
williamr@4
   576
        return false;
williamr@4
   577
      else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf))
williamr@4
   578
        return false;
williamr@4
   579
    } while (__status == _Codecvt::partial);
williamr@4
   580
  }
williamr@4
   581
williamr@4
   582
  return true;
williamr@4
   583
}
williamr@4
   584
williamr@4
   585
williamr@4
   586
//----------------------------------------
williamr@4
   587
// Helper functions for buffer allocation and deallocation
williamr@4
   588
williamr@4
   589
// This member function is called when we're initializing a filebuf's
williamr@4
   590
// internal and external buffers.  The argument is the size of the
williamr@4
   591
// internal buffer; the external buffer is sized using the character
williamr@4
   592
// width in the current encoding.  Preconditions: the buffers are currently
williamr@4
   593
// null.  __n >= 1.  __buf is either a null pointer or a pointer to an
williamr@4
   594
// array show size is at least __n.
williamr@4
   595
williamr@4
   596
// We need __n >= 1 for two different reasons.  For input, the base
williamr@4
   597
// class always needs a buffer because of the semantics of underflow().
williamr@4
   598
// For output, we want to have an internal buffer that's larger by one
williamr@4
   599
// element than the buffer that the base class knows about.  (See
williamr@4
   600
// basic_filebuf<>::overflow() for the reason.)
williamr@4
   601
template <class _CharT, class _Traits>
williamr@4
   602
bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers(_CharT* __buf, streamsize __n) {
williamr@4
   603
  //The major hypothesis in the following implementation is that size_t is unsigned.
williamr@4
   604
  //We also need streamsize byte representation to be larger or equal to the int
williamr@4
   605
  //representation to correctly store the encoding information.
williamr@4
   606
  _STLP_STATIC_ASSERT(!numeric_limits<size_t>::is_signed &&
williamr@4
   607
                      sizeof(streamsize) >= sizeof(int))
williamr@4
   608
williamr@4
   609
  if (__buf == 0) {
williamr@4
   610
    streamsize __bufsize = __n * sizeof(_CharT);
williamr@4
   611
    //We first check that the streamsize representation can't overflow a size_t one.
williamr@4
   612
    //If it can, we check that __bufsize is not higher than the size_t max value.
williamr@4
   613
    if ((sizeof(streamsize) > sizeof(size_t)) &&
williamr@4
   614
        (__bufsize > __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)())))
williamr@4
   615
      return false;
williamr@4
   616
    _M_int_buf = __STATIC_CAST(_CharT*, malloc(__STATIC_CAST(size_t, __bufsize)));
williamr@4
   617
    if (!_M_int_buf)
williamr@4
   618
      return false;
williamr@4
   619
    _M_int_buf_dynamic = true;
williamr@4
   620
  }
williamr@4
   621
  else {
williamr@4
   622
    _M_int_buf = __buf;
williamr@4
   623
    _M_int_buf_dynamic = false;
williamr@4
   624
  }
williamr@4
   625
williamr@4
   626
  streamsize __ebufsiz = (max)(__n * __STATIC_CAST(streamsize, _M_width),
williamr@4
   627
                               __STATIC_CAST(streamsize, _M_codecvt->max_length()));
williamr@4
   628
  _M_ext_buf = 0;
williamr@4
   629
  if ((sizeof(streamsize) < sizeof(size_t)) ||
williamr@4
   630
      ((sizeof(streamsize) == sizeof(size_t)) && numeric_limits<streamsize>::is_signed) ||
williamr@4
   631
      (__ebufsiz <= __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()))) {
williamr@4
   632
    _M_ext_buf = __STATIC_CAST(char*, malloc(__STATIC_CAST(size_t, __ebufsiz)));
williamr@4
   633
  }
williamr@4
   634
williamr@4
   635
  if (!_M_ext_buf) {
williamr@4
   636
    _M_deallocate_buffers();
williamr@4
   637
    return false;
williamr@4
   638
  }
williamr@4
   639
williamr@4
   640
  _M_int_buf_EOS = _M_int_buf + __STATIC_CAST(ptrdiff_t, __n);
williamr@4
   641
  _M_ext_buf_EOS = _M_ext_buf + __STATIC_CAST(ptrdiff_t, __ebufsiz);
williamr@4
   642
  return true;
williamr@4
   643
}
williamr@4
   644
williamr@4
   645
// Abbreviation for the most common case.
williamr@4
   646
template <class _CharT, class _Traits>
williamr@4
   647
bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers() {
williamr@4
   648
  // Choose a buffer that's at least 4096 characters long and that's a
williamr@4
   649
  // multiple of the page size.
williamr@4
   650
  streamsize __default_bufsiz =
williamr@4
   651
    ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size();
williamr@4
   652
  return _M_allocate_buffers(0, __default_bufsiz);
williamr@4
   653
}
williamr@4
   654
williamr@4
   655
template <class _CharT, class _Traits>
williamr@4
   656
void basic_filebuf<_CharT, _Traits>::_M_deallocate_buffers() {
williamr@4
   657
  if (_M_int_buf_dynamic)
williamr@4
   658
    free(_M_int_buf);
williamr@4
   659
  free(_M_ext_buf);
williamr@4
   660
  _M_int_buf     = 0;
williamr@4
   661
  _M_int_buf_EOS = 0;
williamr@4
   662
  _M_ext_buf     = 0;
williamr@4
   663
  _M_ext_buf_EOS = 0;
williamr@4
   664
}
williamr@4
   665
williamr@4
   666
williamr@4
   667
//----------------------------------------
williamr@4
   668
// Helper functiosn for seek and imbue
williamr@4
   669
williamr@4
   670
template <class _CharT, class _Traits>
williamr@4
   671
bool basic_filebuf<_CharT, _Traits>::_M_seek_init(bool __do_unshift) {
williamr@4
   672
  // If we're in error mode, leave it.
williamr@4
   673
   _M_in_error_mode = false;
williamr@4
   674
williamr@4
   675
  // Flush the output buffer if we're in output mode, and (conditionally)
williamr@4
   676
  // emit an unshift sequence.
williamr@4
   677
  if (_M_in_output_mode) {
williamr@4
   678
    bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()),
williamr@4
   679
                                          traits_type::eof());
williamr@4
   680
    if (__do_unshift)
williamr@4
   681
      __ok = __ok && this->_M_unshift();
williamr@4
   682
    if (!__ok) {
williamr@4
   683
      _M_in_output_mode = false;
williamr@4
   684
      _M_in_error_mode = true;
williamr@4
   685
      this->setp(0, 0);
williamr@4
   686
      return false;
williamr@4
   687
    }
williamr@4
   688
  }
williamr@4
   689
williamr@4
   690
  // Discard putback characters, if any.
williamr@4
   691
  if (_M_in_input_mode && _M_in_putback_mode)
williamr@4
   692
    _M_exit_putback_mode();
williamr@4
   693
williamr@4
   694
  return true;
williamr@4
   695
}
williamr@4
   696
williamr@4
   697
williamr@4
   698
/* Change the filebuf's locale.  This member function has no effect
williamr@4
   699
 * unless it is called before any I/O is performed on the stream.
williamr@4
   700
 * This function is called on construction and on an imbue call. In the
williamr@4
   701
 * case of the construction the codecvt facet might be a custom one if
williamr@4
   702
 * the basic_filebuf user has instanciate it with a custom char_traits.
williamr@4
   703
 * The user will have to call imbue before any I/O operation.
williamr@4
   704
 */
williamr@4
   705
template <class _CharT, class _Traits>
williamr@4
   706
void basic_filebuf<_CharT, _Traits>::_M_setup_codecvt(const locale& __loc, bool __on_imbue) {
williamr@4
   707
  if (has_facet<_Codecvt>(__loc)) {
williamr@4
   708
    _M_codecvt = &use_facet<_Codecvt>(__loc) ;
williamr@4
   709
    int __encoding    = _M_codecvt->encoding();
williamr@4
   710
williamr@4
   711
    _M_width          = (max)(__encoding, 1);
williamr@4
   712
    _M_max_width      = _M_codecvt->max_length();
williamr@4
   713
    _M_constant_width = __encoding > 0;
williamr@4
   714
    _M_always_noconv  = _M_codecvt->always_noconv();
williamr@4
   715
  }
williamr@4
   716
  else {
williamr@4
   717
    _M_codecvt = 0;
williamr@4
   718
    _M_width = _M_max_width = 1;
williamr@4
   719
    _M_constant_width = _M_always_noconv  = false;
williamr@4
   720
    if (__on_imbue) {
williamr@4
   721
      //This call will generate an exception reporting the problem.
williamr@4
   722
      use_facet<_Codecvt>(__loc);
williamr@4
   723
    }
williamr@4
   724
  }
williamr@4
   725
}
williamr@4
   726
williamr@4
   727
_STLP_END_NAMESPACE
williamr@4
   728
williamr@4
   729
# undef __BF_int_type__
williamr@4
   730
# undef __BF_pos_type__
williamr@4
   731
# undef __BF_off_type__
williamr@4
   732
williamr@4
   733
#endif /* _STLP_FSTREAM_C */
williamr@4
   734
williamr@4
   735
// Local Variables:
williamr@4
   736
// mode:C++
williamr@4
   737
// End: