]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | /** \file cacheset.h | |
4 | Wrappers around std::set to have set::iterators which behave | |
5 | similar to the Iterators of the cache structures. | |
6 | ||
7 | Provides also a few helper methods which work with these sets */ | |
8 | /*}}}*/ | |
9 | #ifndef APT_CACHESET_H | |
10 | #define APT_CACHESET_H | |
11 | // Include Files /*{{{*/ | |
12 | #include <iostream> | |
13 | #include <fstream> | |
14 | #include <list> | |
15 | #include <map> | |
16 | #include <set> | |
17 | #include <string> | |
18 | ||
19 | #include <apt-pkg/error.h> | |
20 | #include <apt-pkg/pkgcache.h> | |
21 | /*}}}*/ | |
22 | ||
23 | class pkgCacheFile; | |
24 | ||
25 | namespace APT { | |
26 | class PackageSet; | |
27 | class VersionSet; | |
28 | class CacheSetHelper { /*{{{*/ | |
29 | /** \class APT::CacheSetHelper | |
30 | Simple base class with a lot of virtual methods which can be overridden | |
31 | to alter the behavior or the output of the CacheSets. | |
32 | ||
33 | This helper is passed around by the static methods in the CacheSets and | |
34 | used every time they hit an error condition or something could be | |
35 | printed out. | |
36 | */ | |
37 | public: /*{{{*/ | |
38 | CacheSetHelper(bool const &ShowError = true, | |
39 | GlobalError::MsgType ErrorType = GlobalError::ERROR) : | |
40 | ShowError(ShowError), ErrorType(ErrorType) {}; | |
41 | virtual ~CacheSetHelper() {}; | |
42 | ||
43 | virtual void showTaskSelection(PackageSet const &pkgset, std::string const &pattern) {}; | |
44 | virtual void showRegExSelection(PackageSet const &pkgset, std::string const &pattern) {}; | |
45 | virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver, | |
46 | std::string const &ver, bool const &verIsRel) {}; | |
47 | ||
48 | virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str); | |
49 | virtual PackageSet canNotFindTask(pkgCacheFile &Cache, std::string pattern); | |
50 | virtual PackageSet canNotFindRegEx(pkgCacheFile &Cache, std::string pattern); | |
51 | virtual PackageSet canNotFindPackage(pkgCacheFile &Cache, std::string const &str); | |
52 | virtual VersionSet canNotFindAllVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg); | |
53 | virtual VersionSet canNotFindInstCandVer(pkgCacheFile &Cache, | |
54 | pkgCache::PkgIterator const &Pkg); | |
55 | virtual VersionSet canNotFindCandInstVer(pkgCacheFile &Cache, | |
56 | pkgCache::PkgIterator const &Pkg); | |
57 | virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, | |
58 | pkgCache::PkgIterator const &Pkg); | |
59 | virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, | |
60 | pkgCache::PkgIterator const &Pkg); | |
61 | virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache, | |
62 | pkgCache::PkgIterator const &Pkg); | |
63 | ||
64 | bool showErrors() const { return ShowError; }; | |
65 | bool showErrors(bool const &newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); }; | |
66 | GlobalError::MsgType errorType() const { return ErrorType; }; | |
67 | GlobalError::MsgType errorType(GlobalError::MsgType const &newValue) | |
68 | { | |
69 | if (ErrorType == newValue) return ErrorType; | |
70 | else { | |
71 | GlobalError::MsgType const &oldValue = ErrorType; | |
72 | ErrorType = newValue; | |
73 | return oldValue; | |
74 | } | |
75 | }; | |
76 | ||
77 | /*}}}*/ | |
78 | protected: | |
79 | bool ShowError; | |
80 | GlobalError::MsgType ErrorType; | |
81 | }; /*}}}*/ | |
82 | class PackageSet : public std::set<pkgCache::PkgIterator> { /*{{{*/ | |
83 | /** \class APT::PackageSet | |
84 | ||
85 | Simple wrapper around a std::set to provide a similar interface to | |
86 | a set of packages as to the complete set of all packages in the | |
87 | pkgCache. */ | |
88 | public: /*{{{*/ | |
89 | /** \brief smell like a pkgCache::PkgIterator */ | |
90 | class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/ | |
91 | public: | |
92 | const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) : | |
93 | std::set<pkgCache::PkgIterator>::const_iterator(x) {} | |
94 | ||
95 | operator pkgCache::PkgIterator(void) { return **this; } | |
96 | ||
97 | inline const char *Name() const {return (**this).Name(); } | |
98 | inline std::string FullName(bool const &Pretty) const { return (**this).FullName(Pretty); } | |
99 | inline std::string FullName() const { return (**this).FullName(); } | |
100 | inline const char *Section() const {return (**this).Section(); } | |
101 | inline bool Purge() const {return (**this).Purge(); } | |
102 | inline const char *Arch() const {return (**this).Arch(); } | |
103 | inline pkgCache::GrpIterator Group() const { return (**this).Group(); } | |
104 | inline pkgCache::VerIterator VersionList() const { return (**this).VersionList(); } | |
105 | inline pkgCache::VerIterator CurrentVer() const { return (**this).CurrentVer(); } | |
106 | inline pkgCache::DepIterator RevDependsList() const { return (**this).RevDependsList(); } | |
107 | inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); } | |
108 | inline pkgCache::PkgIterator::OkState State() const { return (**this).State(); } | |
109 | inline const char *CandVersion() const { return (**this).CandVersion(); } | |
110 | inline const char *CurVersion() const { return (**this).CurVersion(); } | |
111 | inline pkgCache *Cache() const { return (**this).Cache(); }; | |
112 | inline unsigned long Index() const {return (**this).Index();}; | |
113 | // we have only valid iterators here | |
114 | inline bool end() const { return false; }; | |
115 | ||
116 | friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); } | |
117 | ||
118 | inline pkgCache::Package const * operator->() const { | |
119 | return &***this; | |
120 | }; | |
121 | }; | |
122 | // 103. set::iterator is required to be modifiable, but this allows modification of keys | |
123 | typedef APT::PackageSet::const_iterator iterator; | |
124 | /*}}}*/ | |
125 | ||
126 | using std::set<pkgCache::PkgIterator>::insert; | |
127 | inline void insert(pkgCache::PkgIterator const &P) { if (P.end() == false) std::set<pkgCache::PkgIterator>::insert(P); }; | |
128 | inline void insert(PackageSet const &pkgset) { insert(pkgset.begin(), pkgset.end()); }; | |
129 | ||
130 | /** \brief returns all packages in the cache who belong to the given task | |
131 | ||
132 | A simple helper responsible for search for all members of a task | |
133 | in the cache. Optional it prints a a notice about the | |
134 | packages chosen cause of the given task. | |
135 | \param Cache the packages are in | |
136 | \param pattern name of the task | |
137 | \param helper responsible for error and message handling */ | |
138 | static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper); | |
139 | static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) { | |
140 | CacheSetHelper helper; | |
141 | return FromTask(Cache, pattern, helper); | |
142 | } | |
143 | ||
144 | /** \brief returns all packages in the cache whose name matchs a given pattern | |
145 | ||
146 | A simple helper responsible for executing a regular expression on all | |
147 | package names in the cache. Optional it prints a a notice about the | |
148 | packages chosen cause of the given package. | |
149 | \param Cache the packages are in | |
150 | \param pattern regular expression for package names | |
151 | \param helper responsible for error and message handling */ | |
152 | static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper); | |
153 | static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) { | |
154 | CacheSetHelper helper; | |
155 | return FromRegEx(Cache, pattern, helper); | |
156 | } | |
157 | ||
158 | /** \brief returns all packages specified by a string | |
159 | ||
160 | \param Cache the packages are in | |
161 | \param string String the package name(s) should be extracted from | |
162 | \param helper responsible for error and message handling */ | |
163 | static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper); | |
164 | static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) { | |
165 | CacheSetHelper helper; | |
166 | return FromString(Cache, string, helper); | |
167 | } | |
168 | ||
169 | /** \brief returns a package specified by a string | |
170 | ||
171 | \param Cache the package is in | |
172 | \param string String the package name should be extracted from | |
173 | \param helper responsible for error and message handling */ | |
174 | static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper); | |
175 | static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string) { | |
176 | CacheSetHelper helper; | |
177 | return FromName(Cache, string, helper); | |
178 | } | |
179 | ||
180 | /** \brief returns all packages specified on the commandline | |
181 | ||
182 | Get all package names from the commandline and executes regex's if needed. | |
183 | No special package command is supported, just plain names. | |
184 | \param Cache the packages are in | |
185 | \param cmdline Command line the package names should be extracted from | |
186 | \param helper responsible for error and message handling */ | |
187 | static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper); | |
188 | static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) { | |
189 | CacheSetHelper helper; | |
190 | return FromCommandLine(Cache, cmdline, helper); | |
191 | } | |
192 | ||
193 | struct Modifier { | |
194 | enum Position { NONE, PREFIX, POSTFIX }; | |
195 | unsigned short ID; | |
196 | const char * const Alias; | |
197 | Position Pos; | |
198 | Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {}; | |
199 | }; | |
200 | ||
201 | /** \brief group packages by a action modifiers | |
202 | ||
203 | At some point it is needed to get from the same commandline | |
204 | different package sets grouped by a modifier. Take | |
205 | apt-get install apt awesome- | |
206 | as an example. | |
207 | \param Cache the packages are in | |
208 | \param cmdline Command line the package names should be extracted from | |
209 | \param mods list of modifiers the method should accept | |
210 | \param fallback the default modifier group for a package | |
211 | \param helper responsible for error and message handling */ | |
212 | static std::map<unsigned short, PackageSet> GroupedFromCommandLine( | |
213 | pkgCacheFile &Cache, const char **cmdline, | |
214 | std::list<PackageSet::Modifier> const &mods, | |
215 | unsigned short const &fallback, CacheSetHelper &helper); | |
216 | static std::map<unsigned short, PackageSet> GroupedFromCommandLine( | |
217 | pkgCacheFile &Cache, const char **cmdline, | |
218 | std::list<PackageSet::Modifier> const &mods, | |
219 | unsigned short const &fallback) { | |
220 | CacheSetHelper helper; | |
221 | return GroupedFromCommandLine(Cache, cmdline, | |
222 | mods, fallback, helper); | |
223 | } | |
224 | ||
225 | enum Constructor { UNKNOWN, REGEX, TASK }; | |
226 | Constructor getConstructor() const { return ConstructedBy; }; | |
227 | ||
228 | PackageSet() : ConstructedBy(UNKNOWN) {}; | |
229 | PackageSet(Constructor const &by) : ConstructedBy(by) {}; | |
230 | /*}}}*/ | |
231 | private: /*{{{*/ | |
232 | Constructor ConstructedBy; | |
233 | /*}}}*/ | |
234 | }; /*}}}*/ | |
235 | class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/ | |
236 | /** \class APT::VersionSet | |
237 | ||
238 | Simple wrapper around a std::set to provide a similar interface to | |
239 | a set of versions as to the complete set of all versions in the | |
240 | pkgCache. */ | |
241 | public: /*{{{*/ | |
242 | /** \brief smell like a pkgCache::VerIterator */ | |
243 | class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/ | |
244 | public: | |
245 | const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) : | |
246 | std::set<pkgCache::VerIterator>::const_iterator(x) {} | |
247 | ||
248 | operator pkgCache::VerIterator(void) { return **this; } | |
249 | ||
250 | inline pkgCache *Cache() const { return (**this).Cache(); }; | |
251 | inline unsigned long Index() const {return (**this).Index();}; | |
252 | // we have only valid iterators here | |
253 | inline bool end() const { return false; }; | |
254 | ||
255 | inline pkgCache::Version const * operator->() const { | |
256 | return &***this; | |
257 | }; | |
258 | ||
259 | inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); }; | |
260 | inline const char *VerStr() const { return (**this).VerStr(); }; | |
261 | inline const char *Section() const { return (**this).Section(); }; | |
262 | inline const char *Arch() const { return (**this).Arch(); }; | |
263 | inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); }; | |
264 | inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); }; | |
265 | inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); }; | |
266 | inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); }; | |
267 | inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); }; | |
268 | inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); }; | |
269 | inline bool Downloadable() const { return (**this).Downloadable(); }; | |
270 | inline const char *PriorityType() const { return (**this).PriorityType(); }; | |
271 | inline std::string RelStr() const { return (**this).RelStr(); }; | |
272 | inline bool Automatic() const { return (**this).Automatic(); }; | |
273 | inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); }; | |
274 | }; | |
275 | /*}}}*/ | |
276 | // 103. set::iterator is required to be modifiable, but this allows modification of keys | |
277 | typedef APT::VersionSet::const_iterator iterator; | |
278 | ||
279 | using std::set<pkgCache::VerIterator>::insert; | |
280 | inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); }; | |
281 | inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); }; | |
282 | ||
283 | /** \brief specifies which version(s) will be returned if non is given */ | |
284 | enum Version { | |
285 | /** All versions */ | |
286 | ALL, | |
287 | /** Candidate and installed version */ | |
288 | CANDANDINST, | |
289 | /** Candidate version */ | |
290 | CANDIDATE, | |
291 | /** Installed version */ | |
292 | INSTALLED, | |
293 | /** Candidate or if non installed version */ | |
294 | CANDINST, | |
295 | /** Installed or if non candidate version */ | |
296 | INSTCAND, | |
297 | /** Newest version */ | |
298 | NEWEST | |
299 | }; | |
300 | ||
301 | /** \brief returns all versions specified on the commandline | |
302 | ||
303 | Get all versions from the commandline, uses given default version if | |
304 | non specifically requested and executes regex's if needed on names. | |
305 | \param Cache the packages and versions are in | |
306 | \param cmdline Command line the versions should be extracted from | |
307 | \param helper responsible for error and message handling */ | |
308 | static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, | |
309 | APT::VersionSet::Version const &fallback, CacheSetHelper &helper); | |
310 | static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, | |
311 | APT::VersionSet::Version const &fallback) { | |
312 | CacheSetHelper helper; | |
313 | return FromCommandLine(Cache, cmdline, fallback, helper); | |
314 | } | |
315 | static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) { | |
316 | return FromCommandLine(Cache, cmdline, CANDINST); | |
317 | } | |
318 | ||
319 | static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg, | |
320 | APT::VersionSet::Version const &fallback, CacheSetHelper &helper, | |
321 | bool const &onlyFromName = false); | |
322 | static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg, | |
323 | APT::VersionSet::Version const &fallback) { | |
324 | CacheSetHelper helper; | |
325 | return FromString(Cache, pkg, fallback, helper); | |
326 | } | |
327 | static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) { | |
328 | return FromString(Cache, pkg, CANDINST); | |
329 | } | |
330 | ||
331 | /** \brief returns all versions specified for the package | |
332 | ||
333 | \param Cache the package and versions are in | |
334 | \param P the package in question | |
335 | \param fallback the version(s) you want to get | |
336 | \param helper the helper used for display and error handling */ | |
337 | static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, | |
338 | VersionSet::Version const &fallback, CacheSetHelper &helper); | |
339 | static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, | |
340 | APT::VersionSet::Version const &fallback) { | |
341 | CacheSetHelper helper; | |
342 | return FromPackage(Cache, P, fallback, helper); | |
343 | } | |
344 | static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) { | |
345 | return FromPackage(Cache, P, CANDINST); | |
346 | } | |
347 | ||
348 | struct Modifier { | |
349 | enum Position { NONE, PREFIX, POSTFIX }; | |
350 | unsigned short ID; | |
351 | const char * const Alias; | |
352 | Position Pos; | |
353 | VersionSet::Version SelectVersion; | |
354 | Modifier (unsigned short const &id, const char * const alias, Position const &pos, | |
355 | VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos), | |
356 | SelectVersion(select) {}; | |
357 | }; | |
358 | ||
359 | static std::map<unsigned short, VersionSet> GroupedFromCommandLine( | |
360 | pkgCacheFile &Cache, const char **cmdline, | |
361 | std::list<VersionSet::Modifier> const &mods, | |
362 | unsigned short const &fallback, CacheSetHelper &helper); | |
363 | static std::map<unsigned short, VersionSet> GroupedFromCommandLine( | |
364 | pkgCacheFile &Cache, const char **cmdline, | |
365 | std::list<VersionSet::Modifier> const &mods, | |
366 | unsigned short const &fallback) { | |
367 | CacheSetHelper helper; | |
368 | return GroupedFromCommandLine(Cache, cmdline, | |
369 | mods, fallback, helper); | |
370 | } | |
371 | /*}}}*/ | |
372 | protected: /*{{{*/ | |
373 | ||
374 | /** \brief returns the candidate version of the package | |
375 | ||
376 | \param Cache to be used to query for information | |
377 | \param Pkg we want the candidate version from this package */ | |
378 | static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache, | |
379 | pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper); | |
380 | ||
381 | /** \brief returns the installed version of the package | |
382 | ||
383 | \param Cache to be used to query for information | |
384 | \param Pkg we want the installed version from this package */ | |
385 | static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache, | |
386 | pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper); | |
387 | /*}}}*/ | |
388 | }; /*}}}*/ | |
389 | } | |
390 | #endif |