* apt-pkg/deb/deblistparser.cc:
[ntk/apt.git] / apt-pkg / 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
472ff00e 19#include <apt-pkg/error.h>
e1dbde8d
DK
20#include <apt-pkg/pkgcache.h>
21 /*}}}*/
472ff00e
DK
22
23class pkgCacheFile;
24
e1dbde8d 25namespace APT {
70e706ad
DK
26class PackageSet;
27class VersionSet;
28class CacheSetHelper { /*{{{*/
29/** \class APT::CacheSetHelper
30 Simple base class with a lot of virtual methods which can be overridden
31 to alter the behavior or the output of the CacheSets.
32
33 This helper is passed around by the static methods in the CacheSets and
34 used every time they hit an error condition or something could be
35 printed out.
36*/
37public: /*{{{*/
cd7bbc47
DK
38 CacheSetHelper(bool const &ShowError = true,
39 GlobalError::MsgType ErrorType = GlobalError::ERROR) :
40 ShowError(ShowError), ErrorType(ErrorType) {};
70e706ad
DK
41 virtual ~CacheSetHelper() {};
42
472ff00e
DK
43 virtual void showTaskSelection(PackageSet const &pkgset, std::string const &pattern) {};
44 virtual void showRegExSelection(PackageSet const &pkgset, std::string const &pattern) {};
70e706ad 45 virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
472ff00e 46 std::string const &ver, bool const &verIsRel) {};
70e706ad 47
bd631595 48 virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
70e706ad
DK
49 virtual PackageSet canNotFindTask(pkgCacheFile &Cache, std::string pattern);
50 virtual PackageSet canNotFindRegEx(pkgCacheFile &Cache, std::string pattern);
51 virtual PackageSet canNotFindPackage(pkgCacheFile &Cache, std::string const &str);
52 virtual VersionSet canNotFindAllVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
53 virtual VersionSet canNotFindInstCandVer(pkgCacheFile &Cache,
54 pkgCache::PkgIterator const &Pkg);
cf28bcad
DK
55 virtual VersionSet canNotFindCandInstVer(pkgCacheFile &Cache,
56 pkgCache::PkgIterator const &Pkg);
70e706ad
DK
57 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
58 pkgCache::PkgIterator const &Pkg);
59 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
60 pkgCache::PkgIterator const &Pkg);
61 virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
62 pkgCache::PkgIterator const &Pkg);
63
64 bool showErrors() const { return ShowError; };
65 bool showErrors(bool const &newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); };
cd7bbc47
DK
66 GlobalError::MsgType errorType() const { return ErrorType; };
67 GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
68 {
69 if (ErrorType == newValue) return ErrorType;
70 else {
71 GlobalError::MsgType const &oldValue = ErrorType;
72 ErrorType = newValue;
73 return oldValue;
74 }
75 };
76
70e706ad
DK
77 /*}}}*/
78protected:
79 bool ShowError;
cd7bbc47 80 GlobalError::MsgType ErrorType;
70e706ad 81}; /*}}}*/
d4489d49 82class PackageSet : public std::set<pkgCache::PkgIterator> { /*{{{*/
7959c5ed
DK
83/** \class APT::PackageSet
84
85 Simple wrapper around a std::set to provide a similar interface to
86 a set of packages as to the complete set of all packages in the
87 pkgCache. */
e1dbde8d
DK
88public: /*{{{*/
89 /** \brief smell like a pkgCache::PkgIterator */
dc0f01f7 90 class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/
e1dbde8d
DK
91 public:
92 const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
93 std::set<pkgCache::PkgIterator>::const_iterator(x) {}
94
ffee1c2b
DK
95 operator pkgCache::PkgIterator(void) { return **this; }
96
78c32596
DK
97 inline const char *Name() const {return (**this).Name(); }
98 inline std::string FullName(bool const &Pretty) const { return (**this).FullName(Pretty); }
99 inline std::string FullName() const { return (**this).FullName(); }
100 inline const char *Section() const {return (**this).Section(); }
101 inline bool Purge() const {return (**this).Purge(); }
102 inline const char *Arch() const {return (**this).Arch(); }
103 inline pkgCache::GrpIterator Group() const { return (**this).Group(); }
104 inline pkgCache::VerIterator VersionList() const { return (**this).VersionList(); }
105 inline pkgCache::VerIterator CurrentVer() const { return (**this).CurrentVer(); }
106 inline pkgCache::DepIterator RevDependsList() const { return (**this).RevDependsList(); }
107 inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); }
108 inline pkgCache::PkgIterator::OkState State() const { return (**this).State(); }
109 inline const char *CandVersion() const { return (**this).CandVersion(); }
110 inline const char *CurVersion() const { return (**this).CurVersion(); }
111 inline pkgCache *Cache() const { return (**this).Cache(); };
112 inline unsigned long Index() const {return (**this).Index();};
113 // we have only valid iterators here
114 inline bool end() const { return false; };
e1dbde8d
DK
115
116 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
117
78c32596
DK
118 inline pkgCache::Package const * operator->() const {
119 return &***this;
e1dbde8d
DK
120 };
121 };
122 // 103. set::iterator is required to be modifiable, but this allows modification of keys
6d052eba 123 typedef APT::PackageSet::const_iterator iterator;
dc0f01f7 124 /*}}}*/
ffee1c2b 125
c45f2d19
DK
126 using std::set<pkgCache::PkgIterator>::insert;
127 inline void insert(pkgCache::PkgIterator const &P) { if (P.end() == false) std::set<pkgCache::PkgIterator>::insert(P); };
70e706ad 128 inline void insert(PackageSet const &pkgset) { insert(pkgset.begin(), pkgset.end()); };
c45f2d19 129
dc0f01f7
DK
130 /** \brief returns all packages in the cache who belong to the given task
131
132 A simple helper responsible for search for all members of a task
133 in the cache. Optional it prints a a notice about the
134 packages chosen cause of the given task.
135 \param Cache the packages are in
136 \param pattern name of the task
c8db3fff 137 \param helper responsible for error and message handling */
70e706ad 138 static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
dc0f01f7 139 static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) {
70e706ad 140 CacheSetHelper helper;
446bbcf4 141 return FromTask(Cache, pattern, helper);
dc0f01f7
DK
142 }
143
ffee1c2b
DK
144 /** \brief returns all packages in the cache whose name matchs a given pattern
145
146 A simple helper responsible for executing a regular expression on all
147 package names in the cache. Optional it prints a a notice about the
148 packages chosen cause of the given package.
149 \param Cache the packages are in
150 \param pattern regular expression for package names
c8db3fff 151 \param helper responsible for error and message handling */
70e706ad 152 static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
856d3b06 153 static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
70e706ad 154 CacheSetHelper helper;
446bbcf4 155 return FromRegEx(Cache, pattern, helper);
ffee1c2b
DK
156 }
157
856d3b06
DK
158 /** \brief returns all packages specified by a string
159
160 \param Cache the packages are in
161 \param string String the package name(s) should be extracted from
c8db3fff 162 \param helper responsible for error and message handling */
70e706ad 163 static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
fe870feb 164 static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
70e706ad 165 CacheSetHelper helper;
446bbcf4 166 return FromString(Cache, string, helper);
856d3b06
DK
167 }
168
bd631595
DK
169 /** \brief returns a package specified by a string
170
171 \param Cache the package is in
172 \param string String the package name should be extracted from
c8db3fff 173 \param helper responsible for error and message handling */
bd631595
DK
174 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
175 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string) {
176 CacheSetHelper helper;
446bbcf4 177 return FromName(Cache, string, helper);
bd631595
DK
178 }
179
78c32596
DK
180 /** \brief returns all packages specified on the commandline
181
182 Get all package names from the commandline and executes regex's if needed.
183 No special package command is supported, just plain names.
184 \param Cache the packages are in
185 \param cmdline Command line the package names should be extracted from
c8db3fff 186 \param helper responsible for error and message handling */
70e706ad 187 static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
856d3b06 188 static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
70e706ad 189 CacheSetHelper helper;
446bbcf4 190 return FromCommandLine(Cache, cmdline, helper);
78c32596 191 }
9cc83a6f
DK
192
193 struct Modifier {
194 enum Position { NONE, PREFIX, POSTFIX };
195 unsigned short ID;
196 const char * const Alias;
197 Position Pos;
198 Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {};
199 };
200
c8db3fff
DK
201 /** \brief group packages by a action modifiers
202
203 At some point it is needed to get from the same commandline
204 different package sets grouped by a modifier. Take
205 apt-get install apt awesome-
206 as an example.
207 \param Cache the packages are in
208 \param cmdline Command line the package names should be extracted from
209 \param mods list of modifiers the method should accept
210 \param fallback the default modifier group for a package
211 \param helper responsible for error and message handling */
9cc83a6f
DK
212 static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
213 pkgCacheFile &Cache, const char **cmdline,
214 std::list<PackageSet::Modifier> const &mods,
70e706ad 215 unsigned short const &fallback, CacheSetHelper &helper);
9cc83a6f
DK
216 static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
217 pkgCacheFile &Cache, const char **cmdline,
218 std::list<PackageSet::Modifier> const &mods,
219 unsigned short const &fallback) {
70e706ad 220 CacheSetHelper helper;
446bbcf4 221 return GroupedFromCommandLine(Cache, cmdline,
70e706ad 222 mods, fallback, helper);
9cc83a6f 223 }
c8db3fff
DK
224
225 enum Constructor { UNKNOWN, REGEX, TASK };
226 Constructor getConstructor() const { return ConstructedBy; };
227
228 PackageSet() : ConstructedBy(UNKNOWN) {};
229 PackageSet(Constructor const &by) : ConstructedBy(by) {};
230 /*}}}*/
231private: /*{{{*/
232 Constructor ConstructedBy;
d4489d49
DK
233 /*}}}*/
234}; /*}}}*/
235class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
236/** \class APT::VersionSet
78c32596 237
d4489d49
DK
238 Simple wrapper around a std::set to provide a similar interface to
239 a set of versions as to the complete set of all versions in the
240 pkgCache. */
241public: /*{{{*/
242 /** \brief smell like a pkgCache::VerIterator */
dc0f01f7 243 class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
d4489d49
DK
244 public:
245 const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
246 std::set<pkgCache::VerIterator>::const_iterator(x) {}
78c32596 247
d4489d49
DK
248 operator pkgCache::VerIterator(void) { return **this; }
249
250 inline pkgCache *Cache() const { return (**this).Cache(); };
251 inline unsigned long Index() const {return (**this).Index();};
252 // we have only valid iterators here
253 inline bool end() const { return false; };
254
255 inline pkgCache::Version const * operator->() const {
256 return &***this;
257 };
258
259 inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
260 inline const char *VerStr() const { return (**this).VerStr(); };
261 inline const char *Section() const { return (**this).Section(); };
262 inline const char *Arch() const { return (**this).Arch(); };
d4489d49
DK
263 inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
264 inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
265 inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
266 inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
267 inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
268 inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
269 inline bool Downloadable() const { return (**this).Downloadable(); };
270 inline const char *PriorityType() const { return (**this).PriorityType(); };
472ff00e 271 inline std::string RelStr() const { return (**this).RelStr(); };
d4489d49 272 inline bool Automatic() const { return (**this).Automatic(); };
d4489d49
DK
273 inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
274 };
dc0f01f7 275 /*}}}*/
d4489d49 276 // 103. set::iterator is required to be modifiable, but this allows modification of keys
6d052eba 277 typedef APT::VersionSet::const_iterator iterator;
78c32596 278
c45f2d19
DK
279 using std::set<pkgCache::VerIterator>::insert;
280 inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
70e706ad 281 inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
c45f2d19 282
856d3b06
DK
283 /** \brief specifies which version(s) will be returned if non is given */
284 enum Version {
285 /** All versions */
286 ALL,
287 /** Candidate and installed version */
288 CANDANDINST,
289 /** Candidate version */
290 CANDIDATE,
291 /** Installed version */
292 INSTALLED,
293 /** Candidate or if non installed version */
294 CANDINST,
295 /** Installed or if non candidate version */
296 INSTCAND,
297 /** Newest version */
298 NEWEST
299 };
300
301 /** \brief returns all versions specified on the commandline
302
303 Get all versions from the commandline, uses given default version if
304 non specifically requested and executes regex's if needed on names.
305 \param Cache the packages and versions are in
306 \param cmdline Command line the versions should be extracted from
c8db3fff 307 \param helper responsible for error and message handling */
856d3b06 308 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
70e706ad 309 APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
856d3b06
DK
310 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
311 APT::VersionSet::Version const &fallback) {
70e706ad 312 CacheSetHelper helper;
446bbcf4 313 return FromCommandLine(Cache, cmdline, fallback, helper);
856d3b06
DK
314 }
315 static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
446bbcf4 316 return FromCommandLine(Cache, cmdline, CANDINST);
856d3b06 317 }
55c59998
DK
318
319 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
bd631595
DK
320 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
321 bool const &onlyFromName = false);
55c59998
DK
322 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
323 APT::VersionSet::Version const &fallback) {
70e706ad 324 CacheSetHelper helper;
446bbcf4 325 return FromString(Cache, pkg, fallback, helper);
55c59998
DK
326 }
327 static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
446bbcf4 328 return FromString(Cache, pkg, CANDINST);
55c59998
DK
329 }
330
fb83c1d0
DK
331 /** \brief returns all versions specified for the package
332
333 \param Cache the package and versions are in
334 \param P the package in question
335 \param fallback the version(s) you want to get
336 \param helper the helper used for display and error handling */
c8db3fff 337 static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
fb83c1d0 338 VersionSet::Version const &fallback, CacheSetHelper &helper);
c8db3fff
DK
339 static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
340 APT::VersionSet::Version const &fallback) {
341 CacheSetHelper helper;
446bbcf4 342 return FromPackage(Cache, P, fallback, helper);
c8db3fff
DK
343 }
344 static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
446bbcf4 345 return FromPackage(Cache, P, CANDINST);
c8db3fff 346 }
fb83c1d0 347
55c59998
DK
348 struct Modifier {
349 enum Position { NONE, PREFIX, POSTFIX };
350 unsigned short ID;
351 const char * const Alias;
352 Position Pos;
353 VersionSet::Version SelectVersion;
354 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
355 VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
356 SelectVersion(select) {};
357 };
358
359 static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
360 pkgCacheFile &Cache, const char **cmdline,
361 std::list<VersionSet::Modifier> const &mods,
70e706ad 362 unsigned short const &fallback, CacheSetHelper &helper);
55c59998
DK
363 static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
364 pkgCacheFile &Cache, const char **cmdline,
365 std::list<VersionSet::Modifier> const &mods,
366 unsigned short const &fallback) {
70e706ad 367 CacheSetHelper helper;
446bbcf4 368 return GroupedFromCommandLine(Cache, cmdline,
70e706ad 369 mods, fallback, helper);
55c59998 370 }
856d3b06
DK
371 /*}}}*/
372protected: /*{{{*/
373
374 /** \brief returns the candidate version of the package
375
376 \param Cache to be used to query for information
70e706ad 377 \param Pkg we want the candidate version from this package */
856d3b06 378 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
70e706ad 379 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
856d3b06
DK
380
381 /** \brief returns the installed version of the package
382
383 \param Cache to be used to query for information
70e706ad 384 \param Pkg we want the installed version from this package */
856d3b06 385 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
70e706ad 386 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
e1dbde8d 387 /*}}}*/
d4489d49 388}; /*}}}*/
e1dbde8d
DK
389}
390#endif