From de95ca22ec87cc8b79ceb7beba475301461713a6 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sun, 19 Jun 2016 22:05:32 +0200 Subject: adds an LE file parser and refactors some code. --- binparse/binparse.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ binparse/binparse.hpp | 9 +++++++++ le/CMakeLists.txt | 2 ++ le/le_header.cpp | 6 +++--- le/le_header.hpp | 2 +- le/le_header_parser.cpp | 2 +- mz/mz_header.cpp | 6 +++--- mz/mz_header.hpp | 2 +- mz/mz_header_parser.cpp | 2 +- 9 files changed, 66 insertions(+), 10 deletions(-) diff --git a/binparse/binparse.cpp b/binparse/binparse.cpp index 1dc4fc6..eae6143 100644 --- a/binparse/binparse.cpp +++ b/binparse/binparse.cpp @@ -90,6 +90,51 @@ Offset32 parse(std::istream& is, std::string name) { return Offset32(parse(is, name)); } +template<> +std::array parse>(std::istream& is, std::string name) { + if(!is) { + throw UnexpectedEOS(); + } + + std::array buffer; + is.read(reinterpret_cast(buffer.data()), buffer.size()); + + if(!is) { + throw UnexpectedEOS(name); + } + + return buffer; +} + +template<> +std::array parse>(std::istream& is, std::string name) { + if(!is) { + throw UnexpectedEOS(); + } + + std::array buffer; + is.read(reinterpret_cast(buffer.data()), buffer.size()); + + if(!is) { + throw UnexpectedEOS(name); + } + + return buffer; +} + +void dump_bytes(std::istream& is, std::vector& buffer, std::string name) +{ + if(!is) { + throw UnexpectedEOS(); + } + + is.read(reinterpret_cast(buffer.data()), buffer.size()); + + if(!is) { + throw UnexpectedEOS(name); + } +} + std::string to_string(Magic16 magic) { char* c = reinterpret_cast(&magic); return std::string(c, sizeof(Magic16)); diff --git a/binparse/binparse.hpp b/binparse/binparse.hpp index 1620ab5..5b21086 100644 --- a/binparse/binparse.hpp +++ b/binparse/binparse.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace binparse { @@ -39,6 +40,14 @@ Value32 parse(std::istream& is, std::string name); template<> Offset32 parse(std::istream& is, std::string name); +template<> +std::array parse>(std::istream& is, std::string name); + +template<> +std::array parse>(std::istream& is, std::string name); + +void dump_bytes(std::istream& is, std::vector buffer, std::string name); + std::string to_string(Magic16 magic); //overload inside this namespace to output unsigned char as value not as characters. diff --git a/le/CMakeLists.txt b/le/CMakeLists.txt index a4887c5..646f68b 100644 --- a/le/CMakeLists.txt +++ b/le/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(le STATIC le_header.hpp le_header.cpp + le_file.hpp le_file.cpp ) target_include_directories(le @@ -10,6 +11,7 @@ target_include_directories(le target_link_libraries(le PUBLIC fusion-utils PUBLIC binparse + PUBLIC mz ) add_executable(le_header_parser diff --git a/le/le_header.cpp b/le/le_header.cpp index a429add..f28cc4d 100644 --- a/le/le_header.cpp +++ b/le/le_header.cpp @@ -59,17 +59,17 @@ BOOST_FUSION_ADAPT_STRUCT( namespace le { template -LEHeader parse_file_impl(std::istream& is, indices) { +LEHeader parse_header_impl(std::istream& is, indices) { return {parse::type>::type>(is, boost::fusion::extension::struct_member_name::call())...}; } -LEHeader parse_file(std::istream& is) { +LEHeader parse_header(std::istream& is) { typedef build_indices::value>::type indices; - return parse_file_impl(is, indices{}); + return parse_header_impl(is, indices{}); } std::ostream& output_impl(std::ostream& os, LEHeader const&, indices<>) { diff --git a/le/le_header.hpp b/le/le_header.hpp index 9501127..5d070b6 100644 --- a/le/le_header.hpp +++ b/le/le_header.hpp @@ -100,7 +100,7 @@ struct LEHeader { Value32 heapsize; }; -LEHeader parse_file(std::istream& is); +LEHeader parse_header(std::istream& is); std::ostream& operator<<(std::ostream& os, LEHeader const& header); diff --git a/le/le_header_parser.cpp b/le/le_header_parser.cpp index 524c60d..e8c7096 100644 --- a/le/le_header_parser.cpp +++ b/le/le_header_parser.cpp @@ -50,7 +50,7 @@ int main(int argc, char* argv[]) { std::ifstream file(file_path.string()); file.seekg(0x2aa8); - auto x = le::parse_file(file); + auto x = le::parse_header(file); std::cout << x << std::endl; return 0; diff --git a/mz/mz_header.cpp b/mz/mz_header.cpp index 323c9cd..02241cf 100644 --- a/mz/mz_header.cpp +++ b/mz/mz_header.cpp @@ -36,17 +36,17 @@ NotAMZFileException::NotAMZFileException() {} template -MZHeader parse_file_impl(std::istream& is, indices) { +MZHeader parse_header_impl(std::istream& is, indices) { return {parse::type>::type>(is, boost::fusion::extension::struct_member_name::call())...}; } -MZHeader parse_file(std::istream& is) { +MZHeader parse_header(std::istream& is) { typedef build_indices::value>::type indices; - return parse_file_impl(is, indices{}); + return parse_header_impl(is, indices{}); } std::ostream& output_impl(std::ostream& os, MZHeader const&, indices<>) { diff --git a/mz/mz_header.hpp b/mz/mz_header.hpp index fd42b43..9bef928 100644 --- a/mz/mz_header.hpp +++ b/mz/mz_header.hpp @@ -39,7 +39,7 @@ struct MZHeader { Value16 overlay_number; }; -MZHeader parse_file(std::istream& is); +MZHeader parse_header(std::istream& is); std::ostream& operator<<(std::ostream& os, MZHeader const& header); diff --git a/mz/mz_header_parser.cpp b/mz/mz_header_parser.cpp index 66718d1..6010331 100644 --- a/mz/mz_header_parser.cpp +++ b/mz/mz_header_parser.cpp @@ -49,7 +49,7 @@ int main(int argc, char* argv[]) { } std::ifstream file(file_path.string()); - auto x = mz::parse_file(file); + auto x = mz::parse_header(file); std::cout << x << std::endl; return 0; -- cgit v1.2.3-70-g09d2