version.cc 6.9 KB

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