Version.cpp 2.3 KB

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