From 84e7f93b8760980af5fea0475b4550f8be04dae2 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Wed, 12 Aug 2015 13:07:49 +0200 Subject: Lay foundations for stream writing --- CMakeLists.txt | 1 + include/io/stream_writer.h | 90 ++++++++++++++++++++++++++++++++++++++++++++++ src/io/stream_writer.cpp | 30 ++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 include/io/stream_writer.h create mode 100644 src/io/stream_writer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8524bcdef4..e5c971321d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_library(nbt++ STATIC src/value_initializer.cpp src/io/stream_reader.cpp + src/io/stream_writer.cpp src/text/json_formatter.cpp) diff --git a/include/io/stream_writer.h b/include/io/stream_writer.h new file mode 100644 index 0000000000..ea3dcd1c58 --- /dev/null +++ b/include/io/stream_writer.h @@ -0,0 +1,90 @@ +/* + * 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_WRITER_H_INCLUDED +#define STREAM_WRITER_H_INCLUDED + +#include "tag.h" +#include "endian_str.h" +#include +#include + +namespace nbt +{ +namespace io +{ + +///Exception that gets thrown when writing is not successful +class output_error : public std::runtime_error +{ + using std::runtime_error::runtime_error; +}; + +/** + * @brief Helper class for writing NBT tags to output streams + */ +class stream_writer +{ +public: + /** + * @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 + */ + explicit stream_writer(std::ostream& os, endian::endian e = endian::big) noexcept; + + ///Returns the stream + std::ostream& get_ostr() const; + ///Returns the byte order + endian::endian get_endian() const; + + /** + * @brief Writes a tag type to the stream + */ + void write_type(tag_type tt); + + /** + * @brief Writes a binary number to the stream + */ + template + void write_num(T x); + + /** + * @brief Writes an NBT string to the stream + * + * An NBT string consists of two bytes indicating the length, followed by + * the characters encoded in modified UTF-8. + */ + void write_string(const std::string& str); + +private: + std::ostream& os; + const endian::endian endian; +}; + +template +void stream_writer::write_num(T x) +{ + endian::write(os, x, endian); +} + +} +} + +#endif // STREAM_WRITER_H_INCLUDED diff --git a/src/io/stream_writer.cpp b/src/io/stream_writer.cpp new file mode 100644 index 0000000000..dd90c83322 --- /dev/null +++ b/src/io/stream_writer.cpp @@ -0,0 +1,30 @@ +/* + * 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 . + */ +#include "io/stream_writer.h" + +namespace nbt +{ +namespace io +{ + + + +} +} -- cgit 0.0.5-2-1-g0f52