util.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SuperTux - Scripting reference generator
  2. // Copyright (C) 2023 Vankata453
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "util.hpp"
  17. #include <sstream>
  18. #include <fstream>
  19. bool param_matches(int argc, char** argv, int i,
  20. const std::string& rhs1, const std::string& rhs2,
  21. const std::string& rhs3) // Allow for 3 parameter formats
  22. {
  23. const std::string lhs = argv[i];
  24. return i + 1 < argc && (lhs == rhs1 || lhs == rhs2 || lhs == rhs3);
  25. }
  26. bool starts_with(const std::string& str, const std::string& prefix)
  27. {
  28. return str.rfind(prefix, 0) == 0;
  29. }
  30. void replace(std::string& str, const std::string& from,
  31. const std::string& to, const std::string& to_if_empty)
  32. {
  33. const std::string& to_str = to.empty() ? to_if_empty : to;
  34. size_t start_pos = str.find(from);
  35. while (start_pos != std::string::npos)
  36. {
  37. str.replace(start_pos, from.length(), to_str);
  38. start_pos = str.find(from, start_pos + to_str.length());
  39. }
  40. }
  41. void regex_replace(std::string& str, const std::regex from,
  42. const std::string& to)
  43. {
  44. str = std::regex_replace(str, from, to);
  45. }
  46. std::string read_file(const std::string& path)
  47. {
  48. std::ifstream stream(path);
  49. std::stringstream buffer;
  50. buffer << stream.rdbuf();
  51. stream.close();
  52. return buffer.str();
  53. }
  54. void write_file(const std::string& path, const std::string& content)
  55. {
  56. std::ofstream stream(path);
  57. stream << content;
  58. stream.close();
  59. }
  60. bool attr_equal(tinyxml2::XMLElement* el, const char* attr, const std::string& rhs)
  61. {
  62. const tinyxml2::XMLAttribute* attr_obj = el->FindAttribute(attr);
  63. if (!attr_obj) return false;
  64. const char* val = attr_obj->Value();
  65. return val == NULL ? rhs.empty() : !strcmp(val, rhs.c_str());
  66. }
  67. bool el_equal(tinyxml2::XMLElement* el, const char* child_el, const std::string& rhs)
  68. {
  69. const tinyxml2::XMLElement* child_el_obj = el->FirstChildElement(child_el);
  70. if (!child_el_obj) return false;
  71. const char* text = child_el_obj->GetText();
  72. return text == NULL ? rhs.empty() : !strcmp(text, rhs.c_str());
  73. }
  74. tinyxml2::XMLElement* first_child_with_attribute(tinyxml2::XMLElement* el,
  75. const char* child_name,
  76. const char* child_attr,
  77. const std::string& child_attr_val)
  78. {
  79. tinyxml2::XMLElement* child_el = el->FirstChildElement(child_name);
  80. while (child_el)
  81. {
  82. if (attr_equal(child_el, child_attr, child_attr_val))
  83. return child_el;
  84. child_el = child_el->NextSiblingElement(child_name);
  85. }
  86. return nullptr;
  87. }