debversion.cc 5.7 KB

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