version.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: version.cc,v 1.5 1998/07/19 21:24:18 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. // StrToLong - Convert the string between two iterators to a long /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. static unsigned long StrToLong(const char *begin,const char *end)
  27. {
  28. char S[40];
  29. char *I = S;
  30. for (; begin != end && I < S + 40;)
  31. *I++ = *begin++;
  32. *I = 0;
  33. return strtoul(S,0,10);
  34. }
  35. /*}}}*/
  36. // VersionCompare (op) - Greater than comparison for versions /*{{{*/
  37. // ---------------------------------------------------------------------
  38. /* */
  39. int pkgVersionCompare(const char *A, const char *B)
  40. {
  41. return pkgVersionCompare(A,A + strlen(A),B,B + strlen(B));
  42. }
  43. int pkgVersionCompare(string A,string B)
  44. {
  45. return pkgVersionCompare(A.begin(),A.end(),B.begin(),B.end());
  46. }
  47. /*}}}*/
  48. // VersionCompare - Greater than comparison for versions /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* */
  51. int pkgVersionCompare(const char *A, const char *AEnd, const char *B,
  52. const char *BEnd)
  53. {
  54. // lhs = left hand side, rhs = right hand side
  55. const char *lhs = A;
  56. const char *rhs = B;
  57. /* Consider epochs. They need special handling because an epoch
  58. must not be compared against the first element of the real version.
  59. This works okay when both sides have an epoch but when only one
  60. does it must compare the missing epoch to 0 */
  61. for (;lhs != AEnd && *lhs != ':'; lhs++);
  62. for (;rhs != BEnd && *rhs != ':'; rhs++);
  63. // Parse the epoch out
  64. unsigned long lhsEpoch = 0;
  65. unsigned long rhsEpoch = 0;
  66. if (lhs != AEnd && *lhs == ':')
  67. lhsEpoch = StrToLong(A,lhs);
  68. if (rhs != BEnd && *rhs == ':')
  69. rhsEpoch = StrToLong(B,rhs);
  70. if (lhsEpoch != rhsEpoch)
  71. {
  72. if (lhsEpoch > rhsEpoch)
  73. return 1;
  74. return -1;
  75. }
  76. /* Iterate over the whole string
  77. What this does is to spilt the whole string into groups of
  78. numeric and non numeric portions. For instance:
  79. a67bhgs89
  80. Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
  81. 2.7.2-linux-1
  82. Has '2', '.', '7', '.' ,'-linux-','1' */
  83. lhs = A;
  84. rhs = B;
  85. while (lhs != AEnd && rhs != BEnd)
  86. {
  87. // Starting points
  88. const char *Slhs = lhs;
  89. const char *Srhs = rhs;
  90. // Compute ending points were we have passed over the portion
  91. bool Digit = (isdigit(*lhs) > 0?true:false);
  92. for (;lhs != AEnd && (isdigit(*lhs) > 0?true:false) == Digit; lhs++);
  93. for (;rhs != BEnd && (isdigit(*rhs) > 0?true:false) == Digit; rhs++);
  94. if (Digit == true)
  95. {
  96. // If the lhs has a digit and the rhs does not then true
  97. if (rhs - Srhs == 0)
  98. return -1;
  99. // Generate integers from the strings.
  100. unsigned long Ilhs = StrToLong(Slhs,lhs);
  101. unsigned long Irhs = StrToLong(Srhs,rhs);
  102. if (Ilhs != Irhs)
  103. {
  104. if (Ilhs > Irhs)
  105. return 1;
  106. return -1;
  107. }
  108. }
  109. else
  110. {
  111. // They are equal length so do a straight text compare
  112. for (;Slhs != lhs && Srhs != rhs; Slhs++, Srhs++)
  113. {
  114. if (*Slhs != *Srhs)
  115. {
  116. /* We need to compare non alpha chars as higher than alpha
  117. chars (a < !) This is so things like 7.6p2-4 and 7.6-0
  118. compare higher as well as . and -. I am not sure how
  119. the dpkg code manages to achive the != '-' test, but it
  120. is necessary. */
  121. int lc = *Slhs;
  122. int rc = *Srhs;
  123. if (isalpha(lc) == 0 && lc != '-') lc += 256;
  124. if (isalpha(rc) == 0 && rc != '-') rc += 256;
  125. if (lc > rc)
  126. return 1;
  127. return -1;
  128. }
  129. }
  130. // If the lhs is shorter than the right it is 'less'
  131. if (lhs - Slhs < rhs - Srhs)
  132. return -1;
  133. // If the lhs is longer than the right it is 'more'
  134. if (lhs - Slhs > rhs - Srhs)
  135. return 1;
  136. }
  137. }
  138. // The strings must be equal
  139. if (lhs == AEnd && rhs == BEnd)
  140. return 0;
  141. // lhs is shorter
  142. if (lhs == AEnd)
  143. return -1;
  144. // rhs is shorter
  145. if (rhs == BEnd)
  146. return 1;
  147. // Shouldnt happen
  148. return 1;
  149. }
  150. /*}}}*/
  151. // CheckDep - Check a single dependency /*{{{*/
  152. // ---------------------------------------------------------------------
  153. /* This simply preforms the version comparison and switch based on
  154. operator. */
  155. bool pkgCheckDep(const char *DepVer,const char *PkgVer,int Op)
  156. {
  157. if (DepVer == 0)
  158. return true;
  159. if (PkgVer == 0)
  160. return false;
  161. // Perform the actuall comparision.
  162. int Res = pkgVersionCompare(PkgVer,DepVer);
  163. switch (Op & 0x0F)
  164. {
  165. case pkgCache::Dep::LessEq:
  166. if (Res <= 0)
  167. return true;
  168. break;
  169. case pkgCache::Dep::GreaterEq:
  170. if (Res >= 0)
  171. return true;
  172. break;
  173. case pkgCache::Dep::Less:
  174. if (Res < 0)
  175. return true;
  176. break;
  177. case pkgCache::Dep::Greater:
  178. if (Res > 0)
  179. return true;
  180. break;
  181. case pkgCache::Dep::Equals:
  182. if (Res == 0)
  183. return true;
  184. break;
  185. case pkgCache::Dep::NotEquals:
  186. if (Res != 0)
  187. return true;
  188. break;
  189. }
  190. return false;
  191. }
  192. /*}}}*/