metadata.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  25. bool Metadata::empty() const
  26. {
  27. return m_stringvars.empty();
  28. }
  29. size_t Metadata::size() const
  30. {
  31. return m_stringvars.size();
  32. }
  33. bool Metadata::contains(const std::string &name) const
  34. {
  35. return m_stringvars.find(name) != m_stringvars.end();
  36. }
  37. bool Metadata::operator==(const Metadata &other) const
  38. {
  39. if (size() != other.size())
  40. return false;
  41. for (const auto &sv : m_stringvars) {
  42. if (!other.contains(sv.first) || other.getString(sv.first) != sv.second)
  43. return false;
  44. }
  45. return true;
  46. }
  47. const std::string &Metadata::getString(const std::string &name, u16 recursion) const
  48. {
  49. StringMap::const_iterator it = m_stringvars.find(name);
  50. if (it == m_stringvars.end()) {
  51. static const std::string empty_string = std::string("");
  52. return empty_string;
  53. }
  54. return resolveString(it->second, recursion);
  55. }
  56. /**
  57. * Sets var to name key in the metadata storage
  58. *
  59. * @param name
  60. * @param var
  61. * @return true if key-value pair is created or changed
  62. */
  63. bool Metadata::setString(const std::string &name, const std::string &var)
  64. {
  65. if (var.empty()) {
  66. m_stringvars.erase(name);
  67. return true;
  68. }
  69. StringMap::iterator it = m_stringvars.find(name);
  70. if (it != m_stringvars.end() && it->second == var) {
  71. return false;
  72. }
  73. m_stringvars[name] = var;
  74. return true;
  75. }
  76. const std::string &Metadata::resolveString(const std::string &str, u16 recursion) const
  77. {
  78. if (recursion <= 1 && str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
  79. return getString(str.substr(2, str.length() - 3), recursion + 1);
  80. }
  81. return str;
  82. }