SectionPairSectionValidator.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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-23
  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. using ls::std::core::Class;
  15. using ls::std::io::SectionPairIdentifierValidator;
  16. using ls::std::io::SectionPairRowValidator;
  17. using ls::std::io::SectionPairSectionValidator;
  18. using std::move;
  19. using std::regex;
  20. using std::regex_match;
  21. using std::string;
  22. SectionPairSectionValidator::SectionPairSectionValidator(string _section) : Class("SectionPairSectionValidator"), section(::move(_section))
  23. {}
  24. SectionPairSectionValidator::~SectionPairSectionValidator() noexcept = default;
  25. string SectionPairSectionValidator::getValidationRegex()
  26. {
  27. return SectionPairSectionValidator::_getValidationRegex();
  28. }
  29. bool SectionPairSectionValidator::isValid()
  30. {
  31. string validationRegex = SectionPairSectionValidator::_getValidationRegex();
  32. static regex sectionRegex = regex{"^" + validationRegex};
  33. return regex_match(this->section, sectionRegex);
  34. }
  35. string SectionPairSectionValidator::_getValidationRegex()
  36. {
  37. string newLine = R"(((\n)|(\r\n)))";
  38. string identifier = SectionPairIdentifierValidator::getValidationRegex();
  39. string sectionHeader = newLine + R"(\[{1}()" + identifier + R"()\]{1}()" + newLine + R"({2}))";
  40. string row = SectionPairRowValidator::getValidationRegex();
  41. string atLeastOneRow = R"((()" + row + R"(){1}))";
  42. string optionalRows = R"((()" + row + R"()*))";
  43. return sectionHeader + atLeastOneRow + optionalRows;
  44. }