2017-01-31 15:45:28 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
/*
|
2022-09-26 23:03:43 +02:00
|
|
|
IMetadata
|
2017-01-31 15:45:28 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
bool IMetadata::operator==(const IMetadata &other) const
|
2017-01-31 15:45:28 +01:00
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
StringMap this_map_, other_map_;
|
|
|
|
const StringMap &this_map = getStrings(&this_map_);
|
|
|
|
const StringMap &other_map = other.getStrings(&other_map_);
|
2017-01-31 17:43:45 +01:00
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
if (this_map.size() != other_map.size())
|
2017-01-31 17:43:45 +01:00
|
|
|
return false;
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
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)
|
2017-01-31 17:43:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
const std::string &IMetadata::getString(const std::string &name, std::string *place,
|
|
|
|
u16 recursion) const
|
2017-01-31 15:45:28 +01:00
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
const std::string *raw = getStringRaw(name, place);
|
|
|
|
if (!raw) {
|
2017-01-31 17:43:45 +01:00
|
|
|
static const std::string empty_string = std::string("");
|
|
|
|
return empty_string;
|
|
|
|
}
|
2017-01-31 15:45:28 +01:00
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
return resolveString(*raw, place, recursion);
|
2017-01-31 15:45:28 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
bool IMetadata::getStringToRef(const std::string &name,
|
|
|
|
std::string &str, u16 recursion) const
|
2018-04-06 10:52:29 +02:00
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
const std::string *raw = getStringRaw(name, &str);
|
|
|
|
if (!raw)
|
2018-04-06 10:52:29 +02:00
|
|
|
return false;
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
const std::string &resolved = resolveString(*raw, &str, recursion);
|
|
|
|
if (&resolved != &str)
|
|
|
|
str = resolved;
|
2018-04-06 10:52:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
const std::string &IMetadata::resolveString(const std::string &str, std::string *place,
|
|
|
|
u16 recursion) const
|
2017-01-31 15:45:28 +01:00
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
|
|
|
|
// It may be the case that &str == place, but that's fine.
|
|
|
|
return getString(str.substr(2, str.length() - 3), place, recursion + 1);
|
2017-02-08 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
return str;
|
|
|
|
}
|
2017-02-08 00:15:55 +01:00
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
/*
|
|
|
|
SimpleMetadata
|
|
|
|
*/
|
|
|
|
|
|
|
|
void SimpleMetadata::clear()
|
|
|
|
{
|
|
|
|
m_stringvars.clear();
|
2018-04-06 10:52:29 +02:00
|
|
|
m_modified = true;
|
2017-01-31 15:45:28 +01:00
|
|
|
}
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
bool SimpleMetadata::empty() const
|
2017-01-31 15:45:28 +01:00
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
return m_stringvars.empty();
|
|
|
|
}
|
2017-08-19 09:29:55 +02:00
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
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::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;
|
2017-01-31 15:45:28 +01:00
|
|
|
}
|