1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debversion.cc,v 1.8 2003/09/10 23:39:49 mdz Exp $
4 /* ######################################################################
6 Debian Version - Versioning system for Debian
8 This implements the standard Debian versioning system.
10 ##################################################################### */
12 // Include Files /*{{{*/
15 #include <apt-pkg/debversion.h>
16 #include <apt-pkg/pkgcache.h>
22 debVersioningSystem debVS
;
24 // debVS::debVersioningSystem - Constructor /*{{{*/
25 // ---------------------------------------------------------------------
27 debVersioningSystem::debVersioningSystem()
29 Label
= "Standard .deb";
33 // debVS::CmpFragment - Compare versions /*{{{*/
34 // ---------------------------------------------------------------------
35 /* This compares a fragment of the version. This is a slightly adapted
36 version of what dpkg uses. */
37 #define order(x) ((x) == '~' ? -1 \
40 : isalpha((x)) ? (x) \
42 int debVersioningSystem::CmpFragment(const char *A
,const char *AEnd
,
43 const char *B
,const char *BEnd
)
45 if (A
>= AEnd
&& B
>= BEnd
)
49 if (*B
== '~') return 1;
54 if (*A
== '~') return -1;
58 /* Iterate over the whole string
59 What this does is to split the whole string into groups of
60 numeric and non numeric portions. For instance:
62 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
64 Has '2', '.', '7', '.' ,'-linux-','1' */
67 while (lhs
!= AEnd
&& rhs
!= BEnd
)
71 while (lhs
!= AEnd
&& rhs
!= BEnd
&&
72 (!isdigit(*lhs
) || !isdigit(*rhs
)))
85 while (isdigit(*lhs
) && isdigit(*rhs
))
88 first_diff
= *lhs
- *rhs
;
101 // The strings must be equal
102 if (lhs
== AEnd
&& rhs
== BEnd
)
108 if (*rhs
== '~') return 1;
115 if (*lhs
== '~') return -1;
123 // debVS::CmpVersion - Comparison for versions /*{{{*/
124 // ---------------------------------------------------------------------
125 /* This fragments the version into E:V-R triples and compares each
126 portion separately. */
127 int debVersioningSystem::DoCmpVersion(const char *A
,const char *AEnd
,
128 const char *B
,const char *BEnd
)
130 // Strip off the epoch and compare it
131 const char *lhs
= (const char*) memchr(A
, ':', AEnd
- A
);
132 const char *rhs
= (const char*) memchr(B
, ':', BEnd
- B
);
138 // Special case: a zero epoch is the same as no epoch,
142 for (; *A
== '0'; ++A
);
151 for (; *B
== '0'; ++B
);
160 int Res
= CmpFragment(A
,lhs
,B
,rhs
);
171 const char *dlhs
= (const char*) memrchr(lhs
, '-', AEnd
- lhs
);
172 const char *drhs
= (const char*) memrchr(rhs
, '-', BEnd
- rhs
);
178 // Compare the main version
179 Res
= CmpFragment(lhs
,dlhs
,rhs
,drhs
);
189 // no debian revision need to be treated like -0
190 if (*(dlhs
-1) == '-' && *(drhs
-1) == '-')
191 return CmpFragment(dlhs
,AEnd
,drhs
,BEnd
);
192 else if (*(dlhs
-1) == '-')
194 const char* null
= "0";
195 return CmpFragment(dlhs
,AEnd
,null
, null
+1);
197 else if (*(drhs
-1) == '-')
199 const char* null
= "0";
200 return CmpFragment(null
, null
+1, drhs
, BEnd
);
206 // debVS::CheckDep - Check a single dependency /*{{{*/
207 // ---------------------------------------------------------------------
208 /* This simply preforms the version comparison and switch based on
209 operator. If DepVer is 0 then we are comparing against a provides
211 bool debVersioningSystem::CheckDep(const char *PkgVer
,
212 int Op
,const char *DepVer
)
214 if (DepVer
== 0 || DepVer
[0] == 0)
216 if (PkgVer
== 0 || PkgVer
[0] == 0)
220 size_t const lenPkgVer
= strlen(PkgVer
);
221 size_t const lenDepVer
= strlen(DepVer
);
223 // take a shortcut for equals which are string-equal as well
224 if (Op
== pkgCache::Dep::Equals
&& lenPkgVer
== lenDepVer
&&
225 memcmp(PkgVer
, DepVer
, lenPkgVer
) == 0)
228 // Perform the actual comparision.
229 int const Res
= DoCmpVersion(PkgVer
, PkgVer
+ lenPkgVer
, DepVer
, DepVer
+ lenDepVer
);
232 case pkgCache::Dep::LessEq
:
237 case pkgCache::Dep::GreaterEq
:
242 case pkgCache::Dep::Less
:
247 case pkgCache::Dep::Greater
:
252 case pkgCache::Dep::Equals
:
257 case pkgCache::Dep::NotEquals
:
266 // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
267 // ---------------------------------------------------------------------
268 /* This strips all the debian specific information from the version number */
269 std::string
debVersioningSystem::UpstreamVersion(const char *Ver
)
271 // Strip off the bit before the first colon
273 for (; *I
!= 0 && *I
!= ':'; I
++);
277 // Chop off the trailing -
279 unsigned Last
= strlen(Ver
);
284 return std::string(Ver
,Last
);