ctkVersion.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "ctkException.h"
  17. #include <QStringListIterator>
  18. #include <QDebug>
  19. const QString ctkVersion::SEPARATOR = ".";
  20. const QRegExp ctkVersion::RegExp = QRegExp("[a-zA-Z0-9_\\-]*");
  21. //----------------------------------------------------------------------------
  22. ctkVersion ctkVersion::emptyVersion()
  23. {
  24. static ctkVersion emptyV(false);
  25. return emptyV;
  26. }
  27. //----------------------------------------------------------------------------
  28. ctkVersion ctkVersion::undefinedVersion()
  29. {
  30. static ctkVersion undefinedV(true);
  31. return undefinedV;
  32. }
  33. //----------------------------------------------------------------------------
  34. ctkVersion& ctkVersion::operator=(const ctkVersion& v)
  35. {
  36. majorVersion = v.majorVersion;
  37. minorVersion = v.minorVersion;
  38. microVersion = v.microVersion;
  39. qualifier = v.qualifier;
  40. undefined = v.undefined;
  41. return *this;
  42. }
  43. //----------------------------------------------------------------------------
  44. ctkVersion::ctkVersion(bool undefined)
  45. : majorVersion(0), minorVersion(0), microVersion(0), qualifier(""), undefined(undefined)
  46. {
  47. }
  48. //----------------------------------------------------------------------------
  49. void ctkVersion::validate()
  50. {
  51. if (!RegExp.exactMatch(qualifier))
  52. throw ctkInvalidArgumentException(QString("invalid qualifier: ") + qualifier);
  53. undefined = false;
  54. }
  55. //----------------------------------------------------------------------------
  56. ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion)
  57. : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(""),
  58. undefined(false)
  59. {
  60. }
  61. //----------------------------------------------------------------------------
  62. ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const QString& qualifier)
  63. : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(qualifier),
  64. undefined(true)
  65. {
  66. this->validate();
  67. }
  68. //----------------------------------------------------------------------------
  69. ctkVersion::ctkVersion(const QString& version)
  70. : majorVersion(0), minorVersion(0), microVersion(0), undefined(true)
  71. {
  72. unsigned int maj = 0;
  73. unsigned int min = 0;
  74. unsigned int mic = 0;
  75. QString qual("");
  76. QStringList st = version.split(SEPARATOR);
  77. if (st.empty()) return;
  78. QStringListIterator i(st);
  79. bool ok = true;
  80. maj = i.next().toUInt(&ok);
  81. if (i.hasNext())
  82. {
  83. min = i.next().toUInt(&ok);
  84. if (i.hasNext())
  85. {
  86. mic = i.next().toUInt(&ok);
  87. if (i.hasNext())
  88. {
  89. qual = i.next();
  90. if (i.hasNext())
  91. {
  92. ok = false;
  93. }
  94. }
  95. }
  96. }
  97. if (!ok) throw ctkInvalidArgumentException("invalid format");
  98. majorVersion = maj;
  99. minorVersion = min;
  100. microVersion = mic;
  101. qualifier = qual;
  102. this->validate();
  103. }
  104. //----------------------------------------------------------------------------
  105. ctkVersion::ctkVersion(const ctkVersion& version)
  106. : majorVersion(version.majorVersion), minorVersion(version.minorVersion),
  107. microVersion(version.microVersion), qualifier(version.qualifier),
  108. undefined(version.undefined)
  109. {
  110. }
  111. //----------------------------------------------------------------------------
  112. ctkVersion ctkVersion::parseVersion(const QString& version)
  113. {
  114. if (version.isEmpty())
  115. {
  116. return emptyVersion();
  117. }
  118. QString version2 = version.trimmed();
  119. if (version2.isEmpty())
  120. {
  121. return emptyVersion();
  122. }
  123. return ctkVersion(version2);
  124. }
  125. //----------------------------------------------------------------------------
  126. bool ctkVersion::isUndefined() const
  127. {
  128. return undefined;
  129. }
  130. //----------------------------------------------------------------------------
  131. unsigned int ctkVersion::getMajor() const
  132. {
  133. if (undefined) throw ctkIllegalStateException("Version undefined");
  134. return majorVersion;
  135. }
  136. //----------------------------------------------------------------------------
  137. unsigned int ctkVersion::getMinor() const
  138. {
  139. if (undefined) throw ctkIllegalStateException("Version undefined");
  140. return minorVersion;
  141. }
  142. //----------------------------------------------------------------------------
  143. unsigned int ctkVersion::getMicro() const
  144. {
  145. if (undefined) throw ctkIllegalStateException("Version undefined");
  146. return microVersion;
  147. }
  148. //----------------------------------------------------------------------------
  149. QString ctkVersion::getQualifier() const
  150. {
  151. if (undefined) throw ctkIllegalStateException("Version undefined");
  152. return qualifier;
  153. }
  154. //----------------------------------------------------------------------------
  155. QString ctkVersion::toString() const
  156. {
  157. if (undefined) return "undefined";
  158. QString result;
  159. result += QString::number(majorVersion) + SEPARATOR + QString::number(minorVersion) + SEPARATOR + QString::number(microVersion);
  160. if (!qualifier.isEmpty())
  161. {
  162. result += SEPARATOR + qualifier;
  163. }
  164. return result;
  165. }
  166. //----------------------------------------------------------------------------
  167. bool ctkVersion::operator==(const ctkVersion& other) const
  168. {
  169. if (&other == this)
  170. { // quicktest
  171. return true;
  172. }
  173. if (other.undefined && this->undefined) return true;
  174. if (this->undefined) throw ctkIllegalStateException("Version undefined");
  175. if (other.undefined) return false;
  176. return (majorVersion == other.majorVersion) && (minorVersion == other.minorVersion) && (microVersion
  177. == other.microVersion) && qualifier == other.qualifier;
  178. }
  179. //----------------------------------------------------------------------------
  180. int ctkVersion::compare(const ctkVersion& other) const
  181. {
  182. if (&other == this)
  183. { // quicktest
  184. return 0;
  185. }
  186. if (this->undefined || other.undefined)
  187. throw ctkIllegalStateException("Cannot compare undefined version");
  188. if (majorVersion < other.majorVersion)
  189. {
  190. return -1;
  191. }
  192. if (majorVersion == other.majorVersion)
  193. {
  194. if (minorVersion < other.minorVersion)
  195. {
  196. return -1;
  197. }
  198. if (minorVersion == other.minorVersion)
  199. {
  200. if (microVersion < other.microVersion)
  201. {
  202. return -1;
  203. }
  204. if (microVersion == other.microVersion)
  205. {
  206. return qualifier.compare(other.qualifier);
  207. }
  208. }
  209. }
  210. return 1;
  211. }
  212. //----------------------------------------------------------------------------
  213. bool ctkVersion::operator <(const ctkVersion &object) const
  214. {
  215. return this->compare(object) < 0;
  216. }
  217. //----------------------------------------------------------------------------
  218. QDebug operator<<(QDebug dbg, const ctkVersion& v)
  219. {
  220. dbg << v.toString();
  221. return dbg.maybeSpace();
  222. }