]> git.saurik.com Git - apt.git/blob - apt-pkg/versionmatch.cc
8e8e2d2d175d20343ae8dc08aff6ebfb22a1c918
[apt.git] / apt-pkg / versionmatch.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: versionmatch.cc,v 1.5 2001/05/29 03:07:12 jgg Exp $
4 /* ######################################################################
5
6 Version Matching
7
8 This module takes a matching string and a type and locates the version
9 record that satisfies the constraint described by the matching string.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/versionmatch.h"
16 #endif
17 #include <apt-pkg/versionmatch.h>
18
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/error.h>
21
22 #include <stdio.h>
23 #include <ctype.h>
24 /*}}}*/
25
26 // VersionMatch::pkgVersionMatch - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
28 /* Break up the data string according to the selected type */
29 pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
30 {
31 if (Type == None || Data.length() < 1)
32 return;
33
34 // Cut up the version representation
35 if (Type == Version)
36 {
37 if (Data.end()[-1] == '*')
38 {
39 VerPrefixMatch = true;
40 VerStr = string(Data,0,Data.length()-1);
41 }
42 else
43 VerStr = Data;
44 return;
45 }
46
47 if (Type == Release)
48 {
49 // All empty = match all
50 if (Data == "*")
51 {
52 MatchAll = true;
53 return;
54 }
55
56 MatchAll = false;
57
58 // Are we a simple specification?
59 string::const_iterator I = Data.begin();
60 for (; I != Data.end() && *I != '='; I++);
61 if (I == Data.end())
62 {
63 // Temporary
64 if (isdigit(Data[0]))
65 RelVerStr = Data;
66 else
67 RelArchive = Data;
68
69 if (RelVerStr.end()[-1] == '*')
70 {
71 RelVerPrefixMatch = true;
72 RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
73 }
74 return;
75 }
76
77 char Spec[300];
78 char *Fragments[20];
79 snprintf(Spec,sizeof(Spec),"%s",Data.c_str());
80 if (TokSplitString(',',Spec,Fragments,
81 sizeof(Fragments)/sizeof(Fragments[0])) == false)
82 {
83 Type = None;
84 return;
85 }
86
87 for (unsigned J = 0; Fragments[J] != 0; J++)
88 {
89 if (strlen(Fragments[J]) < 3)
90 continue;
91
92 if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0)
93 RelVerStr = Fragments[J]+2;
94 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0)
95 RelOrigin = Fragments[J]+2;
96 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0)
97 RelArchive = Fragments[J]+2;
98 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
99 RelLabel = Fragments[J]+2;
100 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
101 RelComponent = Fragments[J]+2;
102 }
103
104 if (RelVerStr.end()[-1] == '*')
105 {
106 RelVerPrefixMatch = true;
107 RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
108 }
109 return;
110 }
111
112 if (Type == Origin)
113 {
114 OrSite = Data;
115 return;
116 }
117 }
118 /*}}}*/
119 // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
120 // ---------------------------------------------------------------------
121 /* */
122 bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
123 {
124 const char *Ab = A;
125 const char *Ae = Ab + strlen(A);
126
127 // Strings are not a compatible size.
128 if ((unsigned)(Ae - Ab) != B.length() && Prefix == false ||
129 (unsigned)(Ae - Ab) < B.length())
130 return false;
131
132 // Match (leading?)
133 if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
134 return true;
135
136 return false;
137 }
138 /*}}}*/
139 // VersionMatch::Find - Locate the best match for the select type /*{{{*/
140 // ---------------------------------------------------------------------
141 /* */
142 pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
143 {
144 pkgCache::VerIterator Ver = Pkg.VersionList();
145 for (; Ver.end() == false; Ver++)
146 {
147 if (Type == Version)
148 {
149 if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
150 return Ver;
151 continue;
152 }
153
154 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
155 if (FileMatch(VF.File()) == true)
156 return Ver;
157 }
158
159 // This will be Ended by now.
160 return Ver;
161 }
162 /*}}}*/
163 // VersionMatch::FileMatch - Match against an index file /*{{{*/
164 // ---------------------------------------------------------------------
165 /* This matcher checks against the release file and the origin location
166 to see if the constraints are met. */
167 bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
168 {
169 if (Type == Release)
170 {
171 if (MatchAll == true)
172 return true;
173
174 /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
175 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
176
177 if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
178 RelArchive.empty() == true && RelLabel.empty() == true &&
179 RelComponent.empty() == true)
180 return false;
181
182 if (RelVerStr.empty() == false)
183 if (File->Version == 0 ||
184 MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false)
185 return false;
186 if (RelOrigin.empty() == false)
187 if (File->Origin == 0 ||
188 stringcasecmp(RelOrigin,File.Origin()) != 0)
189 return false;
190 if (RelArchive.empty() == false)
191 {
192 if (File->Archive == 0 ||
193 stringcasecmp(RelArchive,File.Archive()) != 0)
194 return false;
195 }
196 if (RelLabel.empty() == false)
197 if (File->Label == 0 ||
198 stringcasecmp(RelLabel,File.Label()) != 0)
199 return false;
200 if (RelComponent.empty() == false)
201 if (File->Component == 0 ||
202 stringcasecmp(RelComponent,File.Component()) != 0)
203 return false;
204 return true;
205 }
206
207 if (Type == Origin)
208 {
209 if (OrSite.empty() == false)
210 if (File->Site == 0 ||
211 OrSite != File.Site())
212 return false;
213 return true;
214 }
215
216 return false;
217 }
218 /*}}}*/