diff options
Diffstat (limited to '')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/stream.cpp | 47 | ||||
| -rw-r--r-- | src/common/stream.h | 50 |
3 files changed, 99 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0fb5d9708..e50ab2922 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -150,6 +150,8 @@ add_library(common STATIC | |||
| 150 | scope_exit.h | 150 | scope_exit.h |
| 151 | spin_lock.cpp | 151 | spin_lock.cpp |
| 152 | spin_lock.h | 152 | spin_lock.h |
| 153 | stream.cpp | ||
| 154 | stream.h | ||
| 153 | string_util.cpp | 155 | string_util.cpp |
| 154 | string_util.h | 156 | string_util.h |
| 155 | swap.h | 157 | swap.h |
diff --git a/src/common/stream.cpp b/src/common/stream.cpp new file mode 100644 index 000000000..bf0496c26 --- /dev/null +++ b/src/common/stream.cpp | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <stdexcept> | ||
| 6 | #include "common/common_types.h" | ||
| 7 | #include "common/stream.h" | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | Stream::Stream() = default; | ||
| 12 | Stream::~Stream() = default; | ||
| 13 | |||
| 14 | void Stream::Seek(s32 offset, SeekOrigin origin) { | ||
| 15 | if (origin == SeekOrigin::SetOrigin) { | ||
| 16 | if (offset < 0) { | ||
| 17 | position = 0; | ||
| 18 | } else if (position >= buffer.size()) { | ||
| 19 | position = buffer.size(); | ||
| 20 | } else { | ||
| 21 | position = offset; | ||
| 22 | } | ||
| 23 | } else if (origin == SeekOrigin::FromCurrentPos) { | ||
| 24 | Seek(static_cast<s32>(position) + offset, SeekOrigin::SetOrigin); | ||
| 25 | } else if (origin == SeekOrigin::FromEnd) { | ||
| 26 | Seek(static_cast<s32>(buffer.size()) - offset, SeekOrigin::SetOrigin); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | u8 Stream::ReadByte() { | ||
| 31 | if (position < buffer.size()) { | ||
| 32 | return buffer[position++]; | ||
| 33 | } else { | ||
| 34 | throw std::out_of_range("Attempting to read a byte not within the buffer range"); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | void Stream::WriteByte(u8 byte) { | ||
| 39 | if (position == buffer.size()) { | ||
| 40 | buffer.push_back(byte); | ||
| 41 | position++; | ||
| 42 | } else { | ||
| 43 | buffer.insert(buffer.begin() + position, byte); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | } // namespace Common | ||
diff --git a/src/common/stream.h b/src/common/stream.h new file mode 100644 index 000000000..2585c16af --- /dev/null +++ b/src/common/stream.h | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <vector> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | |||
| 10 | namespace Common { | ||
| 11 | |||
| 12 | enum class SeekOrigin { | ||
| 13 | SetOrigin, | ||
| 14 | FromCurrentPos, | ||
| 15 | FromEnd, | ||
| 16 | }; | ||
| 17 | |||
| 18 | class Stream { | ||
| 19 | public: | ||
| 20 | /// Stream creates a bitstream and provides common functionality on the stream. | ||
| 21 | explicit Stream(); | ||
| 22 | ~Stream(); | ||
| 23 | |||
| 24 | /// Reposition bitstream "cursor" to the specified offset from origin | ||
| 25 | void Seek(s32 offset, SeekOrigin origin); | ||
| 26 | |||
| 27 | /// Reads next byte in the stream buffer and increments position | ||
| 28 | u8 ReadByte(); | ||
| 29 | |||
| 30 | /// Writes byte at current position | ||
| 31 | void WriteByte(u8 byte); | ||
| 32 | |||
| 33 | std::size_t GetPosition() const { | ||
| 34 | return position; | ||
| 35 | } | ||
| 36 | |||
| 37 | std::vector<u8>& GetBuffer() { | ||
| 38 | return buffer; | ||
| 39 | } | ||
| 40 | |||
| 41 | const std::vector<u8>& GetBuffer() const { | ||
| 42 | return buffer; | ||
| 43 | } | ||
| 44 | |||
| 45 | private: | ||
| 46 | std::vector<u8> buffer; | ||
| 47 | std::size_t position{0}; | ||
| 48 | }; | ||
| 49 | |||
| 50 | } // namespace Common | ||