util.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // XML text reader, which is able to read text from all child nodes.
  34. class XMLTextReader : public tinyxml2::XMLVisitor
  35. {
  36. private:
  37. std::string& m_buffer;
  38. public:
  39. XMLTextReader(std::string& buffer) :
  40. m_buffer(buffer)
  41. {}
  42. virtual bool Visit(const tinyxml2::XMLText& txt) override
  43. {
  44. const char* val = txt.Value();
  45. if (val) m_buffer += val;
  46. return true;
  47. }
  48. };
  49. #endif