4 changed files with 131 additions and 65 deletions
@ -1,31 +1,46 @@ |
|||||||
#pragma once |
#pragma once |
||||||
|
|
||||||
#include <cstdint> |
#include <cstdint> |
||||||
|
#include <cstring> |
||||||
|
|
||||||
|
#include <SDL_endian.h> |
||||||
|
|
||||||
namespace devilution { |
namespace devilution { |
||||||
|
|
||||||
template <typename T> |
template <typename T> |
||||||
constexpr std::uint16_t LoadLE16(const T *b) |
constexpr uint16_t LoadLE16(const T *b) |
||||||
{ |
{ |
||||||
static_assert(sizeof(T) == 1, "invalid argument"); |
static_assert(sizeof(T) == 1, "invalid argument"); |
||||||
// NOLINTNEXTLINE(readability-magic-numbers)
|
// NOLINTNEXTLINE(readability-magic-numbers)
|
||||||
return (static_cast<std::uint16_t>(b[1]) << 8) | static_cast<std::uint16_t>(b[0]); |
return (static_cast<uint8_t>(b[1]) << 8) | static_cast<uint8_t>(b[0]); |
||||||
} |
} |
||||||
|
|
||||||
template <typename T> |
template <typename T> |
||||||
constexpr std::uint32_t LoadLE32(const T *b) |
constexpr uint32_t LoadLE32(const T *b) |
||||||
{ |
{ |
||||||
static_assert(sizeof(T) == 1, "invalid argument"); |
static_assert(sizeof(T) == 1, "invalid argument"); |
||||||
// NOLINTNEXTLINE(readability-magic-numbers)
|
// NOLINTNEXTLINE(readability-magic-numbers)
|
||||||
return (static_cast<std::uint32_t>(b[3]) << 24) | (static_cast<std::uint32_t>(b[2]) << 16) | (static_cast<std::uint32_t>(b[1]) << 8) | static_cast<std::uint32_t>(b[0]); |
return (static_cast<uint8_t>(b[3]) << 24) | (static_cast<uint8_t>(b[2]) << 16) | (static_cast<uint8_t>(b[1]) << 8) | static_cast<uint8_t>(b[0]); |
||||||
} |
} |
||||||
|
|
||||||
template <typename T> |
template <typename T> |
||||||
constexpr std::uint32_t LoadBE32(const T *b) |
constexpr uint32_t LoadBE32(const T *b) |
||||||
{ |
{ |
||||||
static_assert(sizeof(T) == 1, "invalid argument"); |
static_assert(sizeof(T) == 1, "invalid argument"); |
||||||
// NOLINTNEXTLINE(readability-magic-numbers)
|
// NOLINTNEXTLINE(readability-magic-numbers)
|
||||||
return (static_cast<std::uint32_t>(b[0]) << 24) | (static_cast<std::uint32_t>(b[1]) << 16) | (static_cast<std::uint32_t>(b[2]) << 8) | static_cast<std::uint32_t>(b[3]); |
return (static_cast<uint8_t>(b[0]) << 24) | (static_cast<uint8_t>(b[1]) << 16) | (static_cast<uint8_t>(b[2]) << 8) | static_cast<uint8_t>(b[3]); |
||||||
|
} |
||||||
|
|
||||||
|
inline void WriteLE16(void *out, uint16_t val) |
||||||
|
{ |
||||||
|
const uint16_t littleEndian = SDL_SwapLE16(val); |
||||||
|
memcpy(out, &littleEndian, 2); |
||||||
|
} |
||||||
|
|
||||||
|
inline void WriteLE32(void *out, uint32_t val) |
||||||
|
{ |
||||||
|
const uint32_t littleEndian = SDL_SwapLE32(val); |
||||||
|
memcpy(out, &littleEndian, 4); |
||||||
} |
} |
||||||
|
|
||||||
} // namespace devilution
|
} // namespace devilution
|
||||||
|
|||||||
@ -0,0 +1,78 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <cstring> |
||||||
|
|
||||||
|
#include <fstream> |
||||||
|
|
||||||
|
#include <SDL_endian.h> |
||||||
|
|
||||||
|
#include "utils/endian.hpp" |
||||||
|
|
||||||
|
namespace devilution { |
||||||
|
|
||||||
|
template <typename T = uint8_t> |
||||||
|
T ReadByte(std::ifstream &stream) |
||||||
|
{ |
||||||
|
static_assert(sizeof(T) == 1, "invalid argument"); |
||||||
|
char buf; |
||||||
|
stream.read(&buf, 1); |
||||||
|
return static_cast<T>(buf); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename T = uint16_t> |
||||||
|
T ReadLE16(std::ifstream &stream) |
||||||
|
{ |
||||||
|
static_assert(sizeof(T) == 2, "invalid argument"); |
||||||
|
char buf[2]; |
||||||
|
stream.read(buf, 2); |
||||||
|
return static_cast<T>(LoadLE16(buf)); |
||||||
|
} |
||||||
|
|
||||||
|
template <typename T = uint32_t> |
||||||
|
T ReadLE32(std::ifstream &stream) |
||||||
|
{ |
||||||
|
static_assert(sizeof(T) == 4, "invalid argument"); |
||||||
|
char buf[4]; |
||||||
|
stream.read(buf, 4); |
||||||
|
return static_cast<T>(LoadLE32(buf)); |
||||||
|
} |
||||||
|
|
||||||
|
inline float ReadLEFloat(std::ifstream &stream) |
||||||
|
{ |
||||||
|
static_assert(sizeof(float) == sizeof(uint32_t), "invalid float size"); |
||||||
|
const uint32_t val = ReadLE32(stream); |
||||||
|
float result; |
||||||
|
memcpy(&result, &val, sizeof(float)); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
inline void WriteByte(std::ofstream &out, uint8_t val) |
||||||
|
{ |
||||||
|
out.write(reinterpret_cast<const char *>(&val), 1); |
||||||
|
} |
||||||
|
|
||||||
|
inline void WriteLE16(std::ofstream &out, uint16_t val) |
||||||
|
{ |
||||||
|
char data[2]; |
||||||
|
WriteLE16(data, val); |
||||||
|
out.write(data, 2); |
||||||
|
} |
||||||
|
|
||||||
|
inline void WriteLE32(std::ofstream &out, uint32_t val) |
||||||
|
{ |
||||||
|
char data[4]; |
||||||
|
WriteLE32(data, val); |
||||||
|
out.write(data, 4); |
||||||
|
} |
||||||
|
|
||||||
|
inline void WriteLEFloat(std::ofstream &out, float val) |
||||||
|
{ |
||||||
|
static_assert(sizeof(float) == sizeof(uint32_t), "invalid float size"); |
||||||
|
uint32_t intVal; |
||||||
|
memcpy(&intVal, &val, sizeof(uint32_t)); |
||||||
|
char data[4]; |
||||||
|
WriteLE32(data, intVal); |
||||||
|
out.write(data, 4); |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace devilution
|
||||||
Loading…
Reference in new issue