1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debversion.cc,v 1.2 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
6 Debian Version - Versioning system for Debian
8 This implements the standard Debian versioning system.
10 ##################################################################### */
12 // Include Files /*{{{*/
13 #define APT_COMPATIBILITY 986
15 #pragma implementation "apt-pkg/debversion.h"
18 #include <apt-pkg/debversion.h>
19 #include <apt-pkg/pkgcache.h>
24 debVersioningSystem debVS
;
26 // debVS::debVersioningSystem - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
29 debVersioningSystem::debVersioningSystem()
31 Label
= "Standard .deb";
34 // StrToLong - Convert the string between two iterators to a long /*{{{*/
35 // ---------------------------------------------------------------------
37 static unsigned long StrToLong(const char *begin
,const char *end
)
41 for (; begin
!= end
&& I
< S
+ 40;)
44 return strtoul(S
,0,10);
47 // debVS::CmpFragment - Compare versions /*{{{*/
48 // ---------------------------------------------------------------------
49 /* This compares a fragment of the version. Dpkg has a really short
50 version of this, but it is uh.. interesting to grok. */
51 int debVersioningSystem::CmpFragment(const char *A
,const char *AEnd
,
52 const char *B
,const char *BEnd
)
54 if (A
>= AEnd
&& B
>= BEnd
)
61 /* Iterate over the whole string
62 What this does is to spilt the whole string into groups of
63 numeric and non numeric portions. For instance:
65 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
67 Has '2', '.', '7', '.' ,'-linux-','1' */
70 while (lhs
!= AEnd
&& rhs
!= BEnd
)
73 const char *Slhs
= lhs
;
74 const char *Srhs
= rhs
;
76 // Compute ending points were we have passed over the portion
77 bool Digit
= (isdigit(*lhs
) > 0?true:false);
78 for (;lhs
!= AEnd
&& (isdigit(*lhs
) > 0?true:false) == Digit
; lhs
++);
79 for (;rhs
!= BEnd
&& (isdigit(*rhs
) > 0?true:false) == Digit
; rhs
++);
83 // If the lhs has a digit and the rhs does not then <
87 // Generate integers from the strings.
88 unsigned long Ilhs
= StrToLong(Slhs
,lhs
);
89 unsigned long Irhs
= StrToLong(Srhs
,rhs
);
99 // They are equal length so do a straight text compare
100 for (;Slhs
!= lhs
&& Srhs
!= rhs
; Slhs
++, Srhs
++)
104 /* We need to compare non alpha chars as higher than alpha
108 if (isalpha(lc
) == 0) lc
+= 256;
109 if (isalpha(rc
) == 0) rc
+= 256;
116 // If the lhs is shorter than the right it is 'less'
117 if (lhs
- Slhs
< rhs
- Srhs
)
120 // If the lhs is longer than the right it is 'more'
121 if (lhs
- Slhs
> rhs
- Srhs
)
126 // The strings must be equal
127 if (lhs
== AEnd
&& rhs
== BEnd
)
142 // debVS::CmpVersion - Comparison for versions /*{{{*/
143 // ---------------------------------------------------------------------
144 /* This fragments the version into E:V-R triples and compares each
145 portion separately. */
146 int debVersioningSystem::DoCmpVersion(const char *A
,const char *AEnd
,
147 const char *B
,const char *BEnd
)
149 // Strip off the epoch and compare it
152 for (;lhs
!= AEnd
&& *lhs
!= ':'; lhs
++);
153 for (;rhs
!= BEnd
&& *rhs
!= ':'; rhs
++);
160 int Res
= CmpFragment(A
,lhs
,B
,rhs
);
171 const char *dlhs
= AEnd
-1;
172 const char *drhs
= BEnd
-1;
173 for (;dlhs
> lhs
&& *dlhs
!= '-'; dlhs
--);
174 for (;drhs
> rhs
&& *drhs
!= '-'; drhs
--);
181 // Compare the main version
182 Res
= CmpFragment(lhs
,dlhs
,rhs
,drhs
);
192 return CmpFragment(dlhs
,AEnd
,drhs
,BEnd
);
195 // debVS::CheckDep - Check a single dependency /*{{{*/
196 // ---------------------------------------------------------------------
197 /* This simply preforms the version comparison and switch based on
198 operator. If DepVer is 0 then we are comparing against a provides
200 bool debVersioningSystem::CheckDep(const char *PkgVer
,
201 int Op
,const char *DepVer
)
203 if (DepVer
== 0 || DepVer
[0] == 0)
205 if (PkgVer
== 0 || PkgVer
[0] == 0)
208 // Perform the actual comparision.
209 int Res
= CmpVersion(PkgVer
,DepVer
);
212 case pkgCache::Dep::LessEq
:
217 case pkgCache::Dep::GreaterEq
:
222 case pkgCache::Dep::Less
:
227 case pkgCache::Dep::Greater
:
232 case pkgCache::Dep::Equals
:
237 case pkgCache::Dep::NotEquals
:
246 // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
247 // ---------------------------------------------------------------------
248 /* This strips all the debian specific information from the version number */
249 string
debVersioningSystem::UpstreamVersion(const char *Ver
)
251 // Strip off the bit before the first colon
253 for (; *I
!= 0 && *I
!= ':'; I
++);
257 // Chop off the trailing -
259 unsigned Last
= strlen(Ver
);
264 return string(Ver
,Last
);