-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiLocalization.cpp
More file actions
159 lines (146 loc) · 4.06 KB
/
Copy pathwiLocalization.cpp
File metadata and controls
159 lines (146 loc) · 4.06 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
#include "wiLocalization.h"
#include "wiHelper.h"
#include "wiBacklog.h"
#include "Utility/pugixml.hpp"
namespace wi
{
Localization& Localization::GetSection(const char* name)
{
return GetSection(std::string(name));
}
Localization& Localization::GetSection(const std::string& name)
{
return sections[name];
}
const Localization* Localization::CheckSection(const char* name) const
{
return CheckSection(std::string(name));
}
const Localization* Localization::CheckSection(const std::string& name) const
{
auto it = sections.find(name);
if (it != sections.end())
{
return &it->second;
}
return nullptr;
}
void Localization::SetSectionHint(const char* text)
{
SetSectionHint(std::string(text));
}
void Localization::SetSectionHint(const std::string& text)
{
section_hint = text;
}
void Localization::Add(size_t id, const char* text, const char* hint)
{
auto it = lookup.find(id);
Entry* entry = nullptr;
if (it == lookup.end())
{
// Add new entry:
lookup[id] = entries.size();
entry = &entries.emplace_back();
}
else
{
// Update existing entry:
entry = &entries[it->second];
}
entry->text = text;
entry->id_text = std::to_string(id);
if (hint != nullptr)
{
entry->hint = hint;
}
}
const char* Localization::Get(size_t id) const
{
auto it = lookup.find(id);
if (it == lookup.end())
return nullptr;
size_t index = it->second;
const Entry& entry = entries[index];
return entry.text.c_str();
}
void recursive_import(Localization& section, pugi::xml_node& node)
{
if (strcmp(node.name(), "entry") == 0)
{
const char* hint = nullptr;
if (node.attribute("hint"))
{
hint = node.attribute("hint").as_string();
}
section.Add(node.attribute("id").as_ullong(), node.text().as_string(), hint);
for (pugi::xml_node child : node.children())
{
recursive_import(section, child);
}
}
if (strcmp(node.name(), "section") == 0)
{
Localization& child_section = section.GetSection(node.attribute("name").as_string());
if (node.attribute("hint"))
{
child_section.section_hint = node.attribute("hint").as_string();
}
for (pugi::xml_node child : node.children())
{
recursive_import(child_section, child);
}
}
}
void recursive_export(const Localization& section, pugi::xml_node& node)
{
for (auto& entry : section.entries)
{
pugi::xml_node child_node = node.append_child("entry");
child_node.append_attribute("id") = entry.id_text.c_str();
if (!entry.hint.empty())
{
child_node.append_attribute("hint") = entry.hint.c_str();
}
child_node.append_child(pugi::node_pcdata).set_value(entry.text.c_str());
}
for (auto& it : section.sections)
{
const Localization& child_section = it.second;
pugi::xml_node section_node = node.append_child("section");
section_node.append_attribute("name") = it.first.c_str();
if (!child_section.section_hint.empty())
{
section_node.append_attribute("hint") = child_section.section_hint.c_str();
}
recursive_export(child_section, section_node);
}
}
bool Localization::Import(const std::string& filename)
{
wi::vector<uint8_t> filedata;
bool success = wi::helper::FileRead(filename, filedata);
if (!success)
return false;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_buffer_inplace(filedata.data(), filedata.size());
if (result.status != pugi::xml_parse_status::status_ok)
{
wi::backlog::post("XML error in " + filename + " at offset = " + std::to_string(result.offset) + ": " + result.description(), wi::backlog::LogLevel::Warning);
return false;
}
for (pugi::xml_node child : doc.children())
{
recursive_import(*this, child);
}
return true;
}
bool Localization::Export(const std::string& filename) const
{
pugi::xml_document doc;
doc.append_child(pugi::node_comment).set_value("This file was created by HMMWV4 for language localization.");
doc.append_child(pugi::node_comment).set_value("You can import this file with the wi::Localization::Import() function.");
recursive_export(*this, doc);
return doc.save_file(filename.c_str());
}
}