forked from luanti-org/luanti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.cpp
144 lines (120 loc) · 3.68 KB
/
metadata.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
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "metadata.h"
#include "log.h"
/*
IMetadata
*/
bool IMetadata::operator==(const IMetadata &other) const
{
StringMap this_map_, other_map_;
const StringMap &this_map = getStrings(&this_map_);
const StringMap &other_map = other.getStrings(&other_map_);
if (this_map.size() != other_map.size())
return false;
for (const auto &this_pair : this_map) {
const auto &other_pair = other_map.find(this_pair.first);
if (other_pair == other_map.cend() || other_pair->second != this_pair.second)
return false;
}
return true;
}
const std::string &IMetadata::getString(const std::string &name, std::string *place,
u16 recursion) const
{
const std::string *raw = getStringRaw(name, place);
if (!raw) {
static const std::string empty_string = std::string("");
return empty_string;
}
return resolveString(*raw, place, recursion, true);
}
bool IMetadata::getStringToRef(const std::string &name,
std::string &str, u16 recursion) const
{
const std::string *raw = getStringRaw(name, &str);
if (!raw)
return false;
const std::string &resolved = resolveString(*raw, &str, recursion, true);
if (&resolved != &str)
str = resolved;
return true;
}
const std::string &IMetadata::resolveString(const std::string &str, std::string *place,
u16 recursion, bool deprecated) const
{
if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
if (deprecated) {
warningstream << "Deprecated use of recursive resolution syntax in metadata: ";
safe_print_string(warningstream, str);
warningstream << std::endl;
}
// It may be the case that &str == place, but that's fine.
return getString(str.substr(2, str.length() - 3), place, recursion + 1);
}
return str;
}
/*
SimpleMetadata
*/
void SimpleMetadata::clear()
{
m_stringvars.clear();
m_modified = true;
}
bool SimpleMetadata::empty() const
{
return m_stringvars.empty();
}
size_t SimpleMetadata::size() const
{
return m_stringvars.size();
}
bool SimpleMetadata::contains(const std::string &name) const
{
return m_stringvars.find(name) != m_stringvars.end();
}
const StringMap &SimpleMetadata::getStrings(StringMap *) const
{
return m_stringvars;
}
const std::vector<std::string> &SimpleMetadata::getKeys(std::vector<std::string> *place) const
{
place->clear();
place->reserve(m_stringvars.size());
for (const auto &pair : m_stringvars)
place->push_back(pair.first);
return *place;
}
const std::string *SimpleMetadata::getStringRaw(const std::string &name, std::string *) const
{
const auto found = m_stringvars.find(name);
return found != m_stringvars.cend() ? &found->second : nullptr;
}
bool SimpleMetadata::setString(const std::string &name, const std::string &var)
{
if (var.empty()) {
if (m_stringvars.erase(name) == 0)
return false;
} else {
StringMap::iterator it = m_stringvars.find(name);
if (it != m_stringvars.end() && it->second == var)
return false;
m_stringvars[name] = var;
}
m_modified = true;
return true;
}