SectionPairSectionValidator.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-20
  6. * Changed: 2023-02-21
  7. *
  8. * */
  9. #include <ls-std/io/section-pair/validator/SectionPairIdentifierValidator.hpp>
  10. #include <ls-std/io/section-pair/validator/SectionPairRowValidator.hpp>
  11. #include <ls-std/io/section-pair/validator/SectionPairSectionValidator.hpp>
  12. #include <regex>
  13. #include <string>
  14. ls::std::io::SectionPairSectionValidator::SectionPairSectionValidator(::std::string _section) : ls::std::core::Class("SectionPairSectionValidator"), section(::std::move(_section))
  15. {}
  16. ls::std::io::SectionPairSectionValidator::~SectionPairSectionValidator() = default;
  17. ::std::string ls::std::io::SectionPairSectionValidator::getValidationRegex()
  18. {
  19. return ls::std::io::SectionPairSectionValidator::_getValidationRegex();
  20. }
  21. bool ls::std::io::SectionPairSectionValidator::isValid()
  22. {
  23. ::std::string validationRegex = ls::std::io::SectionPairSectionValidator::_getValidationRegex();
  24. static ::std::regex sectionRegex = ::std::regex{"^" + validationRegex};
  25. return ::std::regex_match(this->section, sectionRegex);
  26. }
  27. ::std::string ls::std::io::SectionPairSectionValidator::_getValidationRegex()
  28. {
  29. ::std::string newLine = R"(((\n)|(\r\n)))";
  30. ::std::string identifier = ls::std::io::SectionPairIdentifierValidator::getValidationRegex();
  31. ::std::string sectionHeader = newLine + R"(\[{1}()" + identifier + R"()\]{1}()" + newLine + R"({2}))";
  32. ::std::string row = ls::std::io::SectionPairRowValidator::getValidationRegex();
  33. ::std::string atLeastOneRow = R"((()" + row + R"(){1}))";
  34. ::std::string optionalRows = R"((()" + row + R"()*))";
  35. return sectionHeader + atLeastOneRow + optionalRows;
  36. }