]>
git.saurik.com Git - apt.git/blob - apt-pkg/version.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: version.cc,v 1.9 1999/04/19 06:03:09 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 // iVersionCompare - Compare versions /*{{{*/
57 // ---------------------------------------------------------------------
58 /* This compares a fragment of the version. */
59 static int iVersionCompare(const char *A
, const char *AEnd
, const char *B
,
62 if (A
>= AEnd
&& B
>= BEnd
)
69 /* Iterate over the whole string
70 What this does is to spilt the whole string into groups of
71 numeric and non numeric portions. For instance:
73 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
75 Has '2', '.', '7', '.' ,'-linux-','1' */
78 while (lhs
!= AEnd
&& rhs
!= BEnd
)
81 const char *Slhs
= lhs
;
82 const char *Srhs
= rhs
;
84 // Compute ending points were we have passed over the portion
85 bool Digit
= (isdigit(*lhs
) > 0?true:false);
86 for (;lhs
!= AEnd
&& (isdigit(*lhs
) > 0?true:false) == Digit
; lhs
++);
87 for (;rhs
!= BEnd
&& (isdigit(*rhs
) > 0?true:false) == Digit
; rhs
++);
91 // If the lhs has a digit and the rhs does not then <
95 // Generate integers from the strings.
96 unsigned long Ilhs
= StrToLong(Slhs
,lhs
);
97 unsigned long Irhs
= StrToLong(Srhs
,rhs
);
107 // They are equal length so do a straight text compare
108 for (;Slhs
!= lhs
&& Srhs
!= rhs
; Slhs
++, Srhs
++)
112 /* We need to compare non alpha chars as higher than alpha
116 if (isalpha(lc
) == 0) lc
+= 256;
117 if (isalpha(rc
) == 0) rc
+= 256;
124 // If the lhs is shorter than the right it is 'less'
125 if (lhs
- Slhs
< rhs
- Srhs
)
128 // If the lhs is longer than the right it is 'more'
129 if (lhs
- Slhs
> rhs
- Srhs
)
134 // The strings must be equal
135 if (lhs
== AEnd
&& rhs
== BEnd
)
150 // VersionCompare - Comparison for versions /*{{{*/
151 // ---------------------------------------------------------------------
152 /* This fragments the version into E:V-R triples and compares each
153 portion seperately. */
154 int pkgVersionCompare(const char *A
, const char *AEnd
, const char *B
,
157 // Strip off the epoch and compare it
160 for (;lhs
!= AEnd
&& *lhs
!= ':'; lhs
++);
161 for (;rhs
!= BEnd
&& *rhs
!= ':'; rhs
++);
168 int Res
= iVersionCompare(A
,lhs
,B
,rhs
);
179 const char *dlhs
= AEnd
-1;
180 const char *drhs
= BEnd
-1;
181 for (;dlhs
> lhs
&& *dlhs
!= '-'; dlhs
--);
182 for (;drhs
> rhs
&& *drhs
!= '-'; drhs
--);
189 // Compare the main version
190 Res
= iVersionCompare(lhs
,dlhs
,rhs
,drhs
);
199 return iVersionCompare(dlhs
,AEnd
,drhs
,BEnd
);
202 // CheckDep - Check a single dependency /*{{{*/
203 // ---------------------------------------------------------------------
204 /* This simply preforms the version comparison and switch based on
206 bool pkgCheckDep(const char *DepVer
,const char *PkgVer
,int Op
)
213 // Perform the actuall comparision.
214 int Res
= pkgVersionCompare(PkgVer
,DepVer
);
217 case pkgCache::Dep::LessEq
:
222 case pkgCache::Dep::GreaterEq
:
227 case pkgCache::Dep::Less
:
232 case pkgCache::Dep::Greater
:
237 case pkgCache::Dep::Equals
:
242 case pkgCache::Dep::NotEquals
:
251 // BaseVersion - Return the upstream version string /*{{{*/
252 // ---------------------------------------------------------------------
253 /* This strips all the debian specific information from the version number */
254 string
pkgBaseVersion(const char *Ver
)
256 // Strip off the bit before the first colon
258 for (; *I
!= 0 && *I
!= ':'; I
++);
262 // Chop off the trailing -
264 unsigned Last
= strlen(Ver
);
269 return string(Ver
,Last
);