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