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