Skip to content

Commit

Permalink
refactor(utils): clean linter warnings (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik authored Nov 3, 2024
1 parent f5c895f commit bf397fe
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 127 deletions.
5 changes: 3 additions & 2 deletions include/xsk/utils/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
namespace xsk::utils
{

class file
struct file
{
public:
using error = std::runtime_error;

static auto read(std::filesystem::path const& file) -> std::vector<u8>;
static auto save(std::filesystem::path const& file, std::vector<u8> const& data) -> void;
static auto save(std::filesystem::path const& file, u8 const* data, usize size) -> void;
Expand Down
16 changes: 8 additions & 8 deletions include/xsk/utils/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
namespace xsk::utils
{

class reader
struct reader
{
public:
using ptr = std::unique_ptr<reader>;
using error = std::runtime_error;

private:
u8 const* data_;
u32 size_;
u32 pos_;
u32 pos_ = 0;
bool swap_;

public:
reader(bool swap = false);
explicit reader(bool swap = false);
reader(std::vector<u8> const& data, bool swap = false);
reader(u8 const* data, u32 size, bool swap = false);
template <typename T>
auto read() -> T;
auto read_cstr() -> std::string;
auto read_bytes(u32 pos, u32 count) -> std::string;
auto is_avail() -> bool;
auto is_avail() const -> bool;
auto seek(u32 size) -> void;
auto seek_neg(u32 size) -> void;
auto align(u32 size) -> u32;
auto data() -> u8 const*;
auto size() -> u32;
auto pos() -> u32;
auto data() const -> u8 const*;
auto size() const -> u32;
auto pos() const -> u32;
auto pos(u32 pos) -> void;
};

Expand Down
31 changes: 10 additions & 21 deletions include/xsk/utils/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,21 @@
namespace xsk::utils
{

class string
struct string
{
public:
template <typename ... Args>
static auto va(std::string const& format, Args ... args) -> std::string
{
usize size = snprintf(nullptr, 0, format.data(), args ...) + 1;
std::vector<char> buf;
buf.resize(size);
snprintf(buf.data(), size, format.data(), args ...);
return std::string(buf.data(), buf.data() + size - 1);
}

static auto oct_to_dec(char const* str) -> std::string;
static auto bin_to_dec(char const* str) -> std::string;
static auto hex_to_dec(char const* str) -> std::string;
static auto iequals(std::string const& a, std::string const& b) -> bool;
static auto is_number(std::string const& s) -> bool;
static auto is_hex_number(std::string const& s) -> bool;
static auto to_lower(std::string const& input) -> std::string;
static auto to_code(std::string const& input) -> std::string;
static auto to_literal(std::string const& input) -> std::string;
static auto fordslash(std::string const& s) -> std::string;
static auto backslash(std::string const& s) -> std::string;
static auto quote(std::string const& s, bool single = true) -> std::string;
static auto unquote(std::string const& s) -> std::string;
static auto is_number(std::string const& str) -> bool;
static auto is_hex_number(std::string const& str) -> bool;
static auto to_lower(std::string const& str) -> std::string;
static auto to_code(std::string const& str) -> std::string;
static auto to_literal(std::string const& str) -> std::string;
static auto fordslash(std::string const& str) -> std::string;
static auto backslash(std::string const& str) -> std::string;
static auto quote(std::string const& str, bool single = true) -> std::string;
static auto unquote(std::string const& str) -> std::string;
static auto split(std::string& str, char delimiter) -> std::vector<std::string>;
static auto clean_buffer_lines(std::vector<u8>& buffer) -> std::vector<std::string>;
static auto parse_code(std::string& line) -> std::vector<std::string>;
Expand Down
21 changes: 13 additions & 8 deletions include/xsk/utils/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,38 @@
namespace xsk::utils
{

class writer
struct writer
{
public:
using ptr = std::unique_ptr<writer>;
using error = std::runtime_error;

private:
static constexpr u32 default_size = 0x100000;
u8* data_;
u32 size_;
u32 pos_;
u32 pos_ = 0;
bool swap_;

public:
writer(bool swap = false);
writer(writer const&) = delete;
writer(writer&&) = delete;
auto operator=(writer const&) -> writer& = delete;
auto operator=(writer&&) -> writer& = delete;
explicit writer(bool swap = false);
writer(u32 size, bool swap = false);
~writer();
auto clear() -> void;
template <typename T>
auto write(T data) -> void;
auto write_string(std::string const& data) -> void;
auto write_cstr(std::string const& data) -> void;
auto is_avail() -> bool;
auto is_avail() const -> bool;
auto seek(u32 size) -> void;
auto seek_neg(u32 size) -> void;
auto align(u32 size) -> u32;
auto data() -> u8 const*;
auto size() -> u32;
auto pos() -> u32;
auto data() const -> u8 const*;
auto size() const -> u32;
auto pos() const -> u32;
auto pos(u32 pos) -> void;
};

Expand Down
5 changes: 3 additions & 2 deletions include/xsk/utils/zlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
namespace xsk::utils
{

class zlib
struct zlib
{
public:
using error = std::runtime_error;

static auto compress(std::vector<u8> const& data) -> std::vector<u8>;
static auto decompress(std::vector<u8> const& data, u32 length) -> std::vector<u8>;
};
Expand Down
2 changes: 1 addition & 1 deletion src/arc/disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ auto disassembler::disassemble_function(function& func) -> void
if ((index & 0x4000) == 0)
inst->opcode = ctx_->opcode_enum(index);
else
throw disasm_error(utils::string::va("invalid opcode index 0x%X at pos '%04X'!", index, inst->index));
throw disasm_error(std::format("invalid opcode index 0x{:X} at pos '{:04X}'!", index, inst->index));
}
else
{
Expand Down
15 changes: 4 additions & 11 deletions src/utils/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ auto file::read(std::filesystem::path const& file) -> std::vector<u8>

if (!stream.good() && !stream.is_open())
{
throw std::runtime_error(std::format("couldn't open file {}", file.string()));
throw error(std::format("couldn't open file {}", file.string()));
}

stream.seekg(0, std::ios::end);
Expand All @@ -42,24 +42,17 @@ auto file::save(std::filesystem::path const& file, std::vector<u8> const& data)

auto file::save(std::filesystem::path const& file, u8 const* data, usize size) -> void
{
auto path = file;
std::filesystem::create_directories(file.parent_path());

std::filesystem::create_directories(path.remove_filename());

auto stream = std::ofstream{ file, std::ios::binary | std::ofstream::out };

if (stream.is_open())
if (auto stream = std::ofstream{ file, std::ios::binary | std::ofstream::out }; stream)
{
stream.write(reinterpret_cast<char const*>(data), size);
stream.close();
}
}

auto file::length(std::filesystem::path const& file) -> usize
{
auto stream = std::ifstream{ file, std::ios::binary };

if (stream.good())
if (auto stream = std::ifstream{ file, std::ios::binary }; stream.good())
{
stream.seekg(0, std::ios::end);
return static_cast<usize>(stream.tellg());
Expand Down
41 changes: 19 additions & 22 deletions src/utils/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
namespace xsk::utils
{

reader::reader(bool swap) : data_{ nullptr }, size_ { 0 }, pos_{ 0 }, swap_{ swap }
reader::reader(bool swap) : data_{ nullptr }, size_ { 0 }, swap_{ swap }
{
}

reader::reader(std::vector<u8> const& data, bool swap) : data_{ data.data() }, size_{ static_cast<u32>(data.size()) }, pos_{ 0 }, swap_{ swap }
reader::reader(std::vector<u8> const& data, bool swap) : data_{ data.data() }, size_{ static_cast<u32>(data.size()) }, swap_{ swap }
{
}

reader::reader(u8 const* data, u32 size, bool swap) : data_{ data }, size_{ size }, pos_{ 0 }, swap_{ swap }
reader::reader(u8 const* data, u32 size, bool swap) : data_{ data }, size_{ size }, swap_{ swap }
{
}

template<> auto reader::read() -> i8
{
if (pos_ + 1 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

auto value = *reinterpret_cast<i8 const*>(data_ + pos_);
pos_ += 1;
Expand All @@ -34,7 +34,7 @@ template<> auto reader::read() -> i8
template<> auto reader::read() -> u8
{
if (pos_ + 1 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

auto value = *reinterpret_cast<u8 const*>(data_ + pos_);
pos_ += 1;
Expand All @@ -44,7 +44,7 @@ template<> auto reader::read() -> u8
template<> auto reader::read() -> i16
{
if (pos_ + 2 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

if (!swap_)
{
Expand All @@ -63,7 +63,7 @@ template<> auto reader::read() -> i16
template<> auto reader::read() -> u16
{
if (pos_ + 2 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

if (!swap_)
{
Expand All @@ -82,7 +82,7 @@ template<> auto reader::read() -> u16
template<> auto reader::read() -> i32
{
if (pos_ + 4 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

if (!swap_)
{
Expand All @@ -103,7 +103,7 @@ template<> auto reader::read() -> i32
template<> auto reader::read() -> u32
{
if (pos_ + 4 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

if (!swap_)
{
Expand All @@ -124,7 +124,7 @@ template<> auto reader::read() -> u32
template<> auto reader::read() -> i64
{
if (pos_ + 8 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

if (!swap_)
{
Expand All @@ -149,7 +149,7 @@ template<> auto reader::read() -> i64
template<> auto reader::read() -> u64
{
if (pos_ + 8 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

if (!swap_)
{
Expand All @@ -174,7 +174,7 @@ template<> auto reader::read() -> u64
template<> auto reader::read() -> f32
{
if (pos_ + 4 > size_)
throw std::runtime_error("reader: out of bounds");
throw error("reader: out of bounds");

if (!swap_)
{
Expand Down Expand Up @@ -215,9 +215,9 @@ auto reader::read_bytes(u32 pos, u32 count) -> std::string
return data;
}

auto reader::is_avail() -> bool
auto reader::is_avail() const -> bool
{
return (pos_ < size_) ? true : false;
return pos_ < size_;
}

auto reader::seek(u32 size) -> void
Expand All @@ -227,7 +227,7 @@ auto reader::seek(u32 size) -> void

auto reader::seek_neg(u32 size) -> void
{
if (pos_ - size >= 0) pos_ -= size;
if (pos_ >= size) pos_ -= size;
}

auto reader::align(u32 size) -> u32
Expand All @@ -239,27 +239,24 @@ auto reader::align(u32 size) -> u32
return pos_ - pos;
}

auto reader::data() -> u8 const*
auto reader::data() const -> u8 const*
{
return data_;
}

auto reader::size() -> u32
auto reader::size() const -> u32
{
return size_;
}

auto reader::pos() -> u32
auto reader::pos() const -> u32
{
return pos_;
}

auto reader::pos(u32 pos) -> void
{
if (pos >= 0 && pos <= size_)
{
pos_ = pos;
}
if (pos <= size_) pos_ = pos;
}

} // namespace xsk::utils
Loading

0 comments on commit bf397fe

Please sign in to comment.