Debian Codegraph @

common/FileReader.h

raw
1 /*
2 * FileReader.h
3 * ------------
4 * Purpose: A basic class for transparent reading of memory-based files.
5 * Notes : (currently none)
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10
11 #pragma once
12
13 #include "openmpt/all/BuildSettings.hpp"
14
15 #include "mpt/io_read/filecursor.hpp"
16 #include "mpt/io_read/filecursor_filename_traits.hpp"
17 #include "mpt/io_read/filecursor_traits_filedata.hpp"
18 #include "mpt/io_read/filecursor_traits_memory.hpp"
19 #include "mpt/io_read/filereader.hpp"
20
21 #include "openmpt/base/Types.hpp"
22
23 #include "mptPathString.h"
24 #include "mptStringBuffer.h"
25
26 #include <algorithm>
27 #include <array>
28 #include <limits>
29 #include <optional>
30 #include <string>
31 #include <vector>
32
33 #include <cstddef>
34 #include <cstring>
35
36 #include "FileReaderFwd.h"
37
38
39 OPENMPT_NAMESPACE_BEGIN
40
41
42 namespace FileReaderExt
43 {
44
45 // Read a string of length srcSize into fixed-length char array destBuffer using a given read mode.
46 // The file cursor is advanced by "srcSize" bytes.
47 // Returns true if at least one byte could be read or 0 bytes were requested.
48 template<mpt::String::ReadWriteMode mode, std::size_t destSize, typename TFileCursor>
49 bool ReadString(TFileCursor &f, char (&destBuffer)[destSize], const std::size_t srcSize)
50 {
51 typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
52 std::size_t realSrcSize = source.size(); // In case fewer bytes are available
53 mpt::String::WriteAutoBuf(destBuffer) = mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize);
54 return (realSrcSize > 0 || srcSize == 0);
55 }
56
57 // Read a string of length srcSize into a std::string dest using a given read mode.
58 // The file cursor is advanced by "srcSize" bytes.
59 // Returns true if at least one character could be read or 0 characters were requested.
60 template<mpt::String::ReadWriteMode mode, typename TFileCursor>
61 bool ReadString(TFileCursor &f, std::string &dest, const std::size_t srcSize)
62 {
63 dest.clear();
64 typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
65 std::size_t realSrcSize = source.size(); // In case fewer bytes are available
66 dest = mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize);
67 return (realSrcSize > 0 || srcSize == 0);
68 }
69
70 // Read a string of length srcSize into a mpt::charbuf dest using a given read mode.
71 // The file cursor is advanced by "srcSize" bytes.
72 // Returns true if at least one character could be read or 0 characters were requested.
73 template<mpt::String::ReadWriteMode mode, std::size_t len, typename TFileCursor>
74 bool ReadString(TFileCursor &f, mpt::charbuf<len> &dest, const std::size_t srcSize)
75 {
76 typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
77 std::size_t realSrcSize = source.size(); // In case fewer bytes are available
78 dest = mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize);
79 return (realSrcSize > 0 || srcSize == 0);
80 }
81
82 // Read a charset encoded string of length srcSize into a mpt::ustring dest using a given read mode.
83 // The file cursor is advanced by "srcSize" bytes.
84 // Returns true if at least one character could be read or 0 characters were requested.
85 template<mpt::String::ReadWriteMode mode, typename TFileCursor>
86 bool ReadString(TFileCursor &f, mpt::ustring &dest, mpt::Charset charset, const std::size_t srcSize)
87 {
88 dest.clear();
89 typename TFileCursor::PinnedView source = f.ReadPinnedView(srcSize); // Make sure the string is cached properly.
90 std::size_t realSrcSize = source.size(); // In case fewer bytes are available
91 dest = mpt::ToUnicode(charset, mpt::String::ReadBuf(mode, mpt::byte_cast<const char*>(source.data()), realSrcSize));
92 return (realSrcSize > 0 || srcSize == 0);
93 }
94
95 // Read a string with a preprended length field of type Tsize (must be a packed<*,*> type) into a std::string dest using a given read mode.
96 // The file cursor is advanced by the string length.
97 // Returns true if the size field could be read and at least one character could be read or 0 characters were requested.
98 template<typename Tsize, mpt::String::ReadWriteMode mode, std::size_t destSize, typename TFileCursor>
99 bool ReadSizedString(TFileCursor &f, char (&destBuffer)[destSize], const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
100 {
101 static_assert(mpt::is_binary_safe<Tsize>::value);
102 Tsize srcSize;
103 if(!mpt::IO::FileReader::Read(f, srcSize))
104 {
105 return false;
106 }
107 return FileReaderExt::ReadString<mode>(f, destBuffer, std::min(static_cast<std::size_t>(srcSize), maxLength));
108 }
109
110 // Read a string with a preprended length field of type Tsize (must be a packed<*,*> type) into a std::string dest using a given read mode.
111 // The file cursor is advanced by the string length.
112 // Returns true if the size field could be read and at least one character could be read or 0 characters were requested.
113 template<typename Tsize, mpt::String::ReadWriteMode mode, typename TFileCursor>
114 bool ReadSizedString(TFileCursor &f, std::string &dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
115 {
116 static_assert(mpt::is_binary_safe<Tsize>::value);
117 Tsize srcSize;
118 if(!mpt::IO::FileReader::Read(f, srcSize))
119 {
120 return false;
121 }
122 return FileReaderExt::ReadString<mode>(f, dest, std::min(static_cast<std::size_t>(srcSize), maxLength));
123 }
124
125 // Read a string with a preprended length field of type Tsize (must be a packed<*,*> type) into a mpt::charbuf dest using a given read mode.
126 // The file cursor is advanced by the string length.
127 // Returns true if the size field could be read and at least one character could be read or 0 characters were requested.
128 template<typename Tsize, mpt::String::ReadWriteMode mode, std::size_t len, typename TFileCursor>
129 bool ReadSizedString(TFileCursor &f, mpt::charbuf<len> &dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
130 {
131 static_assert(mpt::is_binary_safe<Tsize>::value);
132 Tsize srcSize;
133 if(!mpt::IO::FileReader::Read(f, srcSize))
134 {
135 return false;
136 }
137 return FileReaderExt::ReadString<mode>(f, dest, std::min(static_cast<std::size_t>(srcSize), maxLength));
138 }
139
140 } // namespace FileReaderExt
141
142 namespace detail {
143
144 template <typename Ttraits, typename Tfilenametraits>
145 using FileCursor = mpt::IO::FileCursor<Ttraits, Tfilenametraits>;
146
147 template <typename Ttraits, typename Tfilenametraits>
148 class FileReader
149 : public FileCursor<Ttraits, Tfilenametraits>
150 {
151
152 private:
153
154 using traits_type = Ttraits;
155 using filename_traits_type = Tfilenametraits;
156
157 public:
158
159 using pos_type = typename traits_type::pos_type;
160
161 using data_type = typename traits_type::data_type;
162 using ref_data_type = typename traits_type::ref_data_type;
163 using shared_data_type = typename traits_type::shared_data_type;
164 using value_data_type = typename traits_type::value_data_type;
165
166 using shared_filename_type = typename filename_traits_type::shared_filename_type;
167
168 public:
169
170 // Initialize invalid file reader object.
171 FileReader()
172 {
173 return;
174 }
175
176 FileReader(const FileCursor<Ttraits, Tfilenametraits> &other)
177 : FileCursor<Ttraits, Tfilenametraits>(other)
178 {
179 return;
180 }
181 FileReader(FileCursor<Ttraits, Tfilenametraits> &&other)
182 : FileCursor<Ttraits, Tfilenametraits>(std::move(other))
183 {
184 return;
185 }
186
187 // Initialize file reader object with pointer to data and data length.
188 template <typename Tbyte>
189 explicit FileReader(mpt::span<Tbyte> bytedata, shared_filename_type filename = shared_filename_type{})
190 : FileCursor<Ttraits, Tfilenametraits>(bytedata, std::move(filename))
191 {
192 return;
193 }
194
195 // Initialize file reader object based on an existing file reader object window.
196 explicit FileReader(value_data_type other, shared_filename_type filename = shared_filename_type{})
197 : FileCursor<Ttraits, Tfilenametraits>(std::move(other), std::move(filename))
198 {
199 return;
200 }
201
202 public:
203
204 template <typename T>
205 bool Read(T &target)
206 {
207 return mpt::IO::FileReader::Read(*this, target);
208 }
209
210 template <typename T>
211 T ReadIntLE()
212 {
213 return mpt::IO::FileReader::ReadIntLE<T>(*this);
214 }
215
216 template <typename T>
217 T ReadIntBE()
218 {
219 return mpt::IO::FileReader::ReadIntLE<T>(*this);
220 }
221
222 template <typename T>
223 T ReadTruncatedIntLE(std::size_t size)
224 {
225 return mpt::IO::FileReader::ReadTruncatedIntLE<T>(*this, size);
226 }
227
228 template <typename T>
229 T ReadSizedIntLE(std::size_t size)
230 {
231 return mpt::IO::FileReader::ReadSizedIntLE<T>(*this, size);
232 }
233
234 uint32 ReadUint32LE()
235 {
236 return mpt::IO::FileReader::ReadUint32LE(*this);
237 }
238
239 uint32 ReadUint32BE()
240 {
241 return mpt::IO::FileReader::ReadUint32BE(*this);
242 }
243
244 int32 ReadInt32LE()
245 {
246 return mpt::IO::FileReader::ReadInt32LE(*this);
247 }
248
249 int32 ReadInt32BE()
250 {
251 return mpt::IO::FileReader::ReadInt32BE(*this);
252 }
253
254 uint32 ReadUint24LE()
255 {
256 return mpt::IO::FileReader::ReadUint24LE(*this);
257 }
258
259 uint32 ReadUint24BE()
260 {
261 return mpt::IO::FileReader::ReadUint24BE(*this);
262 }
263
264 uint16 ReadUint16LE()
265 {
266 return mpt::IO::FileReader::ReadUint16LE(*this);
267 }
268
269 uint16 ReadUint16BE()
270 {
271 return mpt::IO::FileReader::ReadUint16BE(*this);
272 }
273
274 int16 ReadInt16LE()
275 {
276 return mpt::IO::FileReader::ReadInt16LE(*this);
277 }
278
279 int16 ReadInt16BE()
280 {
281 return mpt::IO::FileReader::ReadInt16BE(*this);
282 }
283
284 char ReadChar()
285 {
286 return mpt::IO::FileReader::ReadChar(*this);
287 }
288
289 uint8 ReadUint8()
290 {
291 return mpt::IO::FileReader::ReadUint8(*this);
292 }
293
294 int8 ReadInt8()
295 {
296 return mpt::IO::FileReader::ReadInt8(*this);
297 }
298
299 float ReadFloatLE()
300 {
301 return mpt::IO::FileReader::ReadFloatLE(*this);
302 }
303
304 float ReadFloatBE()
305 {
306 return mpt::IO::FileReader::ReadFloatBE(*this);
307 }
308
309 double ReadDoubleLE()
310 {
311 return mpt::IO::FileReader::ReadDoubleLE(*this);
312 }
313
314 double ReadDoubleBE()
315 {
316 return mpt::IO::FileReader::ReadDoubleBE(*this);
317 }
318
319 template <typename T>
320 bool ReadStruct(T &target)
321 {
322 return mpt::IO::FileReader::ReadStruct(*this, target);
323 }
324
325 template <typename T>
326 std::size_t ReadStructPartial(T &target, std::size_t partialSize = sizeof(T))
327 {
328 return mpt::IO::FileReader::ReadStructPartial(*this, target, partialSize);
329 }
330
331 bool ReadNullString(std::string &dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
332 {
333 return mpt::IO::FileReader::ReadNullString(*this, dest, maxLength);
334 }
335
336 bool ReadLine(std::string &dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
337 {
338 return mpt::IO::FileReader::ReadLine(*this, dest, maxLength);
339 }
340
341 template<typename T, std::size_t destSize>
342 bool ReadArray(T (&destArray)[destSize])
343 {
344 return mpt::IO::FileReader::ReadArray(*this, destArray);
345 }
346
347 template<typename T, std::size_t destSize>
348 bool ReadArray(std::array<T, destSize> &destArray)
349 {
350 return mpt::IO::FileReader::ReadArray(*this, destArray);
351 }
352
353 template <typename T, std::size_t destSize>
354 std::array<T, destSize> ReadArray()
355 {
356 return mpt::IO::FileReader::ReadArray<T, destSize>(*this);
357 }
358
359 template<typename T>
360 bool ReadVector(std::vector<T> &destVector, std::size_t destSize)
361 {
362 return mpt::IO::FileReader::ReadVector(*this, destVector, destSize);
363 }
364
365 template<std::size_t N>
366 bool ReadMagic(const char (&magic)[N])
367 {
368 return mpt::IO::FileReader::ReadMagic(*this, magic);
369 }
370
371 template<typename T>
372 bool ReadVarInt(T &target)
373 {
374 return mpt::IO::FileReader::ReadVarInt(*this, target);
375 }
376
377 template <typename T>
378 using Item = mpt::IO::FileReader::Chunk<T, FileReader>;
379
380 template <typename T>
381 using ChunkList = mpt::IO::FileReader::ChunkList<T, FileReader>;
382
383 template<typename T>
384 Item<T> ReadNextChunk(pos_type alignment)
385 {
386 return mpt::IO::FileReader::ReadNextChunk<T, FileReader>(*this, alignment);
387 }
388
389 template<typename T>
390 ChunkList<T> ReadChunks(pos_type alignment)
391 {
392 return mpt::IO::FileReader::ReadChunks<T, FileReader>(*this, alignment);
393 }
394
395 template<typename T>
396 ChunkList<T> ReadChunksUntil(pos_type alignment, decltype(T().GetID()) stopAtID)
397 {
398 return mpt::IO::FileReader::ReadChunksUntil<T, FileReader>(*this, alignment, stopAtID);
399 }
400
401 template<mpt::String::ReadWriteMode mode, std::size_t destSize>
402 bool ReadString(char (&destBuffer)[destSize], const std::size_t srcSize)
403 {
404 return FileReaderExt::ReadString<mode>(*this, destBuffer, srcSize);
405 }
406
407 template<mpt::String::ReadWriteMode mode>
408 bool ReadString(std::string &dest, const std::size_t srcSize)
409 {
410 return FileReaderExt::ReadString<mode>(*this, dest, srcSize);
411 }
412
413 template<mpt::String::ReadWriteMode mode, std::size_t len>
414 bool ReadString(mpt::charbuf<len> &dest, const std::size_t srcSize)
415 {
416 return FileReaderExt::ReadString<mode>(*this, dest, srcSize);
417 }
418
419 template<mpt::String::ReadWriteMode mode>
420 bool ReadString(mpt::ustring &dest, mpt::Charset charset, const std::size_t srcSize)
421 {
422 return FileReaderExt::ReadString<mode>(*this, dest, charset, srcSize);
423 }
424
425 template<typename Tsize, mpt::String::ReadWriteMode mode, std::size_t destSize>
426 bool ReadSizedString(char (&destBuffer)[destSize], const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
427 {
428 return FileReaderExt::ReadSizedString<Tsize, mode>(*this, destBuffer, maxLength);
429 }
430
431 template<typename Tsize, mpt::String::ReadWriteMode mode>
432 bool ReadSizedString(std::string &dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
433 {
434 return FileReaderExt::ReadSizedString<Tsize, mode>(*this, dest, maxLength);
435 }
436
437 template<typename Tsize, mpt::String::ReadWriteMode mode, std::size_t len>
438 bool ReadSizedString(mpt::charbuf<len> &dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max())
439 {
440 return FileReaderExt::ReadSizedString<Tsize, mode, len>(*this, dest, maxLength);
441 }
442
443 };
444
445 } // namespace detail
446
447 using FileCursor = detail::FileCursor<mpt::IO::FileCursorTraitsFileData, mpt::IO::FileCursorFilenameTraits<mpt::PathString>>;
448 using FileReader = detail::FileReader<mpt::IO::FileCursorTraitsFileData, mpt::IO::FileCursorFilenameTraits<mpt::PathString>>;
449
450 using ChunkReader = FileReader;
451
452 using MemoryFileCursor = detail::FileCursor<mpt::IO::FileCursorTraitsMemory, mpt::IO::FileCursorFilenameTraitsNone>;
453 using MemoryFileReader = detail::FileReader<mpt::IO::FileCursorTraitsMemory, mpt::IO::FileCursorFilenameTraitsNone>;
454
455
456 OPENMPT_NAMESPACE_END