CModVersion.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * CModVersion.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CModVersion.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. CModVersion CModVersion::GameVersion()
  14. {
  15. return CModVersion(VCMI_VERSION_MAJOR, VCMI_VERSION_MINOR, VCMI_VERSION_PATCH);
  16. }
  17. CModVersion CModVersion::fromString(std::string from)
  18. {
  19. int major = Any;
  20. int minor = Any;
  21. int patch = Any;
  22. try
  23. {
  24. auto pointPos = from.find('.');
  25. major = std::stoi(from.substr(0, pointPos));
  26. if(pointPos != std::string::npos)
  27. {
  28. from = from.substr(pointPos + 1);
  29. pointPos = from.find('.');
  30. minor = std::stoi(from.substr(0, pointPos));
  31. if(pointPos != std::string::npos)
  32. patch = std::stoi(from.substr(pointPos + 1));
  33. }
  34. }
  35. catch(const std::invalid_argument &)
  36. {
  37. return CModVersion();
  38. }
  39. return CModVersion(major, minor, patch);
  40. }
  41. std::string CModVersion::toString() const
  42. {
  43. std::string res;
  44. if(major != Any)
  45. {
  46. res += std::to_string(major);
  47. if(minor != Any)
  48. {
  49. res += '.' + std::to_string(minor);
  50. if(patch != Any)
  51. res += '.' + std::to_string(patch);
  52. }
  53. }
  54. return res;
  55. }
  56. bool CModVersion::compatible(const CModVersion & other, bool checkMinor, bool checkPatch) const
  57. {
  58. if(minor == Any || other.minor == Any)
  59. checkMinor = false;
  60. if(patch == Any || other.patch == Any)
  61. checkPatch = false;
  62. assert(!checkPatch || (checkPatch && checkMinor));
  63. return (major == other.major &&
  64. (!checkMinor || minor >= other.minor) &&
  65. (!checkPatch || minor > other.minor || (minor == other.minor && patch >= other.patch)));
  66. }
  67. bool CModVersion::isNull() const
  68. {
  69. return major == Any;
  70. }
  71. bool operator < (const CModVersion & lesser, const CModVersion & greater)
  72. {
  73. //specific is "greater" than non-specific, that's why do not check for Any value
  74. if(lesser.major == greater.major)
  75. {
  76. if(lesser.minor == greater.minor)
  77. return lesser.patch < greater.patch;
  78. return lesser.minor < greater.minor;
  79. }
  80. return lesser.major < greater.major;
  81. }
  82. VCMI_LIB_NAMESPACE_END