]>
git.saurik.com Git - apt.git/blob - cmdline/cacheset.h
2ca794f28c6de2a80dcd2a1b0144b371264e188f
1 // -*- mode: cpp; mode: fold -*-
4 Wrappers around std::set to have set::iterators which behave
5 similar to the Iterators of the cache structures.
7 Provides also a few helper methods which work with these sets */
10 #define APT_CACHESET_H
11 // Include Files /*{{{*/
19 #include <apt-pkg/cachefile.h>
20 #include <apt-pkg/pkgcache.h>
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.
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
35 CacheSetHelper(bool const &ShowError
= true) : ShowError(ShowError
) {};
36 virtual ~CacheSetHelper() {};
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
) {};
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
);
59 bool showErrors() const { return ShowError
; };
60 bool showErrors(bool const &newValue
) { if (ShowError
== newValue
) return ShowError
; else return ((ShowError
= newValue
) == false); };
65 class PackageSet
: public std::set
<pkgCache::PkgIterator
> { /*{{{*/
66 /** \class APT::PackageSet
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
72 /** \brief smell like a pkgCache::PkgIterator */
73 class const_iterator
: public std::set
<pkgCache::PkgIterator
>::const_iterator
{/*{{{*/
75 const_iterator(std::set
<pkgCache::PkgIterator
>::const_iterator x
) :
76 std::set
<pkgCache::PkgIterator
>::const_iterator(x
) {}
78 operator pkgCache::PkgIterator(void) { return **this; }
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; };
99 friend std::ostream
& operator<<(std::ostream
& out
, const_iterator i
) { return operator<<(out
, (*i
)); }
101 inline pkgCache::Package
const * operator->() const {
105 // 103. set::iterator is required to be modifiable, but this allows modification of keys
106 typedef APT::PackageSet::const_iterator iterator
;
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()); };
113 /** \brief returns all packages in the cache who belong to the given task
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
);
127 /** \brief returns all packages in the cache whose name matchs a given pattern
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
);
141 /** \brief returns all packages specified by a string
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
);
152 /** \brief returns a package specified by a string
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
);
163 /** \brief returns all packages specified on the commandline
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
);
177 enum Position
{ NONE
, PREFIX
, POSTFIX
};
179 const char * const Alias
;
181 Modifier (unsigned short const &id
, const char * const alias
, Position
const &pos
) : ID(id
), Alias(alias
), Pos(pos
) {};
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
);
198 class VersionSet
: public std::set
<pkgCache::VerIterator
> { /*{{{*/
199 /** \class APT::VersionSet
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
205 /** \brief smell like a pkgCache::VerIterator */
206 class const_iterator
: public std::set
<pkgCache::VerIterator
>::const_iterator
{/*{{{*/
208 const_iterator(std::set
<pkgCache::VerIterator
>::const_iterator x
) :
209 std::set
<pkgCache::VerIterator
>::const_iterator(x
) {}
211 operator pkgCache::VerIterator(void) { return **this; }
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; };
218 inline pkgCache::Version
const * operator->() const {
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(); };
241 // 103. set::iterator is required to be modifiable, but this allows modification of keys
242 typedef APT::VersionSet::const_iterator iterator
;
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()); };
248 /** \brief specifies which version(s) will be returned if non is given */
252 /** Candidate and installed version */
254 /** Candidate version */
256 /** Installed version */
258 /** Candidate or if non installed version */
260 /** Installed or if non candidate version */
262 /** Newest version */
266 /** \brief returns all versions specified on the commandline
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
);
280 static APT::VersionSet
FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
) {
281 return APT::VersionSet::FromCommandLine(Cache
, cmdline
, CANDINST
);
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
);
292 static APT::VersionSet
FromString(pkgCacheFile
&Cache
, std::string pkg
) {
293 return APT::VersionSet::FromString(Cache
, pkg
, CANDINST
);
296 /** \brief returns all versions specified for the package
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
);
306 enum Position
{ NONE
, PREFIX
, POSTFIX
};
308 const char * const Alias
;
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
) {};
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
);
331 /** \brief returns the candidate version of the package
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
);
338 /** \brief returns the installed version of the package
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
);