denigma 4.0.0
Loading...
Searching...
No Matches
random_access_reader.h
1/*
2 * Copyright (C) 2026, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17 * THE SOFTWARE.
18 */
19#pragma once
20
21#include <cstddef>
22#include <cstdint>
23#include <filesystem>
24#include <fstream>
25#include <span>
26
27namespace denigma {
28
32{
33public:
34 virtual ~IRandomAccessReader() = default;
35
37 [[nodiscard]] virtual std::uint64_t size() const = 0;
38
40 virtual std::size_t readAt(std::uint64_t offset, std::span<std::byte> output) const = 0;
41};
42
46{
47public:
49 explicit FileRandomAccessReader(const std::filesystem::path& path);
50
51 [[nodiscard]] std::uint64_t size() const override;
52 std::size_t readAt(std::uint64_t offset, std::span<std::byte> output) const override;
53
54private:
55 mutable std::ifstream m_file;
56 std::uint64_t m_size{};
57};
58
62{
63public:
65 explicit BufferRandomAccessReader(std::span<const std::byte> data);
66
67 [[nodiscard]] std::uint64_t size() const override;
68 std::size_t readAt(std::uint64_t offset, std::span<std::byte> output) const override;
69
70private:
71 std::span<const std::byte> m_data;
72};
73
74} // namespace denigma
Random-access reader backed by caller-owned memory.
Definition random_access_reader.h:62
std::size_t readAt(std::uint64_t offset, std::span< std::byte > output) const override
Reads up to output.size() bytes from offset and returns the number of bytes read.
BufferRandomAccessReader(std::span< const std::byte > data)
Uses caller-owned memory as a random-access byte source.
std::uint64_t size() const override
Returns the total readable byte count.
Random-access reader backed by a filesystem file.
Definition random_access_reader.h:46
std::uint64_t size() const override
Returns the total readable byte count.
std::size_t readAt(std::uint64_t offset, std::span< std::byte > output) const override
Reads up to output.size() bytes from offset and returns the number of bytes read.
FileRandomAccessReader(const std::filesystem::path &path)
Opens path for random-access reads.
Random-access byte reader used for container formats such as MUSX.
Definition random_access_reader.h:32
virtual std::size_t readAt(std::uint64_t offset, std::span< std::byte > output) const =0
Reads up to output.size() bytes from offset and returns the number of bytes read.
virtual std::uint64_t size() const =0
Returns the total readable byte count.
Core public API for the Denigma conversion libraries.
Definition articulations.h:32