Debian Codegraph @

src/mpt/io_read/filedata_callbackstream.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_IO_READ_FILEDATA_CALLBACKSTREAM_HPP
4 #define MPT_IO_READ_FILEDATA_CALLBACKSTREAM_HPP
5
6
7
8 #include "mpt/base/integer.hpp"
9 #include "mpt/base/memory.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/io_read/callbackstream.hpp"
12 #include "mpt/io_read/filedata.hpp"
13 #include "mpt/io_read/filedata_base_seekable.hpp"
14 #include "mpt/io_read/filedata_base_unseekable.hpp"
15
16 #include <algorithm>
17 #include <memory>
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 template <typename Tstream>
33 class FileDataCallbackStreamTemplate {
34 public:
35 static bool IsSeekable(CallbackStreamTemplate<Tstream> stream) {
36 if (!stream.stream) {
37 return false;
38 }
39 if (!stream.seek) {
40 return false;
41 }
42 if (!stream.tell) {
43 return false;
44 }
45 int64 oldpos = stream.tell(stream.stream);
46 if (oldpos < 0) {
47 return false;
48 }
49 if (stream.seek(stream.stream, 0, CallbackStream::SeekSet) < 0) {
50 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
51 return false;
52 }
53 if (stream.seek(stream.stream, 0, CallbackStream::SeekEnd) < 0) {
54 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
55 return false;
56 }
57 int64 length = stream.tell(stream.stream);
58 if (length < 0) {
59 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
60 return false;
61 }
62 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
63 return true;
64 }
65
66 static IFileData::pos_type GetLength(CallbackStreamTemplate<Tstream> stream) {
67 if (!stream.stream) {
68 return 0;
69 }
70 if (!stream.seek) {
71 return false;
72 }
73 if (!stream.tell) {
74 return false;
75 }
76 int64 oldpos = stream.tell(stream.stream);
77 if (oldpos < 0) {
78 return 0;
79 }
80 if (stream.seek(stream.stream, 0, CallbackStream::SeekSet) < 0) {
81 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
82 return 0;
83 }
84 if (stream.seek(stream.stream, 0, CallbackStream::SeekEnd) < 0) {
85 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
86 return 0;
87 }
88 int64 length = stream.tell(stream.stream);
89 if (length < 0) {
90 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
91 return 0;
92 }
93 stream.seek(stream.stream, oldpos, CallbackStream::SeekSet);
94 return mpt::saturate_cast<IFileData::pos_type>(length);
95 }
96 };
97
98 using FileDataCallbackStream = FileDataCallbackStreamTemplate<void *>;
99
100
101
102 template <typename Tstream>
103 class FileDataCallbackStreamSeekableTemplate : public FileDataSeekable {
104 private:
105 CallbackStreamTemplate<Tstream> stream;
106
107 public:
108 FileDataCallbackStreamSeekableTemplate(CallbackStreamTemplate<Tstream> s)
109 : FileDataSeekable(FileDataCallbackStream::GetLength(s))
110 , stream(s) {
111 return;
112 }
113
114 private:
115 mpt::byte_span InternalReadSeekable(pos_type pos, mpt::byte_span dst) const override {
116 if (!stream.read) {
117 return dst.first(0);
118 }
119 if (stream.seek(stream.stream, pos, CallbackStream::SeekSet) < 0) {
120 return dst.first(0);
121 }
122 int64 totalread = 0;
123 std::byte * pdst = dst.data();
124 std::size_t count = dst.size();
125 while (count > 0) {
126 int64 readcount = stream.read(stream.stream, pdst, count);
127 if (readcount <= 0) {
128 break;
129 }
130 pdst += static_cast<std::size_t>(readcount);
131 count -= static_cast<std::size_t>(readcount);
132 totalread += readcount;
133 }
134 return dst.first(static_cast<std::size_t>(totalread));
135 }
136 };
137
138 using FileDataCallbackStreamSeekable = FileDataCallbackStreamSeekableTemplate<void *>;
139
140
141
142 template <typename Tstream>
143 class FileDataCallbackStreamUnseekableTemplate : public FileDataUnseekable {
144 private:
145 CallbackStreamTemplate<Tstream> stream;
146 mutable bool eof_reached;
147
148 public:
149 FileDataCallbackStreamUnseekableTemplate(CallbackStreamTemplate<Tstream> s)
150 : FileDataUnseekable()
151 , stream(s)
152 , eof_reached(false) {
153 return;
154 }
155
156 private:
157 bool InternalEof() const override {
158 return eof_reached;
159 }
160
161 mpt::byte_span InternalReadUnseekable(mpt::byte_span dst) const override {
162 if (eof_reached) {
163 return dst.first(0);
164 }
165 if (!stream.read) {
166 eof_reached = true;
167 return dst.first(0);
168 }
169 int64 totalread = 0;
170 std::byte * pdst = dst.data();
171 std::size_t count = dst.size();
172 while (count > 0) {
173 int64 readcount = stream.read(stream.stream, pdst, count);
174 if (readcount <= 0) {
175 eof_reached = true;
176 break;
177 }
178 pdst += static_cast<std::size_t>(readcount);
179 count -= static_cast<std::size_t>(readcount);
180 totalread += readcount;
181 }
182 return dst.first(static_cast<std::size_t>(totalread));
183 }
184 };
185
186 using FileDataCallbackStreamUnseekable = FileDataCallbackStreamUnseekableTemplate<void *>;
187
188
189
190 } // namespace IO
191
192
193
194 } // namespace MPT_INLINE_NS
195 } // namespace mpt
196
197
198
199 #endif // MPT_IO_READ_FILEDATA_CALLBACKSTREAM_HPP