-
Notifications
You must be signed in to change notification settings - Fork 20
/
node.cpp
331 lines (329 loc) · 11.2 KB
/
node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//////////////////////////////////////////////////////////////////////////////
// NoLifeNx - Part of the NoLifeStory project //
// Copyright © 2013 Peter Atashian //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program 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 Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include "node_impl.hpp"
#include "file_impl.hpp"
#include "bitmap.hpp"
#include "audio.hpp"
#include <cstring>
#include <stdexcept>
#include <vector>
#include <sstream>
namespace nl {
node::node(node const & o) :
m_data(o.m_data), m_file(o.m_file) {}
node::node(data const * d, file::data const * f) :
m_data(d), m_file(f) {}
node node::begin() const {
if (!m_data)
return {nullptr, m_file};
return {m_file->node_table + m_data->children, m_file};
}
node node::end() const {
if (!m_data)
return {nullptr, m_file};
return {m_file->node_table + m_data->children + m_data->num, m_file};
}
node node::operator*() const {
return *this;
}
node & node::operator++() {
++m_data;
return *this;
}
node node::operator++(int) {
return {m_data++, m_file};
}
bool node::operator==(node const & o) const {
return m_data == o.m_data;
}
bool node::operator!=(node const & o) const {
return m_data != o.m_data;
}
bool node::operator<(node const & o) const {
return m_data < o.m_data;
}
std::string operator+(std::string s, node n) {
return s + n.get_string();
}
std::string operator+(char const * s, node n) {
return s + n.get_string();
}
std::string operator+(node n, std::string s) {
return n.get_string() + s;
}
std::string operator+(node n, char const * s) {
return n.get_string() + s;
}
node node::operator[](unsigned int n) const {
return operator[](std::to_string(n));
}
node node::operator[](signed int n) const {
return operator[](std::to_string(n));
}
node node::operator[](unsigned long n) const {
return operator[](std::to_string(n));
}
node node::operator[](signed long n) const {
return operator[](std::to_string(n));
}
node node::operator[](unsigned long long n) const {
return operator[](std::to_string(n));
}
node node::operator[](signed long long n) const {
return operator[](std::to_string(n));
}
node node::operator[](std::string const & o) const {
return get_child(o.c_str(), static_cast<uint16_t>(o.length()));
}
node node::operator[](char const * o) const {
return get_child(o, static_cast<uint16_t>(std::strlen(o)));
}
node node::operator[](node const & o) const {
return operator[](o.get_string());
}
node::operator unsigned char() const {
return static_cast<unsigned char>(get_integer());
}
node::operator signed char() const {
return static_cast<signed char>(get_integer());
}
node::operator unsigned short() const {
return static_cast<unsigned short>(get_integer());
}
node::operator signed short() const {
return static_cast<signed short>(get_integer());
}
node::operator unsigned int() const {
return static_cast<unsigned int>(get_integer());
}
node::operator signed int() const {
return static_cast<signed int>(get_integer());
}
node::operator unsigned long() const {
return static_cast<unsigned long>(get_integer());
}
node::operator signed long() const {
return static_cast<signed long>(get_integer());
}
node::operator unsigned long long() const {
return static_cast<unsigned long long>(get_integer());
}
node::operator signed long long() const {
return static_cast<signed long long>(get_integer());
}
node::operator float() const {
return static_cast<float>(get_real());
}
node::operator double() const {
return static_cast<double>(get_real());
}
node::operator long double() const {
return static_cast<long double>(get_real());
}
node::operator std::string() const {
return get_string();
}
node::operator vector2i() const {
return get_vector();
}
node::operator bitmap() const {
return get_bitmap();
}
node::operator audio() const {
return get_audio();
}
node::operator bool() const {
return m_data ? true : false;
}
int64_t node::get_integer(int64_t def) const {
if (!m_data)
return def;
switch (m_data->type) {
case type::none:
case type::vector:
case type::bitmap:
case type::audio:
return def;
case type::integer:
return to_integer();
case type::real:
return static_cast<int64_t>(to_real());
case type::string:
return std::stoll(to_string());
default:
throw std::runtime_error("Unknown node type");
}
}
double node::get_real(double def) const {
if (!m_data)
return def;
switch (m_data->type) {
case type::none:
case type::vector:
case type::bitmap:
case type::audio:
return def;
case type::integer:
return static_cast<double>(to_integer());
case type::real:
return to_real();
case type::string:
return std::stod(to_string());
default:
throw std::runtime_error("Unknown node type");
}
}
std::string node::get_string(std::string def) const {
if (!m_data)
return def;
switch (m_data->type) {
case type::none:
case type::vector:
case type::bitmap:
case type::audio:
return def;
case type::integer:
return std::to_string(to_integer());
case type::real:
return std::to_string(to_real());
case type::string:
return to_string();
default:
throw std::runtime_error("Unknown node type");
}
}
vector2i node::get_vector(vector2i def) const {
if (m_data && m_data->type == type::vector)
return to_vector();
return def;
}
bitmap node::get_bitmap() const {
if (m_data && m_data->type == type::bitmap && m_file->header->bitmap_count)
return to_bitmap();
return {nullptr, 0, 0};
}
audio node::get_audio() const {
if (m_data && m_data->type == type::audio && m_file->header->audio_count)
return to_audio();
return {nullptr, 0};
}
bool node::get_bool() const {
return m_data && m_data->type == type::integer && to_integer() ? true : false;
}
bool node::get_bool(bool def) const {
return m_data && m_data->type == type::integer ? to_integer() ? true : false : def;
}
int32_t node::x() const {
return m_data && m_data->type == type::vector ? m_data->vector[0] : 0;
}
int32_t node::y() const {
return m_data && m_data->type == type::vector ? m_data->vector[1] : 0;
}
std::string node::name() const {
if (!m_data)
return {};
auto const s = reinterpret_cast<char const *>(m_file->base)
+ m_file->string_table[m_data->name];
return {s + 2, *reinterpret_cast<uint16_t const *>(s)};
}
size_t node::size() const {
return m_data ? m_data->num : 0u;
}
node::type node::data_type() const {
return m_data ? m_data->type : type::none;
}
node node::get_child(char const * const o, uint16_t const l) const {
if (!m_data)
return {nullptr, m_file};
auto p = m_file->node_table + m_data->children;
auto n = m_data->num;
auto const b = reinterpret_cast<const char *>(m_file->base);
auto const t = m_file->string_table;
for (;;) {
if (!n)
return {nullptr, m_file};
auto const n2 = static_cast<decltype(n)>(n >> 1);
auto const p2 = p + n2;
auto const sl = b + t[p2->name];
auto const l1 = *reinterpret_cast<uint16_t const *>(sl);
auto const s = reinterpret_cast<uint8_t const *>(sl + 2);
auto const os = reinterpret_cast<uint8_t const *>(o);
bool z = false;
auto const len = l1 < l ? l1 : l;
for (auto i = 0U; i < len; ++i) {
if (s[i] > os[i]) {
n = n2;
z = true;
break;
} else if (s[i] < os[i]) {
p = p2 + 1;
n -= n2 + 1;
z = true;
break;
}
}
if (z)
continue;
else if (l1 < l)
p = p2 + 1, n -= n2 + 1;
else if (l1 > l)
n = n2;
else
return {p2, m_file};
}
}
int64_t node::to_integer() const {
return m_data->ireal;
}
double node::to_real() const {
return m_data->dreal;
}
std::string node::to_string() const {
auto const s = reinterpret_cast<char const *>(m_file->base)
+ m_file->string_table[m_data->string];
return {s + 2, *reinterpret_cast<uint16_t const *>(s)};
}
vector2i node::to_vector() const {
return {m_data->vector[0], m_data->vector[1]};
}
bitmap node::to_bitmap() const {
return {reinterpret_cast<char const *>(m_file->base)
+ m_file->bitmap_table[m_data->bitmap.index],
m_data->bitmap.width, m_data->bitmap.height};
}
audio node::to_audio() const {
return {reinterpret_cast<char const *>(m_file->base)
+ m_file->audio_table[m_data->audio.index],
m_data->audio.length};
}
node node::root() const {
return {m_file->node_table, m_file};
}
node node::resolve(std::string path) const {
std::istringstream stream(path);
std::vector<std::string> parts;
std::string segment;
while (std::getline(stream, segment, '/'))
parts.push_back(segment);
auto n = *this;
for (auto & part : parts) {
n = n[part];
}
return n;
}
}