ctkVersionRange.cpp 3.6 KB

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