157 lines
3.8 KiB
C++
157 lines
3.8 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bit>
|
|
#include <concepts>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <format>
|
|
#include <span>
|
|
#include <string>
|
|
|
|
namespace Void::Binary
|
|
{
|
|
/// Bounds-checked, non-owning reader for binary formats.
|
|
class Reader
|
|
{
|
|
public:
|
|
|
|
explicit constexpr Reader(std::span<const std::byte> bytes) noexcept
|
|
: m_bytes(bytes)
|
|
{
|
|
}
|
|
|
|
[[nodiscard]] constexpr std::size_t position() const noexcept
|
|
{
|
|
return m_position;
|
|
}
|
|
|
|
[[nodiscard]] constexpr std::size_t size() const noexcept
|
|
{
|
|
return m_bytes.size();
|
|
}
|
|
|
|
[[nodiscard]] constexpr std::size_t remaining() const noexcept
|
|
{
|
|
return m_bytes.size() - m_position;
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool empty() const noexcept
|
|
{
|
|
return remaining() == 0;
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<void> seek(std::size_t position)
|
|
{
|
|
if (position > m_bytes.size()) {
|
|
return Void::Result::fail(std::format(
|
|
"Binary::Reader::seek: offset {} exceeds size {}",
|
|
position, m_bytes.size()));
|
|
}
|
|
m_position = position;
|
|
return {};
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<void> skip(std::size_t count)
|
|
{
|
|
if (count > remaining()) {
|
|
return truncated("skip", count);
|
|
}
|
|
m_position += count;
|
|
return {};
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<std::span<const std::byte>>
|
|
readBytes(std::size_t count)
|
|
{
|
|
if (count > remaining()) {
|
|
return std::unexpected{truncatedError("readBytes", count)};
|
|
}
|
|
const auto result = m_bytes.subspan(m_position, count);
|
|
m_position += count;
|
|
return result;
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<std::span<const std::byte>>
|
|
peekBytes(std::size_t count) const
|
|
{
|
|
if (count > remaining()) {
|
|
return std::unexpected{truncatedError("peekBytes", count)};
|
|
}
|
|
return m_bytes.subspan(m_position, count);
|
|
}
|
|
|
|
template <std::integral T>
|
|
requires(!std::same_as<T, bool>)
|
|
[[nodiscard]] Void::Result::Result<T>
|
|
read(std::endian byteOrder = std::endian::native)
|
|
{
|
|
auto bytes = readBytes(sizeof(T));
|
|
if (!bytes) {
|
|
return std::unexpected{bytes.error()};
|
|
}
|
|
std::array<std::byte, sizeof(T)> storage{};
|
|
std::ranges::copy(*bytes, storage.begin());
|
|
T value = std::bit_cast<T>(storage);
|
|
if constexpr (std::endian::native != std::endian::little &&
|
|
std::endian::native != std::endian::big) {
|
|
return Void::Result::fail(
|
|
"Binary::Reader::read: mixed-endian targets are "
|
|
"unsupported");
|
|
} else if (byteOrder != std::endian::native) {
|
|
value = std::byteswap(value);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<std::string>
|
|
readString(std::size_t count)
|
|
{
|
|
auto bytes = readBytes(count);
|
|
if (!bytes) {
|
|
return std::unexpected{bytes.error()};
|
|
}
|
|
return std::string{reinterpret_cast<const char*>(bytes->data()),
|
|
bytes->size()};
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<Reader>
|
|
subreader(std::size_t count)
|
|
{
|
|
auto bytes = readBytes(count);
|
|
if (!bytes) {
|
|
return std::unexpected{bytes.error()};
|
|
}
|
|
return Reader{*bytes};
|
|
}
|
|
|
|
private:
|
|
|
|
std::span<const std::byte> m_bytes;
|
|
std::size_t m_position{0};
|
|
|
|
[[nodiscard]] Void::Result::Error
|
|
truncatedError(std::string_view operation,
|
|
std::size_t requested) const
|
|
{
|
|
return Void::Result::Error{
|
|
std::format("Binary::Reader::{}: requested {} bytes at "
|
|
"offset {}, only {} "
|
|
"remain",
|
|
operation, requested, m_position, remaining())};
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
truncated(std::string_view operation,
|
|
std::size_t requested) const
|
|
{
|
|
return std::unexpected{truncatedError(operation, requested)};
|
|
}
|
|
};
|
|
} // namespace Void::Binary
|