diff options
author | vng <viktor.govako@gmail.com> | 2011-11-25 00:21:39 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:28:43 +0300 |
commit | c11367ac7accbbff694befc754132c3da8b76540 (patch) | |
tree | 5a106577d6acdc5395e3bfc919e7db3ab17e99de /coding/reader_streambuf.cpp | |
parent | 73e02dfa473887e41811da96f84133d790848551 (diff) |
Add ReaderStreamBuf and WriterStreamBuf for std::istream and std::ostream interfaces.
Diffstat (limited to 'coding/reader_streambuf.cpp')
-rw-r--r-- | coding/reader_streambuf.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/coding/reader_streambuf.cpp b/coding/reader_streambuf.cpp new file mode 100644 index 0000000000..3cfaef1fc8 --- /dev/null +++ b/coding/reader_streambuf.cpp @@ -0,0 +1,40 @@ +#include "reader_streambuf.hpp" +#include "reader.hpp" + +#include "../std/algorithm.hpp" + + +ReaderStreamBuf::ReaderStreamBuf(Reader * p) +: m_p(p), m_pos(0), m_size(p->Size()) +{ +} + +ReaderStreamBuf::~ReaderStreamBuf() +{ + delete m_p; +} + +std::streamsize ReaderStreamBuf::xsgetn(char_type * s, std::streamsize n) +{ + uint64_t const count = min(static_cast<uint64_t>(n), m_size - m_pos); + if (count > 0) + { + m_p->Read(m_pos, s, count); + m_pos += count; + } + return count; +} + +ReaderStreamBuf::int_type ReaderStreamBuf::underflow() +{ + std::streamsize s = xsgetn(m_buf, sizeof(m_buf)); + if (s > 0) + { + setg(m_buf, m_buf, m_buf + s); + return traits_type::to_int_type(m_buf[0]); + } + else + { + return traits_type::eof(); + } +} |