Version.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2020-09-28
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <ls-std/core/Version.hpp>
  11. #include <regex>
  12. using ls::standard::core::Version;
  13. using ls::standard::core::type::byte_field;
  14. using ls::standard::core::type::version_type;
  15. using std::regex;
  16. using std::regex_match;
  17. using std::stoi;
  18. using std::string;
  19. using std::string_view;
  20. using std::to_string;
  21. Version::Version(const version_type _majorVersion, const version_type _minorVersion, const version_type _patchVersion) : majorVersion(_majorVersion), minorVersion(_minorVersion), patchVersion(_patchVersion)
  22. {}
  23. Version::~Version() noexcept = default;
  24. byte_field Version::marshal()
  25. {
  26. byte_field data{};
  27. data += to_string(this->majorVersion) + ".";
  28. data += to_string(this->minorVersion) + ".";
  29. data += to_string(this->patchVersion);
  30. return data;
  31. }
  32. void Version::unmarshal(const byte_field &_data)
  33. {
  34. string field = _data;
  35. if (Version::_isValid(_data))
  36. {
  37. size_t position = field.find('.');
  38. string subSequence = field.substr(0, position);
  39. this->majorVersion = stoi(subSequence);
  40. field.erase(0, position + 1);
  41. position = field.find('.');
  42. subSequence = field.substr(0, position);
  43. this->minorVersion = stoi(subSequence);
  44. field.erase(0, position + 1);
  45. this->patchVersion = stoi(field);
  46. }
  47. }
  48. version_type Version::getMajorVersion() const
  49. {
  50. return this->majorVersion;
  51. }
  52. version_type Version::getMinorVersion() const
  53. {
  54. return this->minorVersion;
  55. }
  56. version_type Version::getPatchVersion() const
  57. {
  58. return this->patchVersion;
  59. }
  60. bool Version::isValid(const string &_versionString)
  61. {
  62. return Version::_isValid(_versionString);
  63. }
  64. void Version::setMajorVersion(const version_type _major)
  65. {
  66. this->majorVersion = _major;
  67. }
  68. void Version::setMinorVersion(const version_type _minor)
  69. {
  70. this->minorVersion = _minor;
  71. }
  72. void Version::setPatchVersion(const version_type _patch)
  73. {
  74. this->patchVersion = _patch;
  75. }
  76. bool Version::_isValid(const string_view _versionString)
  77. {
  78. bool isValidVersionString{};
  79. static regex versionRegex{R"(\d+[.]\d+[.]\d+)"};
  80. if (!_versionString.empty())
  81. {
  82. isValidVersionString = regex_match(_versionString.begin(), _versionString.end(), versionRegex);
  83. }
  84. return isValidVersionString;
  85. }