| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_IO_HPP |
| 4 |
#define MPT_IO_IO_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/array.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/span.hpp" |
| 14 |
#include "mpt/endian/integer.hpp" |
| 15 |
#include "mpt/endian/type_traits.hpp" |
| 16 |
#include "mpt/io/base.hpp" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <limits> |
| 20 |
#include <string> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
#include <cassert> |
| 24 |
#include <cstddef> |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
namespace mpt { |
| 29 |
inline namespace MPT_INLINE_NS { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
namespace IO { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
template <typename Tbyte, typename Tfile> |
| 38 |
inline mpt::byte_span ReadRaw(Tfile & f, Tbyte * data, std::size_t size) { |
| 39 |
return mpt::IO::ReadRawImpl(f, mpt::as_span(mpt::byte_cast<std::byte *>(data), size)); |
| 40 |
} |
| 41 |
|
| 42 |
template <typename Tbyte, typename Tfile> |
| 43 |
inline mpt::byte_span ReadRaw(Tfile & f, mpt::span<Tbyte> data) { |
| 44 |
return mpt::IO::ReadRawImpl(f, mpt::byte_cast<mpt::byte_span>(data)); |
| 45 |
} |
| 46 |
|
| 47 |
template <typename Tbyte, typename Tfile> |
| 48 |
inline bool WriteRaw(Tfile & f, const Tbyte * data, std::size_t size) { |
| 49 |
return mpt::IO::WriteRawImpl(f, mpt::as_span(mpt::byte_cast<const std::byte *>(data), size)); |
| 50 |
} |
| 51 |
|
| 52 |
template <typename Tbyte, typename Tfile> |
| 53 |
inline bool WriteRaw(Tfile & f, mpt::span<Tbyte> data) { |
| 54 |
return mpt::IO::WriteRawImpl(f, mpt::byte_cast<mpt::const_byte_span>(data)); |
| 55 |
} |
| 56 |
|
| 57 |
template <typename Tbinary, typename Tfile> |
| 58 |
inline bool Read(Tfile & f, Tbinary & v) { |
| 59 |
return mpt::IO::ReadRaw(f, mpt::as_raw_memory(v)).size() == mpt::as_raw_memory(v).size(); |
| 60 |
} |
| 61 |
|
| 62 |
template <typename Tbinary, typename Tfile> |
| 63 |
inline bool Write(Tfile & f, const Tbinary & v) { |
| 64 |
return mpt::IO::WriteRaw(f, mpt::as_raw_memory(v)); |
| 65 |
} |
| 66 |
|
| 67 |
template <typename Tbinary, typename Tfile> |
| 68 |
inline bool Write(Tfile & f, const std::vector<Tbinary> & v) { |
| 69 |
static_assert(mpt::is_binary_safe<Tbinary>::value); |
| 70 |
return mpt::IO::WriteRaw(f, mpt::as_raw_memory(v)); |
| 71 |
} |
| 72 |
|
| 73 |
template <typename T, typename Tfile> |
| 74 |
inline bool WritePartial(Tfile & f, const T & v, std::size_t size = sizeof(T)) { |
| 75 |
assert(size <= sizeof(T)); |
| 76 |
return mpt::IO::WriteRaw(f, mpt::as_span(mpt::as_raw_memory(v).data(), size)); |
| 77 |
} |
| 78 |
|
| 79 |
template <typename Tfile> |
| 80 |
inline bool ReadByte(Tfile & f, std::byte & v) { |
| 81 |
bool result = false; |
| 82 |
std::byte byte = mpt::as_byte(0); |
| 83 |
const std::size_t readResult = mpt::IO::ReadRaw(f, &byte, sizeof(std::byte)).size(); |
| 84 |
result = (readResult == sizeof(std::byte)); |
| 85 |
v = byte; |
| 86 |
return result; |
| 87 |
} |
| 88 |
|
| 89 |
template <typename T, typename Tfile> |
| 90 |
inline bool ReadBinaryTruncatedLE(Tfile & f, T & v, std::size_t size) { |
| 91 |
bool result = false; |
| 92 |
static_assert(std::numeric_limits<T>::is_integer); |
| 93 |
std::array<uint8, sizeof(T)> bytes = mpt::init_array<uint8, sizeof(T)>(uint8{0}); |
| 94 |
const std::size_t readResult = mpt::IO::ReadRaw(f, bytes.data(), std::min(size, sizeof(T))).size(); |
| 95 |
result = (readResult == std::min(size, sizeof(T))); |
| 96 |
v = mpt::bit_cast<typename mpt::make_le<T>::type>(bytes); |
| 97 |
return result; |
| 98 |
} |
| 99 |
|
| 100 |
template <typename T, typename Tfile> |
| 101 |
inline bool ReadIntLE(Tfile & f, T & v) { |
| 102 |
bool result = false; |
| 103 |
static_assert(std::numeric_limits<T>::is_integer); |
| 104 |
std::array<uint8, sizeof(T)> bytes = mpt::init_array<uint8, sizeof(T)>(uint8{0}); |
| 105 |
const std::size_t readResult = mpt::IO::ReadRaw(f, mpt::as_span(bytes)).size(); |
| 106 |
result = (readResult == sizeof(T)); |
| 107 |
v = mpt::bit_cast<typename mpt::make_le<T>::type>(bytes); |
| 108 |
return result; |
| 109 |
} |
| 110 |
|
| 111 |
template <typename T, typename Tfile> |
| 112 |
inline bool ReadIntBE(Tfile & f, T & v) { |
| 113 |
bool result = false; |
| 114 |
static_assert(std::numeric_limits<T>::is_integer); |
| 115 |
std::array<uint8, sizeof(T)> bytes = mpt::init_array<uint8, sizeof(T)>(uint8{0}); |
| 116 |
const std::size_t readResult = mpt::IO::ReadRaw(f, mpt::as_span(bytes)).size(); |
| 117 |
result = (readResult == sizeof(T)); |
| 118 |
v = mpt::bit_cast<typename mpt::make_be<T>::type>(bytes); |
| 119 |
return result; |
| 120 |
} |
| 121 |
|
| 122 |
template <typename Tfile> |
| 123 |
inline bool ReadAdaptiveInt16LE(Tfile & f, uint16 & v) { |
| 124 |
bool result = true; |
| 125 |
uint8 byte = 0; |
| 126 |
std::size_t additionalBytes = 0; |
| 127 |
v = 0; |
| 128 |
byte = 0; |
| 129 |
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) { |
| 130 |
result = false; |
| 131 |
} |
| 132 |
additionalBytes = (byte & 0x01); |
| 133 |
v = byte >> 1; |
| 134 |
for (std::size_t i = 0; i < additionalBytes; ++i) { |
| 135 |
byte = 0; |
| 136 |
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) { |
| 137 |
result = false; |
| 138 |
} |
| 139 |
v |= (static_cast<uint16>(byte) << (((i + 1) * 8) - 1)); |
| 140 |
} |
| 141 |
return result; |
| 142 |
} |
| 143 |
|
| 144 |
template <typename Tfile> |
| 145 |
inline bool ReadAdaptiveInt32LE(Tfile & f, uint32 & v) { |
| 146 |
bool result = true; |
| 147 |
uint8 byte = 0; |
| 148 |
std::size_t additionalBytes = 0; |
| 149 |
v = 0; |
| 150 |
byte = 0; |
| 151 |
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) { |
| 152 |
result = false; |
| 153 |
} |
| 154 |
additionalBytes = (byte & 0x03); |
| 155 |
v = byte >> 2; |
| 156 |
for (std::size_t i = 0; i < additionalBytes; ++i) { |
| 157 |
byte = 0; |
| 158 |
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) { |
| 159 |
result = false; |
| 160 |
} |
| 161 |
v |= (static_cast<uint32>(byte) << (((i + 1) * 8) - 2)); |
| 162 |
} |
| 163 |
return result; |
| 164 |
} |
| 165 |
|
| 166 |
template <typename Tfile> |
| 167 |
inline bool ReadAdaptiveInt64LE(Tfile & f, uint64 & v) { |
| 168 |
bool result = true; |
| 169 |
uint8 byte = 0; |
| 170 |
std::size_t additionalBytes = 0; |
| 171 |
v = 0; |
| 172 |
byte = 0; |
| 173 |
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) { |
| 174 |
result = false; |
| 175 |
} |
| 176 |
additionalBytes = (1 << (byte & 0x03)) - 1; |
| 177 |
v = byte >> 2; |
| 178 |
for (std::size_t i = 0; i < additionalBytes; ++i) { |
| 179 |
byte = 0; |
| 180 |
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) { |
| 181 |
result = false; |
| 182 |
} |
| 183 |
v |= (static_cast<uint64>(byte) << (((i + 1) * 8) - 2)); |
| 184 |
} |
| 185 |
return result; |
| 186 |
} |
| 187 |
|
| 188 |
template <typename Tsize, typename Tfile> |
| 189 |
inline bool ReadSizedStringLE(Tfile & f, std::string & str, Tsize maxSize = std::numeric_limits<Tsize>::max()) { |
| 190 |
static_assert(std::numeric_limits<Tsize>::is_integer); |
| 191 |
str.clear(); |
| 192 |
Tsize size = 0; |
| 193 |
if (!mpt::IO::ReadIntLE(f, size)) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
if (size > maxSize) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
for (Tsize i = 0; i != size; ++i) { |
| 200 |
char c = '\0'; |
| 201 |
if (!mpt::IO::ReadIntLE(f, c)) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
str.push_back(c); |
| 205 |
} |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
template <typename T, typename Tfile> |
| 212 |
inline bool WriteIntLE(Tfile & f, const T v) { |
| 213 |
static_assert(std::numeric_limits<T>::is_integer); |
| 214 |
return mpt::IO::Write(f, mpt::as_le(v)); |
| 215 |
} |
| 216 |
|
| 217 |
template <typename T, typename Tfile> |
| 218 |
inline bool WriteIntBE(Tfile & f, const T v) { |
| 219 |
static_assert(std::numeric_limits<T>::is_integer); |
| 220 |
return mpt::IO::Write(f, mpt::as_be(v)); |
| 221 |
} |
| 222 |
|
| 223 |
template <typename Tfile> |
| 224 |
inline bool WriteAdaptiveInt16LE(Tfile & f, const uint16 v, std::size_t fixedSize = 0) { |
| 225 |
std::size_t minSize = fixedSize; |
| 226 |
std::size_t maxSize = fixedSize; |
| 227 |
assert(minSize == 0 || minSize == 1 || minSize == 2); |
| 228 |
assert(maxSize == 0 || maxSize == 1 || maxSize == 2); |
| 229 |
assert(maxSize == 0 || maxSize >= minSize); |
| 230 |
if (maxSize == 0) { |
| 231 |
maxSize = 2; |
| 232 |
} |
| 233 |
if (v < 0x80 && minSize <= 1 && 1 <= maxSize) { |
| 234 |
return mpt::IO::WriteIntLE<uint8>(f, static_cast<uint8>(v << 1) | 0x00); |
| 235 |
} else if (v < 0x8000 && minSize <= 2 && 2 <= maxSize) { |
| 236 |
return mpt::IO::WriteIntLE<uint16>(f, static_cast<uint16>(v << 1) | 0x01); |
| 237 |
} else { |
| 238 |
assert(false); |
| 239 |
return false; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
template <typename Tfile> |
| 244 |
inline bool WriteAdaptiveInt32LE(Tfile & f, const uint32 v, std::size_t fixedSize = 0) { |
| 245 |
std::size_t minSize = fixedSize; |
| 246 |
std::size_t maxSize = fixedSize; |
| 247 |
assert(minSize == 0 || minSize == 1 || minSize == 2 || minSize == 3 || minSize == 4); |
| 248 |
assert(maxSize == 0 || maxSize == 1 || maxSize == 2 || maxSize == 3 || maxSize == 4); |
| 249 |
assert(maxSize == 0 || maxSize >= minSize); |
| 250 |
if (maxSize == 0) { |
| 251 |
maxSize = 4; |
| 252 |
} |
| 253 |
if (v < 0x40 && minSize <= 1 && 1 <= maxSize) { |
| 254 |
return mpt::IO::WriteIntLE<uint8>(f, static_cast<uint8>(v << 2) | 0x00); |
| 255 |
} else if (v < 0x4000 && minSize <= 2 && 2 <= maxSize) { |
| 256 |
return mpt::IO::WriteIntLE<uint16>(f, static_cast<uint16>(v << 2) | 0x01); |
| 257 |
} else if (v < 0x400000 && minSize <= 3 && 3 <= maxSize) { |
| 258 |
uint32 value = static_cast<uint32>(v << 2) | 0x02; |
| 259 |
std::byte bytes[3]; |
| 260 |
bytes[0] = static_cast<std::byte>(value >> 0); |
| 261 |
bytes[1] = static_cast<std::byte>(value >> 8); |
| 262 |
bytes[2] = static_cast<std::byte>(value >> 16); |
| 263 |
return mpt::IO::WriteRaw(f, bytes, 3); |
| 264 |
} else if (v < 0x40000000 && minSize <= 4 && 4 <= maxSize) { |
| 265 |
return mpt::IO::WriteIntLE<uint32>(f, static_cast<uint32>(v << 2) | 0x03); |
| 266 |
} else { |
| 267 |
assert(false); |
| 268 |
return false; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
template <typename Tfile> |
| 273 |
inline bool WriteAdaptiveInt64LE(Tfile & f, const uint64 v, std::size_t fixedSize = 0) { |
| 274 |
std::size_t minSize = fixedSize; |
| 275 |
std::size_t maxSize = fixedSize; |
| 276 |
assert(minSize == 0 || minSize == 1 || minSize == 2 || minSize == 4 || minSize == 8); |
| 277 |
assert(maxSize == 0 || maxSize == 1 || maxSize == 2 || maxSize == 4 || maxSize == 8); |
| 278 |
assert(maxSize == 0 || maxSize >= minSize); |
| 279 |
if (maxSize == 0) { |
| 280 |
maxSize = 8; |
| 281 |
} |
| 282 |
if (v < 0x40 && minSize <= 1 && 1 <= maxSize) { |
| 283 |
return mpt::IO::WriteIntLE<uint8>(f, static_cast<uint8>(v << 2) | 0x00); |
| 284 |
} else if (v < 0x4000 && minSize <= 2 && 2 <= maxSize) { |
| 285 |
return mpt::IO::WriteIntLE<uint16>(f, static_cast<uint16>(v << 2) | 0x01); |
| 286 |
} else if (v < 0x40000000 && minSize <= 4 && 4 <= maxSize) { |
| 287 |
return mpt::IO::WriteIntLE<uint32>(f, static_cast<uint32>(v << 2) | 0x02); |
| 288 |
} else if (v < 0x4000000000000000ull && minSize <= 8 && 8 <= maxSize) { |
| 289 |
return mpt::IO::WriteIntLE<uint64>(f, static_cast<uint64>(v << 2) | 0x03); |
| 290 |
} else { |
| 291 |
assert(false); |
| 292 |
return false; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
// Write a variable-length integer, as found in MIDI files. The number of written bytes is placed in the bytesWritten parameter. |
| 297 |
template <typename Tfile, typename T> |
| 298 |
bool WriteVarInt(Tfile & f, const T v, std::size_t * bytesWritten = nullptr) { |
| 299 |
static_assert(std::numeric_limits<T>::is_integer); |
| 300 |
static_assert(!std::numeric_limits<T>::is_signed); |
| 301 |
std::byte out[(sizeof(T) * 8 + 6) / 7]; |
| 302 |
std::size_t numBytes = 0; |
| 303 |
for (uint32 n = (sizeof(T) * 8) / 7; n > 0; n--) { |
| 304 |
if (v >= (static_cast<T>(1) << (n * 7u))) { |
| 305 |
out[numBytes++] = static_cast<std::byte>(((v >> (n * 7u)) & 0x7F) | 0x80); |
| 306 |
} |
| 307 |
} |
| 308 |
out[numBytes++] = static_cast<std::byte>(v & 0x7F); |
| 309 |
assert(numBytes <= std::size(out)); |
| 310 |
if (bytesWritten != nullptr) { |
| 311 |
*bytesWritten = numBytes; |
| 312 |
} |
| 313 |
return mpt::IO::WriteRaw(f, out, numBytes); |
| 314 |
} |
| 315 |
|
| 316 |
template <typename Tsize, typename Tfile> |
| 317 |
inline bool WriteSizedStringLE(Tfile & f, const std::string & str) { |
| 318 |
static_assert(std::numeric_limits<Tsize>::is_integer); |
| 319 |
if (str.size() > std::numeric_limits<Tsize>::max()) { |
| 320 |
return false; |
| 321 |
} |
| 322 |
Tsize size = static_cast<Tsize>(str.size()); |
| 323 |
if (!mpt::IO::WriteIntLE(f, size)) { |
| 324 |
return false; |
| 325 |
} |
| 326 |
if (!mpt::IO::WriteRaw(f, str.data(), str.size())) { |
| 327 |
return false; |
| 328 |
} |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
template <typename Tfile> |
| 333 |
inline bool WriteText(Tfile & f, const std::string & s) { |
| 334 |
return mpt::IO::WriteRaw(f, s.data(), s.size()); |
| 335 |
} |
| 336 |
|
| 337 |
template <typename Tfile> |
| 338 |
inline bool WriteTextCRLF(Tfile & f) { |
| 339 |
return mpt::IO::WriteText(f, "\r\n"); |
| 340 |
} |
| 341 |
|
| 342 |
template <typename Tfile> |
| 343 |
inline bool WriteTextLF(Tfile & f) { |
| 344 |
return mpt::IO::WriteText(f, "\n"); |
| 345 |
} |
| 346 |
|
| 347 |
template <typename Tfile> |
| 348 |
inline bool WriteTextCRLF(Tfile & f, const std::string & s) { |
| 349 |
return mpt::IO::WriteText(f, s) && mpt::IO::WriteTextCRLF(f); |
| 350 |
} |
| 351 |
|
| 352 |
template <typename Tfile> |
| 353 |
inline bool WriteTextLF(Tfile & f, const std::string & s) { |
| 354 |
return mpt::IO::WriteText(f, s) && mpt::IO::WriteTextLF(f); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
} // namespace IO |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
} // namespace MPT_INLINE_NS |
| 364 |
} // namespace mpt |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
#endif // MPT_IO_IO_HPP |