Repository navigation
Expand file tree
/
Copy pathXmlBinGenerator.cpp
More file actions
173 lines (157 loc) · 5.41 KB
/
Copy pathXmlBinGenerator.cpp
File metadata and controls
173 lines (157 loc) · 5.41 KB
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
#include <span>
#include <vector>
#include <algorithm>
#include <tinyxml2.h>
#include "XmlBin.hpp"
#include "Input.hpp"
#include "Output.hpp"
#include "Sort.hpp"
using namespace xmlbin;
struct SegmentAlloc {
ByteOffset offset = 0;
Segment alloc(ByteOffset align, ByteOffset size, Size count) {
const auto alignMask = align - 1u;
if (offset & alignMask)
offset = (offset & ~alignMask) + align;
const auto old = offset;
offset += size * count;
return {old, count};
}
template<class T>
Segment alloc(Size count) {
return alloc(alignof(T), sizeof(T), count);
}
template<class T>
Segment allocFor(const std::vector<T>& list) {
return alloc<T>(Size(list.size()));
}
};
template<class T>
void writeList(Output& os, const std::vector<T>& list) {
os.write(list.data(), sizeof(T) * list.size());
}
template<class T>
std::span<T> getList(std::vector<T>& list, Sequence<T> seq) {
return {list.data() + seq.start.value, seq.count};
}
struct Builder {
std::vector<const char*> strings;
std::vector<UniqueStrEntry> uniqueStrings;
std::vector<NodeId> nodes;
std::vector<Attribute> attrs;
std::vector<Element> elems;
Size stringSize = 1;
StrId getStr(std::string_view str) {
strings.push_back(str.data());
const auto id = stringSize;
stringSize += Size(str.size()) + 1;
return {id};
}
StrId getUniqueStr(std::string_view str) {
const auto getStr = [this](UniqueStrEntry entry) {
return strings[entry.idx];
};
auto pos = std::ranges::lower_bound(uniqueStrings, str,
std::ranges::less{}, getStr);
if (pos == uniqueStrings.end() || str != getStr(*pos)) {
pos =
uniqueStrings.insert(pos, {Index(strings.size()), stringSize});
strings.push_back(str.data());
stringSize += Size(str.size()) + 1;
}
return {pos->offset};
}
Attribute buildAttr(const tinyxml2::XMLAttribute& attr) {
Attribute a;
a.name = getUniqueStr(attr.Name());
a.value = getStr(attr.Value());
return a;
}
void buildElem(const tinyxml2::XMLElement& elem) {
Element e;
e.tag = getUniqueStr(elem.Name());
std::vector<Attribute> attrList;
for (auto p = elem.FirstAttribute(); p; p = p->Next()) {
attrList.push_back(buildAttr(*p));
}
std::ranges::sort(attrList, std::ranges::less{},
[](Attribute& a) { return a.name; });
e.attrs = {Index(attrs.size()), Size(attrList.size())};
attrs.resize(attrs.size() + attrList.size());
eytzinger(e.attrs.count, [in = attrList.data(),
out = attrs.data() + e.attrs.start.value](
unsigned k) mutable { out[k] = *in++; });
auto childIdx = Index(nodes.size());
e.children.start = {childIdx};
for (auto p = elem.FirstChild(); p; p = p->NextSibling())
++e.children.count;
nodes.resize(childIdx + e.children.count);
elems.push_back(e);
for (auto p = elem.FirstChild(); p; p = p->NextSibling()) {
auto& node = nodes[childIdx++];
if (auto child = p->ToElement()) {
node = NodeId(NodeKind::Element, Index(elems.size()));
buildElem(*child);
} else if (p->ToText()) {
node = NodeId(NodeKind::Text, getStr(p->Value()).value);
}
}
}
void generate(Output& os) const {
std::vector<StrId> uniqueStrList(uniqueStrings.size() + 1);
eytzinger(Size(uniqueStrings.size()),
[in = uniqueStrings.data(), out = uniqueStrList.data() + 1](
unsigned k) mutable { out[k] = {(in++)->offset}; });
Context ctx;
{
SegmentAlloc a{sizeof(ctx)};
ctx.strings = a.alloc<char>(stringSize);
ctx.uniqueStrings = a.allocFor(uniqueStrList);
ctx.nodes = a.allocFor(nodes);
ctx.attrs = a.allocFor(attrs);
ctx.elems = a.allocFor(elems);
}
os.write(&ctx, sizeof(ctx));
os.seek(ctx.strings.offset);
os.write("", 1);
for (const std::string_view s : strings) {
os.write(s.data(), s.size() + 1);
}
os.seek(ctx.uniqueStrings.offset);
writeList(os, uniqueStrList);
os.seek(ctx.nodes.offset);
writeList(os, nodes);
os.seek(ctx.attrs.offset);
writeList(os, attrs);
os.seek(ctx.elems.offset);
writeList(os, elems);
}
};
int main(int argc, const char* argv[]) {
if (argc != 3) {
print("usage: {} <input.xml> <output.bin>\n", argv[0]);
return 1;
}
try {
Input in{argv[1]};
tinyxml2::XMLDocument doc;
if (doc.Parse(static_cast<const char*>(in.data()), in.size()) !=
tinyxml2::XML_SUCCESS) {
print("failed to parse {}\n", argv[1]);
return 1;
}
const auto root = doc.RootElement();
if (!root) {
print("failed to retrieve root node.\n");
return 1;
}
Builder ctx;
ctx.buildElem(*root);
Output os{argv[2]};
ctx.generate(os);
return 0;
} catch (const std::exception& e) {
print(e.what());
}
return 1;
}