1 /*
2 * libnbt++ - A library for the Minecraft Named Binary Tag format.
3 * Copyright (C) 2013, 2015 ljfa-ag
4 *
5 * This file is part of libnbt++.
6 *
7 * libnbt++ is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * libnbt++ is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #include "io/stream_reader.h"
21 #include "make_unique.h"
22 #include "tag_compound.h"
23 #include <istream>
24
25 namespace nbt
26 {
27 namespace io
28 {
29
read_compound(std::istream & is,endian::endian e)30 std::pair<std::string, std::unique_ptr<tag_compound>> read_compound(std::istream& is, endian::endian e)
31 {
32 return stream_reader(is, e).read_compound();
33 }
34
read_tag(std::istream & is,endian::endian e)35 std::pair<std::string, std::unique_ptr<tag>> read_tag(std::istream& is, endian::endian e)
36 {
37 return stream_reader(is, e).read_tag();
38 }
39
stream_reader(std::istream & is,endian::endian e)40 stream_reader::stream_reader(std::istream& is, endian::endian e) noexcept:
41 is(is), endian(e)
42 {}
43
get_istr() const44 std::istream& stream_reader::get_istr() const
45 {
46 return is;
47 }
48
get_endian() const49 endian::endian stream_reader::get_endian() const
50 {
51 return endian;
52 }
53
read_compound()54 std::pair<std::string, std::unique_ptr<tag_compound>> stream_reader::read_compound()
55 {
56 if(read_type() != tag_type::Compound)
57 {
58 is.setstate(std::ios::failbit);
59 throw input_error("Tag is not a compound");
60 }
61 std::string key = read_string();
62 auto comp = make_unique<tag_compound>();
63 comp->read_payload(*this);
64 return {std::move(key), std::move(comp)};
65 }
66
read_tag()67 std::pair<std::string, std::unique_ptr<tag>> stream_reader::read_tag()
68 {
69 tag_type type = read_type();
70 std::string key = read_string();
71 std::unique_ptr<tag> t = read_payload(type);
72 return {std::move(key), std::move(t)};
73 }
74
read_payload(tag_type type)75 std::unique_ptr<tag> stream_reader::read_payload(tag_type type)
76 {
77 std::unique_ptr<tag> t = tag::create(type);
78 t->read_payload(*this);
79 return t;
80 }
81
read_type(bool allow_end)82 tag_type stream_reader::read_type(bool allow_end)
83 {
84 int type = is.get();
85 if(!is)
86 throw input_error("Error reading tag type");
87 if(!is_valid_type(type, allow_end))
88 {
89 is.setstate(std::ios::failbit);
90 throw input_error("Invalid tag type: " + std::to_string(type));
91 }
92 return static_cast<tag_type>(type);
93 }
94
read_string()95 std::string stream_reader::read_string()
96 {
97 uint16_t len;
98 read_num(len);
99 if(!is)
100 throw input_error("Error reading string");
101
102 std::string ret(len, '\0');
103 is.read(&ret[0], len); //C++11 allows us to do this
104 if(!is)
105 throw input_error("Error reading string");
106 return ret;
107 }
108
109 }
110 }
111