version.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: version.cc,v 1.4 1998/07/12 23:58:42 jgg Exp $
  4. /* ######################################################################
  5. Version - Version string
  6. Version comparing is done using the == and < operators. STL's
  7. function.h provides the remaining set of comparitors. A directly
  8. callable non-string class version is provided for functions manipulating
  9. the cache file (esp the sort function).
  10. A version is defined to be equal if a case sensitive compare returns
  11. that the two strings are the same. For compatibility with the QSort
  12. function this version returns -1,0,1.
  13. ##################################################################### */
  14. /*}}}*/
  15. // Include Files /*{{{*/
  16. #ifdef __GNUG__
  17. #pragma implementation "apt-pkg/version.h"
  18. #endif
  19. #include <apt-pkg/version.h>
  20. #include <apt-pkg/pkgcache.h>
  21. #include <stdlib.h>
  22. /*}}}*/
  23. // Version::pkgVersion - Default Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. pkgVersion::pkgVersion()
  27. {
  28. }
  29. /*}}}*/
  30. // Version::operator == - Checks if two versions are equal /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* We can't simply perform a string compare because of epochs. */
  33. bool pkgVersion::operator ==(const pkgVersion &Vrhs) const
  34. {
  35. if (pkgVersionCompare(Value.begin(),Value.end(),
  36. Vrhs.Value.begin(),Vrhs.Value.end()) == 0)
  37. return true;
  38. return false;
  39. }
  40. /*}}}*/
  41. // Version::operator < - Checks if this is less than another version /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* All other forms of comparision can be built up from this single function.
  44. a > b -> b < a
  45. a <= b -> !(a > b) -> !(b < a)
  46. a >= b -> !(a < b)
  47. */
  48. bool pkgVersion::operator <(const pkgVersion &Vrhs) const
  49. {
  50. if (pkgVersionCompare(Value.begin(),Value.end(),
  51. Vrhs.Value.begin(),Vrhs.Value.end()) == -1)
  52. return true;
  53. return false;
  54. }
  55. /*}}}*/
  56. // StrToLong - Convert the string between two iterators to a long /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* */
  59. static unsigned long StrToLong(const char *begin,const char *end)
  60. {
  61. char S[40];
  62. char *I = S;
  63. for (; begin != end && I < S + 40;)
  64. *I++ = *begin++;
  65. *I = 0;
  66. return strtoul(S,0,10);
  67. }
  68. /*}}}*/
  69. // VersionCompare (op) - Greater than comparison for versions /*{{{*/
  70. // ---------------------------------------------------------------------
  71. /* */
  72. int pkgVersionCompare(const char *A, const char *B)
  73. {
  74. return pkgVersionCompare(A,A + strlen(A),B,B + strlen(B));
  75. }
  76. int pkgVersionCompare(string A,string B)
  77. {
  78. return pkgVersionCompare(A.begin(),A.end(),B.begin(),B.end());
  79. }
  80. /*}}}*/
  81. // VersionCompare - Greater than comparison for versions /*{{{*/
  82. // ---------------------------------------------------------------------
  83. /* */
  84. int pkgVersionCompare(const char *A, const char *AEnd, const char *B,
  85. const char *BEnd)
  86. {
  87. // lhs = left hand side, rhs = right hand side
  88. const char *lhs = A;
  89. const char *rhs = B;
  90. /* Consider epochs. They need special handling because an epoch
  91. must not be compared against the first element of the real version.
  92. This works okay when both sides have an epoch but when only one
  93. does it must compare the missing epoch to 0 */
  94. for (;lhs != AEnd && *lhs != ':'; lhs++);
  95. for (;rhs != BEnd && *rhs != ':'; rhs++);
  96. // Parse the epoch out
  97. unsigned long lhsEpoch = 0;
  98. unsigned long rhsEpoch = 0;
  99. if (lhs != AEnd && *lhs == ':')
  100. lhsEpoch = StrToLong(A,lhs);
  101. if (rhs != BEnd && *rhs == ':')
  102. rhsEpoch = StrToLong(B,rhs);
  103. if (lhsEpoch != rhsEpoch)
  104. {
  105. if (lhsEpoch > rhsEpoch)
  106. return 1;
  107. return -1;
  108. }
  109. /* Iterate over the whole string
  110. What this does is to spilt the whole string into groups of
  111. numeric and non numeric portions. For instance:
  112. a67bhgs89
  113. Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
  114. 2.7.2-linux-1
  115. Has '2', '.', '7', '.' ,'-linux-','1' */
  116. lhs = A;
  117. rhs = B;
  118. while (lhs != AEnd && rhs != BEnd)
  119. {
  120. // Starting points
  121. const char *Slhs = lhs;
  122. const char *Srhs = rhs;
  123. // Compute ending points were we have passed over the portion
  124. bool Digit = (isdigit(*lhs) > 0?true:false);
  125. for (;lhs != AEnd && (isdigit(*lhs) > 0?true:false) == Digit; lhs++);
  126. for (;rhs != BEnd && (isdigit(*rhs) > 0?true:false) == Digit; rhs++);
  127. if (Digit == true)
  128. {
  129. // If the lhs has a digit and the rhs does not then true
  130. if (rhs - Srhs == 0)
  131. return -1;
  132. // Generate integers from the strings.
  133. unsigned long Ilhs = StrToLong(Slhs,lhs);
  134. unsigned long Irhs = StrToLong(Srhs,rhs);
  135. if (Ilhs != Irhs)
  136. {
  137. if (Ilhs > Irhs)
  138. return 1;
  139. return -1;
  140. }
  141. }
  142. else
  143. {
  144. // They are equal length so do a straight text compare
  145. for (;Slhs != lhs && Srhs != rhs; Slhs++, Srhs++)
  146. {
  147. if (*Slhs != *Srhs)
  148. {
  149. /* We need to compare non alpha chars as higher than alpha
  150. chars (a < !) This is so things like 7.6p2-4 and 7.6-0
  151. compare higher as well as . and -. I am not sure how
  152. the dpkg code manages to achive the != '-' test, but it
  153. is necessary. */
  154. int lc = *Slhs;
  155. int rc = *Srhs;
  156. if (isalpha(lc) == 0 && lc != '-') lc += 256;
  157. if (isalpha(rc) == 0 && rc != '-') rc += 256;
  158. if (lc > rc)
  159. return 1;
  160. return -1;
  161. }
  162. }
  163. // If the lhs is shorter than the right it is 'less'
  164. if (lhs - Slhs < rhs - Srhs)
  165. return -1;
  166. // If the lhs is longer than the right it is 'more'
  167. if (lhs - Slhs > rhs - Srhs)
  168. return 1;
  169. }
  170. }
  171. // The strings must be equal
  172. if (lhs == AEnd && rhs == BEnd)
  173. return 0;
  174. // lhs is shorter
  175. if (lhs == AEnd)
  176. return -1;
  177. // rhs is shorter
  178. if (rhs == BEnd)
  179. return 1;
  180. // Shouldnt happen
  181. return 1;
  182. }
  183. /*}}}*/
  184. // CheckDep - Check a single dependency /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* This simply preforms the version comparison and switch based on
  187. operator. */
  188. bool pkgCheckDep(const char *DepVer,const char *PkgVer,int Op)
  189. {
  190. if (DepVer == 0)
  191. return true;
  192. if (PkgVer == 0)
  193. return false;
  194. // Perform the actuall comparision.
  195. int Res = pkgVersionCompare(PkgVer,DepVer);
  196. switch (Op & 0x0F)
  197. {
  198. case pkgCache::Dep::LessEq:
  199. if (Res <= 0)
  200. return true;
  201. break;
  202. case pkgCache::Dep::GreaterEq:
  203. if (Res >= 0)
  204. return true;
  205. break;
  206. case pkgCache::Dep::Less:
  207. if (Res < 0)
  208. return true;
  209. break;
  210. case pkgCache::Dep::Greater:
  211. if (Res > 0)
  212. return true;
  213. break;
  214. case pkgCache::Dep::Equals:
  215. if (Res == 0)
  216. return true;
  217. break;
  218. case pkgCache::Dep::NotEquals:
  219. if (Res != 0)
  220. return true;
  221. break;
  222. }
  223. return false;
  224. }
  225. /*}}}*/