_fstream.c revision e46c9386c4f79aa40185f79a19fc5b2a7ef528b3
13ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar/*
23ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * Copyright (c) 1996,1997
33ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * Silicon Graphics Computer Systems, Inc.
43ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar *
53ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * Copyright (c) 1999
63ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * Boris Fomitchev
73ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar *
83ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * This material is provided "as is", with absolutely no warranty expressed
93ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * or implied. Any use is at your own risk.
101eb4e64eed08837b85a375d6a953503daa844f07Daniel Dunbar *
111eb4e64eed08837b85a375d6a953503daa844f07Daniel Dunbar * Permission to use or copy this software for any purpose is hereby granted
123ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * without fee, provided the above notices are retained on all copies.
133ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * Permission to modify the code and to distribute modified code is granted,
143ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * provided the above notices are retained, and a notice that the code was
153ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar * modified is included with the above copyright notice.
163ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar *
172c6f6f3c170502c5b810102cf85f05732a2aa9d0Daniel Dunbar */
182c6f6f3c170502c5b810102cf85f05732a2aa9d0Daniel Dunbar#ifndef _STLP_FSTREAM_C
19510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar#define _STLP_FSTREAM_C
203ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar
21dd98e2cad165ca73c769e4f105a4e47c2216387aDaniel Dunbar#ifndef _STLP_INTERNAL_FSTREAM_H
228f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar#  include <stl/_fstream.h>
238f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar#endif
244ad4b3ebbe5769143389dccfcfadb666a4ba5940Daniel Dunbar
25af07f936b698575f2c91f1c1134c116f1ef0deddDaniel Dunbar#ifndef _STLP_INTERNAL_LIMITS
26365c02f65be026f90e67a8e00e7b827cee60e228Daniel Dunbar#  include <stl/_limits.h>
273ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar#endif
284ad4b3ebbe5769143389dccfcfadb666a4ba5940Daniel Dunbar
291b3bb6efc59a21f794b534078f9ae7e95393f510Daniel Dunbar_STLP_BEGIN_NAMESPACE
303ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar
31510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
32510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar// no wchar_t is supported for this mode
33510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# define __BF_int_type__ int
34510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# define __BF_pos_type__ streampos
35510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# define __BF_off_type__ streamoff
36237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar# else
37510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
38510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type
39510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type
40510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar# endif
41510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar
42510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar
43510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar//----------------------------------------------------------------------
44510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar// Public basic_filebuf<> member functions
45510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar
46510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbartemplate <class _CharT, class _Traits>
47510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbarbasic_filebuf<_CharT, _Traits>::basic_filebuf()
48510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar     :  basic_streambuf<_CharT, _Traits>(), _M_base(),
49510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_constant_width(false), _M_always_noconv(false),
50510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_int_buf_dynamic(false),
51510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_in_input_mode(false), _M_in_output_mode(false),
52510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_in_error_mode(false), _M_in_putback_mode(false),
53510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_int_buf(0), _M_int_buf_EOS(0),
54510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_ext_buf(0), _M_ext_buf_EOS(0),
55510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_ext_buf_converted(0), _M_ext_buf_end(0),
56237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    _M_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
57510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_end_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
58510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_mmap_base(0), _M_mmap_len(0),
59510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0),
60510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_codecvt(0),
61510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    _M_width(1), _M_max_width(1)
62510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar{
63734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbar  this->_M_setup_codecvt(locale(), false);
64734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbar}
65734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbar
66734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbartemplate <class _CharT, class _Traits>
67734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbarbasic_filebuf<_CharT, _Traits>::~basic_filebuf() {
68734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbar  this->close();
69734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbar  _M_deallocate_buffers();
70237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar}
71ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
72ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
73ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbartemplate <class _CharT, class _Traits>
74ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar_STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
75ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbarbasic_filebuf<_CharT, _Traits>::underflow() {
76ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  return _Underflow<_CharT, _Traits>::_M_doit(this);
77ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar}
78ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
79ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbartemplate <class _CharT, class _Traits>
80ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbarbasic_filebuf<_CharT, _Traits>*
81e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbarbasic_filebuf<_CharT, _Traits>::close() {
82e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  bool __ok = this->is_open();
83ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
84ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  if (_M_in_output_mode) {
85ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()),
86ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar                                         traits_type::eof());
87ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    __ok == __ok && this->_M_unshift();
88ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  }
89ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  else if (_M_in_input_mode)
90ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      this->_M_exit_input_mode();
91ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
92ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  // Note order of arguments.  We close the file even if __ok is false.
93ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  __ok = _M_base._M_close() && __ok;
94ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
95ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  // Restore the initial state, except that we don't deallocate the buffer
96ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  // or mess with the cached codecvt information.
97e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  _M_state = _M_end_state = _State_type();
98e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  _M_ext_buf_converted = _M_ext_buf_end = 0;
99e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar
100e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  _M_mmap_base = 0;
101e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  _M_mmap_len = 0;
102e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar
103237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  this->setg(0, 0, 0);
104ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  this->setp(0, 0);
105ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
106ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0;
107ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar
108237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode
109237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    = false;
110237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar
111237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  return __ok ? this : 0;
112237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar}
113237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar
114237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar// This member function is called whenever we exit input mode.
115237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar// It unmaps the memory-mapped file, if any, and sets
116237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar// _M_in_input_mode to false.
117237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbartemplate <class _CharT, class _Traits>
118237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbarvoid basic_filebuf<_CharT, _Traits>::_M_exit_input_mode() {
119237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  if (_M_mmap_base != 0) {
120237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    _M_base._M_unmap(_M_mmap_base, _M_mmap_len);
121237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    _M_mmap_base = 0;
122237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    _M_mmap_len = 0;
123237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  }
124237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  _M_in_input_mode = false;
125237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar}
126237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar
127237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar
128237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar//----------------------------------------------------------------------
129237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar// basic_filebuf<> overridden protected virtual member functions
130237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar
131237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbartemplate <class _CharT, class _Traits>
132237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbarstreamsize basic_filebuf<_CharT, _Traits>::showmanyc() {
133237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  // Is there any possibility that reads can succeed?
134237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  if (!this->is_open() || _M_in_output_mode || _M_in_error_mode)
135237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    return -1;
136237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  else if (_M_in_putback_mode)
137237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    return this->egptr() - this->gptr();
138237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  else if (_M_constant_width) {
139237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    streamoff __pos  = _M_base._M_seek(0, ios_base::cur);
140237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    streamoff __size = _M_base._M_file_size();
141237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    return __pos >= 0 && __size > __pos ? __size - __pos : 0;
142237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  }
143237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  else
144237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar    return 0;
145237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar}
146237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar
147237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar
148237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar// Make a putback position available, if necessary, by switching to a
149237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar// special internal buffer used only for putback.  The buffer is
150237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar// [_M_pback_buf, _M_pback_buf + _S_pback_buf_size), but the base
151ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar// class only sees a piece of it at a time.  (We want to make sure
152ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar// that we don't try to read a character that hasn't been initialized.)
153ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar// The end of the putback buffer is always _M_pback_buf + _S_pback_buf_size,
154ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar// but the beginning is usually not _M_pback_buf.
155ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbartemplate <class _CharT, class _Traits>
156ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar__BF_int_type__
157e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbarbasic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
158237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  const int_type __eof = traits_type::eof();
159e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar
160e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  // If we aren't already in input mode, pushback is impossible.
161e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  if (!_M_in_input_mode)
162e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar    return __eof;
163e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar
164e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  // We can use the ordinary get buffer if there's enough space, and
165ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  // if it's a buffer that we're allowed to write to.
166ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  if (this->gptr() != this->eback() &&
167ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      (traits_type::eq_int_type(__c, __eof) ||
168ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar       traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]) ||
169ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar       !_M_mmap_base)) {
170ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    this->gbump(-1);
171ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    if (traits_type::eq_int_type(__c, __eof) ||
172ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar        traits_type::eq(traits_type::to_char_type(__c), *this->gptr()))
173ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      return traits_type::to_int_type(*this->gptr());
174e3d6023cbf99a24609f52adb0af1fa1c6dafefd7Daniel Dunbar  }
175ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  else if (!traits_type::eq_int_type(__c, __eof)) {
176ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    // Are we in the putback buffer already?
177ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    _CharT* __pback_end = _M_pback_buf + __STATIC_CAST(int,_S_pback_buf_size);
178ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    if (_M_in_putback_mode) {
179ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      // Do we have more room in the putback buffer?
180ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      if (this->eback() != _M_pback_buf)
181217acbfa3524d5805fda7900b26c1e779443588dDaniel Dunbar        this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end);
182545c28117c64e78521b8b402caa060ba0b58a530Daniel Dunbar      else
183545c28117c64e78521b8b402caa060ba0b58a530Daniel Dunbar        return __eof;           // No more room in the buffer, so fail.
184217acbfa3524d5805fda7900b26c1e779443588dDaniel Dunbar    }
1853ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar    else {                      // We're not yet in the putback buffer.
1863ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar      _M_saved_eback = this->eback();
1878f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar      _M_saved_gptr  = this->gptr();
1883ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar      _M_saved_egptr = this->egptr();
189734932c7dc77a16b15d466588e10d76bcd6d13c7Daniel Dunbar      this->setg(__pback_end - 1, __pback_end - 1, __pback_end);
1908e6e70928adebea871389febd9b54ca82821dc87Daniel Dunbar      _M_in_putback_mode = true;
191510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar    }
1928e6e70928adebea871389febd9b54ca82821dc87Daniel Dunbar  }
193510d73200ec48496a2b10703385fe99b51e31fa5Daniel Dunbar  else
194217acbfa3524d5805fda7900b26c1e779443588dDaniel Dunbar    return __eof;
195217acbfa3524d5805fda7900b26c1e779443588dDaniel Dunbar
196545c28117c64e78521b8b402caa060ba0b58a530Daniel Dunbar  // We have made a putback position available.  Assign to it, and return.
197545c28117c64e78521b8b402caa060ba0b58a530Daniel Dunbar  *this->gptr() = traits_type::to_char_type(__c);
198217acbfa3524d5805fda7900b26c1e779443588dDaniel Dunbar  return __c;
199f44c58563286bb448a72ed9c9af5f21718633230Daniel Dunbar}
200f44c58563286bb448a72ed9c9af5f21718633230Daniel Dunbar
201f44c58563286bb448a72ed9c9af5f21718633230Daniel Dunbar// This member function flushes the put area, and also outputs the
202f44c58563286bb448a72ed9c9af5f21718633230Daniel Dunbar// character __c (unless __c is eof).  Invariant: we always leave room
203f44c58563286bb448a72ed9c9af5f21718633230Daniel Dunbar// in the internal buffer for one character more than the base class knows
2048e6e70928adebea871389febd9b54ca82821dc87Daniel Dunbar// about.  We see the internal buffer as [_M_int_buf, _M_int_buf_EOS), but
2058e6e70928adebea871389febd9b54ca82821dc87Daniel Dunbar// the base class only sees [_M_int_buf, _M_int_buf_EOS - 1).
206f44c58563286bb448a72ed9c9af5f21718633230Daniel Dunbartemplate <class _CharT, class _Traits>
207e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar__BF_int_type__
2088fa01c8f98ff7c8f40fee5014c9c393d806381a4Daniel Dunbarbasic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
2098fa01c8f98ff7c8f40fee5014c9c393d806381a4Daniel Dunbar  // Switch to output mode, if necessary.
2108fa01c8f98ff7c8f40fee5014c9c393d806381a4Daniel Dunbar  if (!_M_in_output_mode)
2118fa01c8f98ff7c8f40fee5014c9c393d806381a4Daniel Dunbar    if (!_M_switch_to_output_mode())
2128fa01c8f98ff7c8f40fee5014c9c393d806381a4Daniel Dunbar      return traits_type::eof();
213d5dc9f34ef2288ace0003e1fee011cc9e7d2d040Benjamin Kramer
214d5dc9f34ef2288ace0003e1fee011cc9e7d2d040Benjamin Kramer  _CharT* __ibegin = this->_M_int_buf;
215d5dc9f34ef2288ace0003e1fee011cc9e7d2d040Benjamin Kramer  _CharT* __iend   = this->pptr();
216e26bd90e5f7c1e44dd2b74d84d2f158dc7b983fbDaniel Dunbar  this->setp(_M_int_buf, _M_int_buf_EOS - 1);
217e26bd90e5f7c1e44dd2b74d84d2f158dc7b983fbDaniel Dunbar
2188fa01c8f98ff7c8f40fee5014c9c393d806381a4Daniel Dunbar  // Put __c at the end of the internal buffer.
2198fa01c8f98ff7c8f40fee5014c9c393d806381a4Daniel Dunbar  if (!traits_type::eq_int_type(__c, traits_type::eof()))
220e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    *__iend++ = _Traits::to_char_type(__c);
221e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar
222ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  // For variable-width encodings, output may take more than one pass.
223ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar  while (__ibegin != __iend) {
224a1e2fd9b580ae583b91d3288d59c00ca9f6a56b8Daniel Dunbar    const _CharT* __inext = __ibegin;
225ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    char* __enext         = _M_ext_buf;
226ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    typename _Codecvt::result __status
227ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
228ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar                        _M_ext_buf, _M_ext_buf_EOS, __enext);
229ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    if (__status == _Codecvt::noconv) {
230ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
231237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar        ? traits_type::not_eof(__c)
2320311d47665bcdd186af6fc1bcfa40cf186ae7b9aDaniel Dunbar        : _M_output_error();
233ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar    }
234e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar
235e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    // For a constant-width encoding we know that the external buffer
236e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    // is large enough, so failure to consume the entire internal buffer
237e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    // or to produce the correct number of external characters, is an error.
238e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    // For a variable-width encoding, however, we require only that we
239e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    // consume at least one internal character
240e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    else if (__status != _Codecvt::error &&
241237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar             (((__inext == __iend) &&
242e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar               (__enext - _M_ext_buf == _M_width * (__iend - __ibegin))) ||
243ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar              (!_M_constant_width && __inext != __ibegin))) {
244ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar        // We successfully converted part or all of the internal buffer.
245e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar      ptrdiff_t __n = __enext - _M_ext_buf;
246e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar      if (_M_write(_M_ext_buf, __n))
247ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar        __ibegin += __inext - __ibegin;
248ec9587d5bed6149f6df8b57192bb787c62aedb1bDaniel Dunbar      else
249e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar        return _M_output_error();
250e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    }
251e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar    else
252e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar      return _M_output_error();
253e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar  }
254e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar
255237a31bf663c8c3783bd71dc7cfbdd0425a8ba69Daniel Dunbar  return traits_type::not_eof(__c);
2560311d47665bcdd186af6fc1bcfa40cf186ae7b9aDaniel Dunbar}
257e5be6dac24946e9521af78fce8e531e605f563d4Daniel Dunbar
2588e6e70928adebea871389febd9b54ca82821dc87Daniel Dunbar// This member function must be called before any I/O has been
2593ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar// performed on the stream, otherwise it has no effect.
260af96def468042cfbed55a4cc12b1bb917ead4f33Daniel Dunbar//
261af96def468042cfbed55a4cc12b1bb917ead4f33Daniel Dunbar// __buf == 0 && __n == 0 means to make this stream unbuffered.
262c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar// __buf != 0 && __n > 0 means to use __buf as the stream's internal
2638f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar// buffer, rather than the buffer that would otherwise be allocated
2648f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar// automatically.  __buf must be a pointer to an array of _CharT whose
2658f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar// size is at least __n.
266af96def468042cfbed55a4cc12b1bb917ead4f33Daniel Dunbartemplate <class _CharT, class _Traits>
2673ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbarbasic_streambuf<_CharT, _Traits>*
2687ec3dafdda96abcd1ff0841deef4eadfeb8f1208Daniel Dunbarbasic_filebuf<_CharT, _Traits>::setbuf(_CharT* __buf, streamsize __n) {
269  if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode &&
270      _M_int_buf == 0) {
271    if (__buf == 0 && __n == 0)
272      _M_allocate_buffers(0, 1);
273    else if (__buf != 0 && __n > 0)
274      _M_allocate_buffers(__buf, __n);
275  }
276  return this;
277}
278
279#if defined (_STLP_ASSERTIONS)
280// helper class.
281template <class _CharT>
282struct _Filebuf_Tmp_Buf {
283  _CharT* _M_ptr;
284  _Filebuf_Tmp_Buf(ptrdiff_t __n) : _M_ptr(0) { _M_ptr = new _CharT[__n]; }
285  ~_Filebuf_Tmp_Buf() { delete[] _M_ptr; }
286};
287#endif
288
289template <class _CharT, class _Traits>
290__BF_pos_type__
291basic_filebuf<_CharT, _Traits>::seekoff(off_type __off,
292                                        ios_base::seekdir __whence,
293                                        ios_base::openmode /* dummy */) {
294  if (!this->is_open())
295    return pos_type(-1);
296
297  if (!_M_constant_width && __off != 0)
298    return pos_type(-1);
299
300  if (!_M_seek_init(__off != 0 || __whence != ios_base::cur))
301    return pos_type(-1);
302
303  // Seek to beginning or end, regardless of whether we're in input mode.
304  if (__whence == ios_base::beg || __whence == ios_base::end)
305    return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
306                          _State_type());
307
308  // Seek relative to current position.  Complicated if we're in input mode.
309  _STLP_ASSERT(__whence == ios_base::cur)
310  if (!_M_in_input_mode)
311    return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
312                          _State_type());
313
314  if (_M_mmap_base != 0) {
315    // __off is relative to gptr().  We need to do a bit of arithmetic
316    // to get an offset relative to the external file pointer.
317    streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base);
318
319    // if __off == 0, we do not need to exit input mode and to shift file pointer
320    return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust)
321                      : _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur), _State_type());
322  }
323
324  if (_M_constant_width) { // Get or set the position.
325    streamoff __iadj = _M_width * (this->gptr() - this->eback());
326
327    // Compensate for offset relative to gptr versus offset relative
328    // to external pointer.  For a text-oriented stream, where the
329    // compensation is more than just pointer arithmetic, we may get
330    // but not set the current position.
331
332    if (__iadj <= _M_ext_buf_end - _M_ext_buf) {
333      streamoff __eadj =  _M_base._M_get_offset(_M_ext_buf + __STATIC_CAST(ptrdiff_t, __iadj), _M_ext_buf_end);
334
335      return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj)
336                        : _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur), _State_type());
337    }
338  }
339  else {                    // Get the position.  Encoding is var width.
340    // Get position in internal buffer.
341    ptrdiff_t __ipos = this->gptr() - this->eback();
342
343    // Get corresponding position in external buffer.
344    _State_type __state = _M_state;
345    int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_converted,
346                                    __ipos);
347#if defined (_STLP_ASSERTIONS)
348    // Sanity check (expensive): make sure __epos is the right answer.
349    _STLP_ASSERT(__epos >= 0)
350    _State_type __tmp_state = _M_state;
351    _Filebuf_Tmp_Buf<_CharT> __buf(__ipos);
352    _CharT* __ibegin = __buf._M_ptr;
353    _CharT* __inext  = __ibegin;
354    const char* __dummy;
355    typename _Codecvt::result __status
356      = _M_codecvt->in(__tmp_state,
357                       _M_ext_buf, _M_ext_buf + __epos, __dummy,
358                       __ibegin, __ibegin + __ipos, __inext);
359    // The result code is necessarily ok because:
360    // - noconv: impossible for a variable encoding
361    // - error: length method is supposed to count until it reach max value or find an error so we cannot have
362    //          an error again when decoding an external buffer up to length return value.
363    // - partial: idem error, it is also a reason for length to stop counting.
364    _STLP_ASSERT(__status == _Codecvt::ok)
365    _STLP_ASSERT(__inext == __ibegin + __ipos)
366    _STLP_ASSERT(equal(this->eback(), this->gptr(), __ibegin, _STLP_PRIV _Eq_traits<traits_type>()))
367#endif
368    // Get the current position (at the end of the external buffer),
369    // then adjust it.  Again, it might be a text-oriented stream.
370    streamoff __cur = _M_base._M_seek(0, ios_base::cur);
371    streamoff __adj = _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) -
372                      _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end);
373    if (__cur != -1 && __cur + __adj >= 0)
374      return __off == 0 ? pos_type(__cur + __adj)
375                        : _M_seek_return(__cur + __adj, __state);
376  }
377
378  return pos_type(-1);
379}
380
381
382template <class _CharT, class _Traits>
383__BF_pos_type__
384basic_filebuf<_CharT, _Traits>::seekpos(pos_type __pos,
385                                        ios_base::openmode /* dummy */) {
386  if (this->is_open()) {
387    if (!_M_seek_init(true))
388      return pos_type(-1);
389
390    streamoff __off = off_type(__pos);
391    if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) {
392      _M_state = __pos.state();
393      return _M_seek_return(__off, __pos.state());
394    }
395  }
396
397  return pos_type(-1);
398}
399
400
401template <class _CharT, class _Traits>
402int basic_filebuf<_CharT, _Traits>::sync() {
403  if (_M_in_output_mode)
404    return traits_type::eq_int_type(this->overflow(traits_type::eof()),
405                                    traits_type::eof()) ? -1 : 0;
406  return 0;
407}
408
409
410// Change the filebuf's locale.  This member function has no effect
411// unless it is called before any I/O is performed on the stream.
412template <class _CharT, class _Traits>
413void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
414  if (!_M_in_input_mode && !_M_in_output_mode && !_M_in_error_mode) {
415    this->_M_setup_codecvt(__loc);
416  }
417}
418
419//----------------------------------------------------------------------
420// basic_filebuf<> helper functions.
421
422//----------------------------------------
423// Helper functions for switching between modes.
424
425// This member function is called if we're performing the first I/O
426// operation on a filebuf, or if we're performing an input operation
427// immediately after a seek.
428template <class _CharT, class _Traits>
429bool basic_filebuf<_CharT, _Traits>::_M_switch_to_input_mode() {
430  if (this->is_open() && (((int)_M_base.__o_mode() & (int)ios_base::in) !=0)
431      && (_M_in_output_mode == 0) && (_M_in_error_mode == 0)) {
432    if (!_M_int_buf && !_M_allocate_buffers())
433      return false;
434
435    _M_ext_buf_converted = _M_ext_buf;
436    _M_ext_buf_end       = _M_ext_buf;
437
438    _M_end_state    = _M_state;
439
440    _M_in_input_mode = true;
441    return true;
442  }
443
444  return false;
445}
446
447
448// This member function is called if we're performing the first I/O
449// operation on a filebuf, or if we're performing an output operation
450// immediately after a seek.
451template <class _CharT, class _Traits>
452bool basic_filebuf<_CharT, _Traits>::_M_switch_to_output_mode() {
453  if (this->is_open() && (_M_base.__o_mode() & (int)ios_base::out) &&
454      _M_in_input_mode == 0 && _M_in_error_mode == 0) {
455
456    if (!_M_int_buf && !_M_allocate_buffers())
457      return false;
458
459    // In append mode, every write does an implicit seek to the end
460    // of the file.  Whenever leaving output mode, the end of file
461    // get put in the initial shift state.
462    if (_M_base.__o_mode() & ios_base::app)
463      _M_state = _State_type();
464
465    this->setp(_M_int_buf, _M_int_buf_EOS - 1);
466    _M_in_output_mode = true;
467    return true;
468  }
469
470  return false;
471}
472
473
474//----------------------------------------
475// Helper functions for input
476
477// This member function is called if there is an error during input.
478// It puts the filebuf in error mode, clear the get area buffer, and
479// returns eof.
480// returns eof.  Error mode is sticky; it is cleared only by close or
481// seek.
482
483template <class _CharT, class _Traits>
484__BF_int_type__
485basic_filebuf<_CharT, _Traits>::_M_input_error() {
486   this->_M_exit_input_mode();
487  _M_in_output_mode = false;
488  _M_in_error_mode = true;
489  this->setg(0, 0, 0);
490  return traits_type::eof();
491}
492
493template <class _CharT, class _Traits>
494__BF_int_type__
495basic_filebuf<_CharT, _Traits>::_M_underflow_aux() {
496  // We have the state and file position from the end of the internal
497  // buffer.  This round, they become the beginning of the internal buffer.
498  _M_state    = _M_end_state;
499
500  // Fill the external buffer.  Start with any leftover characters that
501  // didn't get converted last time.
502  if (_M_ext_buf_end > _M_ext_buf_converted)
503
504    _M_ext_buf_end = _STLP_STD::copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf);
505    // boris : copy_backward did not work
506    //_M_ext_buf_end = copy_backward(_M_ext_buf_converted, _M_ext_buf_end,
507    //_M_ext_buf+ (_M_ext_buf_end - _M_ext_buf_converted));
508  else
509    _M_ext_buf_end = _M_ext_buf;
510
511  // Now fill the external buffer with characters from the file.  This is
512  // a loop because occasionally we don't get enough external characters
513  // to make progress.
514  for (;;) {
515    ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end);
516    if (__n < 0) {
517      // Read failed, maybe we should set err bit on associated stream...
518      this->setg(0, 0, 0);
519      return traits_type::eof();
520    }
521
522    _M_ext_buf_end += __n;
523
524    // If external buffer is empty there is nothing to do.
525    if (_M_ext_buf == _M_ext_buf_end) {
526      this->setg(0, 0, 0);
527      return traits_type::eof();
528    }
529
530    // Convert the external buffer to internal characters.
531    const char* __enext;
532    _CharT* __inext;
533
534    typename _Codecvt::result __status
535      = _M_codecvt->in(_M_end_state,
536                       _M_ext_buf, _M_ext_buf_end, __enext,
537                       _M_int_buf, _M_int_buf_EOS, __inext);
538
539    /* Error conditions:
540     * (1) Return value of error.
541     * (2) Producing internal characters without consuming external characters.
542     * (3) In fixed-width encodings, producing an internal sequence whose length
543     * is inconsistent with that of the internal sequence.
544     * (4) Failure to produce any characters if we have enough characters in
545     * the external buffer, where "enough" means the largest possible width
546     * of a single character. */
547    if (__status == _Codecvt::noconv)
548      return _Noconv_input<_Traits>::_M_doit(this);
549    else if (__status == _Codecvt::error ||
550            (__inext != _M_int_buf && __enext == _M_ext_buf) ||
551            (_M_constant_width && (__inext - _M_int_buf) *  _M_width != (__enext - _M_ext_buf)) ||
552            (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width))
553      return _M_input_error();
554    else if (__inext != _M_int_buf) {
555      _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf);
556      this->setg(_M_int_buf, _M_int_buf, __inext);
557      return traits_type::to_int_type(*_M_int_buf);
558    }
559    /* We need to go around the loop again to get more external characters.
560     * But if the previous read failed then don't try again for now.
561     * Don't enter error mode for a failed read. Error mode is sticky,
562     * and we might succeed if we try again. */
563    if (__n <= 0) {
564      this->setg(0, 0, 0);
565      return traits_type::eof();
566    }
567  }
568}
569
570//----------------------------------------
571// Helper functions for output
572
573// This member function is called if there is an error during output.
574// It puts the filebuf in error mode, clear the put area buffer, and
575// returns eof.  Error mode is sticky; it is cleared only by close or
576// seek.
577template <class _CharT, class _Traits>
578__BF_int_type__
579basic_filebuf<_CharT, _Traits>::_M_output_error() {
580  _M_in_output_mode = false;
581  _M_in_input_mode = false;
582  _M_in_error_mode = true;
583  this->setp(0, 0);
584  return traits_type::eof();
585}
586
587
588// Write whatever sequence of characters is necessary to get back to
589// the initial shift state.  This function overwrites the external
590// buffer, changes the external file position, and changes the state.
591// Precondition: the internal buffer is empty.
592template <class _CharT, class _Traits>
593bool basic_filebuf<_CharT, _Traits>::_M_unshift() {
594  if (_M_in_output_mode && !_M_constant_width) {
595    typename _Codecvt::result __status;
596    do {
597      char* __enext = _M_ext_buf;
598      __status = _M_codecvt->unshift(_M_state,
599                                     _M_ext_buf, _M_ext_buf_EOS, __enext);
600      if (__status == _Codecvt::noconv ||
601          (__enext == _M_ext_buf && __status == _Codecvt::ok))
602        return true;
603      else if (__status == _Codecvt::error)
604        return false;
605      else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf))
606        return false;
607    } while (__status == _Codecvt::partial);
608  }
609
610  return true;
611}
612
613
614//----------------------------------------
615// Helper functions for buffer allocation and deallocation
616
617// This member function is called when we're initializing a filebuf's
618// internal and external buffers.  The argument is the size of the
619// internal buffer; the external buffer is sized using the character
620// width in the current encoding.  Preconditions: the buffers are currently
621// null.  __n >= 1.  __buf is either a null pointer or a pointer to an
622// array show size is at least __n.
623
624// We need __n >= 1 for two different reasons.  For input, the base
625// class always needs a buffer because of the semantics of underflow().
626// For output, we want to have an internal buffer that's larger by one
627// element than the buffer that the base class knows about.  (See
628// basic_filebuf<>::overflow() for the reason.)
629template <class _CharT, class _Traits>
630bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers(_CharT* __buf, streamsize __n) {
631  //The major hypothesis in the following implementation is that size_t is unsigned.
632  //We also need streamsize byte representation to be larger or equal to the int
633  //representation to correctly store the encoding information.
634  _STLP_STATIC_ASSERT(!numeric_limits<size_t>::is_signed &&
635                      sizeof(streamsize) >= sizeof(int))
636
637  if (__buf == 0) {
638    streamsize __bufsize = __n * sizeof(_CharT);
639    //We first check that the streamsize representation can't overflow a size_t one.
640    //If it can, we check that __bufsize is not higher than the size_t max value.
641    if ((sizeof(streamsize) > sizeof(size_t)) &&
642        (__bufsize > __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)())))
643      return false;
644    _M_int_buf = __STATIC_CAST(_CharT*, malloc(__STATIC_CAST(size_t, __bufsize)));
645    if (!_M_int_buf)
646      return false;
647    _M_int_buf_dynamic = true;
648  }
649  else {
650    _M_int_buf = __buf;
651    _M_int_buf_dynamic = false;
652  }
653
654  streamsize __ebufsiz = (max)(__n * __STATIC_CAST(streamsize, _M_width),
655                               __STATIC_CAST(streamsize, _M_codecvt->max_length()));
656
657  _M_ext_buf = 0;
658  if ((sizeof(streamsize) < sizeof(size_t)) ||
659      ((sizeof(streamsize) == sizeof(size_t)) && numeric_limits<streamsize>::is_signed) ||
660      (__ebufsiz <= __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()))) {
661    _M_ext_buf = __STATIC_CAST(char*, malloc(__STATIC_CAST(size_t, __ebufsiz)));
662  }
663
664  if (!_M_ext_buf) {
665    _M_deallocate_buffers();
666    return false;
667  }
668
669  _M_int_buf_EOS = _M_int_buf + __STATIC_CAST(ptrdiff_t, __n);
670  _M_ext_buf_EOS = _M_ext_buf + __STATIC_CAST(ptrdiff_t, __ebufsiz);
671  return true;
672}
673
674// Abbreviation for the most common case.
675template <class _CharT, class _Traits>
676bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers() {
677  // Choose a buffer that's at least 4096 characters long and that's a
678  // multiple of the page size.
679  streamsize __default_bufsiz =
680    ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size();
681  return _M_allocate_buffers(0, __default_bufsiz);
682}
683
684template <class _CharT, class _Traits>
685void basic_filebuf<_CharT, _Traits>::_M_deallocate_buffers() {
686  if (_M_int_buf_dynamic)
687    free(_M_int_buf);
688  free(_M_ext_buf);
689  _M_int_buf     = 0;
690  _M_int_buf_EOS = 0;
691  _M_ext_buf     = 0;
692  _M_ext_buf_EOS = 0;
693}
694
695
696//----------------------------------------
697// Helper functiosn for seek and imbue
698
699template <class _CharT, class _Traits>
700bool basic_filebuf<_CharT, _Traits>::_M_seek_init(bool __do_unshift) {
701  // If we're in error mode, leave it.
702   _M_in_error_mode = false;
703
704  // Flush the output buffer if we're in output mode, and (conditionally)
705  // emit an unshift sequence.
706  if (_M_in_output_mode) {
707    bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()),
708                                          traits_type::eof());
709    if (__do_unshift)
710      __ok = __ok && this->_M_unshift();
711    if (!__ok) {
712      _M_in_output_mode = false;
713      _M_in_error_mode = true;
714      this->setp(0, 0);
715      return false;
716    }
717  }
718
719  // Discard putback characters, if any.
720  if (_M_in_input_mode && _M_in_putback_mode)
721    _M_exit_putback_mode();
722
723  return true;
724}
725
726
727/* Change the filebuf's locale.  This member function has no effect
728 * unless it is called before any I/O is performed on the stream.
729 * This function is called on construction and on an imbue call. In the
730 * case of the construction the codecvt facet might be a custom one if
731 * the basic_filebuf user has instanciate it with a custom char_traits.
732 * The user will have to call imbue before any I/O operation.
733 */
734template <class _CharT, class _Traits>
735void basic_filebuf<_CharT, _Traits>::_M_setup_codecvt(const locale& __loc, bool __on_imbue) {
736  if (has_facet<_Codecvt>(__loc)) {
737    _M_codecvt = &use_facet<_Codecvt>(__loc) ;
738    int __encoding    = _M_codecvt->encoding();
739
740    _M_width          = (max)(__encoding, 1);
741    _M_max_width      = _M_codecvt->max_length();
742    _M_constant_width = __encoding > 0;
743    _M_always_noconv  = _M_codecvt->always_noconv();
744  }
745  else {
746    _M_codecvt = 0;
747    _M_width = _M_max_width = 1;
748    _M_constant_width = _M_always_noconv  = false;
749    if (__on_imbue) {
750      //This call will generate an exception reporting the problem.
751      use_facet<_Codecvt>(__loc);
752    }
753  }
754}
755
756_STLP_END_NAMESPACE
757
758# undef __BF_int_type__
759# undef __BF_pos_type__
760# undef __BF_off_type__
761
762#endif /* _STLP_FSTREAM_C */
763
764// Local Variables:
765// mode:C++
766// End:
767