]>
git.saurik.com Git - apt.git/blob - apt-pkg/version.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: version.cc,v 1.5 1998/07/19 21:24:18 jgg Exp $
4 /* ######################################################################
6 Version - Version string
8 Version comparing is done using the == and < operators. STL's
9 function.h provides the remaining set of comparitors. A directly
10 callable non-string class version is provided for functions manipulating
11 the cache file (esp the sort function).
13 A version is defined to be equal if a case sensitive compare returns
14 that the two strings are the same. For compatibility with the QSort
15 function this version returns -1,0,1.
17 ##################################################################### */
19 // Include Files /*{{{*/
21 #pragma implementation "apt-pkg/version.h"
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/pkgcache.h>
30 // StrToLong - Convert the string between two iterators to a long /*{{{*/
31 // ---------------------------------------------------------------------
33 static unsigned long StrToLong(const char *begin
,const char *end
)
37 for (; begin
!= end
&& I
< S
+ 40;)
40 return strtoul(S
,0,10);
43 // VersionCompare (op) - Greater than comparison for versions /*{{{*/
44 // ---------------------------------------------------------------------
46 int pkgVersionCompare(const char *A
, const char *B
)
48 return pkgVersionCompare(A
,A
+ strlen(A
),B
,B
+ strlen(B
));
50 int pkgVersionCompare(string A
,string B
)
52 return pkgVersionCompare(A
.begin(),A
.end(),B
.begin(),B
.end());
56 // VersionCompare - Greater than comparison for versions /*{{{*/
57 // ---------------------------------------------------------------------
59 int pkgVersionCompare(const char *A
, const char *AEnd
, const char *B
,
62 // lhs = left hand side, rhs = right hand side
66 /* Consider epochs. They need special handling because an epoch
67 must not be compared against the first element of the real version.
68 This works okay when both sides have an epoch but when only one
69 does it must compare the missing epoch to 0 */
70 for (;lhs
!= AEnd
&& *lhs
!= ':'; lhs
++);
71 for (;rhs
!= BEnd
&& *rhs
!= ':'; rhs
++);
73 // Parse the epoch out
74 unsigned long lhsEpoch
= 0;
75 unsigned long rhsEpoch
= 0;
76 if (lhs
!= AEnd
&& *lhs
== ':')
77 lhsEpoch
= StrToLong(A
,lhs
);
78 if (rhs
!= BEnd
&& *rhs
== ':')
79 rhsEpoch
= StrToLong(B
,rhs
);
80 if (lhsEpoch
!= rhsEpoch
)
82 if (lhsEpoch
> rhsEpoch
)
87 /* Iterate over the whole string
88 What this does is to spilt the whole string into groups of
89 numeric and non numeric portions. For instance:
91 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
93 Has '2', '.', '7', '.' ,'-linux-','1' */
96 while (lhs
!= AEnd
&& rhs
!= BEnd
)
99 const char *Slhs
= lhs
;
100 const char *Srhs
= rhs
;
102 // Compute ending points were we have passed over the portion
103 bool Digit
= (isdigit(*lhs
) > 0?true:false);
104 for (;lhs
!= AEnd
&& (isdigit(*lhs
) > 0?true:false) == Digit
; lhs
++);
105 for (;rhs
!= BEnd
&& (isdigit(*rhs
) > 0?true:false) == Digit
; rhs
++);
109 // If the lhs has a digit and the rhs does not then true
113 // Generate integers from the strings.
114 unsigned long Ilhs
= StrToLong(Slhs
,lhs
);
115 unsigned long Irhs
= StrToLong(Srhs
,rhs
);
125 // They are equal length so do a straight text compare
126 for (;Slhs
!= lhs
&& Srhs
!= rhs
; Slhs
++, Srhs
++)
130 /* We need to compare non alpha chars as higher than alpha
131 chars (a < !) This is so things like 7.6p2-4 and 7.6-0
132 compare higher as well as . and -. I am not sure how
133 the dpkg code manages to achive the != '-' test, but it
137 if (isalpha(lc
) == 0 && lc
!= '-') lc
+= 256;
138 if (isalpha(rc
) == 0 && rc
!= '-') rc
+= 256;
145 // If the lhs is shorter than the right it is 'less'
146 if (lhs
- Slhs
< rhs
- Srhs
)
149 // If the lhs is longer than the right it is 'more'
150 if (lhs
- Slhs
> rhs
- Srhs
)
155 // The strings must be equal
156 if (lhs
== AEnd
&& rhs
== BEnd
)
171 // CheckDep - Check a single dependency /*{{{*/
172 // ---------------------------------------------------------------------
173 /* This simply preforms the version comparison and switch based on
175 bool pkgCheckDep(const char *DepVer
,const char *PkgVer
,int Op
)
182 // Perform the actuall comparision.
183 int Res
= pkgVersionCompare(PkgVer
,DepVer
);
186 case pkgCache::Dep::LessEq
:
191 case pkgCache::Dep::GreaterEq
:
196 case pkgCache::Dep::Less
:
201 case pkgCache::Dep::Greater
:
206 case pkgCache::Dep::Equals
:
211 case pkgCache::Dep::NotEquals
: