Skip to content

Commit

Permalink
Improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ytsutano committed May 5, 2017
1 parent 0764365 commit d6f6577
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/jitana/util/axml_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace jitana {
using runtime_error::runtime_error;
};

struct axml_parser_magic_mismatched : axml_parser_error {
struct axml_parser_not_an_axml_file : axml_parser_error {
using axml_parser_error::axml_parser_error;
};

Expand Down
6 changes: 6 additions & 0 deletions include/jitana/util/stream_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ namespace jitana {
return end_ptr_;
}

/// Returns the buffer size.
size_t size() const
{
return end_ptr_ - begin_ptr_;
}

/// Moves the head.
void move_head(size_t pos = 0) const
{
Expand Down
7 changes: 6 additions & 1 deletion lib/jitana/util/axml_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,16 @@ namespace jitana {
xml_stack_.clear();
xml_stack_.emplace_back(&pt_);

// Make sure that the file is large enough.
if (reader_.size() < sizeof(res_chunk_header)) {
throw axml_parser_not_an_axml_file("not a binary XML file");
}

const auto& header = reader_.get<res_chunk_header>();

// Make sure it's the right file type.
if (header.type != res_xml_type) {
throw axml_parser_magic_mismatched("not a binary XML file");
throw axml_parser_not_an_axml_file("not a binary XML file");
}

// Apply pull parsing.
Expand Down
6 changes: 5 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void process_xml(const std::string& input_filename,
// First, try to read as a binary XML file.
jitana::read_axml(input_filename, pt);
}
catch (const jitana::axml_parser_magic_mismatched& e) {
catch (const jitana::axml_parser_not_an_axml_file& e) {
// Binary parser has faied: try to read it as a normal XML file.
boost::property_tree::read_xml(input_filename, pt);
}
Expand Down Expand Up @@ -111,6 +111,10 @@ int main(int argc, char** argv)
process_xml(input_filename, output_filename);
}
}
catch (std::ios::failure& e) {
std::cerr << "error: failed to open the input file\n";
return 1;
}
catch (std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
Expand Down

0 comments on commit d6f6577

Please sign in to comment.