c8c3dd096dbe349c11c1c3517c15cd20c99b30ad
[ntk/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 helper responsible for error and message handling */
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 helper responsible for error and message handling */
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 helper responsible for error and message handling */
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 helper responsible for error and message handling */
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 helper responsible for error and message handling */
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 /** \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 */
195 static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
196 pkgCacheFile &Cache, const char **cmdline,
197 std::list<PackageSet::Modifier> const &mods,
198 unsigned short const &fallback, CacheSetHelper &helper);
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) {
203 CacheSetHelper helper;
204 return APT::PackageSet::GroupedFromCommandLine(Cache, cmdline,
205 mods, fallback, helper);
206 }
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 /*}}}*/
214 private: /*{{{*/
215 Constructor ConstructedBy;
216 /*}}}*/
217 }; /*}}}*/
218 class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
219 /** \class APT::VersionSet
220
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. */
224 public: /*{{{*/
225 /** \brief smell like a pkgCache::VerIterator */
226 class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
227 public:
228 const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
229 std::set<pkgCache::VerIterator>::const_iterator(x) {}
230
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 };
260 /*}}}*/
261 // 103. set::iterator is required to be modifiable, but this allows modification of keys
262 typedef APT::VersionSet::const_iterator iterator;
263
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); };
266 inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
267
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
292 \param helper responsible for error and message handling */
293 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
294 APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
295 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
296 APT::VersionSet::Version const &fallback) {
297 CacheSetHelper helper;
298 return APT::VersionSet::FromCommandLine(Cache, cmdline, fallback, helper);
299 }
300 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
301 return APT::VersionSet::FromCommandLine(Cache, cmdline, CANDINST);
302 }
303
304 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
305 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
306 bool const &onlyFromName = false);
307 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
308 APT::VersionSet::Version const &fallback) {
309 CacheSetHelper helper;
310 return APT::VersionSet::FromString(Cache, pkg, fallback, helper);
311 }
312 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
313 return APT::VersionSet::FromString(Cache, pkg, CANDINST);
314 }
315
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 */
322 static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
323 VersionSet::Version const &fallback, CacheSetHelper &helper);
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 }
332
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,
347 unsigned short const &fallback, CacheSetHelper &helper);
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) {
352 CacheSetHelper helper;
353 return APT::VersionSet::GroupedFromCommandLine(Cache, cmdline,
354 mods, fallback, helper);
355 }
356 /*}}}*/
357 protected: /*{{{*/
358
359 /** \brief returns the candidate version of the package
360
361 \param Cache to be used to query for information
362 \param Pkg we want the candidate version from this package */
363 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
364 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
365
366 /** \brief returns the installed version of the package
367
368 \param Cache to be used to query for information
369 \param Pkg we want the installed version from this package */
370 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
371 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
372 /*}}}*/
373 }; /*}}}*/
374 }
375 #endif