From f28bb0b4908487f35ab54f038ec164a13c3cc027 Mon Sep 17 00:00:00 2001 From: vng Date: Thu, 24 Nov 2011 23:29:32 +0300 Subject: [PATCH] Forget to commit WriterStreamBuffer. --- coding/reader_streambuf.cpp | 31 +++++++++++++++++++++++++++++++ coding/reader_streambuf.hpp | 31 ++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/coding/reader_streambuf.cpp b/coding/reader_streambuf.cpp index 3cfaef1fc8..2379ce2c35 100644 --- a/coding/reader_streambuf.cpp +++ b/coding/reader_streambuf.cpp @@ -1,5 +1,6 @@ #include "reader_streambuf.hpp" #include "reader.hpp" +#include "file_writer.hpp" #include "../std/algorithm.hpp" @@ -38,3 +39,33 @@ ReaderStreamBuf::int_type ReaderStreamBuf::underflow() return traits_type::eof(); } } + + +WriterStreamBuf::~WriterStreamBuf() +{ + delete m_writer; +} + +std::streamsize WriterStreamBuf::xsputn(char_type const * s, std::streamsize n) +{ + m_writer->Write(s, n); + return n; +} + +WriterStreamBuf::int_type WriterStreamBuf::overflow(int_type c) +{ + if (!traits_type::eq_int_type(c, traits_type::eof())) + { + char_type const t = traits_type::to_char_type(c); + xsputn(&t, 1); + } + return !traits_type::eof(); +} + +int WriterStreamBuf::sync() +{ + FileWriter * p = dynamic_cast(m_writer); + if (p) + p->Flush(); + return 0; +} diff --git a/coding/reader_streambuf.hpp b/coding/reader_streambuf.hpp index c36e383461..9f1fa917ac 100644 --- a/coding/reader_streambuf.hpp +++ b/coding/reader_streambuf.hpp @@ -3,17 +3,23 @@ #include "../std/iostream.hpp" class Reader; +class Writer; -class ReaderStreamBuf : public std::streambuf + +class BaseStreamBuf : public std::streambuf +{ +public: + typedef std::streambuf::traits_type traits_type; + typedef std::streambuf::char_type char_type; + typedef std::streambuf::int_type int_type; +}; + +class ReaderStreamBuf : public BaseStreamBuf { Reader * m_p; uint64_t m_pos, m_size; public: - typedef std::streambuf::traits_type traits_type; - typedef std::streambuf::char_type char_type; - typedef std::streambuf::int_type int_type; - /// Takes the ownership of p. Reader should be allocated in dynamic memory. ReaderStreamBuf(Reader * p); virtual ~ReaderStreamBuf(); @@ -24,3 +30,18 @@ private: char m_buf[1]; }; + +class WriterStreamBuf : public BaseStreamBuf +{ + Writer * m_writer; + +public: + /// Takes the ownership of p. Reader should be allocated in dynamic memory. + WriterStreamBuf(Writer * p) : m_writer(p) {} + virtual ~WriterStreamBuf(); + +private: + virtual std::streamsize xsputn(char_type const * s, std::streamsize n); + virtual int_type overflow(int_type c); + virtual int sync(); +};