2000-04-21 22:33:34 +02:00
|
|
|
// File based streams -*- C++ -*-
|
|
|
|
|
2000-05-08 19:23:37 +02:00
|
|
|
// Copyright (C) 1997-1999, 2000 Free Software Foundation, Inc.
|
2000-04-21 22:33:34 +02:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
// software; you can redistribute it and/or modify it under the
|
|
|
|
// terms of the GNU General Public License as published by the
|
|
|
|
// Free Software Foundation; either version 2, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this library; see the file COPYING. If not, write to the Free
|
|
|
|
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
// USA.
|
|
|
|
|
|
|
|
// As a special exception, you may use this file as part of a free software
|
|
|
|
// library without restriction. Specifically, if other files instantiate
|
|
|
|
// templates or use macros or inline functions from this file, or you compile
|
|
|
|
// this file and link it with other files to produce an executable, this
|
|
|
|
// file does not by itself cause the resulting executable to be covered by
|
|
|
|
// the GNU General Public License. This exception does not however
|
|
|
|
// invalidate any other reasons why the executable file might be covered by
|
|
|
|
// the GNU General Public License.
|
|
|
|
|
|
|
|
//
|
|
|
|
// ISO C++ 14882: 27.8 File-based streams
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef _CPP_BITS_FSTREAM_TCC
|
|
|
|
#define _CPP_BITS_FSTREAM_TCC 1
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
void
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
2000-07-21 02:06:51 +02:00
|
|
|
_M_filebuf_init()
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
_M_buf_unified = true; // Tie input to output for basic_filebuf.
|
2000-05-08 19:23:37 +02:00
|
|
|
_M_buf_size = _M_buf_size_opt;
|
2000-04-21 22:33:34 +02:00
|
|
|
try {
|
|
|
|
_M_file = new __file_type(&_M_lock);
|
|
|
|
}
|
|
|
|
catch(...) {
|
|
|
|
delete _M_file;
|
|
|
|
throw;
|
|
|
|
}
|
2000-07-21 02:06:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
void
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
_M_allocate_buffers()
|
|
|
|
{
|
|
|
|
// Allocate internal buffer.
|
|
|
|
try {
|
|
|
|
_M_buf = new char_type[_M_buf_size];
|
|
|
|
}
|
|
|
|
catch(...) {
|
|
|
|
delete [] _M_buf;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate pback buffer.
|
|
|
|
try {
|
|
|
|
_M_pback = new char_type[_M_pback_size];
|
|
|
|
}
|
|
|
|
catch(...) {
|
|
|
|
delete [] _M_pback;
|
|
|
|
throw;
|
|
|
|
}
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
basic_filebuf()
|
2000-06-24 02:56:05 +02:00
|
|
|
: __streambuf_type(), _M_file(NULL), _M_state_cur(), _M_state_beg(),
|
2000-06-29 20:20:55 +02:00
|
|
|
_M_last_overflowed(false)
|
2000-04-21 22:33:34 +02:00
|
|
|
{ _M_fcvt = &use_facet<__codecvt_type>(this->getloc()); }
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
basic_filebuf(int __fd, const char* /*__name*/, ios_base::openmode __mode)
|
2000-06-24 02:56:05 +02:00
|
|
|
: __streambuf_type(), _M_state_cur(), _M_state_beg(),
|
|
|
|
_M_last_overflowed(false)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
_M_fcvt = &use_facet<__codecvt_type>(this->getloc());
|
2000-07-21 02:06:51 +02:00
|
|
|
_M_filebuf_init();
|
2000-04-21 22:33:34 +02:00
|
|
|
_M_file->sys_open(__fd, __mode);
|
|
|
|
if (this->is_open() && _M_buf_size)
|
|
|
|
{
|
2000-07-21 02:06:51 +02:00
|
|
|
_M_allocate_buffers();
|
2000-04-21 22:33:34 +02:00
|
|
|
_M_mode = __mode;
|
2000-07-21 02:06:51 +02:00
|
|
|
|
2000-04-21 22:33:34 +02:00
|
|
|
// XXX So that istream::getc() will only need to get 1 char,
|
|
|
|
// as opposed to BUF_SIZE.
|
|
|
|
if (__fd == 0)
|
|
|
|
_M_buf_size = 1;
|
|
|
|
|
|
|
|
this->_M_set_indeterminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::__filebuf_type*
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
open(const char* __s, ios_base::openmode __mode)
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
__filebuf_type *__ret = NULL;
|
2000-04-21 22:33:34 +02:00
|
|
|
if (!this->is_open())
|
|
|
|
{
|
2000-07-21 02:06:51 +02:00
|
|
|
_M_filebuf_init();
|
2000-04-21 22:33:34 +02:00
|
|
|
_M_file->open(__s, __mode);
|
|
|
|
if (this->is_open() && _M_buf_size)
|
|
|
|
{
|
2000-07-21 02:06:51 +02:00
|
|
|
_M_allocate_buffers();
|
2000-04-21 22:33:34 +02:00
|
|
|
_M_mode = __mode;
|
|
|
|
|
|
|
|
// For time being, set both (in/out) sets of pointers.
|
|
|
|
_M_set_indeterminate();
|
|
|
|
if (__mode & ios_base::ate
|
|
|
|
&& this->seekoff(0, ios_base::end, __mode) < 0)
|
|
|
|
this->close();
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = this;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::__filebuf_type*
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
close()
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
__filebuf_type *__ret = NULL;
|
2000-04-21 22:33:34 +02:00
|
|
|
if (this->is_open())
|
|
|
|
{
|
|
|
|
bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
|
|
|
|
if (__testput)
|
|
|
|
_M_really_overflow(traits_type::eof());
|
2000-07-21 02:06:51 +02:00
|
|
|
|
|
|
|
// NB: Do this here so that re-opened filebufs will be cool...
|
|
|
|
_M_pback_destroy();
|
|
|
|
|
2000-04-21 22:33:34 +02:00
|
|
|
#if 0
|
|
|
|
// XXX not done
|
|
|
|
if (_M_last_overflowed)
|
|
|
|
{
|
|
|
|
_M_output_unshift();
|
|
|
|
_M_really_overflow(traits_type::eof());
|
|
|
|
}
|
|
|
|
#endif
|
2000-06-24 02:56:05 +02:00
|
|
|
|
2000-07-21 02:06:51 +02:00
|
|
|
_M_mode = ios_base::openmode(0);
|
|
|
|
if (_M_buf_size)
|
|
|
|
delete [] _M_buf;
|
|
|
|
_M_buf = NULL;
|
|
|
|
delete [] _M_pback;
|
|
|
|
_M_pback = NULL;
|
|
|
|
this->setg(NULL, NULL, NULL);
|
|
|
|
this->setp(NULL, NULL);
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = this;
|
2000-07-21 02:06:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Can actually allocate this file as part of an open and never
|
|
|
|
// have it be opened.....
|
|
|
|
if (_M_file)
|
|
|
|
{
|
|
|
|
delete _M_file;
|
|
|
|
_M_file = NULL;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
_M_last_overflowed = false;
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
streamsize
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
showmanyc()
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
streamsize __ret = -1;
|
2000-04-21 22:33:34 +02:00
|
|
|
bool __testin = _M_mode & ios_base::in;
|
|
|
|
|
|
|
|
if (__testin)
|
|
|
|
{
|
|
|
|
bool __testeof = false;
|
|
|
|
if (_M_in_cur >= _M_in_end)
|
|
|
|
__testeof = this->underflow() == traits_type::eof();
|
|
|
|
if (!__testeof)
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = _M_in_end - _M_in_cur;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
_M_last_overflowed = false;
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::int_type
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
underflow()
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
int_type __ret = traits_type::eof();
|
2000-04-21 22:33:34 +02:00
|
|
|
bool __testin = _M_mode & ios_base::in;
|
|
|
|
|
|
|
|
if (__testin)
|
|
|
|
{
|
2000-07-21 02:06:51 +02:00
|
|
|
// Check for pback madness, and if so swich back to the
|
|
|
|
// normal buffers and jet outta here before expensive
|
|
|
|
// fileops happen...
|
|
|
|
if (_M_pback_init)
|
|
|
|
{
|
|
|
|
_M_pback_destroy();
|
|
|
|
if (_M_in_cur < _M_in_end)
|
|
|
|
return traits_type::to_int_type(*_M_in_cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool __testget = _M_in_cur && _M_in_beg < _M_in_cur;
|
|
|
|
bool __testinit = _M_is_indeterminate();
|
|
|
|
bool __testout = _M_mode & ios_base::out;
|
|
|
|
|
2000-04-21 22:33:34 +02:00
|
|
|
// Sync internal and external buffers.
|
|
|
|
// NB: __testget -> __testput as _M_buf_unified here.
|
|
|
|
if (__testget)
|
|
|
|
{
|
|
|
|
if (__testout)
|
|
|
|
_M_really_overflow();
|
|
|
|
else
|
2000-04-28 04:50:55 +02:00
|
|
|
_M_file->seekoff(_M_in_cur - _M_in_beg,
|
|
|
|
ios_base::cur, ios_base::in);
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (__testinit || __testget)
|
|
|
|
{
|
2000-08-09 09:33:39 +02:00
|
|
|
#if 1
|
|
|
|
streamsize __size = _M_file->xsgetn(_M_in_beg, _M_buf_size);
|
|
|
|
if (0 < __size)
|
|
|
|
{
|
|
|
|
_M_set_determinate(__size);
|
|
|
|
streamoff __p = _M_file->seekoff(0 - __size, ios_base::cur,
|
|
|
|
ios_base::in);
|
|
|
|
if (__p == -1)
|
|
|
|
{
|
|
|
|
// XXX Something is wrong, do error checking.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (__testout)
|
|
|
|
_M_out_cur = _M_in_cur;
|
|
|
|
__ret = traits_type::to_int_type(*_M_in_cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// 2000-08-04 bkoz disable
|
2000-04-21 22:33:34 +02:00
|
|
|
// Part one: (Re)fill external buf (_M_file->_IO_*) from
|
|
|
|
// external byte sequence (whatever physical byte sink or
|
|
|
|
// FILE actually is.)
|
2000-08-09 09:33:39 +02:00
|
|
|
char_type __conv_buf[_M_buf_size];
|
2000-04-28 04:50:55 +02:00
|
|
|
streamsize __size = _M_file->xsgetn(__conv_buf, _M_buf_size);
|
2000-04-21 22:33:34 +02:00
|
|
|
|
|
|
|
// Part two: (Re)fill internal buf contents from external buf.
|
|
|
|
if (0 < __size)
|
|
|
|
{
|
|
|
|
_M_set_determinate(__size);
|
|
|
|
|
|
|
|
char* __conv_cur = __conv_buf;
|
|
|
|
_M_state_beg = _M_state_cur;
|
|
|
|
__res_type __r = _M_fcvt->in(_M_state_cur,
|
|
|
|
__conv_buf,
|
|
|
|
__conv_buf + __size,
|
|
|
|
const_cast<const char*&>(__conv_cur),
|
|
|
|
_M_in_beg, _M_in_end, _M_in_cur);
|
|
|
|
|
|
|
|
if (__r == codecvt_base::partial)
|
|
|
|
{
|
|
|
|
// XXX Retry with larger _M_buf size.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set pointers to internal and external buffers
|
|
|
|
// correctly. . .
|
|
|
|
if (__r != codecvt_base::error)
|
|
|
|
{
|
|
|
|
if (__testout)
|
|
|
|
_M_out_cur = _M_in_cur;
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = traits_type::to_int_type(*_M_in_cur);
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Part three: Sync the current internal buffer
|
|
|
|
// position with the (now overshot) external buffer
|
|
|
|
// position.
|
2000-04-28 04:50:55 +02:00
|
|
|
streamoff __p = _M_file->seekoff(0 - __size, ios_base::cur,
|
2000-04-21 22:33:34 +02:00
|
|
|
ios_base::in);
|
2000-04-28 04:50:55 +02:00
|
|
|
if (__p == -1)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
// XXX Something is wrong, do error checking.
|
|
|
|
}
|
|
|
|
}
|
2000-08-09 09:33:39 +02:00
|
|
|
#endif
|
|
|
|
}
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
_M_last_overflowed = false;
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::int_type
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
pbackfail(int_type __i)
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
int_type __ret = traits_type::eof();
|
2000-04-21 22:33:34 +02:00
|
|
|
bool __testin = _M_mode & ios_base::in;
|
|
|
|
|
|
|
|
if (__testin)
|
|
|
|
{
|
2000-07-21 02:06:51 +02:00
|
|
|
bool __testpb = _M_in_beg < _M_in_cur;
|
|
|
|
char_type __c = traits_type::to_char_type(__i);
|
2000-07-26 08:51:38 +02:00
|
|
|
bool __testeof = traits_type::eq_int_type(__i, __ret);
|
2000-07-21 02:06:51 +02:00
|
|
|
|
|
|
|
if (__testpb)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
2000-07-21 02:06:51 +02:00
|
|
|
bool __testout = _M_mode & ios_base::out;
|
2000-04-21 22:33:34 +02:00
|
|
|
bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
|
|
|
|
|
|
|
|
// Try to put back __c into input sequence in one of three ways.
|
|
|
|
// Order these tests done in is unspecified by the standard.
|
2000-07-21 02:06:51 +02:00
|
|
|
if (!__testeof && __testeq)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
--_M_in_cur;
|
|
|
|
if (__testout)
|
|
|
|
--_M_out_cur;
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = __i;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
2000-07-21 02:06:51 +02:00
|
|
|
else if (__testeof)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
--_M_in_cur;
|
|
|
|
if (__testout)
|
|
|
|
--_M_out_cur;
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = traits_type::not_eof(__i);
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
2000-07-21 02:06:51 +02:00
|
|
|
else if (!__testeof)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
--_M_in_cur;
|
|
|
|
if (__testout)
|
|
|
|
--_M_out_cur;
|
2000-07-21 02:06:51 +02:00
|
|
|
_M_pback_create();
|
|
|
|
*_M_in_cur = __c;
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = __i;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2000-07-21 02:06:51 +02:00
|
|
|
{
|
|
|
|
// At the beginning of the buffer, need to make a
|
|
|
|
// putback position available.
|
2000-04-21 22:33:34 +02:00
|
|
|
this->seekoff(-1, ios_base::cur);
|
|
|
|
this->underflow();
|
2000-07-21 02:06:51 +02:00
|
|
|
if (!__testeof)
|
|
|
|
{
|
|
|
|
if (!traits_type::eq(__c, *_M_in_cur))
|
|
|
|
{
|
|
|
|
_M_pback_create();
|
|
|
|
*_M_in_cur = __c;
|
|
|
|
}
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = __i;
|
2000-07-21 02:06:51 +02:00
|
|
|
}
|
|
|
|
else
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = traits_type::not_eof(__i);
|
2000-07-21 02:06:51 +02:00
|
|
|
}
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
_M_last_overflowed = false;
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::int_type
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
overflow(int_type __c)
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
int_type __ret = traits_type::eof();
|
2000-04-21 22:33:34 +02:00
|
|
|
bool __testpos = _M_out_cur && _M_out_cur >= _M_buf + _M_buf_size;
|
|
|
|
bool __testout = _M_mode & ios_base::out;
|
|
|
|
|
|
|
|
if (__testout)
|
|
|
|
{
|
|
|
|
if (!__testpos)
|
|
|
|
{
|
|
|
|
*_M_out_cur = traits_type::to_char_type(__c);
|
2000-07-19 10:04:38 +02:00
|
|
|
_M_out_cur_move(1);
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = traits_type::not_eof(__c);
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
else
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = this->_M_really_overflow(__c);
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_M_last_overflowed = false; // Set in _M_really_overflow, below.
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::int_type
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
_M_really_overflow(int_type __c)
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
int_type __ret = traits_type::eof();
|
2000-04-21 22:33:34 +02:00
|
|
|
bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
|
|
|
|
|
|
|
|
if (__testput)
|
|
|
|
{
|
2000-08-09 09:33:39 +02:00
|
|
|
bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
|
|
|
|
#if 1
|
|
|
|
int __plen = _M_out_end - _M_out_beg;
|
|
|
|
streamsize __len = _M_file->xsputn(_M_out_beg, __plen);
|
|
|
|
if (!__testeof)
|
|
|
|
{
|
|
|
|
char_type __pending = traits_type::to_char_type(__c);
|
|
|
|
__len += _M_file->xsputn(&__pending, 1);
|
|
|
|
++__plen;
|
|
|
|
}
|
|
|
|
traits_type::to_char_type(__c);
|
|
|
|
// NB: Need this so that external byte sequence reflects
|
|
|
|
// internal buffer.
|
|
|
|
_M_file->sync();
|
|
|
|
if (__len == __plen)
|
|
|
|
{
|
|
|
|
_M_set_indeterminate();
|
|
|
|
__ret = traits_type::not_eof(__c);
|
|
|
|
}
|
|
|
|
#else
|
2000-04-21 22:33:34 +02:00
|
|
|
// Part one: Allocate temporary conversion buffer on
|
|
|
|
// stack. Convert internal buffer plus __c (ie,
|
|
|
|
// "pending sequence") to temporary conversion buffer.
|
|
|
|
int __plen = _M_out_end - _M_out_beg;
|
2000-07-21 02:06:51 +02:00
|
|
|
char_type __pbuf[__plen + 1];
|
2000-04-21 22:33:34 +02:00
|
|
|
traits_type::copy(__pbuf, this->pbase(), __plen);
|
|
|
|
if (!__testeof)
|
|
|
|
{
|
|
|
|
__pbuf[__plen] = traits_type::to_char_type(__c);
|
|
|
|
++__plen;
|
|
|
|
}
|
|
|
|
|
|
|
|
char_type* __pend;
|
|
|
|
char __conv_buf[__plen];
|
|
|
|
char* __conv_end;
|
|
|
|
_M_state_beg = _M_state_cur;
|
|
|
|
|
|
|
|
__res_type __r = _M_fcvt->out(_M_state_cur,
|
|
|
|
__pbuf, __pbuf + __plen,
|
|
|
|
const_cast<const char_type*&>(__pend),
|
|
|
|
__conv_buf, __conv_buf + __plen,
|
|
|
|
__conv_end);
|
|
|
|
|
|
|
|
// Part two: (Re)spill converted "pending sequence"
|
|
|
|
// contents (now in temporary conversion buffer) to
|
|
|
|
// external buffer (_M_file->_IO_*) using
|
|
|
|
// _M_file->sys_write(), and do error (minimal) checking.
|
|
|
|
if (__r != codecvt_base::error)
|
|
|
|
{
|
2000-04-28 04:50:55 +02:00
|
|
|
streamsize __len = _M_file->xsputn(__conv_buf, __plen);
|
2000-04-21 22:33:34 +02:00
|
|
|
// NB: Need this so that external byte sequence reflects
|
|
|
|
// internal buffer.
|
|
|
|
_M_file->sync();
|
2000-04-28 04:50:55 +02:00
|
|
|
if (__len == __plen)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
_M_set_indeterminate();
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = traits_type::not_eof(__c);
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
2000-08-09 09:33:39 +02:00
|
|
|
#endif
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
_M_last_overflowed = true;
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::pos_type
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
pos_type __ret = pos_type(off_type(-1));
|
2000-04-21 22:33:34 +02:00
|
|
|
bool __testopen = this->is_open();
|
|
|
|
bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
|
|
|
|
bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
|
|
|
|
int __width = _M_fcvt->encoding();
|
|
|
|
if (__width < 0)
|
|
|
|
__width = 0;
|
|
|
|
bool __testfail = __off != 0 && __width <= 0;
|
|
|
|
|
|
|
|
if (__testopen && !__testfail && (__testin || __testout))
|
|
|
|
{
|
2000-07-21 02:06:51 +02:00
|
|
|
// Ditch any pback buffers to avoid confusion.
|
|
|
|
_M_pback_destroy();
|
|
|
|
|
2000-04-21 22:33:34 +02:00
|
|
|
if (__way != ios_base::cur || __off != 0)
|
|
|
|
{
|
|
|
|
off_type __computed_off = __width * __off;
|
|
|
|
|
|
|
|
bool __testget = _M_in_cur && _M_in_beg < _M_in_end;
|
|
|
|
bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
|
|
|
|
// Sync the internal and external streams.
|
|
|
|
// out
|
|
|
|
if (__testput || _M_last_overflowed)
|
|
|
|
{
|
|
|
|
// Part one: update the output sequence.
|
|
|
|
this->sync();
|
|
|
|
// Part two: output unshift sequence.
|
|
|
|
_M_output_unshift();
|
|
|
|
}
|
|
|
|
//in
|
|
|
|
// NB: underflow() rewinds the external buffer.
|
|
|
|
else if (__testget && __way == ios_base::cur)
|
|
|
|
__computed_off += _M_in_cur - _M_in_beg;
|
|
|
|
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = _M_file->seekoff(__computed_off, __way, __mode);
|
2000-04-21 22:33:34 +02:00
|
|
|
_M_set_indeterminate();
|
|
|
|
}
|
|
|
|
// NB: Need to do this in case _M_file in indeterminate
|
|
|
|
// state, ie _M_file->_offset == -1
|
|
|
|
else
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = _M_file->seekoff(__off, ios_base::cur, __mode);
|
|
|
|
__ret += max(_M_out_cur, _M_in_cur) - _M_buf;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_M_last_overflowed = false;
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
basic_filebuf<_CharT, _Traits>::pos_type
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
seekpos(pos_type __pos, ios_base::openmode __mode)
|
|
|
|
{
|
2000-07-26 08:51:38 +02:00
|
|
|
pos_type __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
off_type __off = __pos;
|
|
|
|
|
2000-07-26 08:51:38 +02:00
|
|
|
__ret = this->seekoff(__off, ios_base::beg, __mode);
|
2000-04-21 22:33:34 +02:00
|
|
|
|
|
|
|
_M_last_overflowed = false;
|
2000-07-26 08:51:38 +02:00
|
|
|
return __ret;
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
void
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
_M_output_unshift()
|
2000-07-21 02:06:51 +02:00
|
|
|
{ }
|
2000-04-21 22:33:34 +02:00
|
|
|
|
|
|
|
template<typename _CharT, typename _Traits>
|
|
|
|
void
|
|
|
|
basic_filebuf<_CharT, _Traits>::
|
|
|
|
imbue(const locale& __loc)
|
|
|
|
{
|
|
|
|
bool __testbeg = gptr() == eback() && pptr() == pbase();
|
|
|
|
bool __teststate = _M_fcvt->encoding() == -1;
|
|
|
|
|
2000-07-19 10:04:38 +02:00
|
|
|
_M_buf_locale_init = true;
|
|
|
|
if (__testbeg && !__teststate && _M_buf_locale != __loc)
|
2000-04-21 22:33:34 +02:00
|
|
|
{
|
|
|
|
// XXX Will need to save these older values.
|
2000-07-19 10:04:38 +02:00
|
|
|
_M_buf_locale = __loc;
|
|
|
|
_M_fcvt = &use_facet<__codecvt_type>(_M_buf_locale);
|
2000-04-21 22:33:34 +02:00
|
|
|
// XXX Necessary?
|
2000-07-19 10:04:38 +02:00
|
|
|
_M_buf_fctype = &use_facet<__ctype_type>(_M_buf_locale);
|
2000-04-21 22:33:34 +02:00
|
|
|
}
|
|
|
|
// NB this may require the reconversion of previously
|
|
|
|
// converted chars. This in turn may cause the reconstruction
|
|
|
|
// of the original file. YIKES!!
|
|
|
|
// XXX The part in the above comment is not done.
|
|
|
|
_M_last_overflowed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
#endif // _CPP_BITS_FSTREAM_TCC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|