forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileloader.cpp
107 lines (94 loc) · 2.73 KB
/
fileloader.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
// Copyright 2022 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#include "otpch.h"
#include <stack>
#include "fileloader.h"
namespace OTB {
constexpr Identifier wildcard = {{'\0', '\0', '\0', '\0'}};
Loader::Loader(const std::string& fileName, const Identifier& acceptedIdentifier):
fileContents(fileName)
{
constexpr auto minimalSize = sizeof(Identifier) + sizeof(Node::START) + sizeof(Node::type) + sizeof(Node::END);
if (fileContents.size() <= minimalSize) {
throw InvalidOTBFormat{};
}
Identifier fileIdentifier;
std::copy(fileContents.begin(), fileContents.begin() + fileIdentifier.size(), fileIdentifier.begin());
if (fileIdentifier != acceptedIdentifier && fileIdentifier != wildcard) {
throw InvalidOTBFormat{};
}
}
using NodeStack = std::stack<Node*, std::vector<Node*>>;
static Node& getCurrentNode(const NodeStack& nodeStack) {
if (nodeStack.empty()) {
throw InvalidOTBFormat{};
}
return *nodeStack.top();
}
const Node& Loader::parseTree()
{
auto it = fileContents.begin() + sizeof(Identifier);
if (static_cast<uint8_t>(*it) != Node::START) {
throw InvalidOTBFormat{};
}
root.type = *(++it);
root.propsBegin = ++it;
NodeStack parseStack;
parseStack.push(&root);
for (; it != fileContents.end(); ++it) {
switch (static_cast<uint8_t>(*it)) {
case Node::START: {
auto& currentNode = getCurrentNode(parseStack);
if (currentNode.children.empty()) {
currentNode.propsEnd = it;
}
currentNode.children.emplace_back();
auto& child = currentNode.children.back();
if (++it == fileContents.end()) {
throw InvalidOTBFormat{};
}
child.type = *it;
child.propsBegin = it + sizeof(Node::type);
parseStack.push(&child);
break;
}
case Node::END: {
auto& currentNode = getCurrentNode(parseStack);
if (currentNode.children.empty()) {
currentNode.propsEnd = it;
}
parseStack.pop();
break;
}
case Node::ESCAPE: {
if (++it == fileContents.end()) {
throw InvalidOTBFormat{};
}
break;
}
default: {
break;
}
}
}
if (!parseStack.empty()) {
throw InvalidOTBFormat{};
}
return root;
}
bool Loader::getProps(const Node& node, PropStream& props)
{
auto size = std::distance(node.propsBegin, node.propsEnd);
if (size == 0) {
return false;
}
propBuffer.resize(size);
bool lastEscaped = false;
auto escapedPropEnd = std::copy_if(node.propsBegin, node.propsEnd, propBuffer.begin(), [&lastEscaped](const char& byte) {
lastEscaped = byte == static_cast<char>(Node::ESCAPE) && !lastEscaped;
return !lastEscaped;
});
props.init(&propBuffer[0], std::distance(propBuffer.begin(), escapedPropEnd));
return true;
}
} //namespace OTB