Version.cpp 2.3 KB

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