mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-28 16:58:57 +08:00
cleaning up file / memory streams
This commit is contained in:
parent
2447490033
commit
c51d397ccc
|
@ -3,6 +3,8 @@
|
|||
|
||||
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
|
||||
{
|
||||
public:
|
||||
|
@ -12,23 +14,23 @@ namespace Blah
|
|||
BufferStream& operator=(BufferStream&& bs) noexcept;
|
||||
~BufferStream();
|
||||
|
||||
virtual size_t length() const override { return m_length; }
|
||||
virtual size_t position() const override { return m_position; }
|
||||
virtual size_t seek(size_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
|
||||
virtual bool is_open() const override { return m_buffer != nullptr; }
|
||||
virtual bool is_readable() const override { return true; }
|
||||
virtual bool is_writable() const override { return true; }
|
||||
virtual void close() override;
|
||||
size_t length() const override;
|
||||
size_t position() const override;
|
||||
size_t seek(size_t seekTo) override;
|
||||
bool is_open() const override;
|
||||
bool is_readable() const override;
|
||||
bool is_writable() const override;
|
||||
void close() override;
|
||||
|
||||
void resize(size_t length);
|
||||
void clear() { m_length = m_position = 0; }
|
||||
void clear();
|
||||
|
||||
char* data() { return m_buffer; }
|
||||
const char* data() const { return m_buffer; }
|
||||
char* data();
|
||||
const char* data() const;
|
||||
|
||||
protected:
|
||||
virtual size_t read_into(void* ptr, size_t length) override;
|
||||
virtual size_t write_from(const void* ptr, size_t length) override;
|
||||
size_t read_data(void* ptr, size_t length) override;
|
||||
size_t write_data(const void* ptr, size_t length) override;
|
||||
|
||||
private:
|
||||
char* m_buffer;
|
||||
|
|
|
@ -21,8 +21,8 @@ namespace Blah
|
|||
void close() override;
|
||||
|
||||
protected:
|
||||
size_t read_into(void* ptr, size_t length) override;
|
||||
size_t write_from(const void* ptr, size_t length) override;
|
||||
size_t read_data(void* ptr, size_t length) override;
|
||||
size_t write_data(const void* ptr, size_t length) override;
|
||||
|
||||
private:
|
||||
FileMode m_mode;
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
namespace Blah
|
||||
{
|
||||
// Memory Stream moves over an existing buffer.
|
||||
// The Buffer must exist for the duration of the Memory Stream.
|
||||
class MemoryStream : public Stream
|
||||
{
|
||||
public:
|
||||
|
@ -12,20 +14,20 @@ namespace Blah
|
|||
MemoryStream& operator=(MemoryStream&& ms) noexcept;
|
||||
~MemoryStream() override { m_data = nullptr; m_length = m_position = 0; }
|
||||
|
||||
size_t length() const override { return m_length; }
|
||||
size_t position() const override { return m_position; }
|
||||
size_t seek(size_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
|
||||
bool is_open() const override { return m_data != nullptr; }
|
||||
bool is_readable() const override { return true; }
|
||||
bool is_writable() const override { return true; }
|
||||
void close() override { m_data = nullptr; m_length = m_position = 0; }
|
||||
size_t length() const override;
|
||||
size_t position() const override;
|
||||
size_t seek(size_t seekTo) override;
|
||||
bool is_open() const override;
|
||||
bool is_readable() const override;
|
||||
bool is_writable() const override;
|
||||
void close() override;
|
||||
|
||||
char* data() { return m_data; }
|
||||
const char* data() const { return m_data; }
|
||||
char* data();
|
||||
const char* data() const;
|
||||
|
||||
protected:
|
||||
size_t read_into(void* ptr, size_t length) override;
|
||||
size_t write_from(const void* ptr, size_t length) override;
|
||||
size_t read_data(void* ptr, size_t length) override;
|
||||
size_t write_data(const void* ptr, size_t length) override;
|
||||
|
||||
private:
|
||||
char* m_data;
|
||||
|
|
|
@ -40,7 +40,7 @@ namespace Blah
|
|||
size_t pipe(Stream& to, size_t length);
|
||||
|
||||
// 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
|
||||
String read_string(int length = -1);
|
||||
|
@ -92,10 +92,10 @@ namespace Blah
|
|||
}
|
||||
|
||||
protected:
|
||||
// reads from the stream into the given buffer, and returns the number of bytes read
|
||||
virtual size_t read_into(void* buffer, size_t length) = 0;
|
||||
// reads the amount of bytes into the given buffer, and returns the amount read
|
||||
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
|
||||
virtual size_t write_from(const void* buffer, size_t length) = 0;
|
||||
// writes the amount of bytes to the stream from the given buffer, and returns the amount written
|
||||
virtual size_t write_data(const void* buffer, size_t length) = 0;
|
||||
};
|
||||
}
|
|
@ -28,12 +28,15 @@ BufferStream::BufferStream(BufferStream&& src) noexcept
|
|||
|
||||
BufferStream& BufferStream::operator=(BufferStream&& src) noexcept
|
||||
{
|
||||
close();
|
||||
|
||||
m_buffer = src.m_buffer;
|
||||
m_length = src.m_length;
|
||||
m_capacity = src.m_capacity;
|
||||
m_position = src.m_position;
|
||||
src.m_buffer = nullptr;
|
||||
src.m_position = src.m_length = src.m_capacity = 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -42,7 +45,22 @@ BufferStream::~BufferStream()
|
|||
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)
|
||||
return 0;
|
||||
|
@ -58,7 +76,7 @@ size_t BufferStream::read_into(void* ptr, size_t 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)
|
||||
return 0;
|
||||
|
@ -78,6 +96,21 @@ size_t BufferStream::write_from(const void* ptr, size_t 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)
|
||||
{
|
||||
if (m_capacity > length)
|
||||
|
@ -115,3 +148,18 @@ void BufferStream::close()
|
|||
m_length = 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;
|
||||
}
|
||||
|
|
|
@ -51,6 +51,30 @@ size_t FileStream::seek(size_t seek_to)
|
|||
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
|
||||
{
|
||||
return m_file.get();
|
||||
|
@ -66,30 +90,6 @@ bool FileStream::is_writable() const
|
|||
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()
|
||||
{
|
||||
m_file.reset();
|
||||
|
|
|
@ -28,7 +28,22 @@ MemoryStream& MemoryStream::operator=(MemoryStream&& src) noexcept
|
|||
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)
|
||||
return 0;
|
||||
|
@ -41,7 +56,7 @@ size_t MemoryStream::read_into(void* ptr, size_t 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)
|
||||
return 0;
|
||||
|
@ -53,3 +68,33 @@ size_t MemoryStream::write_from(const void* ptr, size_t len)
|
|||
m_position += 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;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,11 @@ size_t Stream::pipe(Stream& stream, size_t length)
|
|||
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
|
||||
String Stream::read_string(int length)
|
||||
{
|
||||
|
@ -42,7 +47,7 @@ String Stream::read_string(int length)
|
|||
else
|
||||
{
|
||||
result.set_length(length);
|
||||
read_into(result.cstr(), length);
|
||||
read(result.cstr(), length);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -61,10 +66,10 @@ String Stream::read_line()
|
|||
|
||||
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)
|
||||
{
|
||||
return write_from(string.begin(), string.length());
|
||||
return write(string.begin(), string.length());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user