]> git.saurik.com Git - apt.git/blob - cmdline/cacheset.h
2ca794f28c6de2a80dcd2a1b0144b371264e188f
[apt.git] / cmdline / cacheset.h
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/cachefile.h>
20 #include <apt-pkg/pkgcache.h>
21 /*}}}*/
22 namespace APT {
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
43 virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
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 VersionSet canNotFindCandInstVer(pkgCacheFile &Cache,
51 pkgCache::PkgIterator const &Pkg);
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 /*}}}*/
62 protected:
63 bool ShowError;
64 }; /*}}}*/
65 class PackageSet : public std::set<pkgCache::PkgIterator> { /*{{{*/
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. */
71 public: /*{{{*/
72 /** \brief smell like a pkgCache::PkgIterator */
73 class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/
74 public:
75 const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
76 std::set<pkgCache::PkgIterator>::const_iterator(x) {}
77
78 operator pkgCache::PkgIterator(void) { return **this; }
79
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; };
98
99 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
100
101 inline pkgCache::Package const * operator->() const {
102 return &***this;
103 };
104 };
105 // 103. set::iterator is required to be modifiable, but this allows modification of keys
106 typedef APT::PackageSet::const_iterator iterator;
107 /*}}}*/
108
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); };
111 inline void insert(PackageSet const &pkgset) { insert(pkgset.begin(), pkgset.end()); };
112
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
120 \param out stream to print the notice to */
121 static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
122 static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) {
123 CacheSetHelper helper;
124 return APT::PackageSet::FromTask(Cache, pattern, helper);
125 }
126
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
134 \param out stream to print the notice to */
135 static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
136 static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
137 CacheSetHelper helper;
138 return APT::PackageSet::FromRegEx(Cache, pattern, helper);
139 }
140
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
145 \param out stream to print various notices to */
146 static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
147 static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
148 CacheSetHelper helper;
149 return APT::PackageSet::FromString(Cache, string, helper);
150 }
151
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
156 \param out stream to print various notices to */
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
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
169 \param out stream to print various notices to */
170 static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
171 static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
172 CacheSetHelper helper;
173 return APT::PackageSet::FromCommandLine(Cache, cmdline, helper);
174 }
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
184 static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
185 pkgCacheFile &Cache, const char **cmdline,
186 std::list<PackageSet::Modifier> const &mods,
187 unsigned short const &fallback, CacheSetHelper &helper);
188 static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
189 pkgCacheFile &Cache, const char **cmdline,
190 std::list<PackageSet::Modifier> const &mods,
191 unsigned short const &fallback) {
192 CacheSetHelper helper;
193 return APT::PackageSet::GroupedFromCommandLine(Cache, cmdline,
194 mods, fallback, helper);
195 }
196 /*}}}*/
197 }; /*}}}*/
198 class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
199 /** \class APT::VersionSet
200
201 Simple wrapper around a std::set to provide a similar interface to
202 a set of versions as to the complete set of all versions in the
203 pkgCache. */
204 public: /*{{{*/
205 /** \brief smell like a pkgCache::VerIterator */
206 class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
207 public:
208 const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
209 std::set<pkgCache::VerIterator>::const_iterator(x) {}
210
211 operator pkgCache::VerIterator(void) { return **this; }
212
213 inline pkgCache *Cache() const { return (**this).Cache(); };
214 inline unsigned long Index() const {return (**this).Index();};
215 // we have only valid iterators here
216 inline bool end() const { return false; };
217
218 inline pkgCache::Version const * operator->() const {
219 return &***this;
220 };
221
222 inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
223 inline const char *VerStr() const { return (**this).VerStr(); };
224 inline const char *Section() const { return (**this).Section(); };
225 inline const char *Arch() const { return (**this).Arch(); };
226 inline const char *Arch(bool const pseudo) const { return (**this).Arch(pseudo); };
227 inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
228 inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
229 inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
230 inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
231 inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
232 inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
233 inline bool Downloadable() const { return (**this).Downloadable(); };
234 inline const char *PriorityType() const { return (**this).PriorityType(); };
235 inline string RelStr() const { return (**this).RelStr(); };
236 inline bool Automatic() const { return (**this).Automatic(); };
237 inline bool Pseudo() const { return (**this).Pseudo(); };
238 inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
239 };
240 /*}}}*/
241 // 103. set::iterator is required to be modifiable, but this allows modification of keys
242 typedef APT::VersionSet::const_iterator iterator;
243
244 using std::set<pkgCache::VerIterator>::insert;
245 inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
246 inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
247
248 /** \brief specifies which version(s) will be returned if non is given */
249 enum Version {
250 /** All versions */
251 ALL,
252 /** Candidate and installed version */
253 CANDANDINST,
254 /** Candidate version */
255 CANDIDATE,
256 /** Installed version */
257 INSTALLED,
258 /** Candidate or if non installed version */
259 CANDINST,
260 /** Installed or if non candidate version */
261 INSTCAND,
262 /** Newest version */
263 NEWEST
264 };
265
266 /** \brief returns all versions specified on the commandline
267
268 Get all versions from the commandline, uses given default version if
269 non specifically requested and executes regex's if needed on names.
270 \param Cache the packages and versions are in
271 \param cmdline Command line the versions should be extracted from
272 \param out stream to print various notices to */
273 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
274 APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
275 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
276 APT::VersionSet::Version const &fallback) {
277 CacheSetHelper helper;
278 return APT::VersionSet::FromCommandLine(Cache, cmdline, fallback, helper);
279 }
280 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
281 return APT::VersionSet::FromCommandLine(Cache, cmdline, CANDINST);
282 }
283
284 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
285 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
286 bool const &onlyFromName = false);
287 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
288 APT::VersionSet::Version const &fallback) {
289 CacheSetHelper helper;
290 return APT::VersionSet::FromString(Cache, pkg, fallback, helper);
291 }
292 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
293 return APT::VersionSet::FromString(Cache, pkg, CANDINST);
294 }
295
296 /** \brief returns all versions specified for the package
297
298 \param Cache the package and versions are in
299 \param P the package in question
300 \param fallback the version(s) you want to get
301 \param helper the helper used for display and error handling */
302 static VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
303 VersionSet::Version const &fallback, CacheSetHelper &helper);
304
305 struct Modifier {
306 enum Position { NONE, PREFIX, POSTFIX };
307 unsigned short ID;
308 const char * const Alias;
309 Position Pos;
310 VersionSet::Version SelectVersion;
311 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
312 VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
313 SelectVersion(select) {};
314 };
315
316 static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
317 pkgCacheFile &Cache, const char **cmdline,
318 std::list<VersionSet::Modifier> const &mods,
319 unsigned short const &fallback, CacheSetHelper &helper);
320 static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
321 pkgCacheFile &Cache, const char **cmdline,
322 std::list<VersionSet::Modifier> const &mods,
323 unsigned short const &fallback) {
324 CacheSetHelper helper;
325 return APT::VersionSet::GroupedFromCommandLine(Cache, cmdline,
326 mods, fallback, helper);
327 }
328 /*}}}*/
329 protected: /*{{{*/
330
331 /** \brief returns the candidate version of the package
332
333 \param Cache to be used to query for information
334 \param Pkg we want the candidate version from this package */
335 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
336 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
337
338 /** \brief returns the installed version of the package
339
340 \param Cache to be used to query for information
341 \param Pkg we want the installed version from this package */
342 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
343 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
344 /*}}}*/
345 }; /*}}}*/
346 }
347 #endif