]> git.lizzy.rs Git - minetest.git/blob - src/metadata.cpp
Disable Prometheus in singleplayer mode
[minetest.git] / src / metadata.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "metadata.h"
21 #include "log.h"
22
23 /*
24         IMetadata
25 */
26
27 bool IMetadata::operator==(const IMetadata &other) const
28 {
29         StringMap this_map_, other_map_;
30         const StringMap &this_map = getStrings(&this_map_);
31         const StringMap &other_map = other.getStrings(&other_map_);
32
33         if (this_map.size() != other_map.size())
34                 return false;
35
36         for (const auto &this_pair : this_map) {
37                 const auto &other_pair = other_map.find(this_pair.first);
38                 if (other_pair == other_map.cend() || other_pair->second != this_pair.second)
39                         return false;
40         }
41
42         return true;
43 }
44
45 const std::string &IMetadata::getString(const std::string &name, std::string *place,
46                 u16 recursion) const
47 {
48         const std::string *raw = getStringRaw(name, place);
49         if (!raw) {
50                 static const std::string empty_string = std::string("");
51                 return empty_string;
52         }
53
54         return resolveString(*raw, place, recursion, true);
55 }
56
57 bool IMetadata::getStringToRef(const std::string &name,
58                 std::string &str, u16 recursion) const
59 {
60         const std::string *raw = getStringRaw(name, &str);
61         if (!raw)
62                 return false;
63
64         const std::string &resolved = resolveString(*raw, &str, recursion, true);
65         if (&resolved != &str)
66                 str = resolved;
67         return true;
68 }
69
70 const std::string &IMetadata::resolveString(const std::string &str, std::string *place,
71                 u16 recursion, bool deprecated) const
72 {
73         if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
74                 if (deprecated) {
75                         warningstream << "Deprecated use of recursive resolution syntax in metadata: ";
76                         safe_print_string(warningstream, str);
77                         warningstream << std::endl;
78                 }
79                 // It may be the case that &str == place, but that's fine.
80                 return getString(str.substr(2, str.length() - 3), place, recursion + 1);
81         }
82
83         return str;
84 }
85
86 /*
87         SimpleMetadata
88 */
89
90 void SimpleMetadata::clear()
91 {
92         m_stringvars.clear();
93         m_modified = true;
94 }
95
96 bool SimpleMetadata::empty() const
97 {
98         return m_stringvars.empty();
99 }
100
101 size_t SimpleMetadata::size() const
102 {
103         return m_stringvars.size();
104 }
105
106 bool SimpleMetadata::contains(const std::string &name) const
107 {
108         return m_stringvars.find(name) != m_stringvars.end();
109 }
110
111 const StringMap &SimpleMetadata::getStrings(StringMap *) const
112 {
113         return m_stringvars;
114 }
115
116 const std::vector<std::string> &SimpleMetadata::getKeys(std::vector<std::string> *place) const
117 {
118         place->clear();
119         place->reserve(m_stringvars.size());
120         for (const auto &pair : m_stringvars)
121                 place->push_back(pair.first);
122         return *place;
123 }
124
125 const std::string *SimpleMetadata::getStringRaw(const std::string &name, std::string *) const
126 {
127         const auto found = m_stringvars.find(name);
128         return found != m_stringvars.cend() ? &found->second : nullptr;
129 }
130
131 bool SimpleMetadata::setString(const std::string &name, const std::string &var)
132 {
133         if (var.empty()) {
134                 if (m_stringvars.erase(name) == 0)
135                         return false;
136         } else {
137                 StringMap::iterator it = m_stringvars.find(name);
138                 if (it != m_stringvars.end() && it->second == var)
139                         return false;
140                 m_stringvars[name] = var;
141         }
142         m_modified = true;
143         return true;
144 }