// Truck Computer Dooom! // // Douglas Thrift // // $Id$ #ifndef _fdstream_hpp_ #define _fdstream_hpp_ #include // XXX: I don't know why they didn't implement the rest of this for me #include namespace ext { using __gnu_cxx::stdio_filebuf; template > struct basic_ifdstream : public std::basic_istream<_CharT, _Traits> { 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 stdio_filebuf __filebuf_type; typedef std::basic_istream __istream_type; private: __filebuf_type _M_filebuf; public: basic_ifdstream() : __istream_type(), _M_filebuf() { this->init(&_M_filebuf); } explicit basic_ifdstream(const char *__s, std::ios_base::openmode __mode = std::ios_base::in) : __istream_type(), _M_filebuf() { this->init(&_M_filebuf); this->open(__s, __mode); } basic_ifdstream(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in) : __istream_type(), _M_filebuf() { this->init(&_M_filebuf); this->open(__s, __mode); } basic_ifdstream(int __fd, std::ios_base::openmode __mode = std::ios_base::in) : __istream_type(), _M_filebuf(__fd, __mode) { this->init(&_M_filebuf); } __filebuf_type *rdbuf() const { return const_cast<__filebuf_type *>(&_M_filebuf); } bool is_open() const { return _M_filebuf.is_open(); } void open(const char *__s, std::ios_base::openmode __mode = std::ios_base::in) { if (!_M_filebuf.open(__s, __mode | std::ios_base::in)) this->setstate(std::ios_base::failbit); else this->clear(); } void open(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in) { this->open(__s.c_str(), __mode); } void close() { if (!_M_filebuf.close()) this->setstate(std::ios_base::failbit); } }; template > struct basic_ofdstream : public std::basic_ostream<_CharT, _Traits> { 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 stdio_filebuf __filebuf_type; typedef std::basic_ostream __ostream_type; private: __filebuf_type _M_filebuf; public: basic_ofdstream() : __ostream_type(), _M_filebuf() { this->init(&_M_filebuf); } explicit basic_ofdstream(const char *__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) : __ostream_type(), _M_filebuf() { this->init(&_M_filebuf); this->open(__s, __mode); } basic_ofdstream(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) : __ostream_type(), _M_filebuf() { this->init(&_M_filebuf); this->open(__s, __mode); } basic_ofdstream(int __fd, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) : __ostream_type(), _M_filebuf(__fd, __mode) { this->init(&_M_filebuf); } __filebuf_type *rdbuf() const { return const_cast<__filebuf_type *>(&_M_filebuf); } bool is_open() const { return _M_filebuf.is_open(); } void open(const char *__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) { if (!_M_filebuf.open(__s, __mode | std::ios_base::out)) this->setstate(std::ios_base::failbit); else this->clear(); } void open(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::out | std::ios_base::trunc) { this->open(__s.c_str(), __mode); } void close() { if (!_M_filebuf.close()) this->setstate(std::ios_base::failbit); } }; template > struct basic_fdstream : public std::basic_iostream<_CharT, _Traits> { 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 stdio_filebuf __filebuf_type; typedef std::basic_ios __ios_type; typedef std::basic_iostream __iostream_type; private: __filebuf_type _M_filebuf; public: basic_fdstream() : __iostream_type(), _M_filebuf() { this->init(&_M_filebuf); } explicit basic_fdstream(const char *__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) : __iostream_type(NULL), _M_filebuf() { this->init(&_M_filebuf); this->open(__s, __mode); } basic_fdstream(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) : __iostream_type(NULL), _M_filebuf() { this->init(&_M_filebuf); this->open(__s, __mode); } basic_fdstream(int __fd, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) : __iostream_type(NULL), _M_filebuf(__fd, __mode) { this->init(&_M_filebuf); } __filebuf_type *rdbuf() const { return const_cast<__filebuf_type *>(&_M_filebuf); } bool is_open() const { return _M_filebuf.is_open(); } void open(const char *__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) { if (!_M_filebuf.open(__s, __mode)) this->setstate(std::ios_base::failbit); else this->clear(); } void open(const std::string &__s, std::ios_base::openmode __mode = std::ios_base::in | std::ios_base::out) { this->open(__s.c_str(), __mode); } void close() { if (!_M_filebuf.close()) this->setstate(std::ios_base::failbit); } }; typedef basic_ifdstream ifdstream; typedef basic_ofdstream ofdstream; typedef basic_fdstream fdstream; } #endif//_fdstream_hpp_