summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/tag_array.h2
-rw-r--r--include/tag_compound.h2
-rw-r--r--include/tag_list.h2
-rw-r--r--include/tag_primitive.h2
-rw-r--r--include/tag_string.h2
-rw-r--r--src/tag_array.cpp37
-rw-r--r--src/tag_compound.cpp14
-rw-r--r--src/tag_list.cpp18
-rw-r--r--src/tag_primitive.cpp14
-rw-r--r--src/tag_string.cpp6
10 files changed, 99 insertions, 0 deletions
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<T> 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 <http://www.gnu.org/licenses/>.
*/
#include "tag_array.h"
+#include "io/stream_reader.h"
+#include <istream>
namespace nbt
{
@@ -99,6 +101,41 @@ template<class T> auto tag_array<T>::end() const -> const_iterator { return d
template<class T> auto tag_array<T>::cbegin() const -> const_iterator { return data.cbegin(); }
template<class T> auto tag_array<T>::cend() const -> const_iterator { return data.cend(); }
+//Slightly different between byte_array and int_array
+template<>
+void tag_array<int8_t>::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<char*>(data.data()), length);
+ if(!reader.get_istr())
+ throw io::stream_reader::input_error("Error reading contents of tag_byte_array");
+}
+
+template<>
+void tag_array<int32_t>::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<class T>
bool operator==(const tag_array<T>& lhs, const tag_array<T>& 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 <http://www.gnu.org/licenses/>.
*/
#include "tag_compound.h"
+#include "io/stream_reader.h"
+#include <istream>
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 <istream>
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 <http://www.gnu.org/licenses/>.
*/
#include "tag_primitive.h"
+#include "io/stream_reader.h"
#include <limits>
+#include <sstream>
namespace nbt
{
@@ -63,6 +65,18 @@ T tag_primitive<T>::get() const
}
template<class T>
+void tag_primitive<T>::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<class T>
bool operator==(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs)
{
return lhs.get() == rhs.get();
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 <http://www.gnu.org/licenses/>.
*/
#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();