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. */
39 #define order(x) ((x) == '~' ? -1 \
42 : isalpha((x)) ? (x) \
44 int debVersioningSystem::CmpFragment(const char *A
,const char *AEnd
,
45 const char *B
,const char *BEnd
)
47 if (A
>= AEnd
&& B
>= BEnd
)
51 if (*B
== '~') return 1;
56 if (*A
== '~') return -1;
60 /* Iterate over the whole string
61 What this does is to split the whole string into groups of
62 numeric and non numeric portions. For instance:
64 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
66 Has '2', '.', '7', '.' ,'-linux-','1' */
69 while (lhs
!= AEnd
&& rhs
!= BEnd
)
73 while (lhs
!= AEnd
&& rhs
!= BEnd
&&
74 (!isdigit(*lhs
) || !isdigit(*rhs
)))
87 while (isdigit(*lhs
) && isdigit(*rhs
))
90 first_diff
= *lhs
- *rhs
;
103 // The strings must be equal
104 if (lhs
== AEnd
&& rhs
== BEnd
)
110 if (*rhs
== '~') return 1;
117 if (*lhs
== '~') return -1;
125 // debVS::CmpVersion - Comparison for versions /*{{{*/
126 // ---------------------------------------------------------------------
127 /* This fragments the version into E:V-R triples and compares each
128 portion separately. */
129 int debVersioningSystem::DoCmpVersion(const char *A
,const char *AEnd
,
130 const char *B
,const char *BEnd
)
132 // Strip off the epoch and compare it
133 const char *lhs
= (const char*) memchr(A
, ':', AEnd
- A
);
134 const char *rhs
= (const char*) memchr(B
, ':', BEnd
- B
);
140 // Special case: a zero epoch is the same as no epoch,
144 for (; *A
== '0'; ++A
);
153 for (; *B
== '0'; ++B
);
162 int Res
= CmpFragment(A
,lhs
,B
,rhs
);
173 const char *dlhs
= (const char*) memrchr(lhs
, '-', AEnd
- lhs
);
174 const char *drhs
= (const char*) memrchr(rhs
, '-', BEnd
- rhs
);
180 // Compare the main version
181 Res
= CmpFragment(lhs
,dlhs
,rhs
,drhs
);
191 // no debian revision need to be treated like -0
192 if (*(dlhs
-1) == '-' && *(drhs
-1) == '-')
193 return CmpFragment(dlhs
,AEnd
,drhs
,BEnd
);
194 else if (*(dlhs
-1) == '-')
196 const char* null
= "0";
197 return CmpFragment(dlhs
,AEnd
,null
, null
+1);
199 else if (*(drhs
-1) == '-')
201 const char* null
= "0";
202 return CmpFragment(null
, null
+1, drhs
, BEnd
);
208 // debVS::CheckDep - Check a single dependency /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This simply preforms the version comparison and switch based on
211 operator. If DepVer is 0 then we are comparing against a provides
213 bool debVersioningSystem::CheckDep(const char *PkgVer
,
214 int Op
,const char *DepVer
)
216 if (DepVer
== 0 || DepVer
[0] == 0)
218 if (PkgVer
== 0 || PkgVer
[0] == 0)
222 // fast track for (equal) strings [by location] which are by definition equal versions
223 if (PkgVer
== DepVer
)
224 return Op
== pkgCache::Dep::Equals
|| Op
== pkgCache::Dep::LessEq
|| Op
== pkgCache::Dep::GreaterEq
;
226 // Perform the actual comparison.
227 int const Res
= CmpVersion(PkgVer
, DepVer
);
230 case pkgCache::Dep::LessEq
:
235 case pkgCache::Dep::GreaterEq
:
240 case pkgCache::Dep::Less
:
245 case pkgCache::Dep::Greater
:
250 case pkgCache::Dep::Equals
:
255 case pkgCache::Dep::NotEquals
:
264 // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
265 // ---------------------------------------------------------------------
266 /* This strips all the debian specific information from the version number */
267 std::string
debVersioningSystem::UpstreamVersion(const char *Ver
)
269 // Strip off the bit before the first colon
271 for (; *I
!= 0 && *I
!= ':'; I
++);
275 // Chop off the trailing -
277 unsigned Last
= strlen(Ver
);
282 return std::string(Ver
,Last
);