version.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #ifndef VERSION_H
  2. #define VERSION_H
  3. /**
  4. * Semver - The Semantic Versioning, https://github.com/euskadi31/semver-cpp
  5. *
  6. * Copyright (c) 2013 Axel Etcheverry, 2021 enhancements & fixes Lord-Grey
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is furnished
  13. * to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include <cstdlib>
  26. #include <iostream>
  27. #include <sstream>
  28. #include <string>
  29. namespace semver {
  30. enum PRE_RELEASE {
  31. PRE_RELEASE_ALPHA,
  32. PRE_RELEASE_BETA,
  33. PRE_RELEASE_RC,
  34. PRE_RELEASE_NONE
  35. };
  36. typedef enum PRE_RELEASE pre_release_t;
  37. class version
  38. {
  39. private:
  40. std::string m_version;
  41. int m_major;
  42. int m_minor;
  43. int m_patch;
  44. pre_release_t m_pre_release_type;
  45. std::string m_pre_release_id;
  46. std::string m_pre_release;
  47. std::string m_build;
  48. bool m_is_valid;
  49. bool m_is_stable;
  50. enum m_type {
  51. TYPE_MAJOR,
  52. TYPE_MINOR,
  53. TYPE_PATCH,
  54. TYPE_PRE_RELEASE,
  55. TYPE_PRE_RELEASE_ID,
  56. TYPE_BUILD
  57. };
  58. void parse()
  59. {
  60. int type = TYPE_MAJOR;
  61. std::string major, minor, patch;
  62. for (std::size_t i = 0; i < m_version.length(); i++)
  63. {
  64. char chr = m_version[i];
  65. int chr_dec = chr;
  66. switch (type)
  67. {
  68. case TYPE_MAJOR:
  69. if (chr == '.')
  70. {
  71. type = TYPE_MINOR;
  72. continue;
  73. }
  74. if (chr_dec < 48 || chr_dec > 57)
  75. {
  76. m_is_valid = false;
  77. }
  78. // major
  79. major += chr;
  80. break;
  81. case TYPE_MINOR:
  82. if (chr == '.')
  83. {
  84. type = TYPE_PATCH;
  85. continue;
  86. }
  87. if (chr_dec < 48 || chr_dec > 57)
  88. {
  89. m_is_valid = false;
  90. }
  91. minor += chr;
  92. break;
  93. case TYPE_PATCH:
  94. if (chr == '-')
  95. {
  96. type = TYPE_PRE_RELEASE;
  97. continue;
  98. }
  99. if (chr == '+')
  100. {
  101. type = TYPE_BUILD;
  102. continue;
  103. }
  104. if (chr_dec < 48 || chr_dec > 57)
  105. {
  106. m_is_valid = false;
  107. }
  108. patch += chr;
  109. break;
  110. case TYPE_PRE_RELEASE:
  111. if (chr == '.')
  112. {
  113. type = TYPE_PRE_RELEASE_ID;
  114. m_pre_release += chr;
  115. continue;
  116. }
  117. if (chr == '+')
  118. {
  119. type = TYPE_BUILD;
  120. continue;
  121. }
  122. if (
  123. (chr_dec < 48 || chr_dec > 57) && // 0-9
  124. (chr_dec < 65 || chr_dec > 90) && // A-Z
  125. (chr_dec < 97 || chr_dec > 122) && // a-z
  126. (chr_dec != 45) && // -
  127. (chr_dec != 46) // .
  128. )
  129. {
  130. m_is_valid = false;
  131. }
  132. m_pre_release += chr;
  133. break;
  134. case TYPE_PRE_RELEASE_ID:
  135. if (chr == '+')
  136. {
  137. type = TYPE_BUILD;
  138. continue;
  139. }
  140. if (
  141. (chr_dec < 48 || chr_dec > 57) && // 0-9
  142. (chr_dec < 65 || chr_dec > 90) && // A-Z
  143. (chr_dec < 97 || chr_dec > 122) && // a-z
  144. (chr_dec != 45) // -
  145. )
  146. {
  147. m_is_valid = false;
  148. }
  149. m_pre_release += chr;
  150. m_pre_release_id += chr;
  151. break;
  152. case TYPE_BUILD:
  153. if (
  154. (chr_dec < 48 || chr_dec > 57) && // 0-9
  155. (chr_dec < 65 || chr_dec > 90) && // A-Z
  156. (chr_dec < 97 || chr_dec > 122) && // a-z
  157. (chr_dec != 45) // -
  158. )
  159. {
  160. m_is_valid = false;
  161. }
  162. m_build += chr;
  163. break;
  164. }
  165. }
  166. if (m_is_valid)
  167. {
  168. std::istringstream(major) >> m_major;
  169. std::istringstream(minor) >> m_minor;
  170. std::istringstream(patch) >> m_patch;
  171. if (m_pre_release.empty())
  172. {
  173. m_pre_release_type = PRE_RELEASE_NONE;
  174. }
  175. else if (m_pre_release.find("alpha") != std::string::npos)
  176. {
  177. m_pre_release_type = PRE_RELEASE_ALPHA;
  178. }
  179. else if (m_pre_release.find("beta") != std::string::npos)
  180. {
  181. m_pre_release_type = PRE_RELEASE_BETA;
  182. }
  183. else if (m_pre_release.find("rc") != std::string::npos)
  184. {
  185. m_pre_release_type = PRE_RELEASE_RC;
  186. }
  187. if (m_major == 0 && m_minor == 0 && m_patch == 0)
  188. {
  189. m_is_valid = false;
  190. }
  191. if (!m_pre_release_id.empty() && m_pre_release_id[0] == '0')
  192. {
  193. m_is_valid = false;
  194. }
  195. if (m_major == 0)
  196. {
  197. m_is_stable = false;
  198. }
  199. if (m_pre_release_type != PRE_RELEASE_NONE)
  200. {
  201. m_is_stable = false;
  202. }
  203. }
  204. }
  205. public:
  206. /**
  207. * Parse the version string
  208. */
  209. version(const std::string& version)
  210. {
  211. setVersion(version);
  212. }
  213. version(const version&) = default;
  214. ~version() = default;
  215. /**
  216. * Set version
  217. */
  218. bool setVersion(const std::string& version)
  219. {
  220. m_version = version;
  221. m_major = 0;
  222. m_minor = 0;
  223. m_patch = 0;
  224. m_build = "";
  225. m_pre_release = "";
  226. m_pre_release_id = "";
  227. m_is_stable = true;
  228. if (version.empty())
  229. {
  230. m_is_valid = false;
  231. }
  232. else
  233. {
  234. m_is_valid = true;
  235. parse();
  236. }
  237. return m_is_valid;
  238. }
  239. /**
  240. * Get full version
  241. */
  242. const std::string& getVersion() const
  243. {
  244. return m_version;
  245. }
  246. /**
  247. * Get the major of the version
  248. */
  249. const int& getMajor() const
  250. {
  251. return m_major;
  252. }
  253. /**
  254. * Get the minor of the version
  255. */
  256. const int& getMinor() const
  257. {
  258. return m_minor;
  259. }
  260. /**
  261. * Get the patch of the version
  262. */
  263. const int& getPatch() const
  264. {
  265. return m_patch;
  266. }
  267. /**
  268. * Get the build of the version
  269. */
  270. const std::string& getBuild() const
  271. {
  272. return m_build;
  273. }
  274. /**
  275. * Get the release type of the version
  276. */
  277. const pre_release_t& getPreReleaseType() const
  278. {
  279. return m_pre_release_type;
  280. }
  281. /**
  282. * Get the release identifier of the version
  283. */
  284. const std::string& getPreReleaseId() const
  285. {
  286. return m_pre_release_id;
  287. }
  288. /**
  289. * Get the release of the version
  290. */
  291. const std::string& getPreRelease() const
  292. {
  293. return m_pre_release;
  294. }
  295. /**
  296. * Check if the version is stable
  297. */
  298. const bool& isStable() const
  299. {
  300. return m_is_stable;
  301. }
  302. /**
  303. * Check if the version is valid
  304. */
  305. const bool& isValid() const
  306. {
  307. return m_is_valid;
  308. }
  309. int compare(version& rgt)
  310. {
  311. if ((*this) == rgt)
  312. {
  313. return 0;
  314. }
  315. if ((*this) > rgt)
  316. {
  317. return 1;
  318. }
  319. return -1;
  320. }
  321. version& operator= (version& rgt)
  322. {
  323. if ((*this) != rgt)
  324. {
  325. this->m_version = rgt.getVersion();
  326. this->m_major = rgt.getMajor();
  327. this->m_minor = rgt.getMinor();
  328. this->m_patch = rgt.getPatch();
  329. this->m_pre_release_type = rgt.getPreReleaseType();
  330. this->m_pre_release_id = rgt.getPreReleaseId();
  331. this->m_pre_release = rgt.getPreRelease();
  332. this->m_build = rgt.getBuild();
  333. this->m_is_valid = rgt.isValid();
  334. this->m_is_stable = rgt.isStable();
  335. }
  336. return *this;
  337. }
  338. friend bool operator== (version &lft, version &rgt)
  339. {
  340. return !(lft != rgt);
  341. }
  342. friend bool operator!= (version &lft, version &rgt)
  343. {
  344. return (lft > rgt) || (lft < rgt);
  345. }
  346. friend bool operator> (version &lft, version &rgt)
  347. {
  348. // Major
  349. if (lft.getMajor() < 0 && rgt.getMajor() >= 0)
  350. {
  351. return false;
  352. }
  353. if (lft.getMajor() >= 0 && rgt.getMajor() < 0)
  354. {
  355. return true;
  356. }
  357. if (lft.getMajor() > rgt.getMajor())
  358. {
  359. return true;
  360. }
  361. if (lft.getMajor() < rgt.getMajor())
  362. {
  363. return false;
  364. }
  365. // Minor
  366. if (lft.getMinor() < 0 && rgt.getMinor() >= 0)
  367. {
  368. return false;
  369. }
  370. if (lft.getMinor() >= 0 && rgt.getMinor() < 0)
  371. {
  372. return true;
  373. }
  374. if (lft.getMinor() > rgt.getMinor())
  375. {
  376. return true;
  377. }
  378. if (lft.getMinor() < rgt.getMinor())
  379. {
  380. return false;
  381. }
  382. // Patch
  383. if (lft.getPatch() < 0 && rgt.getPatch() >= 0)
  384. {
  385. return false;
  386. }
  387. if (lft.getPatch() >= 0 && rgt.getPatch() < 0)
  388. {
  389. return true;
  390. }
  391. if (lft.getPatch() > rgt.getPatch())
  392. {
  393. return true;
  394. }
  395. if (lft.getPatch() < rgt.getPatch())
  396. {
  397. return false;
  398. }
  399. // Pre release
  400. if (
  401. (lft.getPreReleaseType() == rgt.getPreReleaseType()) &&
  402. (lft.getPreReleaseId() == rgt.getPreReleaseId())
  403. )
  404. {
  405. return false;
  406. }
  407. if (
  408. (lft.getPreReleaseType() == rgt.getPreReleaseType()) &&
  409. (lft.getPreReleaseId().find_first_not_of("0123456789") == std::string::npos) &&
  410. (rgt.getPreReleaseId().find_first_not_of("0123456789") == std::string::npos)
  411. )
  412. {
  413. if (atoi(lft.getPreReleaseId().c_str()) > atoi(rgt.getPreReleaseId().c_str()))
  414. {
  415. return true;
  416. }
  417. else
  418. {
  419. return false;
  420. }
  421. }
  422. if (
  423. (lft.getPreReleaseType() == rgt.getPreReleaseType()) &&
  424. (lft.getPreReleaseId().compare(rgt.getPreReleaseId()) > 0)
  425. )
  426. {
  427. return true;
  428. }
  429. if (lft.getPreReleaseType() > rgt.getPreReleaseType())
  430. {
  431. return true;
  432. }
  433. return false;
  434. }
  435. friend bool operator>= (version &lft, version &rgt)
  436. {
  437. return (lft > rgt) || (lft == rgt);
  438. }
  439. friend bool operator< (version &lft, version &rgt)
  440. {
  441. return (rgt > lft);
  442. }
  443. friend bool operator<= (version &lft, version &rgt)
  444. {
  445. return (lft < rgt) || (lft == rgt);
  446. }
  447. friend std::ostream& operator<< (std::ostream& out, const version& value)
  448. {
  449. out << value.getVersion();
  450. return out;
  451. }
  452. };
  453. } // end semver namespace
  454. #endif // VERSION_H