#ifndef _CPP_FSTREAM
#define _CPP_FSTREAM 1
#pragma GCC system_header
#include <istream>
#include <ostream>
#include <locale> // For codecvt
#include <bits/basic_file.h>
#include <bits/gthr.h>
namespace std
{
template<typename _CharT, typename _Traits>
class basic_filebuf : public basic_streambuf<_CharT, _Traits>
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
typedef basic_streambuf<char_type, traits_type> __streambuf_type;
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
typedef __basic_file<char> __file_type;
typedef typename traits_type::state_type __state_type;
typedef codecvt<char_type, char, __state_type> __codecvt_type;
typedef typename __codecvt_type::result __res_type;
typedef ctype<char_type> __ctype_type;
friend class ios_base;
protected:
__c_lock _M_lock;
__file_type _M_file;
__state_type _M_state_cur;
__state_type _M_state_beg;
bool _M_buf_allocated;
char_type _M_unbuf[4];
bool _M_last_overflowed;
public:
basic_filebuf();
basic_filebuf(__c_file* __f, ios_base::openmode __mode,
int_type __s = static_cast<int_type>(BUFSIZ));
int
fd();
virtual
~basic_filebuf()
{
this->close();
_M_last_overflowed = false;
}
bool
is_open() const { return _M_file.is_open(); }
__filebuf_type*
open(const char* __s, ios_base::openmode __mode);
__filebuf_type*
close();
protected:
void
_M_allocate_internal_buffer();
void
_M_destroy_internal_buffer();
virtual streamsize
showmanyc();
virtual int_type
underflow();
virtual int_type
pbackfail(int_type __c = _Traits::eof());
virtual int_type
overflow(int_type __c = _Traits::eof());
int_type
_M_really_overflow(int_type __c = _Traits::eof());
void
_M_convert_to_external(char_type*, streamsize, streamsize&, streamsize&);
virtual __streambuf_type*
setbuf(char_type* __s, streamsize __n);
virtual pos_type
seekoff(off_type __off, ios_base::seekdir __way,
ios_base::openmode __mode = ios_base::in | ios_base::out);
virtual pos_type
seekpos(pos_type __pos,
ios_base::openmode __mode = ios_base::in | ios_base::out);
virtual int
sync()
{
bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
if (__testput && !_M_file.sync())
{
streamoff __cur = _M_file.seekoff(0, ios_base::cur);
off_type __off = _M_out_cur - _M_out_beg;
_M_really_overflow();
_M_file.seekpos(__cur + __off);
}
_M_last_overflowed = false;
return 0;
}
virtual void
imbue(const locale& __loc);
virtual streamsize
xsgetn(char_type* __s, streamsize __n)
{
streamsize __ret = 0;
if (_M_pback_init)
{
while (__ret < __n && _M_in_cur < _M_in_end)
{
*__s = *_M_in_cur;
++__ret;
++__s;
++_M_in_cur;
}
_M_pback_destroy();
}
if (__ret < __n)
__ret += __streambuf_type::xsgetn(__s, __n - __ret);
return __ret;
}
virtual streamsize
xsputn(const char_type* __s, streamsize __n)
{
_M_pback_destroy();
return __streambuf_type::xsputn(__s, __n);
}
void
_M_output_unshift();
};
template<typename _CharT, typename _Traits>
class basic_ifstream : public basic_istream<_CharT, _Traits>
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
typedef basic_istream<char_type, traits_type> __istream_type;
private:
__filebuf_type _M_filebuf;
public:
basic_ifstream()
: __istream_type(NULL), _M_filebuf()
{ this->init(&_M_filebuf); }
explicit
basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
: __istream_type(NULL), _M_filebuf()
{
this->init(&_M_filebuf);
this->open(__s, __mode);
}
~basic_ifstream()
{ }
__filebuf_type*
rdbuf() const
{ return const_cast<__filebuf_type*>(&_M_filebuf); }
bool
is_open() { return _M_filebuf.is_open(); }
void
open(const char* __s, ios_base::openmode __mode = ios_base::in)
{
if (!_M_filebuf.open(__s, __mode | ios_base::in))
this->setstate(ios_base::failbit);
}
void
close()
{
if (!_M_filebuf.close())
this->setstate(ios_base::failbit);
}
};
template<typename _CharT, typename _Traits>
class basic_ofstream : public basic_ostream<_CharT,_Traits>
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
typedef basic_ostream<char_type, traits_type> __ostream_type;
private:
__filebuf_type _M_filebuf;
public:
basic_ofstream()
: __ostream_type(NULL), _M_filebuf()
{ this->init(&_M_filebuf); }
explicit
basic_ofstream(const char* __s,
ios_base::openmode __mode = ios_base::out|ios_base::trunc)
: __ostream_type(NULL), _M_filebuf()
{
this->init(&_M_filebuf);
this->open(__s, __mode);
}
~basic_ofstream()
{ }
__filebuf_type*
rdbuf() const
{ return const_cast<__filebuf_type*>(&_M_filebuf); }
bool
is_open() { return _M_filebuf.is_open(); }
void
open(const char* __s,
ios_base::openmode __mode = ios_base::out | ios_base::trunc)
{
if (!_M_filebuf.open(__s, __mode | ios_base::out))
this->setstate(ios_base::failbit);
}
void
close()
{
if (!_M_filebuf.close())
this->setstate(ios_base::failbit);
}
};
template<typename _CharT, typename _Traits>
class basic_fstream : public basic_iostream<_CharT, _Traits>
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
typedef basic_filebuf<char_type, traits_type> __filebuf_type;
typedef basic_ios<char_type, traits_type> __ios_type;
typedef basic_iostream<char_type, traits_type> __iostream_type;
private:
__filebuf_type _M_filebuf;
public:
basic_fstream()
: __iostream_type(NULL), _M_filebuf()
{ this->init(&_M_filebuf); }
explicit
basic_fstream(const char* __s,
ios_base::openmode __mode = ios_base::in | ios_base::out)
: __iostream_type(NULL), _M_filebuf()
{
this->init(&_M_filebuf);
this->open(__s, __mode);
}
~basic_fstream()
{ }
__filebuf_type*
rdbuf() const
{ return const_cast<__filebuf_type*>(&_M_filebuf); }
bool
is_open() { return _M_filebuf.is_open(); }
void
open(const char* __s,
ios_base::openmode __mode = ios_base::in | ios_base::out)
{
if (!_M_filebuf.open(__s, __mode))
setstate(ios_base::failbit);
}
void
close()
{
if (!_M_filebuf.close())
setstate(ios_base::failbit);
}
};
}
#ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
# define export
#endif
#ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
# include <bits/fstream.tcc>
#endif
#endif