cleaning up file / memory streams

This commit is contained in:
Noel Berry 2021-05-09 19:30:01 -07:00
parent 2447490033
commit c51d397ccc
8 changed files with 166 additions and 64 deletions

View File

@ -3,6 +3,8 @@
namespace Blah namespace Blah
{ {
// Buffer Stream reads and writes to an internal buffer.
// It will grow the capacity if it needs to while writing.
class BufferStream : public Stream class BufferStream : public Stream
{ {
public: public:
@ -12,23 +14,23 @@ namespace Blah
BufferStream& operator=(BufferStream&& bs) noexcept; BufferStream& operator=(BufferStream&& bs) noexcept;
~BufferStream(); ~BufferStream();
virtual size_t length() const override { return m_length; } size_t length() const override;
virtual size_t position() const override { return m_position; } size_t position() const override;
virtual size_t seek(size_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); } size_t seek(size_t seekTo) override;
virtual bool is_open() const override { return m_buffer != nullptr; } bool is_open() const override;
virtual bool is_readable() const override { return true; } bool is_readable() const override;
virtual bool is_writable() const override { return true; } bool is_writable() const override;
virtual void close() override; void close() override;
void resize(size_t length); void resize(size_t length);
void clear() { m_length = m_position = 0; } void clear();
char* data() { return m_buffer; } char* data();
const char* data() const { return m_buffer; } const char* data() const;
protected: protected:
virtual size_t read_into(void* ptr, size_t length) override; size_t read_data(void* ptr, size_t length) override;
virtual size_t write_from(const void* ptr, size_t length) override; size_t write_data(const void* ptr, size_t length) override;
private: private:
char* m_buffer; char* m_buffer;

View File

@ -21,8 +21,8 @@ namespace Blah
void close() override; void close() override;
protected: protected:
size_t read_into(void* ptr, size_t length) override; size_t read_data(void* ptr, size_t length) override;
size_t write_from(const void* ptr, size_t length) override; size_t write_data(const void* ptr, size_t length) override;
private: private:
FileMode m_mode; FileMode m_mode;

View File

@ -3,6 +3,8 @@
namespace Blah namespace Blah
{ {
// Memory Stream moves over an existing buffer.
// The Buffer must exist for the duration of the Memory Stream.
class MemoryStream : public Stream class MemoryStream : public Stream
{ {
public: public:
@ -12,20 +14,20 @@ namespace Blah
MemoryStream& operator=(MemoryStream&& ms) noexcept; MemoryStream& operator=(MemoryStream&& ms) noexcept;
~MemoryStream() override { m_data = nullptr; m_length = m_position = 0; } ~MemoryStream() override { m_data = nullptr; m_length = m_position = 0; }
size_t length() const override { return m_length; } size_t length() const override;
size_t position() const override { return m_position; } size_t position() const override;
size_t seek(size_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); } size_t seek(size_t seekTo) override;
bool is_open() const override { return m_data != nullptr; } bool is_open() const override;
bool is_readable() const override { return true; } bool is_readable() const override;
bool is_writable() const override { return true; } bool is_writable() const override;
void close() override { m_data = nullptr; m_length = m_position = 0; } void close() override;
char* data() { return m_data; } char* data();
const char* data() const { return m_data; } const char* data() const;
protected: protected:
size_t read_into(void* ptr, size_t length) override; size_t read_data(void* ptr, size_t length) override;
size_t write_from(const void* ptr, size_t length) override; size_t write_data(const void* ptr, size_t length) override;
private: private:
char* m_data; char* m_data;

View File

@ -40,7 +40,7 @@ namespace Blah
size_t pipe(Stream& to, size_t length); size_t pipe(Stream& to, size_t length);
// reads the amount of bytes into the given buffer, and returns the amount read // reads the amount of bytes into the given buffer, and returns the amount read
size_t read(void* buffer, size_t length) { return read_into(buffer, length); } size_t read(void* buffer, size_t length);
// reads a string. if length < 0, assumes null-terminated // reads a string. if length < 0, assumes null-terminated
String read_string(int length = -1); String read_string(int length = -1);
@ -92,10 +92,10 @@ namespace Blah
} }
protected: protected:
// reads from the stream into the given buffer, and returns the number of bytes read // reads the amount of bytes into the given buffer, and returns the amount read
virtual size_t read_into(void* buffer, size_t length) = 0; virtual size_t read_data(void* buffer, size_t length) = 0;
// writes from the stream from the given buffer, and returns the number of bytes written // writes the amount of bytes to the stream from the given buffer, and returns the amount written
virtual size_t write_from(const void* buffer, size_t length) = 0; virtual size_t write_data(const void* buffer, size_t length) = 0;
}; };
} }

View File

@ -28,12 +28,15 @@ BufferStream::BufferStream(BufferStream&& src) noexcept
BufferStream& BufferStream::operator=(BufferStream&& src) noexcept BufferStream& BufferStream::operator=(BufferStream&& src) noexcept
{ {
close();
m_buffer = src.m_buffer; m_buffer = src.m_buffer;
m_length = src.m_length; m_length = src.m_length;
m_capacity = src.m_capacity; m_capacity = src.m_capacity;
m_position = src.m_position; m_position = src.m_position;
src.m_buffer = nullptr; src.m_buffer = nullptr;
src.m_position = src.m_length = src.m_capacity = 0; src.m_position = src.m_length = src.m_capacity = 0;
return *this; return *this;
} }
@ -42,7 +45,22 @@ BufferStream::~BufferStream()
delete[] m_buffer; delete[] m_buffer;
} }
size_t BufferStream::read_into(void* ptr, size_t len) size_t BufferStream::length() const
{
return m_length;
}
size_t BufferStream::position() const
{
return m_position;
}
size_t BufferStream::seek(size_t seekTo)
{
return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo));
}
size_t BufferStream::read_data(void* ptr, size_t len)
{ {
if (m_buffer == nullptr || ptr == nullptr) if (m_buffer == nullptr || ptr == nullptr)
return 0; return 0;
@ -58,7 +76,7 @@ size_t BufferStream::read_into(void* ptr, size_t len)
return len; return len;
} }
size_t BufferStream::write_from(const void* ptr, size_t len) size_t BufferStream::write_data(const void* ptr, size_t len)
{ {
if (len < 0) if (len < 0)
return 0; return 0;
@ -78,6 +96,21 @@ size_t BufferStream::write_from(const void* ptr, size_t len)
return len; return len;
} }
bool BufferStream::is_open() const
{
return m_buffer != nullptr;
}
bool BufferStream::is_readable() const
{
return true;
}
bool BufferStream::is_writable() const
{
return true;
}
void BufferStream::resize(size_t length) void BufferStream::resize(size_t length)
{ {
if (m_capacity > length) if (m_capacity > length)
@ -115,3 +148,18 @@ void BufferStream::close()
m_length = 0; m_length = 0;
m_capacity = 0; m_capacity = 0;
} }
void BufferStream::clear()
{
m_length = m_position = 0;
}
char* BufferStream::data()
{
return m_buffer;
}
const char* BufferStream::data() const
{
return m_buffer;
}

View File

@ -51,6 +51,30 @@ size_t FileStream::seek(size_t seek_to)
return 0; return 0;
} }
size_t FileStream::read_data(void* ptr, size_t length)
{
if (length <= 0)
return 0;
if (m_file)
return m_file->read((unsigned char*)ptr, length);
BLAH_ASSERT(false, "Unable to read from Stream");
return 0;
}
size_t FileStream::write_data(const void* ptr, size_t length)
{
if (length <= 0)
return 0;
if (m_file)
return m_file->write((const unsigned char*)ptr, length);
BLAH_ASSERT(false, "Unable to write to Stream");
return 0;
}
bool FileStream::is_open() const bool FileStream::is_open() const
{ {
return m_file.get(); return m_file.get();
@ -66,30 +90,6 @@ bool FileStream::is_writable() const
return m_file.get() && (m_mode != FileMode::OpenRead); return m_file.get() && (m_mode != FileMode::OpenRead);
} }
size_t FileStream::read_into(void* ptr, size_t length)
{
if (length <= 0)
return 0;
if (m_file)
return m_file->read((unsigned char*)ptr, length);
BLAH_ASSERT(false, "Unable to read from Stream");
return 0;
}
size_t FileStream::write_from(const void* ptr, size_t length)
{
if (length <= 0)
return 0;
if (m_file)
return m_file->write((const unsigned char*)ptr, length);
BLAH_ASSERT(false, "Unable to write to Stream");
return 0;
}
void FileStream::close() void FileStream::close()
{ {
m_file.reset(); m_file.reset();

View File

@ -28,7 +28,22 @@ MemoryStream& MemoryStream::operator=(MemoryStream&& src) noexcept
return *this; return *this;
} }
size_t MemoryStream::read_into(void* ptr, size_t len) size_t MemoryStream::length() const
{
return m_length;
}
size_t MemoryStream::position() const
{
return m_position;
}
size_t MemoryStream::seek(size_t seekTo)
{
return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo));
}
size_t MemoryStream::read_data(void* ptr, size_t len)
{ {
if (len < 0 || ptr == nullptr) if (len < 0 || ptr == nullptr)
return 0; return 0;
@ -41,7 +56,7 @@ size_t MemoryStream::read_into(void* ptr, size_t len)
return len; return len;
} }
size_t MemoryStream::write_from(const void* ptr, size_t len) size_t MemoryStream::write_data(const void* ptr, size_t len)
{ {
if (len < 0 || ptr == nullptr) if (len < 0 || ptr == nullptr)
return 0; return 0;
@ -53,3 +68,33 @@ size_t MemoryStream::write_from(const void* ptr, size_t len)
m_position += len; m_position += len;
return len; return len;
} }
bool MemoryStream::is_open() const
{
return m_data != nullptr;
}
bool MemoryStream::is_readable() const
{
return true;
}
bool MemoryStream::is_writable() const
{
return true;
}
void MemoryStream::close()
{
m_data = nullptr; m_length = m_position = 0;
}
char* MemoryStream::data()
{
return m_data;
}
const char* MemoryStream::data() const
{
return m_data;
}

View File

@ -28,6 +28,11 @@ size_t Stream::pipe(Stream& stream, size_t length)
return result; return result;
} }
size_t Stream::read(void* buffer, size_t length)
{
return read_data(buffer, length);
}
// reads a string. if length < 0, assumes null-terminated // reads a string. if length < 0, assumes null-terminated
String Stream::read_string(int length) String Stream::read_string(int length)
{ {
@ -42,7 +47,7 @@ String Stream::read_string(int length)
else else
{ {
result.set_length(length); result.set_length(length);
read_into(result.cstr(), length); read(result.cstr(), length);
} }
return result; return result;
@ -61,10 +66,10 @@ String Stream::read_line()
size_t Stream::write(const void* buffer, size_t length) size_t Stream::write(const void* buffer, size_t length)
{ {
return write_from(buffer, length); return write_data(buffer, length);
} }
size_t Stream::write(const String& string) size_t Stream::write(const String& string)
{ {
return write_from(string.begin(), string.length()); return write(string.begin(), string.length());
} }