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 	Metadata
25 */
26 
clear()27 void Metadata::clear()
28 {
29 	m_stringvars.clear();
30 	m_modified = true;
31 }
32 
empty() const33 bool Metadata::empty() const
34 {
35 	return m_stringvars.empty();
36 }
37 
size() const38 size_t Metadata::size() const
39 {
40 	return m_stringvars.size();
41 }
42 
contains(const std::string & name) const43 bool Metadata::contains(const std::string &name) const
44 {
45 	return m_stringvars.find(name) != m_stringvars.end();
46 }
47 
operator ==(const Metadata & other) const48 bool Metadata::operator==(const Metadata &other) const
49 {
50 	if (size() != other.size())
51 		return false;
52 
53 	for (const auto &sv : m_stringvars) {
54 		if (!other.contains(sv.first) || other.getString(sv.first) != sv.second)
55 			return false;
56 	}
57 
58 	return true;
59 }
60 
getString(const std::string & name,u16 recursion) const61 const std::string &Metadata::getString(const std::string &name, u16 recursion) const
62 {
63 	StringMap::const_iterator it = m_stringvars.find(name);
64 	if (it == m_stringvars.end()) {
65 		static const std::string empty_string = std::string("");
66 		return empty_string;
67 	}
68 
69 	return resolveString(it->second, recursion);
70 }
71 
getStringToRef(const std::string & name,std::string & str,u16 recursion) const72 bool Metadata::getStringToRef(
73 		const std::string &name, std::string &str, u16 recursion) const
74 {
75 	StringMap::const_iterator it = m_stringvars.find(name);
76 	if (it == m_stringvars.end()) {
77 		return false;
78 	}
79 
80 	str = resolveString(it->second, recursion);
81 	return true;
82 }
83 
84 /**
85  * Sets var to name key in the metadata storage
86  *
87  * @param name
88  * @param var
89  * @return true if key-value pair is created or changed
90  */
setString(const std::string & name,const std::string & var)91 bool Metadata::setString(const std::string &name, const std::string &var)
92 {
93 	if (var.empty()) {
94 		m_stringvars.erase(name);
95 		return true;
96 	}
97 
98 	StringMap::iterator it = m_stringvars.find(name);
99 	if (it != m_stringvars.end() && it->second == var) {
100 		return false;
101 	}
102 
103 	m_stringvars[name] = var;
104 	m_modified = true;
105 	return true;
106 }
107 
resolveString(const std::string & str,u16 recursion) const108 const std::string &Metadata::resolveString(const std::string &str, u16 recursion) const
109 {
110 	if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
111 		return getString(str.substr(2, str.length() - 3), recursion + 1);
112 	}
113 
114 	return str;
115 }
116