metadata.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "metadata.h"
  17. #include "log.h"
  18. /*
  19. Metadata
  20. */
  21. void Metadata::clear()
  22. {
  23. m_stringvars.clear();
  24. m_modified = true;
  25. }
  26. bool Metadata::empty() const
  27. {
  28. return m_stringvars.empty();
  29. }
  30. size_t Metadata::size() const
  31. {
  32. return m_stringvars.size();
  33. }
  34. bool Metadata::contains(const std::string &name) const
  35. {
  36. return m_stringvars.find(name) != m_stringvars.end();
  37. }
  38. bool Metadata::operator==(const Metadata &other) const
  39. {
  40. if (size() != other.size())
  41. return false;
  42. for (const auto &sv : m_stringvars) {
  43. if (!other.contains(sv.first) || other.getString(sv.first) != sv.second)
  44. return false;
  45. }
  46. return true;
  47. }
  48. const std::string &Metadata::getString(const std::string &name, u16 recursion) const
  49. {
  50. StringMap::const_iterator it = m_stringvars.find(name);
  51. if (it == m_stringvars.end()) {
  52. static const std::string empty_string = std::string("");
  53. return empty_string;
  54. }
  55. return resolveString(it->second, recursion);
  56. }
  57. bool Metadata::getStringToRef(
  58. const std::string &name, std::string &str, u16 recursion) const
  59. {
  60. StringMap::const_iterator it = m_stringvars.find(name);
  61. if (it == m_stringvars.end()) {
  62. return false;
  63. }
  64. str = resolveString(it->second, recursion);
  65. return true;
  66. }
  67. /**
  68. * Sets var to name key in the metadata storage
  69. *
  70. * @param name
  71. * @param var
  72. * @return true if key-value pair is created or changed
  73. */
  74. bool Metadata::setString(const std::string &name, const std::string &var)
  75. {
  76. if (var.empty()) {
  77. m_stringvars.erase(name);
  78. return true;
  79. }
  80. StringMap::iterator it = m_stringvars.find(name);
  81. if (it != m_stringvars.end() && it->second == var) {
  82. return false;
  83. }
  84. m_stringvars[name] = var;
  85. m_modified = true;
  86. return true;
  87. }
  88. const std::string &Metadata::resolveString(const std::string &str, u16 recursion) const
  89. {
  90. if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
  91. return getString(str.substr(2, str.length() - 3), recursion + 1);
  92. }
  93. return str;
  94. }