Skip to content

Commit

Permalink
error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
moonshadow565 committed Dec 19, 2019
1 parent 7c8019c commit f7ed0a2
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 140 deletions.
38 changes: 23 additions & 15 deletions src/ritobin/binary_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,23 @@ namespace ritobin {
struct BinBinaryReader {
Bin& bin;
BinaryReader reader;
std::string error;
std::vector<std::pair<std::string, char const*>> error;

#define bin_assert(...) do { \
if(auto start = reader.cur_; !(__VA_ARGS__)) { \
return fail_msg(#__VA_ARGS__, start); \
} } while(false)

bool process() noexcept {
bin.sections.clear();
error.clear();
if (!read_sections()) {
error.append("Failed to read @ " + std::to_string(reader.position()));
return false;
}
bin_assert(read_sections());
return true;
}

private:
// NOTE: change this macro to include full stack messages
#define bin_assert(...) do { if(!(__VA_ARGS__)) { return fail_fast(); } } while(false)
inline constexpr bool fail_fast() const noexcept { return false; }
bool fail_msg(char const* msg, char const* pos) noexcept {
error.emplace_back(msg, pos);
return false;
}

bool read_sections() noexcept {
std::array<char, 4> magic = {};
Expand All @@ -128,7 +129,7 @@ namespace ritobin {
}

bool read_linked(bool hasLinks) noexcept {
List linkedList = { Type::STRING };
List linkedList = { Type::STRING, {} };
if (hasLinks) {
uint32_t linkedFilesCount = {};
bin_assert(reader.read(linkedFilesCount));
Expand All @@ -147,10 +148,10 @@ namespace ritobin {
std::vector<uint32_t> entryNameHashes;
bin_assert(reader.read(entryCount));
bin_assert(reader.read(entryNameHashes, entryCount));
Map entriesMap = { Type::HASH, Type::EMBED };
Map entriesMap = { Type::HASH, Type::EMBED, {} };
for (uint32_t entryNameHash : entryNameHashes) {
Hash entryKeyHash = {};
Embed entry = { { entryNameHash } };
Embed entry = { { entryNameHash }, {} };
bin_assert(read_entry(entryKeyHash, entry));
entriesMap.items.emplace_back(Pair{ std::move(entryKeyHash), std::move(entry) });
}
Expand Down Expand Up @@ -289,9 +290,16 @@ namespace ritobin {
};

void Bin::read_binary(char const* data, size_t size) {
BinBinaryReader reader = { *this, { data, data, data + size } };
BinBinaryReader reader = { *this, { data, data, data + size }, {} };
if (!reader.process()) {
throw std::runtime_error(std::move(reader.error));
std::string error;
for(auto e = reader.error.crbegin(); e != reader.error.crend(); e++) {
error.append(e->first);
error.append(" @ ");
error.append(std::to_string(data - e->second));
error.append("\n");
}
throw std::runtime_error(std::move(error));
}
}
}
35 changes: 19 additions & 16 deletions src/ritobin/binary_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,25 @@ namespace ritobin {
struct BinBinaryWriter {
Bin const& bin;
BinaryWriter writer;
std::string error;
std::vector<std::string> error;

#define bin_assert(...) do { \
if(!(__VA_ARGS__)) { \
return fail_msg(#__VA_ARGS__ "\n"); \
} } while(false)

bool process() noexcept {
error.clear();
writer.buffer_.clear();
if (!write_header()) {
error.append("Failed to write header");
return false;
}
if (!write_entries()) {
error.append("Failed to write entries");
return false;
}
bin_assert(write_header());
bin_assert(write_entries());
return true;
}
private:
// NOTE: change this macro to include full stack messages
#define bin_assert(...) do { if(!(__VA_ARGS__)) { return fail_fast(); } } while(false)
inline constexpr bool fail_fast() const noexcept { return false; }
bool fail_msg(char const* msg) noexcept {
error.emplace_back(msg);
return false;
}

bool write_header() noexcept {
auto type_section = bin.sections.find("type");
Expand Down Expand Up @@ -166,7 +166,6 @@ namespace ritobin {
return true;
}


bool write_value_visit(Embed const& value) noexcept {
writer.write(value.name);
size_t position = writer.position();
Expand Down Expand Up @@ -244,7 +243,7 @@ namespace ritobin {
return true;
}

bool write_value_visit(None const& value) noexcept {
bool write_value_visit(None const&) noexcept {
return true;
}

Expand All @@ -269,9 +268,13 @@ namespace ritobin {
};

void Bin::write_binary(std::vector<char>& out) const {
BinBinaryWriter writer = { *this, { out } };
BinBinaryWriter writer = { *this, { out }, {} };
if (!writer.process()) {
throw std::runtime_error(std::move(writer.error));
std::string error;
for(auto e = writer.error.crbegin(); e != writer.error.crend(); e++) {
error.append(*e);
}
throw std::runtime_error(std::move(error));
}
}
}
Loading

0 comments on commit f7ed0a2

Please sign in to comment.