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>
24 debVersioningSystem debVS
;
26 // debVS::debVersioningSystem - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
29 debVersioningSystem::debVersioningSystem()
31 Label
= "Standard .deb";
35 // debVS::CmpFragment - Compare versions /*{{{*/
36 // ---------------------------------------------------------------------
37 /* This compares a fragment of the version. This is a slightly adapted
38 version of what dpkg uses in dpkg/lib/dpkg/version.c.
39 In particular, the a | b = NULL check is removed as we check this in the
40 caller, we use an explicit end for a | b strings and we check ~ explicit. */
41 static int order(char c
)
54 int debVersioningSystem::CmpFragment(const char *A
,const char *AEnd
,
55 const char *B
,const char *BEnd
)
57 /* Iterate over the whole string
58 What this does is to split the whole string into groups of
59 numeric and non numeric portions. For instance:
61 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
63 Has '2', '.', '7', '.' ,'-linux-','1' */
66 while (lhs
!= AEnd
&& rhs
!= BEnd
)
70 while (lhs
!= AEnd
&& rhs
!= BEnd
&&
71 (!isdigit(*lhs
) || !isdigit(*rhs
)))
84 while (isdigit(*lhs
) && isdigit(*rhs
))
87 first_diff
= *lhs
- *rhs
;
100 // The strings must be equal
101 if (lhs
== AEnd
&& rhs
== BEnd
)
107 if (*rhs
== '~') return 1;
114 if (*lhs
== '~') return -1;
122 // debVS::CmpVersion - Comparison for versions /*{{{*/
123 // ---------------------------------------------------------------------
124 /* This fragments the version into E:V-R triples and compares each
125 portion separately. */
126 int debVersioningSystem::DoCmpVersion(const char *A
,const char *AEnd
,
127 const char *B
,const char *BEnd
)
129 // Strip off the epoch and compare it
130 const char *lhs
= (const char*) memchr(A
, ':', AEnd
- A
);
131 const char *rhs
= (const char*) memchr(B
, ':', BEnd
- B
);
137 // Special case: a zero epoch is the same as no epoch,
141 for (; *A
== '0'; ++A
);
150 for (; *B
== '0'; ++B
);
159 int Res
= CmpFragment(A
,lhs
,B
,rhs
);
170 const char *dlhs
= (const char*) memrchr(lhs
, '-', AEnd
- lhs
);
171 const char *drhs
= (const char*) memrchr(rhs
, '-', BEnd
- rhs
);
177 // Compare the main version
178 Res
= CmpFragment(lhs
,dlhs
,rhs
,drhs
);
188 // no debian revision need to be treated like -0
189 if (*(dlhs
-1) == '-' && *(drhs
-1) == '-')
190 return CmpFragment(dlhs
,AEnd
,drhs
,BEnd
);
191 else if (*(dlhs
-1) == '-')
193 const char* null
= "0";
194 return CmpFragment(dlhs
,AEnd
,null
, null
+1);
196 else if (*(drhs
-1) == '-')
198 const char* null
= "0";
199 return CmpFragment(null
, null
+1, drhs
, BEnd
);
205 // debVS::CheckDep - Check a single dependency /*{{{*/
206 // ---------------------------------------------------------------------
207 /* This simply preforms the version comparison and switch based on
208 operator. If DepVer is 0 then we are comparing against a provides
210 bool debVersioningSystem::CheckDep(const char *PkgVer
,
211 int Op
,const char *DepVer
)
213 if (DepVer
== 0 || DepVer
[0] == 0)
215 if (PkgVer
== 0 || PkgVer
[0] == 0)
219 // fast track for (equal) strings [by location] which are by definition equal versions
220 if (PkgVer
== DepVer
)
221 return Op
== pkgCache::Dep::Equals
|| Op
== pkgCache::Dep::LessEq
|| Op
== pkgCache::Dep::GreaterEq
;
223 // Perform the actual comparison.
224 int const Res
= CmpVersion(PkgVer
, DepVer
);
227 case pkgCache::Dep::LessEq
:
232 case pkgCache::Dep::GreaterEq
:
237 case pkgCache::Dep::Less
:
242 case pkgCache::Dep::Greater
:
247 case pkgCache::Dep::Equals
:
252 case pkgCache::Dep::NotEquals
:
261 // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
262 // ---------------------------------------------------------------------
263 /* This strips all the debian specific information from the version number */
264 std::string
debVersioningSystem::UpstreamVersion(const char *Ver
)
266 // Strip off the bit before the first colon
268 for (; *I
!= 0 && *I
!= ':'; I
++);
272 // Chop off the trailing -
274 unsigned Last
= strlen(Ver
);
279 return std::string(Ver
,Last
);