ctkVersionRange.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "ctkVersionRange_p.h"
  16. #include <ctkException.h>
  17. //----------------------------------------------------------------------------
  18. ctkVersionRange ctkVersionRange::defaultVersionRange()
  19. {
  20. static ctkVersionRange defaultVR;
  21. return defaultVR;
  22. }
  23. //----------------------------------------------------------------------------
  24. ctkVersionRange::ctkVersionRange(const QString& vr)
  25. {
  26. bool op = vr.startsWith("(");
  27. bool ob = vr.startsWith("[");
  28. if (op || ob) {
  29. bool cp = vr.endsWith(")");
  30. bool cb = vr.endsWith("]");
  31. int comma = vr.indexOf(',');
  32. if (comma > 0 && (cp || cb))
  33. {
  34. low = ctkVersion(vr.mid(1, comma-1).trimmed());
  35. high = ctkVersion(vr.mid(comma+1, vr.length()-comma-2).trimmed());
  36. lowIncluded = ob;
  37. highIncluded = cb;
  38. }
  39. else
  40. {
  41. throw ctkInvalidArgumentException("Illegal version range: " + vr);
  42. }
  43. }
  44. else
  45. {
  46. low = ctkVersion(vr);
  47. high = ctkVersion();
  48. lowIncluded = true;
  49. highIncluded = false;
  50. }
  51. }
  52. //----------------------------------------------------------------------------
  53. ctkVersionRange::ctkVersionRange()
  54. {
  55. low = ctkVersion(ctkVersion::emptyVersion());
  56. high = ctkVersion();
  57. lowIncluded = true;
  58. highIncluded = false;
  59. }
  60. //----------------------------------------------------------------------------
  61. ctkVersionRange::~ctkVersionRange()
  62. {
  63. }
  64. //----------------------------------------------------------------------------
  65. bool ctkVersionRange::isSpecified() const
  66. {
  67. return !(*this == defaultVersionRange());
  68. }
  69. //----------------------------------------------------------------------------
  70. bool ctkVersionRange::withinRange(const ctkVersion& ver) const
  71. {
  72. if (*this == defaultVersionRange())
  73. {
  74. return true;
  75. }
  76. int c = low.compare(ver);
  77. if (c < 0 || (c == 0 && lowIncluded))
  78. {
  79. if (high.isUndefined())
  80. {
  81. return true;
  82. }
  83. c = high.compare(ver);
  84. return c > 0 || (c == 0 && highIncluded);
  85. }
  86. return false;
  87. }
  88. //----------------------------------------------------------------------------
  89. bool ctkVersionRange::withinRange(const ctkVersionRange& range) const
  90. {
  91. if (*this == range) {
  92. return true;
  93. }
  94. int c = low.compare(range.low);
  95. if (c < 0 || (c == 0 && lowIncluded == range.lowIncluded))
  96. {
  97. if (high.isUndefined())
  98. {
  99. return true;
  100. }
  101. c = high.compare(range.high);
  102. return c > 0 || (c == 0 && highIncluded == range.highIncluded);
  103. }
  104. return false;
  105. }
  106. //----------------------------------------------------------------------------
  107. int ctkVersionRange::compare(const ctkVersionRange& obj) const
  108. {
  109. return low.compare(obj.low);
  110. }
  111. //----------------------------------------------------------------------------
  112. QString ctkVersionRange::toString() const
  113. {
  114. if (!high.isUndefined())
  115. {
  116. QString res;
  117. if (lowIncluded)
  118. {
  119. res += '[';
  120. }
  121. else
  122. {
  123. res += '(';
  124. }
  125. res += low.toString() + "," + high.toString();
  126. if (highIncluded)
  127. {
  128. res += ']';
  129. }
  130. else
  131. {
  132. res += ')';
  133. }
  134. return res;
  135. }
  136. else
  137. {
  138. return low.toString();
  139. }
  140. }
  141. //----------------------------------------------------------------------------
  142. bool ctkVersionRange::operator==(const ctkVersionRange& r) const
  143. {
  144. if (low == r.low)
  145. {
  146. if (!high.isUndefined())
  147. {
  148. return (high == r.high) &&
  149. (lowIncluded == r.lowIncluded) &&
  150. (highIncluded == r.highIncluded);
  151. }
  152. else
  153. {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }