diff options
| -rw-r--r-- | include/io/stream_reader.h | 7 | ||||
| -rw-r--r-- | src/io/stream_reader.cpp | 8 | ||||
| -rw-r--r-- | test/io/read_test.cpp | 9 |
3 files changed, 19 insertions, 5 deletions
diff --git a/include/io/stream_reader.h b/include/io/stream_reader.h index 7d97824927..75dba6dddb 100644 --- a/include/io/stream_reader.h +++ b/include/io/stream_reader.h @@ -25,6 +25,7 @@ #include <iosfwd> #include <memory> #include <stdexcept> +#include <utility> namespace nbt { @@ -56,6 +57,12 @@ public: endian::endian get_endian() const; /** + * @brief Reads a named tag from the stream + * @throw input_error on failure + */ + std::pair<std::string, std::unique_ptr<tag>> read_tag(); + + /** * @brief Reads a tag of the given type without name from the stream * @throw input_error on failure */ diff --git a/src/io/stream_reader.cpp b/src/io/stream_reader.cpp index ff0fe183ea..541a416d83 100644 --- a/src/io/stream_reader.cpp +++ b/src/io/stream_reader.cpp @@ -39,6 +39,14 @@ endian::endian stream_reader::get_endian() const return endian; } +std::pair<std::string, std::unique_ptr<tag>> stream_reader::read_tag() +{ + tag_type type = read_type(); + std::string key = read_string(); + std::unique_ptr<tag> t = read_payload(type); + return {std::move(key), std::move(t)}; +} + std::unique_ptr<tag> stream_reader::read_payload(tag_type type) { std::unique_ptr<tag> t = tag::create(type); diff --git a/test/io/read_test.cpp b/test/io/read_test.cpp index a6de378e1d..0b29483f1e 100644 --- a/test/io/read_test.cpp +++ b/test/io/read_test.cpp @@ -104,12 +104,11 @@ void test_read_bigtest() ASSERT(file); nbt::io::stream_reader reader(file); - ASSERT(reader.read_type() == tag_type::Compound); - ASSERT(reader.read_string() == "Level"); - auto tagptr = reader.read_payload(tag_type::Compound); + auto pair = reader.read_tag(); + ASSERT(pair.first == "Level"); - ASSERT(tagptr->get_type() == tag_type::Compound); - const tag_compound& comp = tagptr->as<tag_compound>(); + ASSERT(pair.second->get_type() == tag_type::Compound); + const tag_compound& comp = pair.second->as<tag_compound>(); ASSERT(comp.size() == 13); |
