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