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