feature(preprocessor): implement preprocessor & parse command (#48)
This commit is contained in:
@ -23,7 +23,8 @@ reader::reader(u8 const* data, u32 size, bool swap) : data_{ data }, size_{ size
|
||||
|
||||
template<> auto reader::read() -> i8
|
||||
{
|
||||
if (pos_ + 1 > size_) return i8{};
|
||||
if (pos_ + 1 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
auto value = *reinterpret_cast<i8 const*>(data_ + pos_);
|
||||
pos_ += 1;
|
||||
@ -32,7 +33,8 @@ template<> auto reader::read() -> i8
|
||||
|
||||
template<> auto reader::read() -> u8
|
||||
{
|
||||
if (pos_ + 1 > size_) return u8{};
|
||||
if (pos_ + 1 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
auto value = *reinterpret_cast<u8 const*>(data_ + pos_);
|
||||
pos_ += 1;
|
||||
@ -41,7 +43,8 @@ template<> auto reader::read() -> u8
|
||||
|
||||
template<> auto reader::read() -> i16
|
||||
{
|
||||
if (pos_ + 2 > size_) return i16{};
|
||||
if (pos_ + 2 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -59,7 +62,8 @@ template<> auto reader::read() -> i16
|
||||
|
||||
template<> auto reader::read() -> u16
|
||||
{
|
||||
if (pos_ + 2 > size_) return u16{};
|
||||
if (pos_ + 2 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -77,7 +81,8 @@ template<> auto reader::read() -> u16
|
||||
|
||||
template<> auto reader::read() -> i32
|
||||
{
|
||||
if (pos_ + 4 > size_) return i32{};
|
||||
if (pos_ + 4 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -97,7 +102,8 @@ template<> auto reader::read() -> i32
|
||||
|
||||
template<> auto reader::read() -> u32
|
||||
{
|
||||
if (pos_ + 4 > size_) return u32{};
|
||||
if (pos_ + 4 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -117,7 +123,8 @@ template<> auto reader::read() -> u32
|
||||
|
||||
template<> auto reader::read() -> i64
|
||||
{
|
||||
if (pos_ + 8 > size_) return i64{};
|
||||
if (pos_ + 8 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -141,7 +148,8 @@ template<> auto reader::read() -> i64
|
||||
|
||||
template<> auto reader::read() -> u64
|
||||
{
|
||||
if (pos_ + 8 > size_) return u64{};
|
||||
if (pos_ + 8 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -165,7 +173,8 @@ template<> auto reader::read() -> u64
|
||||
|
||||
template<> auto reader::read() -> f32
|
||||
{
|
||||
if (pos_ + 4 > size_) return f32{};
|
||||
if (pos_ + 4 > size_)
|
||||
throw std::runtime_error("reader: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -198,7 +207,7 @@ auto reader::read_bytes(u32 pos, u32 count) -> std::string
|
||||
|
||||
for (auto i = pos; i < pos + count; i++)
|
||||
{
|
||||
data += fmt::format("{:02X} ", *reinterpret_cast<u8 const*>(data_ + i));
|
||||
fmt::format_to(std::back_insert_iterator(data), "{:02X} ", *reinterpret_cast<u8 const*>(data_ + i));
|
||||
}
|
||||
|
||||
data.pop_back();
|
||||
|
@ -33,7 +33,8 @@ auto writer::clear() -> void
|
||||
|
||||
template<> auto writer::write(i8 data) -> void
|
||||
{
|
||||
if (pos_ + 1 > size_) return;
|
||||
if (pos_ + 1 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
*reinterpret_cast<i8*>(data_ + pos_) = data;
|
||||
pos_ += 1;
|
||||
@ -41,7 +42,8 @@ template<> auto writer::write(i8 data) -> void
|
||||
|
||||
template<> auto writer::write(u8 data) -> void
|
||||
{
|
||||
if (pos_ + 1 > size_) return;
|
||||
if (pos_ + 1 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
*reinterpret_cast<u8*>(data_ + pos_) = data;
|
||||
pos_ += 1;
|
||||
@ -49,7 +51,8 @@ template<> auto writer::write(u8 data) -> void
|
||||
|
||||
template<> auto writer::write(i16 data) -> void
|
||||
{
|
||||
if (pos_ + 2 > size_) return;
|
||||
if (pos_ + 2 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -66,7 +69,8 @@ template<> auto writer::write(i16 data) -> void
|
||||
|
||||
template<> auto writer::write(u16 data) -> void
|
||||
{
|
||||
if (pos_ + 2 > size_) return;
|
||||
if (pos_ + 2 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -83,7 +87,8 @@ template<> auto writer::write(u16 data) -> void
|
||||
|
||||
template<> auto writer::write(i32 data) -> void
|
||||
{
|
||||
if (pos_ + 4 > size_) return;
|
||||
if (pos_ + 4 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -102,7 +107,8 @@ template<> auto writer::write(i32 data) -> void
|
||||
|
||||
template<> auto writer::write(u32 data) -> void
|
||||
{
|
||||
if (pos_ + 4 > size_) return;
|
||||
if (pos_ + 4 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -121,7 +127,8 @@ template<> auto writer::write(u32 data) -> void
|
||||
|
||||
template<> auto writer::write(i64 data) -> void
|
||||
{
|
||||
if (pos_ + 8 > size_) return;
|
||||
if (pos_ + 8 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -144,7 +151,8 @@ template<> auto writer::write(i64 data) -> void
|
||||
|
||||
template<> auto writer::write(u64 data) -> void
|
||||
{
|
||||
if (pos_ + 8 > size_) return;
|
||||
if (pos_ + 8 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -167,7 +175,8 @@ template<> auto writer::write(u64 data) -> void
|
||||
|
||||
template<> auto writer::write(f32 data) -> void
|
||||
{
|
||||
if (pos_ + 4 > size_) return;
|
||||
if (pos_ + 4 > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
if (!swap_)
|
||||
{
|
||||
@ -186,7 +195,8 @@ template<> auto writer::write(f32 data) -> void
|
||||
|
||||
auto writer::write_string(std::string const& data) -> void
|
||||
{
|
||||
if (pos_ + data.size() > size_) return;
|
||||
if (pos_ + data.size() > size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
std::memcpy(reinterpret_cast<void*>(data_ + pos_), data.data(), data.size());
|
||||
pos_ += static_cast<u32>(data.size());
|
||||
@ -194,28 +204,13 @@ auto writer::write_string(std::string const& data) -> void
|
||||
|
||||
auto writer::write_cstr(std::string const& data) -> void
|
||||
{
|
||||
if (pos_ + data.size() >= size_) return;
|
||||
if (pos_ + data.size() >= size_)
|
||||
throw std::runtime_error("writer: out of bounds");
|
||||
|
||||
std::memcpy(reinterpret_cast<void*>(data_ + pos_), data.data(), data.size());
|
||||
pos_ += static_cast<u32>(data.size() + 1);
|
||||
}
|
||||
|
||||
auto writer::read_bytes(u32 pos, u32 count) -> std::string
|
||||
{
|
||||
auto data = std::string{};
|
||||
|
||||
data.reserve(count * 3);
|
||||
|
||||
for (auto i = pos; i < pos + count; i++)
|
||||
{
|
||||
data += fmt::format("{:02X} ", *reinterpret_cast<const u8*>(data_ + i));
|
||||
}
|
||||
|
||||
data.pop_back();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
auto writer::is_avail() -> bool
|
||||
{
|
||||
return (pos_ < size_) ? true : false;
|
||||
|
@ -28,7 +28,6 @@ public:
|
||||
auto write(T data) -> void;
|
||||
auto write_string(std::string const& data) -> void;
|
||||
auto write_cstr(std::string const& data) -> void;
|
||||
auto read_bytes(u32 pos, u32 count) -> std::string;
|
||||
auto is_avail() -> bool;
|
||||
auto seek(u32 size) -> void;
|
||||
auto seek_neg(u32 size) -> void;
|
||||
|
Reference in New Issue
Block a user