summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorljfa <ljfa-ag@web.de>2015-08-13 11:31:57 +0200
committerljfa <ljfa-ag@web.de>2015-08-13 11:31:57 +0200
commitaac94b91ad83ad8f7832976e6da55d15de6e54e1 (patch)
tree9caa79222933a300354c1cd2224915da471e94f5
parentbaf4a77df0ac95386b80bd915c2926c86e2c5a46 (diff)
downloadProject-Tick-aac94b91ad83ad8f7832976e6da55d15de6e54e1.tar.gz
Project-Tick-aac94b91ad83ad8f7832976e6da55d15de6e54e1.zip
Add io::read_tag and write_tag functions
-rw-r--r--CMakeLists.txt2
-rw-r--r--include/io/stream_reader.h20
-rw-r--r--include/io/stream_writer.h12
-rw-r--r--src/io/stream_reader.cpp10
-rw-r--r--src/io/stream_writer.cpp5
-rw-r--r--test/read_test.cpp6
-rw-r--r--test/write_test.cpp10
7 files changed, 55 insertions, 10 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d67e0be642..a9fdebbcf9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.0)
project(libnbt++
- VERSION 2.0.1)
+ VERSION 2.1)
add_definitions(-std=c++11)
include_directories(include)
diff --git a/include/io/stream_reader.h b/include/io/stream_reader.h
index 926d2453c6..877f6498ed 100644
--- a/include/io/stream_reader.h
+++ b/include/io/stream_reader.h
@@ -40,7 +40,27 @@ class input_error : public std::runtime_error
};
/**
+* @brief Reads a named tag from the stream, making sure that it is a compound
+* @param is the stream to read from
+* @param e the byte order of the source data. The Java edition
+* of Minecraft uses Big Endian, the Pocket edition uses Little Endian
+* @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(std::istream& is, endian::endian e = endian::big);
+
+/**
+* @brief Reads a named tag from the stream
+* @param is the stream to read from
+* @param e the byte order of the source data. The Java edition
+* of Minecraft uses Big Endian, the Pocket edition uses Little Endian
+* @throw input_error on failure
+*/
+std::pair<std::string, std::unique_ptr<tag>> read_tag(std::istream& is, endian::endian e = endian::big);
+
+/**
* @brief Helper class for reading NBT tags from input streams
+ *
+ * Can be reused to read multiple tags
*/
class stream_reader
{
diff --git a/include/io/stream_writer.h b/include/io/stream_writer.h
index c94a7d4ece..c771426bb3 100644
--- a/include/io/stream_writer.h
+++ b/include/io/stream_writer.h
@@ -37,7 +37,19 @@ class output_error : public std::runtime_error
};*/
/**
+* @brief Writes a named tag into the stream, including the tag type
+* @param key the name of the tag
+* @param t the tag
+* @param os the stream to write to
+* @param e the byte order of the written data. The Java edition
+* of Minecraft uses Big Endian, the Pocket edition uses Little Endian
+*/
+void write_tag(const std::string& key, const tag& t, std::ostream& os, endian::endian e = endian::big);
+
+/**
* @brief Helper class for writing NBT tags to output streams
+ *
+ * Can be reused to write multiple tags
*/
class stream_writer
{
diff --git a/src/io/stream_reader.cpp b/src/io/stream_reader.cpp
index 15f29dba1d..f6f30a5b49 100644
--- a/src/io/stream_reader.cpp
+++ b/src/io/stream_reader.cpp
@@ -27,6 +27,16 @@ namespace nbt
namespace io
{
+std::pair<std::string, std::unique_ptr<tag_compound>> read_compound(std::istream& is, endian::endian e)
+{
+ return stream_reader(is, e).read_compound();
+}
+
+std::pair<std::string, std::unique_ptr<tag>> read_tag(std::istream& is, endian::endian e)
+{
+ return stream_reader(is, e).read_tag();
+}
+
stream_reader::stream_reader(std::istream& is, endian::endian e) noexcept:
is(is), endian(e)
{}
diff --git a/src/io/stream_writer.cpp b/src/io/stream_writer.cpp
index f21fdb701c..ee8b0ee2b6 100644
--- a/src/io/stream_writer.cpp
+++ b/src/io/stream_writer.cpp
@@ -25,6 +25,11 @@ namespace nbt
namespace io
{
+void write_tag(const std::string& key, const tag& t, std::ostream& os, endian::endian e)
+{
+ stream_writer(os, e).write_tag(key, t);
+}
+
void stream_writer::write_tag(const std::string& key, const tag& t)
{
write_type(t.get_type());
diff --git a/test/read_test.cpp b/test/read_test.cpp
index b9d8c59f6c..90ee392451 100644
--- a/test/read_test.cpp
+++ b/test/read_test.cpp
@@ -141,9 +141,8 @@ void test_read_bigtest()
//Uses an extended variant of Notch's original bigtest file
std::ifstream file("bigtest_uncompr", std::ios::binary);
ASSERT(file);
- nbt::io::stream_reader reader(file);
- auto pair = reader.read_compound();
+ auto pair = nbt::io::read_compound(file);
ASSERT(pair.first == "Level");
verify_bigtest_structure(*pair.second);
std::clog << "test_read_bigtest passed" << std::endl;
@@ -154,9 +153,8 @@ void test_read_littletest()
//Same as bigtest, but little endian
std::ifstream file("littletest_uncompr", std::ios::binary);
ASSERT(file);
- nbt::io::stream_reader reader(file, endian::little);
- auto pair = reader.read_compound();
+ auto pair = nbt::io::read_compound(file, endian::little);
ASSERT(pair.first == "Level");
ASSERT(pair.second->get_type() == tag_type::Compound);
verify_bigtest_structure(*pair.second);
diff --git a/test/write_test.cpp b/test/write_test.cpp
index 78d6d8eaa6..d6eac53a53 100644
--- a/test/write_test.cpp
+++ b/test/write_test.cpp
@@ -206,25 +206,25 @@ void test_write_bigtest()
written tag.
Smaller-grained tests are already done above. */
std::ifstream file("bigtest_uncompr", std::ios::binary);
- const auto orig_pair = io::stream_reader(file).read_compound();
+ const auto orig_pair = io::read_compound(file);
std::stringstream sstr;
//Write into stream in Big Endian
- io::stream_writer(sstr).write_tag(orig_pair.first, *orig_pair.second);
+ io::write_tag(orig_pair.first, *orig_pair.second, sstr);
ASSERT(sstr);
//Read from stream in Big Endian and compare
- auto written_pair = io::stream_reader(sstr).read_compound();
+ auto written_pair = io::read_compound(sstr);
ASSERT(orig_pair.first == written_pair.first);
ASSERT(*orig_pair.second == *written_pair.second);
sstr.str(""); //Reset and reuse stream
//Write into stream in Little Endian
- io::stream_writer(sstr, endian::little).write_tag(orig_pair.first, *orig_pair.second);
+ io::write_tag(orig_pair.first, *orig_pair.second, sstr, endian::little);
ASSERT(sstr);
//Read from stream in Little Endian and compare
- written_pair = io::stream_reader(sstr, endian::little).read_compound();
+ written_pair = io::read_compound(sstr, endian::little);
ASSERT(orig_pair.first == written_pair.first);
ASSERT(*orig_pair.second == *written_pair.second);