summaryrefslogtreecommitdiff
path: root/binparse
diff options
context:
space:
mode:
Diffstat (limited to 'binparse')
-rw-r--r--binparse/otreestream.hpp7
-rw-r--r--binparse/output.cpp5
-rw-r--r--binparse/output.hpp26
-rw-r--r--binparse/parse.cpp8
-rw-r--r--binparse/parse.hpp4
-rw-r--r--binparse/types.hpp2
6 files changed, 50 insertions, 2 deletions
diff --git a/binparse/otreestream.hpp b/binparse/otreestream.hpp
index 3f57d4e..f7795e3 100644
--- a/binparse/otreestream.hpp
+++ b/binparse/otreestream.hpp
@@ -36,6 +36,13 @@ class indent {
public:
indent(std::ostream& os);
+ indent() = delete;
+ indent(indent const&) = delete;
+ indent(indent&&) = delete;
+
+ indent& operator=(indent const&) = delete;
+ indent& operator=(indent&&) = delete;
+
~indent();
};
diff --git a/binparse/output.cpp b/binparse/output.cpp
index 78babd8..16447a7 100644
--- a/binparse/output.cpp
+++ b/binparse/output.cpp
@@ -43,6 +43,11 @@ std::ostream& operator<<(std::ostream& os, Offset32 o) {
return os << std::hex << std::setw(sizeof(o) * 2) << std::setfill('0') << static_cast<uint32_t>(o);
}
+std::ostream&operator<<(std::ostream& os, PString8 s)
+{
+ return os << "(" << std::string(s).size() << ") " << std::string(s);
+}
+
std::ostream&operator<<(std::ostream& os, std::array<uint8_t, 8> a)
{
return os;
diff --git a/binparse/output.hpp b/binparse/output.hpp
index 705a380..c5db2da 100644
--- a/binparse/output.hpp
+++ b/binparse/output.hpp
@@ -13,6 +13,7 @@
#include <vector>
#include <map>
#include <streambuf>
+#include <typeinfo>
namespace binparse {
@@ -31,6 +32,8 @@ std::ostream& operator<<(std::ostream& os, Offset16 o);
std::ostream& operator<<(std::ostream& os, Value32 v);
std::ostream& operator<<(std::ostream& os, Offset32 o);
+std::ostream& operator<<(std::ostream& os, PString8 s);
+
std::ostream& operator<<(std::ostream& os, std::array<uint8_t, 8> a);
std::ostream& operator<<(std::ostream& os, std::array<uint8_t, 22> a);
@@ -47,7 +50,7 @@ std::ostream& operator<<(std::ostream& os, std::map<Key, Value> const& map) {
for(auto&& entry : map) {
os << entry.first << ": " << entry.second << std::endl;
}
- } catch(std::exception&) {
+ } catch(std::bad_cast&) {
os << std::endl;
for(auto&& entry : map) {
os << entry.first << ": " << entry.second << std::endl;
@@ -57,6 +60,25 @@ std::ostream& operator<<(std::ostream& os, std::map<Key, Value> const& map) {
return os;
}
+template <typename ValueType>
+std::ostream& operator<<(std::ostream& os, std::vector<ValueType> const& vec) {
+ try {
+ indent scoped(os);
+ os << std::endl;
+ for(auto&& entry : vec) {
+ os << entry << std::endl;
+ }
+ } catch(std::bad_cast&) {
+ os << std::endl;
+ for(auto&& entry : vec) {
+ os << entry << std::endl;
+ }
+ }
+ return os;
+}
+
+
+
template<typename T>
std::ostream& output_impl(std::ostream& os, T const&, indices<>) {
return os;
@@ -78,7 +100,7 @@ std::ostream& operator<<(std::ostream& os, T const& t) {
indent scoped(os);
os << std::endl;
output_impl<T>(os, t, indices{});
- } catch(std::exception&) {
+ } catch(std::bad_cast&) {
os << std::endl;
output_impl<T>(os, t, indices{});
}
diff --git a/binparse/parse.cpp b/binparse/parse.cpp
index cf7c0be..a26e72e 100644
--- a/binparse/parse.cpp
+++ b/binparse/parse.cpp
@@ -90,6 +90,14 @@ Offset32 parse<Offset32>(std::istream& is, std::string name) {
}
template<>
+PString8 parse<PString8>(std::istream& is, std::string name) {
+ uint8_t len = parse<uint8_t>(is, name + "_len");
+ std::vector<char> data(len, 0);
+ is.read(data.data(), len);
+ return PString8(std::string(data.begin(), data.end()));
+}
+
+template<>
std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::string name) {
if(!is) {
throw UnexpectedEOS();
diff --git a/binparse/parse.hpp b/binparse/parse.hpp
index 8015ba3..2103177 100644
--- a/binparse/parse.hpp
+++ b/binparse/parse.hpp
@@ -11,6 +11,7 @@
#include <stdexcept>
#include <array>
#include <vector>
+#include <string>
namespace binparse {
@@ -48,6 +49,9 @@ template<>
Offset32 parse<Offset32>(std::istream& is, std::string name);
template<>
+PString8 parse<PString8>(std::istream& is, std::string name);
+
+template<>
std::array<uint8_t, 8> parse<std::array<uint8_t, 8>>(std::istream& is, std::string name);
template<>
diff --git a/binparse/types.hpp b/binparse/types.hpp
index 9b6c4a6..d1b1910 100644
--- a/binparse/types.hpp
+++ b/binparse/types.hpp
@@ -17,6 +17,8 @@ BOOST_STRONG_TYPEDEF(uint16_t, Value16)
BOOST_STRONG_TYPEDEF(uint32_t, Offset32)
BOOST_STRONG_TYPEDEF(uint32_t, Value32)
+BOOST_STRONG_TYPEDEF(std::string, PString8);
+
template <typename T>
T operator+(T const lh, T const rh) {
return T(0 + lh + rh);