Add ReaderStreamBuf and WriterStreamBuf for std::istream and std::ostream interfaces.

This commit is contained in:
vng 2011-11-24 23:21:39 +03:00 committed by Alex Zolotarev
parent 73e02dfa47
commit c11367ac7a
4 changed files with 102 additions and 0 deletions

View file

@ -26,6 +26,7 @@ SOURCES += \
reader.cpp \
zip_reader.cpp \
mmap_reader.cpp \
reader_streambuf.cpp \
HEADERS += \
internal/xmlparser.h \
@ -79,3 +80,4 @@ HEADERS += \
read_write_utils.hpp \
file_reader_stream.hpp \
file_writer_stream.hpp \
reader_streambuf.hpp \

View file

@ -6,6 +6,10 @@
#include "../file_reader.hpp"
#include "../file_writer.hpp"
#include "../buffer_reader.hpp"
#include "../reader_streambuf.hpp"
#include "../../std/fstream.hpp"
namespace
{
@ -97,3 +101,33 @@ UNIT_TEST(SharedMemReader)
TEST_EQUAL(s1, "123", ());
TEST_EQUAL(s2, "123", ());
}
UNIT_TEST(ReaderStreamBuf)
{
string const name = "test.txt";
{
WriterStreamBuf buffer(new FileWriter(name));
ostream s(&buffer);
s << "hey!" << '\n' << 1 << '\n' << 3.14 << '\n' << 0x0102030405060708ull << std::endl;
}
{
ReaderStreamBuf buffer(new FileReader(name));
istream s(&buffer);
std::string str;
int i;
double d;
unsigned long long ull;
s >> str >> i >> d >> ull;
TEST_EQUAL(str, "hey!", ());
TEST_EQUAL(i, 1, ());
TEST_ALMOST_EQUAL(d, 3.14, ());
TEST_EQUAL(ull, 0x0102030405060708ull, ());
}
FileWriter::DeleteFileX(name);
}

View file

@ -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();
}
}

View file

@ -0,0 +1,26 @@
#pragma once
#include "../std/iostream.hpp"
class Reader;
class ReaderStreamBuf : public std::streambuf
{
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();
private:
virtual std::streamsize xsgetn(char_type * s, std::streamsize n);
virtual int_type underflow();
char m_buf[1];
};