debversion.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debversion.cc,v 1.5 2002/11/23 07:54:36 jgg 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. return -1;
  44. if (B >= BEnd)
  45. return 1;
  46. /* Iterate over the whole string
  47. What this does is to spilt the whole string into groups of
  48. numeric and non numeric portions. For instance:
  49. a67bhgs89
  50. Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
  51. 2.7.2-linux-1
  52. Has '2', '.', '7', '.' ,'-linux-','1' */
  53. const char *lhs = A;
  54. const char *rhs = B;
  55. while (lhs != AEnd && rhs != BEnd)
  56. {
  57. // Starting points
  58. const char *Slhs = lhs;
  59. const char *Srhs = rhs;
  60. int first_diff = 0;
  61. while ((lhs != AEnd && !isdigit(*lhs)) ||
  62. (rhs != BEnd && !isdigit(*rhs)) )
  63. {
  64. int vc = order(*lhs);
  65. int rc = order(*rhs);
  66. if (vc != rc)
  67. return vc - rc;
  68. lhs++; rhs++;
  69. }
  70. while (*lhs == '0')
  71. lhs++;
  72. while (*rhs == '0')
  73. rhs++;
  74. while (isdigit(*lhs) && isdigit(*rhs))
  75. {
  76. if (!first_diff)
  77. first_diff = *lhs - *rhs;
  78. lhs++;
  79. rhs++;
  80. }
  81. if (isdigit(*lhs))
  82. return 1;
  83. if (isdigit(*rhs))
  84. return -1;
  85. if (first_diff)
  86. return first_diff;
  87. }
  88. // The strings must be equal
  89. if (lhs == AEnd && rhs == BEnd)
  90. return 0;
  91. // lhs is shorter
  92. if (lhs == AEnd)
  93. return -1;
  94. // rhs is shorter
  95. if (rhs == BEnd)
  96. return 1;
  97. // Shouldnt happen
  98. return 1;
  99. }
  100. /*}}}*/
  101. // debVS::CmpVersion - Comparison for versions /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* This fragments the version into E:V-R triples and compares each
  104. portion separately. */
  105. int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd,
  106. const char *B,const char *BEnd)
  107. {
  108. // Strip off the epoch and compare it
  109. const char *lhs = A;
  110. const char *rhs = B;
  111. for (;lhs != AEnd && *lhs != ':'; lhs++);
  112. for (;rhs != BEnd && *rhs != ':'; rhs++);
  113. if (lhs == AEnd)
  114. lhs = A;
  115. if (rhs == BEnd)
  116. rhs = B;
  117. // Compare the epoch
  118. int Res = CmpFragment(A,lhs,B,rhs);
  119. if (Res != 0)
  120. return Res;
  121. // Skip the :
  122. if (lhs != A)
  123. lhs++;
  124. if (rhs != B)
  125. rhs++;
  126. // Find the last -
  127. const char *dlhs = AEnd-1;
  128. const char *drhs = BEnd-1;
  129. for (;dlhs > lhs && *dlhs != '-'; dlhs--);
  130. for (;drhs > rhs && *drhs != '-'; drhs--);
  131. if (dlhs == lhs)
  132. dlhs = AEnd;
  133. if (drhs == rhs)
  134. drhs = BEnd;
  135. // Compare the main version
  136. Res = CmpFragment(lhs,dlhs,rhs,drhs);
  137. if (Res != 0)
  138. return Res;
  139. // Skip the -
  140. if (dlhs != lhs)
  141. dlhs++;
  142. if (drhs != rhs)
  143. drhs++;
  144. return CmpFragment(dlhs,AEnd,drhs,BEnd);
  145. }
  146. /*}}}*/
  147. // debVS::CheckDep - Check a single dependency /*{{{*/
  148. // ---------------------------------------------------------------------
  149. /* This simply preforms the version comparison and switch based on
  150. operator. If DepVer is 0 then we are comparing against a provides
  151. with no version. */
  152. bool debVersioningSystem::CheckDep(const char *PkgVer,
  153. int Op,const char *DepVer)
  154. {
  155. if (DepVer == 0 || DepVer[0] == 0)
  156. return true;
  157. if (PkgVer == 0 || PkgVer[0] == 0)
  158. return false;
  159. // Perform the actual comparision.
  160. int Res = CmpVersion(PkgVer,DepVer);
  161. switch (Op & 0x0F)
  162. {
  163. case pkgCache::Dep::LessEq:
  164. if (Res <= 0)
  165. return true;
  166. break;
  167. case pkgCache::Dep::GreaterEq:
  168. if (Res >= 0)
  169. return true;
  170. break;
  171. case pkgCache::Dep::Less:
  172. if (Res < 0)
  173. return true;
  174. break;
  175. case pkgCache::Dep::Greater:
  176. if (Res > 0)
  177. return true;
  178. break;
  179. case pkgCache::Dep::Equals:
  180. if (Res == 0)
  181. return true;
  182. break;
  183. case pkgCache::Dep::NotEquals:
  184. if (Res != 0)
  185. return true;
  186. break;
  187. }
  188. return false;
  189. }
  190. /*}}}*/
  191. // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
  192. // ---------------------------------------------------------------------
  193. /* This strips all the debian specific information from the version number */
  194. string debVersioningSystem::UpstreamVersion(const char *Ver)
  195. {
  196. // Strip off the bit before the first colon
  197. const char *I = Ver;
  198. for (; *I != 0 && *I != ':'; I++);
  199. if (*I == ':')
  200. Ver = I + 1;
  201. // Chop off the trailing -
  202. I = Ver;
  203. unsigned Last = strlen(Ver);
  204. for (; *I != 0; I++)
  205. if (*I == '-')
  206. Last = I - Ver;
  207. return string(Ver,Last);
  208. }
  209. /*}}}*/