From 47531478a60d979faf8d744d17c246c8e40a7d24 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 4 Aug 2015 18:24:04 +0200 Subject: Lay foundations for stream_reader class --- include/io/stream_reader.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 include/io/stream_reader.h (limited to 'include/io') 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 . + */ +#ifndef STREAM_READER_H_INCLUDED +#define STREAM_READER_H_INCLUDED + +#include "endian_str.h" +#include "tag.h" +#include +#include + +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 + 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 -- cgit 0.0.5-2-1-g0f52