diff options
| author | ljfa <ljfa-ag@web.de> | 2015-08-07 15:24:13 +0200 |
|---|---|---|
| committer | ljfa <ljfa-ag@web.de> | 2015-08-07 15:31:36 +0200 |
| commit | 20fb31d81b3dfcf66867b4660a887f7b113f4833 (patch) | |
| tree | 6ac975a3acce213080c5673962c37edd1fb95956 | |
| parent | c596c4f7ae529e2dd9976c1963634825595c2218 (diff) | |
| download | Project-Tick-20fb31d81b3dfcf66867b4660a887f7b113f4833.tar.gz Project-Tick-20fb31d81b3dfcf66867b4660a887f7b113f4833.zip | |
Add read_compound method
| -rw-r--r-- | include/io/stream_reader.h | 7 | ||||
| -rw-r--r-- | src/io/stream_reader.cpp | 15 | ||||
| -rw-r--r-- | test/read_test.cpp | 17 |
3 files changed, 33 insertions, 6 deletions
diff --git a/include/io/stream_reader.h b/include/io/stream_reader.h index 75dba6dddb..330d9c5c69 100644 --- a/include/io/stream_reader.h +++ b/include/io/stream_reader.h @@ -22,6 +22,7 @@ #include "endian_str.h" #include "tag.h" +#include "tagfwd.h" #include <iosfwd> #include <memory> #include <stdexcept> @@ -57,6 +58,12 @@ public: endian::endian get_endian() const; /** + * @brief Reads a named tag from the stream, making sure that it is a compound + * @throw input_error on failure, or if the tag in the stream is not a compound + */ + std::pair<std::string, std::unique_ptr<tag_compound>> read_compound(); + + /** * @brief Reads a named tag from the stream * @throw input_error on failure */ diff --git a/src/io/stream_reader.cpp b/src/io/stream_reader.cpp index 541a416d83..15f29dba1d 100644 --- a/src/io/stream_reader.cpp +++ b/src/io/stream_reader.cpp @@ -18,6 +18,8 @@ * along with libnbt++. If not, see <http://www.gnu.org/licenses/>. */ #include "io/stream_reader.h" +#include "make_unique.h" +#include "tag_compound.h" #include <istream> namespace nbt @@ -39,6 +41,19 @@ endian::endian stream_reader::get_endian() const return endian; } +std::pair<std::string, std::unique_ptr<tag_compound>> stream_reader::read_compound() +{ + if(read_type() != tag_type::Compound) + { + is.setstate(std::ios::failbit); + throw input_error("Tag is not a compound"); + } + std::string key = read_string(); + auto comp = make_unique<tag_compound>(); + comp->read_payload(*this); + return {std::move(key), std::move(comp)}; +} + std::pair<std::string, std::unique_ptr<tag>> stream_reader::read_tag() { tag_type type = read_type(); diff --git a/test/read_test.cpp b/test/read_test.cpp index ae900a502d..d95c762d43 100644 --- a/test/read_test.cpp +++ b/test/read_test.cpp @@ -140,10 +140,9 @@ void test_read_bigtest() ASSERT(file); nbt::io::stream_reader reader(file); - auto pair = reader.read_tag(); + auto pair = reader.read_compound(); ASSERT(pair.first == "Level"); - ASSERT(pair.second->get_type() == tag_type::Compound); - verify_bigtest_structure(pair.second->as<tag_compound>()); + verify_bigtest_structure(*pair.second); } void test_read_littletest() @@ -153,10 +152,10 @@ void test_read_littletest() ASSERT(file); nbt::io::stream_reader reader(file, endian::little); - auto pair = reader.read_tag(); + auto pair = reader.read_compound(); ASSERT(pair.first == "Level"); ASSERT(pair.second->get_type() == tag_type::Compound); - verify_bigtest_structure(pair.second->as<tag_compound>()); + verify_bigtest_structure(*pair.second); } void test_read_errors() @@ -197,8 +196,14 @@ void test_read_misc() std::ifstream file; nbt::io::stream_reader reader(file); + //Toplevel tag other than compound file.open("toplevel_string", std::ios::binary); - ASSERT(file); + EXPECT_EXCEPTION(reader.read_compound(), io::input_error); + ASSERT(!file); + + //Rewind and try again with read_tag + file.clear(); + ASSERT(file.seekg(0)); auto pair = reader.read_tag(); ASSERT(pair.first == "Test (toplevel tag_string)"); ASSERT(*pair.second == tag_string( |
