From 43842d2246731a1fab79d9326bd5bec8098505d1 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Wed, 5 Aug 2015 14:01:13 +0200 Subject: Implement read_payload for all tags --- include/tag_array.h | 2 ++ include/tag_compound.h | 2 ++ include/tag_list.h | 2 ++ include/tag_primitive.h | 2 ++ include/tag_string.h | 2 ++ src/tag_array.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/tag_compound.cpp | 14 ++++++++++++++ src/tag_list.cpp | 18 ++++++++++++++++++ src/tag_primitive.cpp | 14 ++++++++++++++ src/tag_string.cpp | 6 ++++++ 10 files changed, 99 insertions(+) diff --git a/include/tag_array.h b/include/tag_array.h index 9a79489c56..5ce7dd366e 100644 --- a/include/tag_array.h +++ b/include/tag_array.h @@ -104,6 +104,8 @@ public: const_iterator cbegin() const; const_iterator cend() const; + void read_payload(io::stream_reader& reader) override; + private: std::vector data; }; diff --git a/include/tag_compound.h b/include/tag_compound.h index 8e09ff4834..60376d2446 100644 --- a/include/tag_compound.h +++ b/include/tag_compound.h @@ -117,6 +117,8 @@ public: const_iterator cbegin() const; const_iterator cend() const; + void read_payload(io::stream_reader& reader) override; + friend bool operator==(const tag_compound& lhs, const tag_compound& rhs); friend bool operator!=(const tag_compound& lhs, const tag_compound& rhs); diff --git a/include/tag_list.h b/include/tag_list.h index ac35b1d40d..324d2de6d8 100644 --- a/include/tag_list.h +++ b/include/tag_list.h @@ -158,6 +158,8 @@ public: const_iterator cbegin() const; const_iterator cend() const; + void read_payload(io::stream_reader& reader) override; + /** * @brief Equality comparison for lists * diff --git a/include/tag_primitive.h b/include/tag_primitive.h index ac41568920..b1bb9920c0 100644 --- a/include/tag_primitive.h +++ b/include/tag_primitive.h @@ -53,6 +53,8 @@ public: tag_primitive& operator=(T value); void set(T value); + void read_payload(io::stream_reader& reader) override; + private: T value; }; diff --git a/include/tag_string.h b/include/tag_string.h index 0cb057125f..2a8a3d3f4e 100644 --- a/include/tag_string.h +++ b/include/tag_string.h @@ -51,6 +51,8 @@ public: void set(const std::string& str); void set(std::string&& str); + void read_payload(io::stream_reader& reader) override; + private: std::string value; }; diff --git a/src/tag_array.cpp b/src/tag_array.cpp index a944899402..f521768dd5 100644 --- a/src/tag_array.cpp +++ b/src/tag_array.cpp @@ -18,6 +18,8 @@ * along with libnbt++. If not, see . */ #include "tag_array.h" +#include "io/stream_reader.h" +#include namespace nbt { @@ -99,6 +101,41 @@ template auto tag_array::end() const -> const_iterator { return d template auto tag_array::cbegin() const -> const_iterator { return data.cbegin(); } template auto tag_array::cend() const -> const_iterator { return data.cend(); } +//Slightly different between byte_array and int_array +template<> +void tag_array::read_payload(io::stream_reader& reader) +{ + int32_t length; + reader.read_num(length); + if(length < 0 || !reader.get_istr()) + throw io::stream_reader::input_error("Error reading length of tag_byte_array"); + + data.resize(length); + reader.get_istr().read(reinterpret_cast(data.data()), length); + if(!reader.get_istr()) + throw io::stream_reader::input_error("Error reading contents of tag_byte_array"); +} + +template<> +void tag_array::read_payload(io::stream_reader& reader) +{ + int32_t length; + reader.read_num(length); + if(length < 0 || !reader.get_istr()) + throw io::stream_reader::input_error("Error reading length of tag_int_array"); + + data.clear(); + data.reserve(length); + for(int32_t i = 0; i < length; ++i) + { + int32_t val; + reader.read_num(val); + data.push_back(val); + } + if(!reader.get_istr()) + throw io::stream_reader::input_error("Error reading contents of tag_int_array"); +} + template bool operator==(const tag_array& lhs, const tag_array& rhs) { diff --git a/src/tag_compound.cpp b/src/tag_compound.cpp index d3eaf06440..f6aeea5e79 100644 --- a/src/tag_compound.cpp +++ b/src/tag_compound.cpp @@ -18,6 +18,8 @@ * along with libnbt++. If not, see . */ #include "tag_compound.h" +#include "io/stream_reader.h" +#include namespace nbt { @@ -95,6 +97,18 @@ auto tag_compound::end() const -> const_iterator { return tags.end(); } auto tag_compound::cbegin() const -> const_iterator { return tags.cbegin(); } auto tag_compound::cend() const -> const_iterator { return tags.cend(); } +void tag_compound::read_payload(io::stream_reader& reader) +{ + clear(); + tag_type tt; + while((tt = reader.read_type(true)) != tag_type::End) + { + std::string key = reader.read_string(); + auto tptr = reader.read_payload(tt); + tags.emplace(key, value(std::move(tptr))); + } +} + bool operator==(const tag_compound& lhs, const tag_compound& rhs) { return lhs.tags == rhs.tags; diff --git a/src/tag_list.cpp b/src/tag_list.cpp index 7900f9872e..c5d9eb5901 100644 --- a/src/tag_list.cpp +++ b/src/tag_list.cpp @@ -19,6 +19,8 @@ */ #include "tag_list.h" #include "nbt_tags.h" +#include "io/stream_reader.h" +#include namespace nbt { @@ -130,6 +132,22 @@ auto tag_list::end() const -> const_iterator { return tags.end(); } auto tag_list::cbegin() const -> const_iterator { return tags.cbegin(); } auto tag_list::cend() const -> const_iterator { return tags.cend(); } +void tag_list::read_payload(io::stream_reader& reader) +{ + tag_type lt = reader.read_type(); + + int32_t length; + reader.read_num(length); + if(length < 0 || !reader.get_istr()) + throw io::stream_reader::input_error("Error reading length of tag_list"); + + reset(lt); + tags.reserve(length); + + for(int32_t i = 0; i < length; ++i) + tags.emplace_back(reader.read_payload(lt)); +} + bool operator==(const tag_list& lhs, const tag_list& rhs) { return lhs.el_type_ == rhs.el_type_ && lhs.tags == rhs.tags; diff --git a/src/tag_primitive.cpp b/src/tag_primitive.cpp index 69bd9695bd..15fc87d752 100644 --- a/src/tag_primitive.cpp +++ b/src/tag_primitive.cpp @@ -18,7 +18,9 @@ * along with libnbt++. If not, see . */ #include "tag_primitive.h" +#include "io/stream_reader.h" #include +#include namespace nbt { @@ -62,6 +64,18 @@ T tag_primitive::get() const return value; } +template +void tag_primitive::read_payload(io::stream_reader& reader) +{ + reader.read_num(value); + if(!reader.get_istr()) + { + std::ostringstream str; + str << "Error reading " << type; + throw io::stream_reader::input_error(str.str()); + } +} + template bool operator==(const tag_primitive& lhs, const tag_primitive& rhs) { diff --git a/src/tag_string.cpp b/src/tag_string.cpp index 58635d5e0a..4671339561 100644 --- a/src/tag_string.cpp +++ b/src/tag_string.cpp @@ -18,6 +18,7 @@ * along with libnbt++. If not, see . */ #include "tag_string.h" +#include "io/stream_reader.h" namespace nbt { @@ -77,6 +78,11 @@ void tag_string::set(std::string&& str) value = std::move(str); } +void tag_string::read_payload(io::stream_reader& reader) +{ + value = reader.read_string(); +} + bool operator==(const tag_string& lhs, const tag_string& rhs) { return lhs.get() == rhs.get(); -- cgit 0.0.5-2-1-g0f52