util.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef UTIL_HEADER
  17. #define UTIL_HEADER
  18. #include <string>
  19. #include <regex>
  20. #include <tinyxml2.h>
  21. bool param_matches(int argc, char** argv, int i,
  22. const std::string& rhs1, const std::string& rhs2,
  23. const std::string& rhs3);
  24. bool starts_with(const std::string& str, const std::string& prefix);
  25. void replace(std::string& str, const std::string& from,
  26. const std::string& to, const std::string& to_if_empty = "");
  27. void regex_replace(std::string& str, const std::regex from,
  28. const std::string& to);
  29. std::string read_file(const std::string& path);
  30. void write_file(const std::string& path, const std::string& content);
  31. bool attr_equal(tinyxml2::XMLElement* el, const char* attr, const std::string& rhs);
  32. bool el_equal(tinyxml2::XMLElement* el, const char* child_el, const std::string& rhs);
  33. tinyxml2::XMLElement* first_child_with_attribute(tinyxml2::XMLElement* el,
  34. const char* child_name,
  35. const char* child_attr,
  36. const std::string& child_attr_val);
  37. // XML text reader, which is able to read text from all child nodes.
  38. class XMLTextReader : public tinyxml2::XMLVisitor
  39. {
  40. private:
  41. std::string& m_buffer;
  42. public:
  43. XMLTextReader(std::string& buffer) :
  44. m_buffer(buffer)
  45. {}
  46. virtual bool Visit(const tinyxml2::XMLText& txt) override
  47. {
  48. const char* val = txt.Value();
  49. if (val) m_buffer += val;
  50. return true;
  51. }
  52. };
  53. #endif