summaryrefslogtreecommitdiff
path: root/libnbtplusplus/include/io/stream_reader.h
diff options
context:
space:
mode:
authorMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 19:56:58 +0300
committerMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 19:56:58 +0300
commita4b5ffbaadb591066e2a97f8d450fb1d93e56a6e (patch)
treeae7c5841f264eea66484f9a413111ce012fa7a86 /libnbtplusplus/include/io/stream_reader.h
parent7fb132859fda54aa96bc9dd46d302b343eeb5a02 (diff)
parent1a0ffe372f4da8408c5d08a36013536a3396b9e6 (diff)
downloadProject-Tick-a4b5ffbaadb591066e2a97f8d450fb1d93e56a6e.tar.gz
Project-Tick-a4b5ffbaadb591066e2a97f8d450fb1d93e56a6e.zip
Add 'libnbtplusplus/' from commit '1a0ffe372f4da8408c5d08a36013536a3396b9e6'
git-subtree-dir: libnbtplusplus git-subtree-mainline: 7fb132859fda54aa96bc9dd46d302b343eeb5a02 git-subtree-split: 1a0ffe372f4da8408c5d08a36013536a3396b9e6
Diffstat (limited to 'libnbtplusplus/include/io/stream_reader.h')
-rw-r--r--libnbtplusplus/include/io/stream_reader.h150
1 files changed, 150 insertions, 0 deletions
diff --git a/libnbtplusplus/include/io/stream_reader.h b/libnbtplusplus/include/io/stream_reader.h
new file mode 100644
index 0000000000..05f8d99c92
--- /dev/null
+++ b/libnbtplusplus/include/io/stream_reader.h
@@ -0,0 +1,150 @@
+/*
+ * SPDX-FileCopyrightText: 2013, 2015 ljfa-ag <ljfa-ag@web.de>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ */
+
+/*
+ * 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 "tag_compound.h"
+#include <iosfwd>
+#include <memory>
+#include <stdexcept>
+#include <utility>
+
+namespace nbt
+{
+ namespace io
+ {
+
+ /// Exception that gets thrown when reading is not successful
+ class NBT_EXPORT input_error : public std::runtime_error
+ {
+ using std::runtime_error::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
+ */
+ NBT_EXPORT 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
+ */
+ NBT_EXPORT 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 NBT_EXPORT stream_reader
+ {
+ public:
+ /**
+ * @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
+ */
+ explicit 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;
+
+ /**
+ * @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
+ */
+ 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
+ */
+ std::unique_ptr<tag> read_payload(tag_type type);
+
+ /**
+ * @brief Reads a tag type from the stream
+ * @param allow_end whether to consider tag_type::End valid
+ * @throw input_error on failure
+ */
+ tag_type read_type(bool allow_end = false);
+
+ /**
+ * @brief Reads a binary number from the stream
+ *
+ * On failure, will set the failbit on 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.
+ * @throw input_error on failure
+ */
+ std::string read_string();
+
+ private:
+ std::istream& is;
+ int depth = 0;
+ const endian::endian endian;
+ };
+
+ template <class T> void stream_reader::read_num(T& x)
+ {
+ endian::read(is, x, endian);
+ }
+
+ } // namespace io
+} // namespace nbt
+
+#endif // STREAM_READER_H_INCLUDED