]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debversion.cc
Tidy style for new dpkg version compare function
[apt.git] / apt-pkg / deb / debversion.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debversion.cc,v 1.5 2002/11/23 07:54:36 jgg Exp $
4 /* ######################################################################
5
6 Debian Version - Versioning system for Debian
7
8 This implements the standard Debian versioning system.
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
13 #define APT_COMPATIBILITY 986
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/debversion.h"
16 #endif
17
18 #include <apt-pkg/debversion.h>
19 #include <apt-pkg/pkgcache.h>
20
21 #include <stdlib.h>
22 #include <ctype.h>
23 /*}}}*/
24
25 debVersioningSystem debVS;
26
27 // debVS::debVersioningSystem - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 debVersioningSystem::debVersioningSystem()
31 {
32 Label = "Standard .deb";
33 }
34 /*}}}*/
35
36 // debVS::CmpFragment - Compare versions /*{{{*/
37 // ---------------------------------------------------------------------
38 /* This compares a fragment of the version. This is a slightly adapted
39 version of what dpkg uses. */
40 #define order(x) ((x) == '~' ? -1 \
41 : isdigit((x)) ? 0 \
42 : !(x) ? 0 \
43 : isalpha((x)) ? (x) \
44 : (x) + 256)
45 int debVersioningSystem::CmpFragment(const char *A,const char *AEnd,
46 const char *B,const char *BEnd)
47 {
48 if (A >= AEnd && B >= BEnd)
49 return 0;
50 if (A >= AEnd)
51 return -1;
52 if (B >= BEnd)
53 return 1;
54
55 /* Iterate over the whole string
56 What this does is to spilt the whole string into groups of
57 numeric and non numeric portions. For instance:
58 a67bhgs89
59 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
60 2.7.2-linux-1
61 Has '2', '.', '7', '.' ,'-linux-','1' */
62 const char *lhs = A;
63 const char *rhs = B;
64 while (lhs != AEnd && rhs != BEnd)
65 {
66 // Starting points
67 const char *Slhs = lhs;
68 const char *Srhs = rhs;
69 int first_diff = 0;
70
71 while ((lhs != AEnd && !isdigit(*lhs)) ||
72 (rhs != BEnd && !isdigit(*rhs)) )
73 {
74 int vc = order(*lhs);
75 int rc = order(*rhs);
76 if (vc != rc)
77 return vc - rc;
78 lhs++; rhs++;
79 }
80
81 while (*lhs == '0')
82 lhs++;
83 while (*rhs == '0')
84 rhs++;
85 while (isdigit(*lhs) && isdigit(*rhs))
86 {
87 if (!first_diff)
88 first_diff = *lhs - *rhs;
89 lhs++;
90 rhs++;
91 }
92
93 if (isdigit(*lhs))
94 return 1;
95 if (isdigit(*rhs))
96 return -1;
97 if (first_diff)
98 return first_diff;
99 }
100
101 // The strings must be equal
102 if (lhs == AEnd && rhs == BEnd)
103 return 0;
104
105 // lhs is shorter
106 if (lhs == AEnd)
107 return -1;
108
109 // rhs is shorter
110 if (rhs == BEnd)
111 return 1;
112
113 // Shouldnt happen
114 return 1;
115 }
116 /*}}}*/
117 // debVS::CmpVersion - Comparison for versions /*{{{*/
118 // ---------------------------------------------------------------------
119 /* This fragments the version into E:V-R triples and compares each
120 portion separately. */
121 int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd,
122 const char *B,const char *BEnd)
123 {
124 // Strip off the epoch and compare it
125 const char *lhs = A;
126 const char *rhs = B;
127 for (;lhs != AEnd && *lhs != ':'; lhs++);
128 for (;rhs != BEnd && *rhs != ':'; rhs++);
129 if (lhs == AEnd)
130 lhs = A;
131 if (rhs == BEnd)
132 rhs = B;
133
134 // Compare the epoch
135 int Res = CmpFragment(A,lhs,B,rhs);
136 if (Res != 0)
137 return Res;
138
139 // Skip the :
140 if (lhs != A)
141 lhs++;
142 if (rhs != B)
143 rhs++;
144
145 // Find the last -
146 const char *dlhs = AEnd-1;
147 const char *drhs = BEnd-1;
148 for (;dlhs > lhs && *dlhs != '-'; dlhs--);
149 for (;drhs > rhs && *drhs != '-'; drhs--);
150
151 if (dlhs == lhs)
152 dlhs = AEnd;
153 if (drhs == rhs)
154 drhs = BEnd;
155
156 // Compare the main version
157 Res = CmpFragment(lhs,dlhs,rhs,drhs);
158 if (Res != 0)
159 return Res;
160
161 // Skip the -
162 if (dlhs != lhs)
163 dlhs++;
164 if (drhs != rhs)
165 drhs++;
166
167 return CmpFragment(dlhs,AEnd,drhs,BEnd);
168 }
169 /*}}}*/
170 // debVS::CheckDep - Check a single dependency /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This simply preforms the version comparison and switch based on
173 operator. If DepVer is 0 then we are comparing against a provides
174 with no version. */
175 bool debVersioningSystem::CheckDep(const char *PkgVer,
176 int Op,const char *DepVer)
177 {
178 if (DepVer == 0 || DepVer[0] == 0)
179 return true;
180 if (PkgVer == 0 || PkgVer[0] == 0)
181 return false;
182
183 // Perform the actual comparision.
184 int Res = CmpVersion(PkgVer,DepVer);
185 switch (Op & 0x0F)
186 {
187 case pkgCache::Dep::LessEq:
188 if (Res <= 0)
189 return true;
190 break;
191
192 case pkgCache::Dep::GreaterEq:
193 if (Res >= 0)
194 return true;
195 break;
196
197 case pkgCache::Dep::Less:
198 if (Res < 0)
199 return true;
200 break;
201
202 case pkgCache::Dep::Greater:
203 if (Res > 0)
204 return true;
205 break;
206
207 case pkgCache::Dep::Equals:
208 if (Res == 0)
209 return true;
210 break;
211
212 case pkgCache::Dep::NotEquals:
213 if (Res != 0)
214 return true;
215 break;
216 }
217
218 return false;
219 }
220 /*}}}*/
221 // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
222 // ---------------------------------------------------------------------
223 /* This strips all the debian specific information from the version number */
224 string debVersioningSystem::UpstreamVersion(const char *Ver)
225 {
226 // Strip off the bit before the first colon
227 const char *I = Ver;
228 for (; *I != 0 && *I != ':'; I++);
229 if (*I == ':')
230 Ver = I + 1;
231
232 // Chop off the trailing -
233 I = Ver;
234 unsigned Last = strlen(Ver);
235 for (; *I != 0; I++)
236 if (*I == '-')
237 Last = I - Ver;
238
239 return string(Ver,Last);
240 }
241 /*}}}*/