// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) // (C) Copyright 2003-2007 Jonathan Turkanis // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) // See http://www.boost.org/libs/iostreams for documentation. // // Contains wrappers for standard file buffers, together // with convenience typedefs: // - flushable_basic_file_sink // - flushable_basic_file // // Copied from boost_1_38_0/boost/iostreams/device/file.hpp. #ifndef BOOST_IOSTREAMS_FLUSHABLE_FILE_HPP_INCLUDED #define BOOST_IOSTREAMS_FLUSHABLE_FILE_HPP_INCLUDED #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma once #endif #include #ifndef BOOST_IOSTREAMS_NO_LOCALE # include #endif #include // pathnames, char_traits. #include #include // openmode, seekdir, int types. #include #include // seek. #include // Must come last. #include // MSVC. namespace boost { namespace iostreams { template class flushable_basic_file { public: typedef Ch char_type; struct category : public seekable_device_tag, public closable_tag, public localizable_tag, public flushable_tag { }; flushable_basic_file( const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::in | BOOST_IOS::out, BOOST_IOS::openmode base_mode = BOOST_IOS::in | BOOST_IOS::out ); std::streamsize read(char_type* s, std::streamsize n); std::streamsize write(const char_type* s, std::streamsize n); std::streampos seek( stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode which = BOOST_IOS::in | BOOST_IOS::out ); void open( const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::in | BOOST_IOS::out, BOOST_IOS::openmode base_mode = BOOST_IOS::in | BOOST_IOS::out ); bool is_open() const; void close(); bool flush(); #ifndef BOOST_IOSTREAMS_NO_LOCALE void imbue(const std::locale& loc) { pimpl_->file_.pubimbue(loc); } #endif private: struct impl { impl(const std::string& path, BOOST_IOS::openmode mode) { file_.open(path.c_str(), mode); } ~impl() { if (file_.is_open()) file_.close(); } BOOST_IOSTREAMS_BASIC_FILEBUF(Ch) file_; }; shared_ptr pimpl_; }; typedef flushable_basic_file flushable_file; typedef flushable_basic_file flushable_wfile; template struct flushable_basic_file_sink : private flushable_basic_file { typedef Ch char_type; struct category : output_seekable, device_tag, closable_tag, flushable_tag { }; using flushable_basic_file::write; using flushable_basic_file::seek; using flushable_basic_file::is_open; using flushable_basic_file::close; using flushable_basic_file::flush; flushable_basic_file_sink( const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out ) : flushable_basic_file(path, mode & ~BOOST_IOS::in, BOOST_IOS::out) { } void open( const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out ) { flushable_basic_file::open(path, mode & ~BOOST_IOS::in, BOOST_IOS::out); } }; typedef flushable_basic_file_sink flushable_file_sink; typedef flushable_basic_file_sink flushable_wfile_sink; //------------------Implementation of basic_file------------------------------// template flushable_basic_file::flushable_basic_file ( const std::string& path, BOOST_IOS::openmode mode, BOOST_IOS::openmode base_mode ) { open(path, mode, base_mode); } template inline std::streamsize flushable_basic_file::read (char_type* s, std::streamsize n) { std::streamsize result = pimpl_->file_.sgetn(s, n); return result != 0 ? result : -1; } template inline std::streamsize flushable_basic_file::write (const char_type* s, std::streamsize n) { return pimpl_->file_.sputn(s, n); } template std::streampos flushable_basic_file::seek ( stream_offset off, BOOST_IOS::seekdir way, BOOST_IOS::openmode ) { return iostreams::seek(pimpl_->file_, off, way); } template void flushable_basic_file::open ( const std::string& path, BOOST_IOS::openmode mode, BOOST_IOS::openmode base_mode ) { pimpl_.reset(new impl(path, mode | base_mode)); } template bool flushable_basic_file::is_open() const { return pimpl_->file_.is_open(); } template void flushable_basic_file::close() { pimpl_->file_.close(); } template bool flushable_basic_file::flush() { pimpl_->file_.pubsync(); return true;} //----------------------------------------------------------------------------// } } // End namespaces iostreams, boost. #include // MSVC #endif // #ifndef BOOST_IOSTREAMS_FILE_HPP_INCLUDED