ctkVersion.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkVersion.h"
  16. #include <stdexcept>
  17. #include <QStringListIterator>
  18. #include <QDebug>
  19. const QString ctkVersion::SEPARATOR = ".";
  20. const QRegExp ctkVersion::RegExp = QRegExp("[a-zA-Z0-9_\\-]*");
  21. ctkVersion ctkVersion::emptyVersion()
  22. {
  23. static ctkVersion emptyV(false);
  24. return emptyV;
  25. }
  26. ctkVersion ctkVersion::undefinedVersion()
  27. {
  28. static ctkVersion undefinedV(true);
  29. return undefinedV;
  30. }
  31. ctkVersion& ctkVersion::operator=(const ctkVersion& v)
  32. {
  33. majorVersion = v.majorVersion;
  34. minorVersion = v.minorVersion;
  35. microVersion = v.microVersion;
  36. qualifier = v.qualifier;
  37. undefined = v.undefined;
  38. return *this;
  39. }
  40. ctkVersion::ctkVersion(bool undefined)
  41. : majorVersion(0), minorVersion(0), microVersion(0), qualifier(""), undefined(undefined)
  42. {
  43. }
  44. void ctkVersion::validate()
  45. {
  46. if (!RegExp.exactMatch(qualifier))
  47. throw std::invalid_argument(std::string("invalid qualifier: ") + qualifier.toStdString());
  48. undefined = false;
  49. }
  50. ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion)
  51. : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(""),
  52. undefined(false)
  53. {
  54. }
  55. ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const QString& qualifier)
  56. : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(qualifier),
  57. undefined(true)
  58. {
  59. this->validate();
  60. }
  61. ctkVersion::ctkVersion(const QString& version)
  62. : majorVersion(0), minorVersion(0), microVersion(0), undefined(true)
  63. {
  64. unsigned int maj = 0;
  65. unsigned int min = 0;
  66. unsigned int mic = 0;
  67. QString qual("");
  68. QStringList st = version.split(SEPARATOR);
  69. if (st.empty()) return;
  70. QStringListIterator i(st);
  71. bool ok = true;
  72. maj = i.next().toUInt(&ok);
  73. if (i.hasNext())
  74. {
  75. min = i.next().toUInt(&ok);
  76. if (i.hasNext())
  77. {
  78. mic = i.next().toUInt(&ok);
  79. if (i.hasNext())
  80. {
  81. qual = i.next();
  82. if (i.hasNext())
  83. {
  84. ok = false;
  85. }
  86. }
  87. }
  88. }
  89. if (!ok) throw std::invalid_argument("invalid format");
  90. majorVersion = maj;
  91. minorVersion = min;
  92. microVersion = mic;
  93. qualifier = qual;
  94. this->validate();
  95. }
  96. ctkVersion::ctkVersion(const ctkVersion& version)
  97. : majorVersion(version.majorVersion), minorVersion(version.minorVersion),
  98. microVersion(version.microVersion), qualifier(version.qualifier),
  99. undefined(version.undefined)
  100. {
  101. }
  102. ctkVersion ctkVersion::parseVersion(const QString& version)
  103. {
  104. if (version.isEmpty())
  105. {
  106. return emptyVersion();
  107. }
  108. QString version2 = version.trimmed();
  109. if (version2.isEmpty())
  110. {
  111. return emptyVersion();
  112. }
  113. return ctkVersion(version2);
  114. }
  115. bool ctkVersion::isUndefined() const
  116. {
  117. return undefined;
  118. }
  119. unsigned int ctkVersion::getMajor() const
  120. {
  121. if (undefined) throw std::logic_error("Version undefined");
  122. return majorVersion;
  123. }
  124. unsigned int ctkVersion::getMinor() const
  125. {
  126. if (undefined) throw std::logic_error("Version undefined");
  127. return minorVersion;
  128. }
  129. unsigned int ctkVersion::getMicro() const
  130. {
  131. if (undefined) throw std::logic_error("Version undefined");
  132. return microVersion;
  133. }
  134. QString ctkVersion::getQualifier() const
  135. {
  136. if (undefined) throw std::logic_error("Version undefined");
  137. return qualifier;
  138. }
  139. QString ctkVersion::toString() const
  140. {
  141. if (undefined) return "undefined";
  142. QString result;
  143. result += QString::number(majorVersion) + SEPARATOR + QString::number(minorVersion) + SEPARATOR + QString::number(microVersion);
  144. if (!qualifier.isEmpty())
  145. {
  146. result += SEPARATOR + qualifier;
  147. }
  148. return result;
  149. }
  150. bool ctkVersion::operator==(const ctkVersion& other) const
  151. {
  152. if (&other == this)
  153. { // quicktest
  154. return true;
  155. }
  156. if (other.undefined && this->undefined) return true;
  157. if (this->undefined) throw std::logic_error("Version undefined");
  158. if (other.undefined) return false;
  159. return (majorVersion == other.majorVersion) && (minorVersion == other.minorVersion) && (microVersion
  160. == other.microVersion) && qualifier == other.qualifier;
  161. }
  162. int ctkVersion::compare(const ctkVersion& other) const
  163. {
  164. if (&other == this)
  165. { // quicktest
  166. return 0;
  167. }
  168. if (this->undefined || other.undefined)
  169. throw std::logic_error("Cannot compare undefined version");
  170. if (majorVersion < other.majorVersion)
  171. {
  172. return -1;
  173. }
  174. if (majorVersion == other.majorVersion)
  175. {
  176. if (minorVersion < other.minorVersion)
  177. {
  178. return -1;
  179. }
  180. if (minorVersion == other.minorVersion)
  181. {
  182. if (microVersion < other.microVersion)
  183. {
  184. return -1;
  185. }
  186. if (microVersion == other.microVersion)
  187. {
  188. return qualifier.compare(other.qualifier);
  189. }
  190. }
  191. }
  192. return 1;
  193. }
  194. QDebug operator<<(QDebug dbg, const ctkVersion& v)
  195. {
  196. dbg << v.toString();
  197. return dbg.maybeSpace();
  198. }