2 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
4 * Copyright (c) 1996,1997
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
20 #ifndef _STLP_FSTREAM_C
21 #define _STLP_FSTREAM_C
23 # ifndef _STLP_INTERNAL_FSTREAM_H
24 # include <stl/_fstream.h>
27 # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION)
31 # if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
32 // no wchar_t is supported for this mode
33 # define __BF_int_type__ int
34 # define __BF_pos_type__ streampos
35 # define __BF_off_type__ streamoff
37 # define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
38 # define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type
39 # define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type
43 //----------------------------------------------------------------------
44 // Public basic_filebuf<> member functions
46 template <class _CharT, class _Traits>
47 basic_filebuf<_CharT, _Traits>::basic_filebuf()
48 : basic_streambuf<_CharT, _Traits>(), _M_base(),
49 _M_constant_width(false), _M_always_noconv(false),
50 _M_int_buf_dynamic(false),
51 _M_in_input_mode(false), _M_in_output_mode(false),
52 _M_in_error_mode(false), _M_in_putback_mode(false),
53 _M_int_buf(0), _M_int_buf_EOS(0),
54 _M_ext_buf(0), _M_ext_buf_EOS(0),
55 _M_ext_buf_converted(0), _M_ext_buf_end(0),
56 _M_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
57 _M_end_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
58 _M_mmap_base(0), _M_mmap_len(0),
59 _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0),
61 _M_width(1), _M_max_width(1)
63 this->_M_setup_codecvt(locale());
66 template <class _CharT, class _Traits>
67 basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
69 _M_deallocate_buffers();
73 template <class _CharT, class _Traits>
74 _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
75 basic_filebuf<_CharT, _Traits>::underflow()
77 return _Underflow<_CharT, _Traits>::_M_doit(this);
80 template <class _CharT, class _Traits>
81 basic_filebuf<_CharT, _Traits>*
82 basic_filebuf<_CharT, _Traits>::close()
84 bool __ok = this->is_open();
86 if (_M_in_output_mode) {
87 __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()),
89 __ok == __ok && this->_M_unshift();
91 else if (_M_in_input_mode)
92 this->_M_exit_input_mode();
94 // Note order of arguments. We close the file even if __ok is false.
95 __ok = _M_base._M_close() && __ok;
97 // Restore the initial state, except that we don't deallocate the buffer
98 // or mess with the cached codecvt information.
99 _M_state = _M_end_state = _State_type();
100 _M_ext_buf_converted = _M_ext_buf_end = 0;
107 #endif // __SYMBIAN32__
112 #endif // __SYMBIAN32__
114 _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0;
116 _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode
119 return __ok ? this : 0;
122 // This member function is called whenever we exit input mode.
123 // It unmaps the memory-mapped file, if any, and sets
124 // _M_in_input_mode to false.
125 template <class _CharT, class _Traits>
126 void basic_filebuf<_CharT, _Traits>::_M_exit_input_mode()
128 if (_M_mmap_base != 0)
129 _M_base._M_unmap(_M_mmap_base, _M_mmap_len);
130 _M_in_input_mode = false;
135 //----------------------------------------------------------------------
136 // basic_filebuf<> overridden protected virtual member functions
138 template <class _CharT, class _Traits>
139 streamsize basic_filebuf<_CharT, _Traits>::showmanyc()
141 // Is there any possibility that reads can succeed?
143 if (!this->is_open() || !(_M_base.__o_mode() & (int)ios_base::in) || _M_in_error_mode)
145 if (!this->is_open() || _M_in_output_mode || _M_in_error_mode)
149 else if (_M_in_putback_mode)
150 return this->egptr() - this->gptr();
152 else if (_M_constant_width) {
153 streamoff __pos = _M_base._M_seek(0, ios_base::cur);
154 streamoff __size = _M_base._M_file_size();
159 return __pos >= 0 && __size > __pos ? __size - __pos : -1;
167 // Make a putback position available, if necessary, by switching to a
168 // special internal buffer used only for putback. The buffer is
169 // [_M_pback_buf, _M_pback_buf + _S_pback_buf_size), but the base
170 // class only sees a piece of it at a time. (We want to make sure
171 // that we don't try to read a character that hasn't been initialized.)
172 // The end of the putback buffer is always _M_pback_buf + _S_pback_buf_size,
173 // but the beginning is usually not _M_pback_buf.
174 template <class _CharT, class _Traits>
176 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
178 const int_type __eof = traits_type::eof();
180 // If we aren't already in input mode, pushback is impossible.
181 if (!_M_in_input_mode)
184 // We can use the ordinary get buffer if there's enough space, and
185 // if it's a buffer that we're allowed to write to.
186 if (this->gptr() != this->eback() &&
187 (traits_type::eq_int_type(__c, __eof) ||
188 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]) ||
191 if (traits_type::eq_int_type(__c, __eof) ||
192 traits_type::eq(traits_type::to_char_type(__c), *this->gptr()))
193 return traits_type::to_int_type(*this->gptr());
195 else if (!traits_type::eq_int_type(__c, __eof)) {
196 // Are we in the putback buffer already?
197 _CharT* __pback_end = _M_pback_buf + __STATIC_CAST(int,_S_pback_buf_size);
198 if (_M_in_putback_mode) {
199 // Do we have more room in the putback buffer?
200 if (this->eback() != _M_pback_buf)
201 this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end);
203 return __eof; // No more room in the buffer, so fail.
205 else { // We're not yet in the putback buffer.
206 _M_saved_eback = this->eback();
207 _M_saved_gptr = this->gptr();
208 _M_saved_egptr = this->egptr();
209 this->setg(__pback_end - 1, __pback_end - 1, __pback_end);
210 _M_in_putback_mode = true;
216 if (traits_type::eq_int_type(__c, __eof) )
218 if(_M_in_putback_mode)
220 _M_in_putback_mode = false;
221 // we are at putback mode
222 if(_M_saved_eback != _M_saved_gptr)
224 this->setg(_M_saved_eback, _M_saved_gptr - 1, _M_saved_egptr);
225 return *this->gptr();
228 this->setg(_M_saved_eback, _M_saved_gptr, _M_saved_egptr);
235 streamoff __pos = _M_base._M_seek(0, ios_base::cur);
236 streamoff __size = _M_base._M_file_size();
237 if( __size > 0 && __pos > 0 && __pos == __size)
238 __pos = _M_base._M_seek(__pos - 1, ios_base::beg);
240 this->setg(this->eback(), this->gptr(), this->egptr());
241 return *this->gptr();
245 this->setg(this->eback(), this->gptr() - 1, this->egptr());
246 return *this->gptr();
253 // We have made a putback position available. Assign to it, and return.
254 *this->gptr() = traits_type::to_char_type(__c);
258 // This member function flushes the put area, and also outputs the
259 // character __c (unless __c is eof). Invariant: we always leave room
260 // in the internal buffer for one character more than the base class knows
261 // about. We see the internal buffer as [_M_int_buf, _M_int_buf_EOS), but
262 // the base class only sees [_M_int_buf, _M_int_buf_EOS - 1).
263 template <class _CharT, class _Traits>
265 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
267 // Switch to output mode, if necessary.
268 bool putflag = false;
269 if (!_M_in_output_mode)
275 if (!_M_switch_to_output_mode())
276 return traits_type::eof();
278 _CharT* __ibegin = this->_M_int_buf;
279 _CharT* __iend = this->pptr();
280 this->setp(_M_int_buf, _M_int_buf_EOS - 1);
282 // Put __c at the end of the internal buffer.
283 if (!traits_type::eq_int_type(__c, traits_type::eof()))
286 int current_pos = this->gptr() - this->eback();
287 streamoff __size = _M_base._M_file_size();
288 if(current_pos > 0 && current_pos < __size && _M_base.__is_open())
289 _M_base._M_seek(current_pos, ios_base::beg);
292 // For variable-width encodings, output may take more than one pass.
293 while (__ibegin != __iend) {
294 const _CharT* __inext = __ibegin;
295 char* __enext = _M_ext_buf;
296 typename _Codecvt::result __status
297 = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
298 _M_ext_buf, _M_ext_buf_EOS, __enext);
299 if (__status == _Codecvt::noconv)
302 if(_Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend))
306 *(this->gptr()) = __c;
307 this->setg(this->eback(), this->gptr() + 1, this->egptr());
309 if(putflag && this->pptr() < this->epptr())
310 this->setp(this->pptr() + 1, this->epptr());
311 return traits_type::not_eof(__c);
314 return _M_output_error();
317 return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
318 ? traits_type::not_eof(__c)
321 // For a constant-width encoding we know that the external buffer
322 // is large enough, so failure to consume the entire internal buffer
323 // or to produce the correct number of external characters, is an error.
324 // For a variable-width encoding, however, we require only that we
325 // consume at least one internal character
326 else if (__status != _Codecvt::error &&
327 ((__inext == __iend && (__enext - _M_ext_buf ==
328 _M_width * (__iend - __ibegin))) ||
329 (!_M_constant_width && __inext != __ibegin))) {
330 // We successfully converted part or all of the internal buffer.
331 ptrdiff_t __n = __enext - _M_ext_buf;
332 if (_M_write(_M_ext_buf, __n))
333 __ibegin += __inext - __ibegin;
335 return _M_output_error();
338 return _M_output_error();
341 return traits_type::not_eof(__c);
344 // This member function must be called before any I/O has been
345 // performed on the stream, otherwise it has no effect.
347 // __buf == 0 && __n == 0 means to make ths stream unbuffered.
348 // __buf != 0 && __n > 0 means to use __buf as the stream's internal
349 // buffer, rather than the buffer that would otherwise be allocated
350 // automatically. __buf must be a pointer to an array of _CharT whose
351 // size is at least __n.
352 template <class _CharT, class _Traits>
353 basic_streambuf<_CharT, _Traits>*
354 basic_filebuf<_CharT, _Traits>::setbuf(_CharT* __buf, streamsize __n)
356 if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode &&
358 if (__buf == 0 && __n == 0)
359 _M_allocate_buffers(0, 1);
360 else if (__buf != 0 && __n > 0)
361 _M_allocate_buffers(__buf, __n);
366 template <class _CharT, class _Traits>
368 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off,
369 ios_base::seekdir __whence,
370 ios_base::openmode /* dummy */)
372 if (this->is_open() &&
373 (__off == 0 || (_M_constant_width && this->_M_base._M_in_binary_mode()))) {
375 if (!_M_seek_init(__off != 0 || __whence != ios_base::cur))
378 // Seek to beginning or end, regardless of whether we're in input mode.
379 if (__whence == ios_base::beg || __whence == ios_base::end)
380 return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
383 // Seek relative to current position. Complicated if we're in input mode.
384 else if (__whence == ios_base::cur) {
386 if (!_M_in_input_mode)
387 return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
389 else if (_M_mmap_base != 0) {
390 // __off is relative to gptr(). We need to do a bit of arithmetic
391 // to get an offset relative to the external file pointer.
392 streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base);
394 // if __off == 0, we do not need to exit input mode and to shift file pointer
396 return pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust);
400 return _M_seek_return(_M_base._M_seek(__off, ios_base::cur), _State_type());
402 return _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur), _State_type());
405 else if (_M_constant_width) { // Get or set the position.
407 streamoff __iadj = _M_width * (this->gptr() - this->eback());
409 // Compensate for offset relative to gptr versus offset relative
410 // to external pointer. For a text-oriented stream, where the
411 // compensation is more than just pointer arithmetic, we may get
412 // but not set the current position.
414 if (__iadj <= _M_ext_buf_end - _M_ext_buf) {
416 streamoff __eadj = _M_base._M_get_offset(_M_ext_buf + __iadj, _M_ext_buf_end);
419 return pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj);
421 return _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur), _State_type());
427 else { // Get the position. Encoding is var width.
428 // Get position in internal buffer.
429 ptrdiff_t __ipos = this->gptr() - this->eback();
431 // Get corresponding position in external buffer.
432 _State_type __state = _M_state;
433 int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_end,
436 // Sanity check (expensive): make sure __epos is the right answer.
437 _State_type __tmp_state = _M_state;
438 _Filebuf_Tmp_Buf<_CharT> __buf(__ipos);
439 _CharT* __ibegin = __buf._M_ptr;
440 _CharT* __inext = __ibegin;
443 typename _Codecvt::result __status
444 = _M_codecvt->in(__tmp_state,
445 _M_ext_buf, _M_ext_buf + __epos, __dummy,
446 __ibegin, __ibegin + __ipos, __inext);
447 if (__status != _Codecvt::error &&
448 (__status == _Codecvt::noconv ||
449 (__inext == __ibegin + __ipos &&
450 equal(this->gptr(), this->eback(), __ibegin,
451 _Eq_traits<traits_type>())))) {
452 // Get the current position (at the end of the external buffer),
453 // then adjust it. Again, it might be a text-oriented stream.
454 streamoff __cur = _M_base._M_seek(0, ios_base::cur);
456 _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) -
457 _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end);
458 if (__cur != -1 && __cur + __adj >= 0)
459 return _M_seek_return(__cur + __adj, __state);
463 else // We failed the sanity check.
467 else // Unrecognized value for __whence.
475 template <class _CharT, class _Traits>
477 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __pos,
478 ios_base::openmode /* dummy */)
480 if (this->is_open()) {
481 if (!_M_seek_init(true))
484 streamoff __off = off_type(__pos);
485 if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) {
486 _M_state = __pos.state();
487 return _M_seek_return(__off, __pos.state());
497 template <class _CharT, class _Traits>
498 int basic_filebuf<_CharT, _Traits>::sync()
500 if (_M_in_output_mode)
501 return traits_type::eq_int_type(this->overflow(traits_type::eof()),
510 // Change the filebuf's locale. This member function has no effect
511 // unless it is called before any I/O is performed on the stream.
512 template <class _CharT, class _Traits>
513 void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
515 if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode) {
516 this->_M_setup_codecvt(__loc);
520 //----------------------------------------------------------------------
521 // basic_filebuf<> helper functions.
523 //----------------------------------------
524 // Helper functions for switching between modes.
526 // This member function is called if we're performing the first I/O
527 // operation on a filebuf, or if we're performing an input operation
528 // immediately after a seek.
529 template <class _CharT, class _Traits>
530 bool basic_filebuf<_CharT, _Traits>::_M_switch_to_input_mode()
533 if (this->is_open() && (((int)_M_base.__o_mode() & (int)ios_base::in) !=0)
534 #ifndef __SYMBIAN32__
535 && (_M_in_output_mode == 0)
537 && (_M_in_error_mode == 0)) {
538 #ifdef __STLP_NO_WRITE_SIDE_BUFFERING__
539 // If file has been opened in input|output mode
540 if ((((int)_M_base.__o_mode() & (int)ios_base::out) !=0)
541 #ifndef __SYMBIAN32__
544 && !_M_int_buf && !_M_allocate_buffers(0, 1))
546 if (!_M_int_buf && !_M_allocate_buffers())
550 _M_ext_buf_converted = _M_ext_buf;
551 _M_ext_buf_end = _M_ext_buf;
553 _M_end_state = _M_state;
555 _M_in_input_mode = true;
564 // This member function is called if we're performing the first I/O
565 // operation on a filebuf, or if we're performing an output operation
566 // immediately after a seek.
567 template <class _CharT, class _Traits>
568 bool basic_filebuf<_CharT, _Traits>::_M_switch_to_output_mode()
570 if (this->is_open() && (_M_base.__o_mode() & (int)ios_base::out) &&
572 _M_in_error_mode == 0) {
574 _M_in_input_mode == 0 && _M_in_error_mode == 0) {
575 #endif //__SYMBIAN32__
577 #ifdef __STLP_NO_WRITE_SIDE_BUFFERING__
578 if (!_M_int_buf && !_M_allocate_buffers(0, 1))
580 if (!_M_int_buf && !_M_allocate_buffers())
584 // In append mode, every write does an implicit seek to the end
585 // of the file. Whenever leaving output mode, the end of file
586 // get put in the initial shift state.
587 if (_M_base.__o_mode() & ios_base::app)
588 _M_state = _State_type();
590 this->setp(_M_int_buf, _M_int_buf_EOS - 1);
591 _M_in_output_mode = true;
600 //----------------------------------------
601 // Helper functions for input
603 // This member function is called if there is an error during input.
604 // It puts the filebuf in error mode, clear the get area buffer, and
606 // returns eof. Error mode is sticky; it is cleared only by close or
609 template <class _CharT, class _Traits>
611 basic_filebuf<_CharT, _Traits>::_M_input_error()
613 this->_M_exit_input_mode();
614 _M_in_output_mode = false;
615 _M_in_error_mode = true;
617 return traits_type::eof();
620 template <class _CharT, class _Traits>
622 basic_filebuf<_CharT, _Traits>::_M_underflow_aux()
624 // We have the state and file position from the end of the internal
625 // buffer. This round, they become the beginning of the internal buffer.
626 _M_state = _M_end_state;
628 // Fill the external buffer. Start with any leftover characters that
629 // didn't get converted last time.
630 if (_M_ext_buf_end > _M_ext_buf_converted)
632 _M_ext_buf_end = copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf);
633 // boris : copy_backward did not work
634 //_M_ext_buf_end = copy_backward(_M_ext_buf_converted, _M_ext_buf_end,
635 //_M_ext_buf+ (_M_ext_buf_end - _M_ext_buf_converted));
639 if(_M_ext_buf == NULL)
640 _M_allocate_buffers(0, MMAP_CHUNK);
642 _M_ext_buf_end = _M_ext_buf;
644 // Now fill the external buffer with characters from the file. This is
645 // a loop because occasonally we don't get enough external characters
648 ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end);
650 // Don't enter error mode for a failed read. Error mode is sticky,
651 // and we might succeed if we try again.
652 #ifdef __SYMBIAN32__ //plum hall bug 577
653 int nn = (char*)_M_ext_buf_end-(char*)_M_ext_buf; //number of chars unconverted last time
654 if ( (__n <= 0) && ( nn<=0 ) )
658 return traits_type::eof();
660 // Convert the external buffer to internal characters.
661 _M_ext_buf_end += __n;
665 typename _Codecvt::result __status
666 = _M_codecvt->in(_M_end_state,
667 _M_ext_buf, _M_ext_buf_end, __enext,
668 _M_int_buf, _M_int_buf_EOS, __inext);
670 // Error conditions: (1) Return value of error. (2) Producing internal
671 // characters without consuming external characters. (3) In fixed-width
672 // encodings, producing an internal sequence whose length is inconsistent
673 // with that of the internal sequence. (4) Failure to produce any
674 // characters if we have enough characters in the external buffer, where
675 // "enough" means the largest possible width of a single character.
676 if (__status == _Codecvt::noconv)
677 return _Noconv_input<_Traits>::_M_doit(this);
679 else if (__status == _Codecvt::error ||
680 (__inext != _M_int_buf && __enext == _M_ext_buf) ||
681 (_M_constant_width &&
682 // __inext - _M_int_buf != _M_width * (__enext - _M_ext_buf)) ||
683 (__inext - _M_int_buf) * _M_width != (__enext - _M_ext_buf)) ||
684 (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width))
685 return _M_input_error();
687 else if (__inext != _M_int_buf) {
688 _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf);
689 this->setg(_M_int_buf, _M_int_buf, __inext);
690 return traits_type::to_int_type(*_M_int_buf);
692 // We need to go around the loop again to get more external characters.
696 //----------------------------------------
697 // Helper functions for output
699 // This member function is called if there is an error during output.
700 // It puts the filebuf in error mode, clear the put area buffer, and
701 // returns eof. Error mode is sticky; it is cleared only by close or
703 template <class _CharT, class _Traits>
705 basic_filebuf<_CharT, _Traits>::_M_output_error()
707 _M_in_output_mode = false;
708 _M_in_input_mode = false;
709 _M_in_error_mode = true;
711 return traits_type::eof();
715 // Write whatever sequence of characters is necessary to get back to
716 // the initial shift state. This function overwrites the external
717 // buffer, changes the external file position, and changes the state.
718 // Precondition: the internal buffer is empty.
719 template <class _CharT, class _Traits>
720 bool basic_filebuf<_CharT, _Traits>::_M_unshift()
722 if (_M_in_output_mode && !_M_constant_width) {
723 typename _Codecvt::result __status;
725 char* __enext = _M_ext_buf;
726 __status = _M_codecvt->unshift(_M_state,
727 _M_ext_buf, _M_ext_buf_EOS, __enext);
728 if (__status == _Codecvt::noconv ||
729 (__enext == _M_ext_buf && __status == _Codecvt::ok))
731 else if (__status == _Codecvt::error)
733 else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf))
735 } while(__status == _Codecvt::partial);
742 //----------------------------------------
743 // Helper functions for buffer allocation and deallocation
745 // This member function is called when we're initializing a filebuf's
746 // internal and external buffers. The argument is the size of the
747 // internal buffer; the external buffer is sized using the character
748 // width in the current encoding. Preconditions: the buffers are currently
749 // null. __n >= 1. __buf is either a null pointer or a pointer to an
750 // array show size is at least __n.
752 // We need __n >= 1 for two different reasons. For input, the base
753 // class always needs a buffer because of the sementics of underflow().
754 // For output, we want to have an internal buffer that's larger by one
755 // element than the buffer that the base class knows about. (See
756 // basic_filebuf<>::overflow() for the reason.)
757 template <class _CharT, class _Traits>
759 basic_filebuf<_CharT, _Traits>::_M_allocate_buffers(_CharT* __buf, streamsize __n)
763 _M_int_buf = __STATIC_CAST(_CharT*,malloc(__n * sizeof(_CharT)));
766 _M_int_buf_dynamic = true;
770 _M_int_buf_dynamic = false;
773 size_t __ebufsiz = (max)(__n * (max)(_M_codecvt->encoding(), 1),
774 streamsize(_M_codecvt->max_length()));
776 _M_ext_buf = __STATIC_CAST(char*,malloc(__ebufsiz));
778 _M_deallocate_buffers();
782 _M_int_buf_EOS = _M_int_buf + __n;
783 _M_ext_buf_EOS = _M_ext_buf + __ebufsiz;
787 // Abbreviation for the most common case.
788 template <class _CharT, class _Traits>
789 bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers()
791 // Choose a buffer that's at least 4096 characters long and that's a
792 // multiple of the page size.
793 streamsize __default_bufsiz =
794 ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size();
795 return _M_allocate_buffers(0, __default_bufsiz);
798 template <class _CharT, class _Traits>
799 void basic_filebuf<_CharT, _Traits>::_M_deallocate_buffers()
801 if (_M_int_buf_dynamic)
811 //----------------------------------------
812 // Helper functiosn for seek and imbue
814 template <class _CharT, class _Traits>
815 bool basic_filebuf<_CharT, _Traits>::_M_seek_init(bool __do_unshift) {
816 // If we're in error mode, leave it.
817 _M_in_error_mode = false;
819 // Flush the output buffer if we're in output mode, and (conditionally)
820 // emit an unshift sequence.
821 if (_M_in_output_mode) {
822 bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()),
825 __ok = __ok && this->_M_unshift();
827 _M_in_output_mode = false;
828 _M_in_error_mode = true;
834 // Discard putback characters, if any.
835 if (_M_in_input_mode && _M_in_putback_mode)
836 _M_exit_putback_mode();
842 // Change the filebuf's locale. This member function has no effect
843 // unless it is called before any I/O is performed on the stream.
844 template <class _CharT, class _Traits>
845 void basic_filebuf<_CharT, _Traits>::_M_setup_codecvt(const locale& __loc)
847 _M_codecvt = &use_facet<_Codecvt>(__loc) ;
848 int __encoding = _M_codecvt->encoding();
850 _M_width = (max)(__encoding, 1);
851 _M_max_width = _M_codecvt->max_length();
852 _M_constant_width = __encoding > 0;
853 _M_always_noconv = _M_codecvt->always_noconv();
858 template <class _CharT, class _Traits>
859 _STLP_EXP_DECLSPEC basic_fstream<_CharT, _Traits>::basic_fstream()
860 : basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
865 template <class _CharT, class _Traits>
866 _STLP_EXP_DECLSPEC basic_fstream<_CharT, _Traits>::~basic_fstream(){}
869 template <class _CharT, class _Traits>
870 int basic_filebuf<_CharT, _Traits>::save_read_buffer ()
872 return _M_in_putback_mode ? _M_saved_egptr - _M_saved_gptr : 0;
874 template <class _CharT, class _Traits>
875 void basic_filebuf<_CharT, _Traits>::_change_input_mode ()
877 _M_in_input_mode = 1;
884 # undef __BF_int_type__
885 # undef __BF_pos_type__
886 # undef __BF_off_type__
888 # endif /* defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) */
890 #endif /* _STLP_FSTREAM_C */