Fstream C++ Source Code
Fstream C++ Source Code
#define _FSTREAM_TCC 1
namespace std
{
template<typename _CharT, typename _Traits>
void
basic_filebuf<_CharT, _Traits>::
_M_allocate_internal_buffer()
{
// Allocate internal buffer only if one doesn't already exist
// (either allocated or provided by the user via setbuf).
if (!_M_buf_allocated && !this->_M_buf)
{
this->_M_buf = new char_type[this->_M_buf_size];
_M_buf_allocated = true;
}
}
// 27.8.1.3,4
if ((__mode & ios_base::ate)
&& this->seekoff(0, ios_base::end, __mode)
== pos_type(off_type(-1)))
this->close();
else
__ret = this;
}
}
return __ret;
}
if (!_M_file.close())
__testfail = true;
if (!__testfail)
__ret = this;
}
return __ret;
}
delete [] _M_ext_buf;
_M_ext_buf = __buf;
_M_ext_buf_size = __blen;
}
else if (__remainder)
std::memmove(_M_ext_buf, _M_ext_next, __remainder);
_M_ext_next = _M_ext_buf;
_M_ext_end = _M_ext_buf + __remainder;
_M_state_last = _M_state_cur;
do
{
if (__rlen > 0)
{
// Sanity check!
// This may fail if the return value of
// codecvt::max_length() is bogus.
if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
{
__throw_ios_failure(__N("basic_filebuf::underflow "
"codecvt::max_length() "
"is not valid"));
}
streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
if (__elen == 0)
__got_eof = true;
else if (__elen == -1)
break;
_M_ext_end += __elen;
}
char_type* __iend;
__r = _M_codecvt->in(_M_state_cur, _M_ext_next,
_M_ext_end, _M_ext_next, this->eback(),
this->eback() + __buflen, __iend);
if (__r == codecvt_base::noconv)
{
size_t __avail = _M_ext_end - _M_ext_buf;
__ilen = std::min(__avail, __buflen);
traits_type::copy(this->eback(),
reinterpret_cast<char_type*>(_M_ext_buf), __ilen);
_M_ext_next = _M_ext_buf + __ilen;
}
else
__ilen = __iend - this->eback();
__rlen = 1;
}
while (__ilen == 0 && !__got_eof);
}
if (__ilen > 0)
{
_M_set_buffer(__ilen);
_M_reading = true;
__ret = traits_type::to_int_type(*this->gptr());
}
else if (__got_eof)
{
// If the actual end of file is reached, set 'uncommitted'
// mode, thus allowing an immediate write without an
// intervening seek.
_M_set_buffer(-1);
_M_reading = false;
// However, reaching it while looping on partial means that
// the file has got an incomplete character.
if (__r == codecvt_base::partial)
__throw_ios_failure(__N("basic_filebuf::underflow "
"incomplete character in file"));
}
else if (__r == codecvt_base::error)
__throw_ios_failure(__N("basic_filebuf::underflow "
"invalid byte sequence in file"));
else
__throw_ios_failure(__N("basic_filebuf::underflow "
"error reading the file"));
}
return __ret;
}
// Try to put back __i into input sequence in one of three ways.
// Order these tests done in is unspecified by the standard.
if (!__testeof && traits_type::eq_int_type(__i, __tmp))
__ret = __i;
else if (__testeof)
__ret = traits_type::not_eof(__i);
else if (!__testpb)
{
_M_create_pback();
_M_reading = true;
*this->gptr() = traits_type::to_char_type(__i);
__ret = __i;
}
}
return __ret;
}
char* __bend;
const char_type* __iend;
codecvt_base::result __r;
__r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
__iend, __buf, __buf + __blen, __bend);
return __ret;
}
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 171. Strange seekpos() semantics due to joint position
// According to the resolution of DR 171, seekpos should ignore the last
// argument (of type openmode).
template<typename _CharT, typename _Traits>
typename basic_filebuf<_CharT, _Traits>::pos_type
basic_filebuf<_CharT, _Traits>::
seekpos(pos_type __pos, ios_base::openmode)
{
pos_type __ret = pos_type(off_type(-1));
if (this->is_open())
{
// Ditch any pback buffers to avoid confusion.
_M_destroy_pback();
__ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
}
return __ret;
}
do
{
char* __next;
__r = _M_codecvt->unshift(_M_state_cur, __buf,
__buf + __blen, __next);
if (__r == codecvt_base::error)
__testvalid = false;
else if (__r == codecvt_base::ok ||
__r == codecvt_base::partial)
{
__ilen = __next - __buf;
if (__ilen > 0)
{
const streamsize __elen = _M_file.xsputn(__buf, __ilen);
if (__elen != __ilen)
__testvalid = false;
}
}
}
while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
if (__testvalid)
{
// This second call to overflow() is required by the standard,
// but it's not clear why it's needed, since the output buffer
// should be empty by this point (it should have been emptied
// in the first call to overflow()).
const int_type __tmp = this->overflow();
if (traits_type::eq_int_type(__tmp, traits_type::eof()))
__testvalid = false;
}
}
return __testvalid;
}
if (this->is_open())
{
// encoding() == -1 is ok only at the beginning.
if ((_M_reading || _M_writing)
&& __check_facet(_M_codecvt).encoding() == -1)
__testvalid = false;
else
{
if (_M_reading)
{
if (__check_facet(_M_codecvt).always_noconv())
{
if (_M_codecvt_tmp
&& !__check_facet(_M_codecvt_tmp).always_noconv())
__testvalid = this->seekoff(0, ios_base::cur, this->_M_mode)
!= pos_type(off_type(-1));
}
else
{
// External position corresponding to gptr().
_M_ext_next = _M_ext_buf
+ _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next,
this->gptr() - this->eback());
const streamsize __remainder = _M_ext_end - _M_ext_next;
if (__remainder)
std::memmove(_M_ext_buf, _M_ext_next, __remainder);
_M_ext_next = _M_ext_buf;
_M_ext_end = _M_ext_buf + __remainder;
_M_set_buffer(-1);
_M_state_last = _M_state_cur = _M_state_beg;
}
}
else if (_M_writing && (__testvalid = _M_terminate_output()))
_M_set_buffer(-1);
}
}
if (__testvalid)
_M_codecvt = _M_codecvt_tmp;
else
_M_codecvt = 0;
}
// Inhibit implicit instantiations for required instantiations,
// which are defined via explicit instantiations elsewhere.
// NB: This syntax is a GNU extension.
#if _GLIBCXX_EXTERN_TEMPLATE
extern template class basic_filebuf<char>;
extern template class basic_ifstream<char>;
extern template class basic_ofstream<char>;
extern template class basic_fstream<char>;
#ifdef _GLIBCXX_USE_WCHAR_T
extern template class basic_filebuf<wchar_t>;
extern template class basic_ifstream<wchar_t>;
extern template class basic_ofstream<wchar_t>;
extern template class basic_fstream<wchar_t>;
#endif
#endif
} // namespace std
#endif