ctkVersionRange.cpp 3.6 KB

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