CModVersion.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * CModVersion.cpp, 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. bool doCheckMinor = checkMinor && minor != Any && other.minor != Any;
  59. bool doCheckPatch = checkPatch && patch != Any && other.patch != Any;
  60. assert(!doCheckPatch || (doCheckPatch && doCheckMinor));
  61. return (major == other.major &&
  62. (!doCheckMinor || minor >= other.minor) &&
  63. (!doCheckPatch || minor > other.minor || (minor == other.minor && patch >= other.patch)));
  64. }
  65. bool CModVersion::isNull() const
  66. {
  67. return major == Any;
  68. }
  69. bool operator < (const CModVersion & lesser, const CModVersion & greater)
  70. {
  71. //specific is "greater" than non-specific, that's why do not check for Any value
  72. if(lesser.major == greater.major)
  73. {
  74. if(lesser.minor == greater.minor)
  75. return lesser.patch < greater.patch;
  76. return lesser.minor < greater.minor;
  77. }
  78. return lesser.major < greater.major;
  79. }
  80. VCMI_LIB_NAMESPACE_END