]> git.saurik.com Git - apt.git/blob - cmdline/cacheset.h
21c42c51109bdc5c2e7078158afc31bf57749870
[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 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 }; /*}}}*/
63 class PackageSet : public std::set<pkgCache::PkgIterator> { /*{{{*/
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. */
69 public: /*{{{*/
70 /** \brief smell like a pkgCache::PkgIterator */
71 class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/
72 public:
73 const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
74 std::set<pkgCache::PkgIterator>::const_iterator(x) {}
75
76 operator pkgCache::PkgIterator(void) { return **this; }
77
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; };
96
97 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
98
99 inline pkgCache::Package const * operator->() const {
100 return &***this;
101 };
102 };
103 // 103. set::iterator is required to be modifiable, but this allows modification of keys
104 typedef APT::PackageSet::const_iterator iterator;
105 /*}}}*/
106
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); };
109 inline void insert(PackageSet const &pkgset) { insert(pkgset.begin(), pkgset.end()); };
110
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 */
119 static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
120 static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) {
121 CacheSetHelper helper;
122 return APT::PackageSet::FromTask(Cache, pattern, helper);
123 }
124
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 */
133 static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
134 static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
135 CacheSetHelper helper;
136 return APT::PackageSet::FromRegEx(Cache, pattern, helper);
137 }
138
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 */
144 static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
145 static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
146 CacheSetHelper helper;
147 return APT::PackageSet::FromString(Cache, string, helper);
148 }
149
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
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 */
168 static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
169 static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
170 CacheSetHelper helper;
171 return APT::PackageSet::FromCommandLine(Cache, cmdline, helper);
172 }
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,
185 unsigned short const &fallback, CacheSetHelper &helper);
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) {
190 CacheSetHelper helper;
191 return APT::PackageSet::GroupedFromCommandLine(Cache, cmdline,
192 mods, fallback, helper);
193 }
194 /*}}}*/
195 }; /*}}}*/
196 class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
197 /** \class APT::VersionSet
198
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 */
204 class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
205 public:
206 const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
207 std::set<pkgCache::VerIterator>::const_iterator(x) {}
208
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 };
238 /*}}}*/
239 // 103. set::iterator is required to be modifiable, but this allows modification of keys
240 typedef APT::VersionSet::const_iterator iterator;
241
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); };
244 inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
245
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,
272 APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
273 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
274 APT::VersionSet::Version const &fallback) {
275 CacheSetHelper helper;
276 return APT::VersionSet::FromCommandLine(Cache, cmdline, fallback, helper);
277 }
278 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
279 return APT::VersionSet::FromCommandLine(Cache, cmdline, CANDINST);
280 }
281
282 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
283 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
284 bool const &onlyFromName = false);
285 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
286 APT::VersionSet::Version const &fallback) {
287 CacheSetHelper helper;
288 return APT::VersionSet::FromString(Cache, pkg, fallback, helper);
289 }
290 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
291 return APT::VersionSet::FromString(Cache, pkg, CANDINST);
292 }
293
294 struct Modifier {
295 enum Position { NONE, PREFIX, POSTFIX };
296 unsigned short ID;
297 const char * const Alias;
298 Position Pos;
299 VersionSet::Version SelectVersion;
300 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
301 VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
302 SelectVersion(select) {};
303 };
304
305 static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
306 pkgCacheFile &Cache, const char **cmdline,
307 std::list<VersionSet::Modifier> const &mods,
308 unsigned short const &fallback, CacheSetHelper &helper);
309 static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
310 pkgCacheFile &Cache, const char **cmdline,
311 std::list<VersionSet::Modifier> const &mods,
312 unsigned short const &fallback) {
313 CacheSetHelper helper;
314 return APT::VersionSet::GroupedFromCommandLine(Cache, cmdline,
315 mods, fallback, helper);
316 }
317 /*}}}*/
318 protected: /*{{{*/
319
320 /** \brief returns the candidate version of the package
321
322 \param Cache to be used to query for information
323 \param Pkg we want the candidate version from this package */
324 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
325 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
326
327 /** \brief returns the installed version of the package
328
329 \param Cache to be used to query for information
330 \param Pkg we want the installed version from this package */
331 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
332 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
333
334 static void AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
335 pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
336 CacheSetHelper &helper);
337
338 /*}}}*/
339 }; /*}}}*/
340 }
341 #endif