diff options
Diffstat (limited to 'src/common/io')
-rw-r--r-- | src/common/io/MemoryStream.cpp | 54 | ||||
-rw-r--r-- | src/common/io/Stream.cpp | 21 |
2 files changed, 75 insertions, 0 deletions
diff --git a/src/common/io/MemoryStream.cpp b/src/common/io/MemoryStream.cpp new file mode 100644 index 00000000..e3c9c605 --- /dev/null +++ b/src/common/io/MemoryStream.cpp @@ -0,0 +1,54 @@ +#include "cru/common/io/MemoryStream.hpp" + +namespace cru::io { +bool MemoryStream::CanSeek() { return true; } + +Index MemoryStream::Tell() { return position_; } + +void MemoryStream::Seek(Index offset, SeekOrigin origin) { + switch (origin) { + case SeekOrigin::Current: + position_ += offset; + break; + case SeekOrigin::Begin: + position_ = offset; + break; + case SeekOrigin::End: + position_ = size_ + offset; + break; + } +} + +bool MemoryStream::CanRead() { return true; } + +Index MemoryStream::Read(std::byte *buffer, Index offset, Index size) { + if (position_ + size > size_) { + size = size_ - position_; + } + if (size <= 0) { + return 0; + } + std::memcpy(buffer + offset, buffer_ + position_, size); + position_ += size; + return size; +} + +bool MemoryStream::CanWrite() { return !read_only_; } + +Index MemoryStream::Write(const std::byte *buffer, Index offset, Index size) { + if (read_only_) { + return 0; + } + if (position_ + size > size_) { + size = size_ - position_; + } + if (size <= 0) { + return 0; + } + std::memcpy(buffer_ + position_, buffer + offset, size); + position_ += size; + return size; +} + +void MemoryStream::Flush() {} +} // namespace cru::io diff --git a/src/common/io/Stream.cpp b/src/common/io/Stream.cpp new file mode 100644 index 00000000..6addfdc0 --- /dev/null +++ b/src/common/io/Stream.cpp @@ -0,0 +1,21 @@ +#include "cru/common/io/Stream.hpp" + +namespace cru::io { +void Stream::Rewind() { Seek(0); } + +Index Stream::GetSize() { + Index current_position = Tell(); + Seek(0, SeekOrigin::End); + Index size = Tell(); + Seek(current_position); + return size; +} + +Index Stream::Read(std::byte* buffer, Index size) { + return Read(buffer, 0, size); +} + +Index Stream::Write(const std::byte* buffer, Index size) { + return Write(buffer, 0, size); +} +} // namespace cru::io |