summaryrefslogtreecommitdiff
path: root/include/io
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-08-04 18:24:04 +0200
committerljfa-ag <ljfa-ag@web.de>2015-08-04 18:24:04 +0200
commit47531478a60d979faf8d744d17c246c8e40a7d24 (patch)
tree5200eb3e818f418d81ac00af3998dbfb8b118071 /include/io
parent61b2560a524ad7fe7d5e34cfccd70a7e92e7b8b9 (diff)
downloadProject-Tick-47531478a60d979faf8d744d17c246c8e40a7d24.tar.gz
Project-Tick-47531478a60d979faf8d744d17c246c8e40a7d24.zip
Lay foundations for stream_reader class
Diffstat (limited to 'include/io')
-rw-r--r--include/io/stream_reader.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/io/stream_reader.h b/include/io/stream_reader.h
new file mode 100644
index 0000000000..142402a516
--- /dev/null
+++ b/include/io/stream_reader.h
@@ -0,0 +1,79 @@
+/*
+ * libnbt++ - A library for the Minecraft Named Binary Tag format.
+ * Copyright (C) 2013, 2015 ljfa-ag
+ *
+ * This file is part of libnbt++.
+ *
+ * libnbt++ is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * libnbt++ is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef STREAM_READER_H_INCLUDED
+#define STREAM_READER_H_INCLUDED
+
+#include "endian_str.h"
+#include "tag.h"
+#include <iosfwd>
+#include <stdexcept>
+
+namespace nbt
+{
+namespace io
+{
+
+/**
+ * @brief Helper class for reading NBT tags from input streams
+ */
+class stream_reader
+{
+public:
+ ///Exception that gets thrown when reading is not successful
+ class input_error : public std::runtime_error
+ {
+ input_error(const std::string& what_arg):
+ std::runtime_error(what_arg) {}
+ };
+
+ /**
+ * @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
+ */
+ stream_reader(std::istream& is, endian::endian e = endian::big) noexcept;
+
+ ///Returns the stream
+ std::istream& get_istr() const;
+ ///Returns the byte order
+ endian::endian get_endian() const;
+
+ ///Reads a tag type from the stream
+ tag_type read_type(bool allow_end = false);
+ ///Reads a number from the stream
+ template<class T>
+ void read_num(T& x);
+ /**
+ * @brief Reads an NBT string from the stream
+ *
+ * An NBT string consists of two bytes indicating the length, followed by
+ * the characters encoded in modified UTF-8.
+ */
+ std::string read_string();
+
+private:
+ std::istream& is;
+ const endian::endian endian;
+};
+
+}
+}
+
+#endif // STREAM_READER_H_INCLUDED