debversion.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debversion.cc,v 1.8 2003/09/10 23:39:49 mdz Exp $
  4. /* ######################################################################
  5. Debian Version - Versioning system for Debian
  6. This implements the standard Debian versioning system.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #define APT_COMPATIBILITY 986
  11. #ifdef __GNUG__
  12. #pragma implementation "apt-pkg/debversion.h"
  13. #endif
  14. #include <apt-pkg/debversion.h>
  15. #include <apt-pkg/pkgcache.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. /*}}}*/
  19. debVersioningSystem debVS;
  20. // debVS::debVersioningSystem - Constructor /*{{{*/
  21. // ---------------------------------------------------------------------
  22. /* */
  23. debVersioningSystem::debVersioningSystem()
  24. {
  25. Label = "Standard .deb";
  26. }
  27. /*}}}*/
  28. // debVS::CmpFragment - Compare versions /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* This compares a fragment of the version. This is a slightly adapted
  31. version of what dpkg uses. */
  32. #define order(x) ((x) == '~' ? -1 \
  33. : isdigit((x)) ? 0 \
  34. : !(x) ? 0 \
  35. : isalpha((x)) ? (x) \
  36. : (x) + 256)
  37. int debVersioningSystem::CmpFragment(const char *A,const char *AEnd,
  38. const char *B,const char *BEnd)
  39. {
  40. if (A >= AEnd && B >= BEnd)
  41. return 0;
  42. if (A >= AEnd)
  43. {
  44. if (*B == '~') return 1;
  45. return -1;
  46. }
  47. if (B >= BEnd)
  48. {
  49. if (*A == '~') return -1;
  50. return 1;
  51. }
  52. /* Iterate over the whole string
  53. What this does is to spilt the whole string into groups of
  54. numeric and non numeric portions. For instance:
  55. a67bhgs89
  56. Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
  57. 2.7.2-linux-1
  58. Has '2', '.', '7', '.' ,'-linux-','1' */
  59. const char *lhs = A;
  60. const char *rhs = B;
  61. while (lhs != AEnd && rhs != BEnd)
  62. {
  63. int first_diff = 0;
  64. while (lhs != AEnd && rhs != BEnd &&
  65. (!isdigit(*lhs) || !isdigit(*rhs)))
  66. {
  67. int vc = order(*lhs);
  68. int rc = order(*rhs);
  69. if (vc != rc)
  70. return vc - rc;
  71. lhs++; rhs++;
  72. }
  73. while (*lhs == '0')
  74. lhs++;
  75. while (*rhs == '0')
  76. rhs++;
  77. while (isdigit(*lhs) && isdigit(*rhs))
  78. {
  79. if (!first_diff)
  80. first_diff = *lhs - *rhs;
  81. lhs++;
  82. rhs++;
  83. }
  84. if (isdigit(*lhs))
  85. return 1;
  86. if (isdigit(*rhs))
  87. return -1;
  88. if (first_diff)
  89. return first_diff;
  90. }
  91. // The strings must be equal
  92. if (lhs == AEnd && rhs == BEnd)
  93. return 0;
  94. // lhs is shorter
  95. if (lhs == AEnd)
  96. {
  97. if (*rhs == '~') return 1;
  98. return -1;
  99. }
  100. // rhs is shorter
  101. if (rhs == BEnd)
  102. {
  103. if (*lhs == '~') return -1;
  104. return 1;
  105. }
  106. // Shouldnt happen
  107. return 1;
  108. }
  109. /*}}}*/
  110. // debVS::CmpVersion - Comparison for versions /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* This fragments the version into E:V-R triples and compares each
  113. portion separately. */
  114. int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd,
  115. const char *B,const char *BEnd)
  116. {
  117. // Strip off the epoch and compare it
  118. const char *lhs = A;
  119. const char *rhs = B;
  120. for (;lhs != AEnd && *lhs != ':'; lhs++);
  121. for (;rhs != BEnd && *rhs != ':'; rhs++);
  122. if (lhs == AEnd)
  123. lhs = A;
  124. if (rhs == BEnd)
  125. rhs = B;
  126. // Compare the epoch
  127. int Res = CmpFragment(A,lhs,B,rhs);
  128. if (Res != 0)
  129. return Res;
  130. // Skip the :
  131. if (lhs != A)
  132. lhs++;
  133. if (rhs != B)
  134. rhs++;
  135. // Find the last -
  136. const char *dlhs = AEnd-1;
  137. const char *drhs = BEnd-1;
  138. for (;dlhs > lhs && *dlhs != '-'; dlhs--);
  139. for (;drhs > rhs && *drhs != '-'; drhs--);
  140. if (dlhs == lhs)
  141. dlhs = AEnd;
  142. if (drhs == rhs)
  143. drhs = BEnd;
  144. // Compare the main version
  145. Res = CmpFragment(lhs,dlhs,rhs,drhs);
  146. if (Res != 0)
  147. return Res;
  148. // Skip the -
  149. if (dlhs != lhs)
  150. dlhs++;
  151. if (drhs != rhs)
  152. drhs++;
  153. return CmpFragment(dlhs,AEnd,drhs,BEnd);
  154. }
  155. /*}}}*/
  156. // debVS::CheckDep - Check a single dependency /*{{{*/
  157. // ---------------------------------------------------------------------
  158. /* This simply preforms the version comparison and switch based on
  159. operator. If DepVer is 0 then we are comparing against a provides
  160. with no version. */
  161. bool debVersioningSystem::CheckDep(const char *PkgVer,
  162. int Op,const char *DepVer)
  163. {
  164. if (DepVer == 0 || DepVer[0] == 0)
  165. return true;
  166. if (PkgVer == 0 || PkgVer[0] == 0)
  167. return false;
  168. // Perform the actual comparision.
  169. int Res = CmpVersion(PkgVer,DepVer);
  170. switch (Op & 0x0F)
  171. {
  172. case pkgCache::Dep::LessEq:
  173. if (Res <= 0)
  174. return true;
  175. break;
  176. case pkgCache::Dep::GreaterEq:
  177. if (Res >= 0)
  178. return true;
  179. break;
  180. case pkgCache::Dep::Less:
  181. if (Res < 0)
  182. return true;
  183. break;
  184. case pkgCache::Dep::Greater:
  185. if (Res > 0)
  186. return true;
  187. break;
  188. case pkgCache::Dep::Equals:
  189. if (Res == 0)
  190. return true;
  191. break;
  192. case pkgCache::Dep::NotEquals:
  193. if (Res != 0)
  194. return true;
  195. break;
  196. }
  197. return false;
  198. }
  199. /*}}}*/
  200. // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
  201. // ---------------------------------------------------------------------
  202. /* This strips all the debian specific information from the version number */
  203. string debVersioningSystem::UpstreamVersion(const char *Ver)
  204. {
  205. // Strip off the bit before the first colon
  206. const char *I = Ver;
  207. for (; *I != 0 && *I != ':'; I++);
  208. if (*I == ':')
  209. Ver = I + 1;
  210. // Chop off the trailing -
  211. I = Ver;
  212. unsigned Last = strlen(Ver);
  213. for (; *I != 0; I++)
  214. if (*I == '-')
  215. Last = I - Ver;
  216. return string(Ver,Last);
  217. }
  218. /*}}}*/