Debian Codegraph @

src/mpt/io_read/filedata_base_unseekable_buffer.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_IO_READ_FILEDATA_BASE_UNSEEKABLE_BUFFER_HPP
4 #define MPT_IO_READ_FILEDATA_BASE_UNSEEKABLE_BUFFER_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 FileDataUnseekableBuffer : public IFileData {
33
34 private:
35 mutable std::vector<std::byte> cache;
36 mutable bool streamFullyCached;
37
38 protected:
39 FileDataUnseekableBuffer()
40 : streamFullyCached(false) {
41 return;
42 }
43
44 private:
45 enum : std::size_t {
46 QUANTUM_SIZE = mpt::IO::BUFFERSIZE_SMALL,
47 BUFFER_SIZE = mpt::IO::BUFFERSIZE_NORMAL
48 };
49
50 void CacheStream() const {
51 if (streamFullyCached) {
52 return;
53 }
54 while (!InternalEof()) {
55 InternalReadContinue(cache, BUFFER_SIZE);
56 }
57 streamFullyCached = true;
58 }
59
60 void CacheStreamUpTo(pos_type pos, pos_type length) const {
61 if (streamFullyCached) {
62 return;
63 }
64 if (length > std::numeric_limits<pos_type>::max() - pos) {
65 length = std::numeric_limits<pos_type>::max() - pos;
66 }
67 std::size_t target = mpt::saturate_cast<std::size_t>(pos + length);
68 if (target <= cache.size()) {
69 return;
70 }
71 while (!InternalEof() && (cache.size() < target)) {
72 std::size_t readsize = ((target - cache.size()) > QUANTUM_SIZE) ? BUFFER_SIZE : QUANTUM_SIZE;
73 InternalReadContinue(cache, readsize);
74 }
75 }
76
77 private:
78 void ReadCached(pos_type pos, mpt::byte_span dst) const {
79 std::copy(cache.begin() + static_cast<std::size_t>(pos), cache.begin() + static_cast<std::size_t>(pos + dst.size()), dst.data());
80 }
81
82 public:
83 bool IsValid() const override {
84 return true;
85 }
86
87 bool HasFastGetLength() const override {
88 return false;
89 }
90
91 bool HasPinnedView() const override {
92 return true; // we have the cache which is required for seeking anyway
93 }
94
95 const std::byte * GetRawData() const override {
96 CacheStream();
97 return cache.data();
98 }
99
100 pos_type GetLength() const override {
101 CacheStream();
102 return cache.size();
103 }
104
105 mpt::byte_span Read(pos_type pos, mpt::byte_span dst) const override {
106 CacheStreamUpTo(pos, dst.size());
107 if (pos >= static_cast<pos_type>(cache.size())) {
108 return dst.first(0);
109 }
110 pos_type cache_avail = std::min(static_cast<pos_type>(cache.size()) - pos, static_cast<pos_type>(dst.size()));
111 ReadCached(pos, dst.subspan(0, static_cast<std::size_t>(cache_avail)));
112 return dst.subspan(0, static_cast<std::size_t>(cache_avail));
113 }
114
115 bool CanRead(pos_type pos, pos_type length) const override {
116 CacheStreamUpTo(pos, length);
117 if ((pos == static_cast<pos_type>(cache.size())) && (length == 0)) {
118 return true;
119 }
120 if (pos >= static_cast<pos_type>(cache.size())) {
121 return false;
122 }
123 return length <= static_cast<pos_type>(cache.size()) - pos;
124 }
125
126 pos_type GetReadableLength(pos_type pos, pos_type length) const override {
127 CacheStreamUpTo(pos, length);
128 if (pos >= cache.size()) {
129 return 0;
130 }
131 return std::min(static_cast<pos_type>(cache.size()) - pos, length);
132 }
133
134 private:
135 virtual bool InternalEof() const = 0;
136 virtual void InternalReadContinue(std::vector<std::byte> & streamCache, std::size_t suggestedCount) const = 0;
137 };
138
139
140
141 } // namespace IO
142
143
144
145 } // namespace MPT_INLINE_NS
146 } // namespace mpt
147
148
149
150 #endif // MPT_IO_READ_FILEDATA_BASE_UNSEEKABLE_BUFFER_HPP