3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
27 bool IMetadata::operator==(const IMetadata &other) const
29 StringMap this_map_, other_map_;
30 const StringMap &this_map = getStrings(&this_map_);
31 const StringMap &other_map = other.getStrings(&other_map_);
33 if (this_map.size() != other_map.size())
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)
45 const std::string &IMetadata::getString(const std::string &name, std::string *place,
48 const std::string *raw = getStringRaw(name, place);
50 static const std::string empty_string = std::string("");
54 return resolveString(*raw, place, recursion, true);
57 bool IMetadata::getStringToRef(const std::string &name,
58 std::string &str, u16 recursion) const
60 const std::string *raw = getStringRaw(name, &str);
64 const std::string &resolved = resolveString(*raw, &str, recursion, true);
65 if (&resolved != &str)
70 const std::string &IMetadata::resolveString(const std::string &str, std::string *place,
71 u16 recursion, bool deprecated) const
73 if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
75 warningstream << "Deprecated use of recursive resolution syntax in metadata: ";
76 safe_print_string(warningstream, str);
77 warningstream << std::endl;
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);
90 void SimpleMetadata::clear()
96 bool SimpleMetadata::empty() const
98 return m_stringvars.empty();
101 size_t SimpleMetadata::size() const
103 return m_stringvars.size();
106 bool SimpleMetadata::contains(const std::string &name) const
108 return m_stringvars.find(name) != m_stringvars.end();
111 const StringMap &SimpleMetadata::getStrings(StringMap *) const
116 const std::vector<std::string> &SimpleMetadata::getKeys(std::vector<std::string> *place) const
119 place->reserve(m_stringvars.size());
120 for (const auto &pair : m_stringvars)
121 place->push_back(pair.first);
125 const std::string *SimpleMetadata::getStringRaw(const std::string &name, std::string *) const
127 const auto found = m_stringvars.find(name);
128 return found != m_stringvars.cend() ? &found->second : nullptr;
131 bool SimpleMetadata::setString(const std::string &name, const std::string &var)
134 if (m_stringvars.erase(name) == 0)
137 StringMap::iterator it = m_stringvars.find(name);
138 if (it != m_stringvars.end() && it->second == var)
140 m_stringvars[name] = var;