Debian Codegraph @

src/mpt/io_read/filedata_base_unseekable.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_IO_READ_FILEDATA_BASE_UNSEEKABLE_HPP
4 #define MPT_IO_READ_FILEDATA_BASE_UNSEEKABLE_HPP
5
6
7
8 #include "mpt/base/algorithm.hpp"
9 #include "mpt/base/memory.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/base/numeric.hpp"
12 #include "mpt/io/base.hpp"
13 #include "mpt/io_read/filedata.hpp"
14
15 #include <algorithm>
16 #include <limits>
17 #include <vector>
18
19 #include <cstddef>
20
21
22
23 namespace mpt {
24 inline namespace MPT_INLINE_NS {
25
26
27
28 namespace IO {
29
30
31
32 class FileDataUnseekable : public IFileData {
33
34 private:
35 mutable std::vector<std::byte> cache;
36 mutable std::size_t cachesize;
37 mutable bool streamFullyCached;
38
39 protected:
40 FileDataUnseekable()
41 : cachesize(0)
42 , streamFullyCached(false) {
43 return;
44 }
45
46 private:
47 enum : std::size_t {
48 QUANTUM_SIZE = mpt::IO::BUFFERSIZE_SMALL,
49 BUFFER_SIZE = mpt::IO::BUFFERSIZE_NORMAL
50 };
51
52 void EnsureCacheBuffer(std::size_t requiredbuffersize) const {
53 if (cache.size() - cachesize >= requiredbuffersize) {
54 return;
55 }
56 if (cache.size() == 0) {
57 cache.resize(mpt::saturate_align_up<std::size_t>(cachesize + requiredbuffersize, BUFFER_SIZE));
58 } else if (mpt::exponential_grow(cache.size()) < cachesize + requiredbuffersize) {
59 cache.resize(mpt::saturate_align_up<std::size_t>(cachesize + requiredbuffersize, BUFFER_SIZE));
60 } else {
61 cache.resize(mpt::exponential_grow(cache.size()));
62 }
63 }
64
65 void CacheStream() const {
66 if (streamFullyCached) {
67 return;
68 }
69 while (!InternalEof()) {
70 EnsureCacheBuffer(BUFFER_SIZE);
71 std::size_t readcount = InternalReadUnseekable(mpt::span(&cache[cachesize], BUFFER_SIZE)).size();
72 cachesize += readcount;
73 }
74 streamFullyCached = true;
75 }
76
77 void CacheStreamUpTo(pos_type pos, pos_type length) const {
78 if (streamFullyCached) {
79 return;
80 }
81 if (length > std::numeric_limits<pos_type>::max() - pos) {
82 length = std::numeric_limits<pos_type>::max() - pos;
83 }
84 std::size_t target = mpt::saturate_cast<std::size_t>(pos + length);
85 if (target <= cachesize) {
86 return;
87 }
88 std::size_t alignedpos = mpt::saturate_align_up<std::size_t>(target, QUANTUM_SIZE);
89 while (!InternalEof() && (cachesize < alignedpos)) {
90 EnsureCacheBuffer(BUFFER_SIZE);
91 std::size_t readcount = InternalReadUnseekable(mpt::span(&cache[cachesize], BUFFER_SIZE)).size();
92 cachesize += readcount;
93 }
94 if (InternalEof()) {
95 streamFullyCached = true;
96 }
97 }
98
99 private:
100 void ReadCached(pos_type pos, mpt::byte_span dst) const {
101 std::copy(cache.begin() + static_cast<std::size_t>(pos), cache.begin() + static_cast<std::size_t>(pos + dst.size()), dst.data());
102 }
103
104 public:
105 bool IsValid() const override {
106 return true;
107 }
108
109 bool HasFastGetLength() const override {
110 return false;
111 }
112
113 bool HasPinnedView() const override {
114 return true; // we have the cache which is required for seeking anyway
115 }
116
117 const std::byte * GetRawData() const override {
118 CacheStream();
119 return cache.data();
120 }
121
122 pos_type GetLength() const override {
123 CacheStream();
124 return cachesize;
125 }
126
127 mpt::byte_span Read(pos_type pos, mpt::byte_span dst) const override {
128 CacheStreamUpTo(pos, dst.size());
129 if (pos >= static_cast<pos_type>(cachesize)) {
130 return dst.first(0);
131 }
132 pos_type cache_avail = std::min(static_cast<pos_type>(cachesize) - pos, static_cast<pos_type>(dst.size()));
133 ReadCached(pos, dst.subspan(0, static_cast<std::size_t>(cache_avail)));
134 return dst.subspan(0, static_cast<std::size_t>(cache_avail));
135 }
136
137 bool CanRead(pos_type pos, pos_type length) const override {
138 CacheStreamUpTo(pos, length);
139 if ((pos == static_cast<pos_type>(cachesize)) && (length == 0)) {
140 return true;
141 }
142 if (pos >= static_cast<pos_type>(cachesize)) {
143 return false;
144 }
145 return length <= static_cast<pos_type>(cachesize) - pos;
146 }
147
148 pos_type GetReadableLength(pos_type pos, pos_type length) const override {
149 CacheStreamUpTo(pos, length);
150 if (pos >= cachesize) {
151 return 0;
152 }
153 return std::min(static_cast<pos_type>(cachesize) - pos, length);
154 }
155
156 private:
157 virtual bool InternalEof() const = 0;
158 virtual mpt::byte_span InternalReadUnseekable(mpt::byte_span dst) const = 0;
159 };
160
161
162
163 } // namespace IO
164
165
166
167 } // namespace MPT_INLINE_NS
168 } // namespace mpt
169
170
171
172 #endif // MPT_IO_READ_FILEDATA_BASE_UNSEEKABLE_HPP