| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_READ_FILEREADER_HPP |
| 4 |
#define MPT_IO_READ_FILEREADER_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/alloc.hpp" |
| 9 |
#include "mpt/base/bit.hpp" |
| 10 |
#include "mpt/base/integer.hpp" |
| 11 |
#include "mpt/base/memory.hpp" |
| 12 |
#include "mpt/base/namespace.hpp" |
| 13 |
#include "mpt/base/saturate_cast.hpp" |
| 14 |
#include "mpt/base/span.hpp" |
| 15 |
#include "mpt/base/utility.hpp" |
| 16 |
#include "mpt/io/base.hpp" |
| 17 |
#include "mpt/endian/floatingpoint.hpp" |
| 18 |
#include "mpt/endian/integer.hpp" |
| 19 |
#include "mpt/endian/type_traits.hpp" |
| 20 |
#include "mpt/string/utility.hpp" |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <array> |
| 24 |
#include <limits> |
| 25 |
#include <optional> |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include <cassert> |
| 30 |
#include <cstddef> |
| 31 |
#include <cstring> |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
namespace mpt { |
| 36 |
inline namespace MPT_INLINE_NS { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
namespace IO { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
namespace FileReader { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
// change to show warnings for functions which trigger pre-caching the whole file for unseekable streams |
| 49 |
//#define MPT_FILEREADER_DEPRECATED [[deprecated]] |
| 50 |
#define MPT_FILEREADER_DEPRECATED |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
// TFileCursor members begin |
| 55 |
|
| 56 |
template <typename TFileCursor> |
| 57 |
std::optional<typename TFileCursor::filename_type> GetOptionalFileName(const TFileCursor & f) { |
| 58 |
return f.GetOptionalFileName(); |
| 59 |
} |
| 60 |
|
| 61 |
// Returns true if the object points to a valid (non-empty) stream. |
| 62 |
template <typename TFileCursor> |
| 63 |
bool IsValid(const TFileCursor & f) { |
| 64 |
return f.IsValid(); |
| 65 |
} |
| 66 |
|
| 67 |
// Reset cursor to first byte in file. |
| 68 |
template <typename TFileCursor> |
| 69 |
void Rewind(TFileCursor & f) { |
| 70 |
f.Rewind(); |
| 71 |
} |
| 72 |
|
| 73 |
// Seek to a position in the mapped file. |
| 74 |
// Returns false if position is invalid. |
| 75 |
template <typename TFileCursor> |
| 76 |
bool Seek(TFileCursor & f, typename TFileCursor::pos_type position) { |
| 77 |
return f.Seek(position); |
| 78 |
} |
| 79 |
|
| 80 |
// Increases position by skipBytes. |
| 81 |
// Returns true if skipBytes could be skipped or false if the file end was reached earlier. |
| 82 |
template <typename TFileCursor> |
| 83 |
bool Skip(TFileCursor & f, typename TFileCursor::pos_type skipBytes) { |
| 84 |
return f.Skip(skipBytes); |
| 85 |
} |
| 86 |
|
| 87 |
// Decreases position by skipBytes. |
| 88 |
// Returns true if skipBytes could be skipped or false if the file start was reached earlier. |
| 89 |
template <typename TFileCursor> |
| 90 |
bool SkipBack(TFileCursor & f, typename TFileCursor::pos_type skipBytes) { |
| 91 |
return f.SkipBack(skipBytes); |
| 92 |
} |
| 93 |
|
| 94 |
// Returns cursor position in the mapped file. |
| 95 |
template <typename TFileCursor> |
| 96 |
typename TFileCursor::pos_type GetPosition(const TFileCursor & f) { |
| 97 |
return f.GetPosition(); |
| 98 |
} |
| 99 |
|
| 100 |
// Return true IFF seeking and GetLength() is fast. |
| 101 |
// In particular, it returns false for unseekable stream where GetLength() |
| 102 |
// requires pre-caching. |
| 103 |
template <typename TFileCursor> |
| 104 |
bool HasFastGetLength(const TFileCursor & f) { |
| 105 |
return f.HasFastGetLength(); |
| 106 |
} |
| 107 |
|
| 108 |
// Returns size of the mapped file in bytes. |
| 109 |
template <typename TFileCursor> |
| 110 |
MPT_FILEREADER_DEPRECATED typename TFileCursor::pos_type GetLength(const TFileCursor & f) { |
| 111 |
return f.GetLength(); |
| 112 |
} |
| 113 |
|
| 114 |
// Return byte count between cursor position and end of file, i.e. how many bytes can still be read. |
| 115 |
template <typename TFileCursor> |
| 116 |
MPT_FILEREADER_DEPRECATED typename TFileCursor::pos_type BytesLeft(const TFileCursor & f) { |
| 117 |
return f.BytesLeft(); |
| 118 |
} |
| 119 |
|
| 120 |
template <typename TFileCursor> |
| 121 |
bool EndOfFile(const TFileCursor & f) { |
| 122 |
return f.EndOfFile(); |
| 123 |
} |
| 124 |
|
| 125 |
template <typename TFileCursor> |
| 126 |
bool NoBytesLeft(const TFileCursor & f) { |
| 127 |
return f.NoBytesLeft(); |
| 128 |
} |
| 129 |
|
| 130 |
// Check if "amount" bytes can be read from the current position in the stream. |
| 131 |
template <typename TFileCursor> |
| 132 |
bool CanRead(const TFileCursor & f, typename TFileCursor::pos_type amount) { |
| 133 |
return f.CanRead(amount); |
| 134 |
} |
| 135 |
|
| 136 |
// Check if file size is at least size, without potentially caching the whole file to query the exact file length. |
| 137 |
template <typename TFileCursor> |
| 138 |
bool LengthIsAtLeast(const TFileCursor & f, typename TFileCursor::pos_type size) { |
| 139 |
return f.LengthIsAtLesat(size); |
| 140 |
} |
| 141 |
|
| 142 |
// Check if file size is exactly size, without potentially caching the whole file if it is larger. |
| 143 |
template <typename TFileCursor> |
| 144 |
bool LengthIs(const TFileCursor & f, typename TFileCursor::pos_type size) { |
| 145 |
return f.LengthIs(size); |
| 146 |
} |
| 147 |
|
| 148 |
// Create a new FileCursor object for parsing a sub chunk at a given position with a given length. |
| 149 |
// The file cursor is not modified. |
| 150 |
template <typename TFileCursor> |
| 151 |
TFileCursor GetChunkAt(const TFileCursor & f, typename TFileCursor::pos_type position, typename TFileCursor::pos_type length) { |
| 152 |
return f.GetChunkAt(position, length); |
| 153 |
} |
| 154 |
|
| 155 |
// Create a new FileCursor object for parsing a sub chunk at the current position with a given length. |
| 156 |
// The file cursor is not advanced. |
| 157 |
template <typename TFileCursor> |
| 158 |
TFileCursor GetChunk(TFileCursor & f, typename TFileCursor::pos_type length) { |
| 159 |
return f.GetChunk(length); |
| 160 |
} |
| 161 |
|
| 162 |
// Create a new FileCursor object for parsing a sub chunk at the current position with a given length. |
| 163 |
// The file cursor is advanced by "length" bytes. |
| 164 |
template <typename TFileCursor> |
| 165 |
TFileCursor ReadChunk(TFileCursor & f, typename TFileCursor::pos_type length) { |
| 166 |
return f.ReadChunk(length); |
| 167 |
} |
| 168 |
|
| 169 |
// Returns a pinned view into the remaining raw data from cursor position. |
| 170 |
template <typename TFileCursor> |
| 171 |
typename TFileCursor::PinnedView GetPinnedView(const TFileCursor & f) { |
| 172 |
return f.GetPinnedView(); |
| 173 |
} |
| 174 |
|
| 175 |
// Returns a pinned view into the remeining raw data from cursor position, clamped at size. |
| 176 |
template <typename TFileCursor> |
| 177 |
typename TFileCursor::PinnedView GetPinnedView(const TFileCursor & f, std::size_t size) { |
| 178 |
return f.GetPinnedView(size); |
| 179 |
} |
| 180 |
|
| 181 |
// Returns a pinned view into the remeining raw data from cursor position. |
| 182 |
// File cursor is advaned by the size of the returned pinned view. |
| 183 |
template <typename TFileCursor> |
| 184 |
typename TFileCursor::PinnedView ReadPinnedView(TFileCursor & f) { |
| 185 |
return f.ReadPinnedView(); |
| 186 |
} |
| 187 |
|
| 188 |
// Returns a pinned view into the remeining raw data from cursor position, clamped at size. |
| 189 |
// File cursor is advaned by the size of the returned pinned view. |
| 190 |
template <typename TFileCursor> |
| 191 |
typename TFileCursor::PinnedView ReadPinnedView(TFileCursor & f, std::size_t size) { |
| 192 |
return f.ReadPinnedView(size); |
| 193 |
} |
| 194 |
|
| 195 |
template <typename Tspan, typename TFileCursor> |
| 196 |
Tspan GetRawWithOffset(const TFileCursor & f, std::size_t offset, Tspan dst) { |
| 197 |
return f.template GetRawWithOffset<Tspan>(offset, dst); |
| 198 |
} |
| 199 |
|
| 200 |
template <typename Tspan, typename TFileCursor> |
| 201 |
Tspan GetRaw(const TFileCursor & f, Tspan dst) { |
| 202 |
return f.template GetRaw<Tspan>(dst); |
| 203 |
} |
| 204 |
|
| 205 |
template <typename Tspan, typename TFileCursor> |
| 206 |
Tspan ReadRaw(TFileCursor & f, Tspan dst) { |
| 207 |
return f.template ReadRaw<Tspan>(dst); |
| 208 |
} |
| 209 |
|
| 210 |
template <typename TFileCursor> |
| 211 |
std::vector<std::byte> GetRawDataAsByteVector(const TFileCursor & f) { |
| 212 |
return f.GetRawDataAsByteVector(); |
| 213 |
} |
| 214 |
|
| 215 |
template <typename TFileCursor> |
| 216 |
std::vector<std::byte> ReadRawDataAsByteVector(TFileCursor & f) { |
| 217 |
return f.ReadRawDataAsByteVector(); |
| 218 |
} |
| 219 |
|
| 220 |
template <typename TFileCursor> |
| 221 |
std::vector<std::byte> GetRawDataAsByteVector(const TFileCursor & f, std::size_t size) { |
| 222 |
return f.GetRawDataAsByteVector(size); |
| 223 |
} |
| 224 |
|
| 225 |
template <typename TFileCursor> |
| 226 |
std::vector<std::byte> ReadRawDataAsByteVector(TFileCursor & f, std::size_t size) { |
| 227 |
return f.ReadRawDataAsByteVector(size); |
| 228 |
} |
| 229 |
|
| 230 |
// TFileCursor members end |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
// Read a "T" object from the stream. |
| 235 |
// If not enough bytes can be read, false is returned. |
| 236 |
// If successful, the file cursor is advanced by the size of "T". |
| 237 |
template <typename T, typename TFileCursor> |
| 238 |
bool Read(TFileCursor & f, T & target) { |
| 239 |
// cppcheck false-positive |
| 240 |
// cppcheck-suppress uninitvar |
| 241 |
mpt::byte_span dst = mpt::as_raw_memory(target); |
| 242 |
if (dst.size() != f.GetRaw(dst).size()) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
f.Skip(dst.size()); |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
// Read an array of binary-safe T values. |
| 250 |
// If successful, the file cursor is advanced by the size of the array. |
| 251 |
// Otherwise, the target is zeroed. |
| 252 |
template <typename T, std::size_t destSize, typename TFileCursor> |
| 253 |
bool ReadArray(TFileCursor & f, T (&destArray)[destSize]) { |
| 254 |
static_assert(mpt::is_binary_safe<T>::value); |
| 255 |
if (!f.CanRead(sizeof(destArray))) { |
| 256 |
mpt::reset(destArray); |
| 257 |
return false; |
| 258 |
} |
| 259 |
f.ReadRaw(mpt::as_raw_memory(destArray)); |
| 260 |
return true; |
| 261 |
} |
| 262 |
|
| 263 |
// Read an array of binary-safe T values. |
| 264 |
// If successful, the file cursor is advanced by the size of the array. |
| 265 |
// Otherwise, the target is zeroed. |
| 266 |
template <typename T, std::size_t destSize, typename TFileCursor> |
| 267 |
bool ReadArray(TFileCursor & f, std::array<T, destSize> & destArray) { |
| 268 |
static_assert(mpt::is_binary_safe<T>::value); |
| 269 |
if (!f.CanRead(sizeof(destArray))) { |
| 270 |
mpt::reset(destArray); |
| 271 |
return false; |
| 272 |
} |
| 273 |
f.ReadRaw(mpt::as_raw_memory(destArray)); |
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
// Read destSize elements of binary-safe type T into a vector. |
| 278 |
// If successful, the file cursor is advanced by the size of the vector. |
| 279 |
// Otherwise, the vector is resized to destSize, but possibly existing contents are not cleared. |
| 280 |
template <typename T, typename TFileCursor> |
| 281 |
bool ReadVector(TFileCursor & f, std::vector<T> & destVector, size_t destSize) { |
| 282 |
static_assert(mpt::is_binary_safe<T>::value); |
| 283 |
destVector.resize(destSize); |
| 284 |
if (!f.CanRead(sizeof(T) * destSize)) { |
| 285 |
return false; |
| 286 |
} |
| 287 |
f.ReadRaw(mpt::as_raw_memory(destVector)); |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
template <typename T, std::size_t destSize, typename TFileCursor> |
| 292 |
std::array<T, destSize> ReadArray(TFileCursor & f) { |
| 293 |
std::array<T, destSize> destArray; |
| 294 |
ReadArray(f, destArray); |
| 295 |
return destArray; |
| 296 |
} |
| 297 |
|
| 298 |
// Read some kind of integer in little-endian format. |
| 299 |
// If successful, the file cursor is advanced by the size of the integer. |
| 300 |
template <typename T, typename TFileCursor> |
| 301 |
T ReadIntLE(TFileCursor & f) { |
| 302 |
static_assert(std::numeric_limits<T>::is_integer == true, "Target type is a not an integer"); |
| 303 |
typename mpt::make_le<T>::type target; |
| 304 |
if (!FileReader::Read(f, target)) { |
| 305 |
return 0; |
| 306 |
} |
| 307 |
return target; |
| 308 |
} |
| 309 |
|
| 310 |
// Read some kind of integer in big-endian format. |
| 311 |
// If successful, the file cursor is advanced by the size of the integer. |
| 312 |
template <typename T, typename TFileCursor> |
| 313 |
T ReadIntBE(TFileCursor & f) { |
| 314 |
static_assert(std::numeric_limits<T>::is_integer == true, "Target type is a not an integer"); |
| 315 |
typename mpt::make_be<T>::type target; |
| 316 |
if (!FileReader::Read(f, target)) { |
| 317 |
return 0; |
| 318 |
} |
| 319 |
return target; |
| 320 |
} |
| 321 |
|
| 322 |
// Read a integer in little-endian format which has some of its higher bytes not stored in file. |
| 323 |
// If successful, the file cursor is advanced by the given size. |
| 324 |
template <typename T, typename TFileCursor> |
| 325 |
T ReadTruncatedIntLE(TFileCursor & f, std::size_t size) { |
| 326 |
static_assert(std::numeric_limits<T>::is_integer == true, "Target type is a not an integer"); |
| 327 |
assert(sizeof(T) >= size); |
| 328 |
if (size == 0) { |
| 329 |
return 0; |
| 330 |
} |
| 331 |
if (!f.CanRead(size)) { |
| 332 |
return 0; |
| 333 |
} |
| 334 |
uint8 buf[sizeof(T)]; |
| 335 |
bool negative = false; |
| 336 |
for (std::size_t i = 0; i < sizeof(T); ++i) { |
| 337 |
uint8 byte = 0; |
| 338 |
if (i < size) { |
| 339 |
FileReader::Read(f, byte); |
| 340 |
negative = std::numeric_limits<T>::is_signed && ((byte & 0x80) != 0x00); |
| 341 |
} else { |
| 342 |
// sign or zero extend |
| 343 |
byte = negative ? 0xff : 0x00; |
| 344 |
} |
| 345 |
buf[i] = byte; |
| 346 |
} |
| 347 |
return mpt::bit_cast<typename mpt::make_le<T>::type>(buf); |
| 348 |
} |
| 349 |
|
| 350 |
// Read a supplied-size little endian integer to a fixed size variable. |
| 351 |
// The data is properly sign-extended when fewer bytes are stored. |
| 352 |
// If more bytes are stored, higher order bytes are silently ignored. |
| 353 |
// If successful, the file cursor is advanced by the given size. |
| 354 |
template <typename T, typename TFileCursor> |
| 355 |
T ReadSizedIntLE(TFileCursor & f, std::size_t size) { |
| 356 |
static_assert(std::numeric_limits<T>::is_integer == true, "Target type is a not an integer"); |
| 357 |
if (size == 0) { |
| 358 |
return 0; |
| 359 |
} |
| 360 |
if (!f.CanRead(size)) { |
| 361 |
return 0; |
| 362 |
} |
| 363 |
if (size < sizeof(T)) { |
| 364 |
return FileReader::ReadTruncatedIntLE<T>(f, size); |
| 365 |
} |
| 366 |
T retval = FileReader::ReadIntLE<T>(f); |
| 367 |
f.Skip(size - sizeof(T)); |
| 368 |
return retval; |
| 369 |
} |
| 370 |
|
| 371 |
// Read unsigned 32-Bit integer in little-endian format. |
| 372 |
// If successful, the file cursor is advanced by the size of the integer. |
| 373 |
template <typename TFileCursor> |
| 374 |
uint32 ReadUint32LE(TFileCursor & f) { |
| 375 |
return FileReader::ReadIntLE<uint32>(f); |
| 376 |
} |
| 377 |
|
| 378 |
// Read unsigned 32-Bit integer in big-endian format. |
| 379 |
// If successful, the file cursor is advanced by the size of the integer. |
| 380 |
template <typename TFileCursor> |
| 381 |
uint32 ReadUint32BE(TFileCursor & f) { |
| 382 |
return FileReader::ReadIntBE<uint32>(f); |
| 383 |
} |
| 384 |
|
| 385 |
// Read signed 32-Bit integer in little-endian format. |
| 386 |
// If successful, the file cursor is advanced by the size of the integer. |
| 387 |
template <typename TFileCursor> |
| 388 |
int32 ReadInt32LE(TFileCursor & f) { |
| 389 |
return FileReader::ReadIntLE<int32>(f); |
| 390 |
} |
| 391 |
|
| 392 |
// Read signed 32-Bit integer in big-endian format. |
| 393 |
// If successful, the file cursor is advanced by the size of the integer. |
| 394 |
template <typename TFileCursor> |
| 395 |
int32 ReadInt32BE(TFileCursor & f) { |
| 396 |
return FileReader::ReadIntBE<int32>(f); |
| 397 |
} |
| 398 |
|
| 399 |
// Read unsigned 24-Bit integer in little-endian format. |
| 400 |
// If successful, the file cursor is advanced by the size of the integer. |
| 401 |
template <typename TFileCursor> |
| 402 |
uint32 ReadUint24LE(TFileCursor & f) { |
| 403 |
const auto arr = FileReader::ReadArray<uint8, 3>(f); |
| 404 |
return arr[0] | (arr[1] << 8) | (arr[2] << 16); |
| 405 |
} |
| 406 |
|
| 407 |
// Read unsigned 24-Bit integer in big-endian format. |
| 408 |
// If successful, the file cursor is advanced by the size of the integer. |
| 409 |
template <typename TFileCursor> |
| 410 |
uint32 ReadUint24BE(TFileCursor & f) { |
| 411 |
const auto arr = FileReader::ReadArray<uint8, 3>(f); |
| 412 |
return (arr[0] << 16) | (arr[1] << 8) | arr[2]; |
| 413 |
} |
| 414 |
|
| 415 |
// Read unsigned 16-Bit integer in little-endian format. |
| 416 |
// If successful, the file cursor is advanced by the size of the integer. |
| 417 |
template <typename TFileCursor> |
| 418 |
uint16 ReadUint16LE(TFileCursor & f) { |
| 419 |
return FileReader::ReadIntLE<uint16>(f); |
| 420 |
} |
| 421 |
|
| 422 |
// Read unsigned 16-Bit integer in big-endian format. |
| 423 |
// If successful, the file cursor is advanced by the size of the integer. |
| 424 |
template <typename TFileCursor> |
| 425 |
uint16 ReadUint16BE(TFileCursor & f) { |
| 426 |
return FileReader::ReadIntBE<uint16>(f); |
| 427 |
} |
| 428 |
|
| 429 |
// Read signed 16-Bit integer in little-endian format. |
| 430 |
// If successful, the file cursor is advanced by the size of the integer. |
| 431 |
template <typename TFileCursor> |
| 432 |
int16 ReadInt16LE(TFileCursor & f) { |
| 433 |
return FileReader::ReadIntLE<int16>(f); |
| 434 |
} |
| 435 |
|
| 436 |
// Read signed 16-Bit integer in big-endian format. |
| 437 |
// If successful, the file cursor is advanced by the size of the integer. |
| 438 |
template <typename TFileCursor> |
| 439 |
int16 ReadInt16BE(TFileCursor & f) { |
| 440 |
return FileReader::ReadIntBE<int16>(f); |
| 441 |
} |
| 442 |
|
| 443 |
// Read a single 8bit character. |
| 444 |
// If successful, the file cursor is advanced by the size of the integer. |
| 445 |
template <typename TFileCursor> |
| 446 |
char ReadChar(TFileCursor & f) { |
| 447 |
char target; |
| 448 |
if (!FileReader::Read(f, target)) { |
| 449 |
return 0; |
| 450 |
} |
| 451 |
return target; |
| 452 |
} |
| 453 |
|
| 454 |
// Read unsigned 8-Bit integer. |
| 455 |
// If successful, the file cursor is advanced by the size of the integer. |
| 456 |
template <typename TFileCursor> |
| 457 |
uint8 ReadUint8(TFileCursor & f) { |
| 458 |
uint8 target; |
| 459 |
if (!FileReader::Read(f, target)) { |
| 460 |
return 0; |
| 461 |
} |
| 462 |
return target; |
| 463 |
} |
| 464 |
|
| 465 |
// Read signed 8-Bit integer. If successful, the file cursor is advanced by the size of the integer. |
| 466 |
template <typename TFileCursor> |
| 467 |
int8 ReadInt8(TFileCursor & f) { |
| 468 |
int8 target; |
| 469 |
if (!FileReader::Read(f, target)) { |
| 470 |
return 0; |
| 471 |
} |
| 472 |
return target; |
| 473 |
} |
| 474 |
|
| 475 |
// Read 32-Bit float in little-endian format. |
| 476 |
// If successful, the file cursor is advanced by the size of the float. |
| 477 |
template <typename TFileCursor> |
| 478 |
float ReadFloatLE(TFileCursor & f) { |
| 479 |
IEEE754binary32LE target; |
| 480 |
if (!FileReader::Read(f, target)) { |
| 481 |
return 0.0f; |
| 482 |
} |
| 483 |
return target; |
| 484 |
} |
| 485 |
|
| 486 |
// Read 32-Bit float in big-endian format. |
| 487 |
// If successful, the file cursor is advanced by the size of the float. |
| 488 |
template <typename TFileCursor> |
| 489 |
float ReadFloatBE(TFileCursor & f) { |
| 490 |
IEEE754binary32BE target; |
| 491 |
if (!FileReader::Read(f, target)) { |
| 492 |
return 0.0f; |
| 493 |
} |
| 494 |
return target; |
| 495 |
} |
| 496 |
|
| 497 |
// Read 64-Bit float in little-endian format. |
| 498 |
// If successful, the file cursor is advanced by the size of the float. |
| 499 |
template <typename TFileCursor> |
| 500 |
double ReadDoubleLE(TFileCursor & f) { |
| 501 |
IEEE754binary64LE target; |
| 502 |
if (!FileReader::Read(f, target)) { |
| 503 |
return 0.0; |
| 504 |
} |
| 505 |
return target; |
| 506 |
} |
| 507 |
|
| 508 |
// Read 64-Bit float in big-endian format. |
| 509 |
// If successful, the file cursor is advanced by the size of the float. |
| 510 |
template <typename TFileCursor> |
| 511 |
double ReadDoubleBE(TFileCursor & f) { |
| 512 |
IEEE754binary64BE target; |
| 513 |
if (!FileReader::Read(f, target)) { |
| 514 |
return 0.0; |
| 515 |
} |
| 516 |
return target; |
| 517 |
} |
| 518 |
|
| 519 |
// Read a struct. |
| 520 |
// If successful, the file cursor is advanced by the size of the struct. Otherwise, the target is zeroed. |
| 521 |
template <typename T, typename TFileCursor> |
| 522 |
bool ReadStruct(TFileCursor & f, T & target) { |
| 523 |
static_assert(mpt::is_binary_safe<T>::value); |
| 524 |
if (!FileReader::Read(f, target)) { |
| 525 |
mpt::reset(target); |
| 526 |
return false; |
| 527 |
} |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
// Allow to read a struct partially (if there's less memory available than the struct's size, fill it up with zeros). |
| 532 |
// The file cursor is advanced by "partialSize" bytes. |
| 533 |
template <typename T, typename TFileCursor> |
| 534 |
std::size_t ReadStructPartial(TFileCursor & f, T & target, std::size_t partialSize = sizeof(T)) { |
| 535 |
static_assert(mpt::is_binary_safe<T>::value); |
| 536 |
std::size_t copyBytes = std::min(partialSize, sizeof(T)); |
| 537 |
if (!f.CanRead(copyBytes)) { |
| 538 |
copyBytes = mpt::saturate_cast<std::size_t>(f.BytesLeft()); |
| 539 |
} |
| 540 |
f.GetRaw(mpt::span(mpt::as_raw_memory(target).data(), copyBytes)); |
| 541 |
std::memset(mpt::as_raw_memory(target).data() + copyBytes, 0, sizeof(target) - copyBytes); |
| 542 |
f.Skip(partialSize); |
| 543 |
return copyBytes; |
| 544 |
} |
| 545 |
|
| 546 |
// Read a null-terminated string into a std::string |
| 547 |
template <typename TFileCursor> |
| 548 |
bool ReadNullString(TFileCursor & f, std::string & dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max()) { |
| 549 |
dest.clear(); |
| 550 |
if (!f.CanRead(1)) { |
| 551 |
return false; |
| 552 |
} |
| 553 |
char buffer[mpt::IO::BUFFERSIZE_MINUSCULE]; |
| 554 |
std::size_t avail = 0; |
| 555 |
while ((avail = std::min(f.GetRaw(mpt::as_span(buffer)).size(), maxLength - dest.length())) != 0) { |
| 556 |
auto end = std::find(buffer, buffer + avail, '\0'); |
| 557 |
dest.insert(dest.end(), buffer, end); |
| 558 |
f.Skip(end - buffer); |
| 559 |
if (end < buffer + avail) { |
| 560 |
// Found null char |
| 561 |
f.Skip(1); |
| 562 |
break; |
| 563 |
} |
| 564 |
} |
| 565 |
return dest.length() != 0; |
| 566 |
} |
| 567 |
|
| 568 |
// Read a string up to the next line terminator into a std::string |
| 569 |
template <typename TFileCursor> |
| 570 |
bool ReadLine(TFileCursor & f, std::string & dest, const std::size_t maxLength = std::numeric_limits<std::size_t>::max()) { |
| 571 |
dest.clear(); |
| 572 |
if (!f.CanRead(1)) { |
| 573 |
return false; |
| 574 |
} |
| 575 |
char buffer[mpt::IO::BUFFERSIZE_MINUSCULE]; |
| 576 |
char c = '\0'; |
| 577 |
std::size_t avail = 0; |
| 578 |
while ((avail = std::min(f.GetRaw(mpt::as_span(buffer)).size(), maxLength - dest.length())) != 0) { |
| 579 |
auto end = std::find_if(buffer, buffer + avail, mpt::is_any_line_ending<char>); |
| 580 |
dest.insert(dest.end(), buffer, end); |
| 581 |
f.Skip(end - buffer); |
| 582 |
if (end < buffer + avail) { |
| 583 |
// Found line ending |
| 584 |
f.Skip(1); |
| 585 |
// Handle CRLF line ending |
| 586 |
if (*end == '\r') { |
| 587 |
if (FileReader::Read(f, c) && c != '\n') { |
| 588 |
f.SkipBack(1); |
| 589 |
} |
| 590 |
} |
| 591 |
break; |
| 592 |
} |
| 593 |
} |
| 594 |
return true; |
| 595 |
} |
| 596 |
|
| 597 |
// Compare a magic string with the current stream position. |
| 598 |
// Returns true if they are identical and advances the file cursor by the the length of the "magic" string. |
| 599 |
// Returns false if the string could not be found. The file cursor is not advanced in this case. |
| 600 |
template <size_t N, typename TFileCursor> |
| 601 |
bool ReadMagic(TFileCursor & f, const char (&magic)[N]) { |
| 602 |
assert(magic[N - 1] == '\0'); |
| 603 |
for (std::size_t i = 0; i < N - 1; ++i) { |
| 604 |
assert(magic[i] != '\0'); |
| 605 |
} |
| 606 |
constexpr std::size_t magicLength = N - 1; |
| 607 |
std::byte buffer[magicLength] = {}; |
| 608 |
if (f.GetRaw(mpt::span(buffer, magicLength)).size() != magicLength) { |
| 609 |
return false; |
| 610 |
} |
| 611 |
if (std::memcmp(buffer, magic, magicLength)) { |
| 612 |
return false; |
| 613 |
} |
| 614 |
f.Skip(magicLength); |
| 615 |
return true; |
| 616 |
} |
| 617 |
|
| 618 |
// Read variable-length unsigned integer (as found in MIDI files). |
| 619 |
// If successful, the file cursor is advanced by the size of the integer and true is returned. |
| 620 |
// False is returned if not enough bytes were left to finish reading of the integer or if an overflow happened (source doesn't fit into target integer). |
| 621 |
// In case of an overflow, the target is also set to the maximum value supported by its data type. |
| 622 |
template <typename T, typename TFileCursor> |
| 623 |
bool ReadVarInt(TFileCursor & f, T & target) { |
| 624 |
static_assert(std::numeric_limits<T>::is_integer == true && std::numeric_limits<T>::is_signed == false, "Target type is not an unsigned integer"); |
| 625 |
|
| 626 |
if (f.NoBytesLeft()) { |
| 627 |
target = 0; |
| 628 |
return false; |
| 629 |
} |
| 630 |
|
| 631 |
std::byte bytes[16]; // More than enough for any valid VarInt |
| 632 |
std::size_t avail = f.GetRaw(mpt::as_span(bytes)).size(); |
| 633 |
std::size_t readPos = 1; |
| 634 |
|
| 635 |
uint8 b = mpt::byte_cast<uint8>(bytes[0]); |
| 636 |
target = (b & 0x7F); |
| 637 |
std::size_t writtenBits = static_cast<std::size_t>(mpt::bit_width(target)); // Bits used in the most significant byte |
| 638 |
|
| 639 |
while (readPos < avail && (b & 0x80) != 0) { |
| 640 |
b = mpt::byte_cast<uint8>(bytes[readPos++]); |
| 641 |
target <<= 7; |
| 642 |
target |= (b & 0x7F); |
| 643 |
writtenBits += 7; |
| 644 |
if (readPos == avail) { |
| 645 |
f.Skip(readPos); |
| 646 |
avail = f.GetRaw(mpt::as_span(bytes)).size(); |
| 647 |
readPos = 0; |
| 648 |
} |
| 649 |
} |
| 650 |
f.Skip(readPos); |
| 651 |
|
| 652 |
if (writtenBits > sizeof(target) * 8u) { |
| 653 |
// Overflow |
| 654 |
target = std::numeric_limits<T>::max(); |
| 655 |
return false; |
| 656 |
} else if ((b & 0x80) != 0) { |
| 657 |
// Reached EOF |
| 658 |
return false; |
| 659 |
} |
| 660 |
return true; |
| 661 |
} |
| 662 |
|
| 663 |
template <typename Tid, typename Tsize> |
| 664 |
struct ChunkHeader { |
| 665 |
using id_type = Tid; |
| 666 |
using size_type = Tsize; |
| 667 |
friend constexpr bool declare_binary_safe(const ChunkHeader &) noexcept { |
| 668 |
return true; |
| 669 |
} |
| 670 |
id_type id{}; |
| 671 |
size_type size{}; |
| 672 |
id_type GetID() const { |
| 673 |
return id; |
| 674 |
} |
| 675 |
size_type GetLength() const { |
| 676 |
return size; |
| 677 |
} |
| 678 |
}; |
| 679 |
|
| 680 |
template <typename TChunkHeader, typename TFileCursor> |
| 681 |
struct Chunk { |
| 682 |
TChunkHeader header; |
| 683 |
TFileCursor data; |
| 684 |
TChunkHeader GetHeader() const { |
| 685 |
return header; |
| 686 |
} |
| 687 |
TFileCursor GetData() const { |
| 688 |
return data; |
| 689 |
} |
| 690 |
}; |
| 691 |
|
| 692 |
template <typename TChunkHeader, typename TFileCursor> |
| 693 |
struct ChunkList { |
| 694 |
|
| 695 |
using id_type = decltype(TChunkHeader().GetID()); |
| 696 |
using size_type = decltype(TChunkHeader().GetLength()); |
| 697 |
|
| 698 |
std::vector<Chunk<TChunkHeader, TFileCursor>> chunks; |
| 699 |
|
| 700 |
// Check if the list contains a given chunk. |
| 701 |
bool ChunkExists(id_type id) const { |
| 702 |
return std::find_if(chunks.begin(), chunks.end(), [id](const Chunk<TChunkHeader, TFileCursor> & chunk) { return chunk.GetHeader().GetID() == id; }) != chunks.end(); |
| 703 |
} |
| 704 |
|
| 705 |
// Retrieve the first chunk with a given ID. |
| 706 |
TFileCursor GetChunk(id_type id) const { |
| 707 |
auto chunk = std::find_if(chunks.begin(), chunks.end(), [id](const Chunk<TChunkHeader, TFileCursor> & chunk) { return chunk.GetHeader().GetID() == id; }); |
| 708 |
if (chunk == chunks.end()) { |
| 709 |
return TFileCursor(); |
| 710 |
} |
| 711 |
return chunk->GetData(); |
| 712 |
} |
| 713 |
|
| 714 |
// Retrieve all chunks with a given ID. |
| 715 |
std::vector<TFileCursor> GetAllChunks(id_type id) const { |
| 716 |
std::vector<TFileCursor> result; |
| 717 |
for (const auto & chunk : chunks) { |
| 718 |
if (chunk.GetHeader().GetID() == id) { |
| 719 |
result.push_back(chunk.GetData()); |
| 720 |
} |
| 721 |
} |
| 722 |
return result; |
| 723 |
} |
| 724 |
}; |
| 725 |
|
| 726 |
// Read a single "TChunkHeader" chunk. |
| 727 |
// T is required to have the methods GetID() and GetLength(). |
| 728 |
// GetLength() must return the chunk size in bytes, and GetID() the chunk ID. |
| 729 |
template <typename TChunkHeader, typename TFileCursor> |
| 730 |
Chunk<TChunkHeader, TFileCursor> ReadNextChunk(TFileCursor & f, typename TFileCursor::pos_type alignment) { |
| 731 |
Chunk<TChunkHeader, TFileCursor> result; |
| 732 |
if (!FileReader::Read(f, result.header)) { |
| 733 |
return Chunk<TChunkHeader, TFileCursor>(); |
| 734 |
} |
| 735 |
typename TFileCursor::pos_type dataSize = result.header.GetLength(); |
| 736 |
result.data = f.ReadChunk(dataSize); |
| 737 |
if (alignment > 1) { |
| 738 |
if ((dataSize % alignment) != 0) { |
| 739 |
f.Skip(alignment - (dataSize % alignment)); |
| 740 |
} |
| 741 |
} |
| 742 |
return result; |
| 743 |
} |
| 744 |
|
| 745 |
// Read a series of "TChunkHeader" chunks until the end of file is reached. |
| 746 |
// T is required to have the methods GetID() and GetLength(). |
| 747 |
// GetLength() must return the chunk size in bytes, and GetID() the chunk ID. |
| 748 |
template <typename TChunkHeader, typename TFileCursor> |
| 749 |
ChunkList<TChunkHeader, TFileCursor> ReadChunks(TFileCursor & f, typename TFileCursor::pos_type alignment) { |
| 750 |
ChunkList<TChunkHeader, TFileCursor> result; |
| 751 |
while (f.CanRead(sizeof(TChunkHeader))) { |
| 752 |
result.chunks.push_back(FileReader::ReadNextChunk<TChunkHeader, TFileCursor>(f, alignment)); |
| 753 |
} |
| 754 |
return result; |
| 755 |
} |
| 756 |
|
| 757 |
// Read a series of "TChunkHeader" chunks until a given chunk ID is found. |
| 758 |
// T is required to have the methods GetID() and GetLength(). |
| 759 |
// GetLength() must return the chunk size in bytes, and GetID() the chunk ID. |
| 760 |
template <typename TChunkHeader, typename TFileCursor> |
| 761 |
ChunkList<TChunkHeader, TFileCursor> ReadChunksUntil(TFileCursor & f, typename TFileCursor::pos_type alignment, decltype(TChunkHeader().GetID()) lastID) { |
| 762 |
ChunkList<TChunkHeader, TFileCursor> result; |
| 763 |
while (f.CanRead(sizeof(TChunkHeader))) { |
| 764 |
result.chunks.push_back(FileReader::ReadNextChunk<TChunkHeader, TFileCursor>(f, alignment)); |
| 765 |
if (result.chunks.back().GetHeader().GetID() == lastID) { |
| 766 |
break; |
| 767 |
} |
| 768 |
} |
| 769 |
return result; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
} // namespace FileReader |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
} // namespace IO |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
namespace FR = mpt::IO::FileReader; |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
} // namespace MPT_INLINE_NS |
| 787 |
} // namespace mpt |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
#endif // MPT_IO_READ_FILEREADER_HPP |