]>
git.saurik.com Git - apt.git/blob - apt-pkg/version.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: version.cc,v 1.7 1998/11/28 20:50:24 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 // cout << string(A,AEnd) << ',' << string(B,BEnd) << endl;
63 if (A
>= AEnd
&& B
>= BEnd
)
70 /* Iterate over the whole string
71 What this does is to spilt the whole string into groups of
72 numeric and non numeric portions. For instance:
74 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
76 Has '2', '.', '7', '.' ,'-linux-','1' */
79 while (lhs
!= AEnd
&& rhs
!= BEnd
)
82 const char *Slhs
= lhs
;
83 const char *Srhs
= rhs
;
85 // Compute ending points were we have passed over the portion
86 bool Digit
= (isdigit(*lhs
) > 0?true:false);
87 for (;lhs
!= AEnd
&& (isdigit(*lhs
) > 0?true:false) == Digit
; lhs
++);
88 for (;rhs
!= BEnd
&& (isdigit(*rhs
) > 0?true:false) == Digit
; rhs
++);
92 // If the lhs has a digit and the rhs does not then <
96 // Generate integers from the strings.
97 unsigned long Ilhs
= StrToLong(Slhs
,lhs
);
98 unsigned long Irhs
= StrToLong(Srhs
,rhs
);
108 // They are equal length so do a straight text compare
109 for (;Slhs
!= lhs
&& Srhs
!= rhs
; Slhs
++, Srhs
++)
113 /* We need to compare non alpha chars as higher than alpha
117 if (isalpha(lc
) == 0) lc
+= 256;
118 if (isalpha(rc
) == 0) rc
+= 256;
125 // If the lhs is shorter than the right it is 'less'
126 if (lhs
- Slhs
< rhs
- Srhs
)
129 // If the lhs is longer than the right it is 'more'
130 if (lhs
- Slhs
> rhs
- Srhs
)
135 // The strings must be equal
136 if (lhs
== AEnd
&& rhs
== BEnd
)
151 // VersionCompare - Comparison for versions /*{{{*/
152 // ---------------------------------------------------------------------
153 /* This fragments the version into E:V-R triples and compares each
154 portion seperately. */
155 int pkgVersionCompare(const char *A
, const char *AEnd
, const char *B
,
158 // Strip off the epoch and compare it
161 for (;lhs
!= AEnd
&& *lhs
!= ':'; lhs
++);
162 for (;rhs
!= BEnd
&& *rhs
!= ':'; rhs
++);
169 int Res
= iVersionCompare(A
,lhs
,B
,rhs
);
180 const char *dlhs
= AEnd
-1;
181 const char *drhs
= BEnd
-1;
182 for (;dlhs
> lhs
&& *dlhs
!= '-'; dlhs
--);
183 for (;drhs
> rhs
&& *drhs
!= '-'; drhs
--);
190 // Compare the main version
191 Res
= iVersionCompare(lhs
,dlhs
,rhs
,drhs
);
200 return iVersionCompare(dlhs
,AEnd
,drhs
,BEnd
);
203 // CheckDep - Check a single dependency /*{{{*/
204 // ---------------------------------------------------------------------
205 /* This simply preforms the version comparison and switch based on
207 bool pkgCheckDep(const char *DepVer
,const char *PkgVer
,int Op
)
214 // Perform the actuall comparision.
215 int Res
= pkgVersionCompare(PkgVer
,DepVer
);
218 case pkgCache::Dep::LessEq
:
223 case pkgCache::Dep::GreaterEq
:
228 case pkgCache::Dep::Less
:
233 case pkgCache::Dep::Greater
:
238 case pkgCache::Dep::Equals
:
243 case pkgCache::Dep::NotEquals
: