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