]> git.saurik.com Git - apt.git/blame - apt-pkg/cacheset.h
test/integration/test-ubuntu-bug-346386-apt-get-update-paywall: use downloadfile()
[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 12#include <fstream>
9cc83a6f 13#include <map>
ffee1c2b 14#include <set>
c4cca791 15#include <list>
e1dbde8d 16#include <string>
15fc8636 17#include <iterator>
ffee1c2b 18
453b82a3
DK
19#include <stddef.h>
20
472ff00e 21#include <apt-pkg/error.h>
e1dbde8d 22#include <apt-pkg/pkgcache.h>
453b82a3 23#include <apt-pkg/cacheiterators.h>
b9dadc24
DK
24
25#ifndef APT_8_CLEANER_HEADERS
26#include <apt-pkg/cachefile.h>
453b82a3
DK
27#endif
28#ifndef APT_10_CLEANER_HEADERS
29#include <iostream>
b9dadc24 30#endif
e1dbde8d 31 /*}}}*/
472ff00e
DK
32
33class pkgCacheFile;
34
e1dbde8d 35namespace APT {
15fc8636
DK
36class PackageContainerInterface;
37class VersionContainerInterface;
38
70e706ad
DK
39class CacheSetHelper { /*{{{*/
40/** \class APT::CacheSetHelper
41 Simple base class with a lot of virtual methods which can be overridden
42 to alter the behavior or the output of the CacheSets.
43
44 This helper is passed around by the static methods in the CacheSets and
45 used every time they hit an error condition or something could be
46 printed out.
47*/
48public: /*{{{*/
15fc8636 49 CacheSetHelper(bool const ShowError = true,
cd7bbc47 50 GlobalError::MsgType ErrorType = GlobalError::ERROR) :
d3e8fbb3
DK
51 ShowError(ShowError), ErrorType(ErrorType) {}
52 virtual ~CacheSetHelper() {}
70e706ad 53
15fc8636
DK
54 virtual void showTaskSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
55 virtual void showRegExSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
16724b66
MV
56#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
57 virtual void showFnmatchSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
58#endif
70e706ad 59 virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
15fc8636 60 std::string const &ver, bool const verIsRel);
70e706ad 61
15fc8636
DK
62 virtual void canNotFindTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
63 virtual void canNotFindRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
16724b66
MV
64#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
65 virtual void canNotFindFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
66#endif
15fc8636
DK
67 virtual void canNotFindPackage(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str);
68
69 virtual void canNotFindAllVer(VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
70 virtual void canNotFindInstCandVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
70e706ad 71 pkgCache::PkgIterator const &Pkg);
15fc8636
DK
72 virtual void canNotFindCandInstVer(VersionContainerInterface * const vci,
73 pkgCacheFile &Cache,
cf28bcad 74 pkgCache::PkgIterator const &Pkg);
15fc8636
DK
75
76 virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
70e706ad
DK
77 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
78 pkgCache::PkgIterator const &Pkg);
79 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
80 pkgCache::PkgIterator const &Pkg);
81 virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
82 pkgCache::PkgIterator const &Pkg);
83
d3e8fbb3
DK
84 bool showErrors() const { return ShowError; }
85 bool showErrors(bool const newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); }
86 GlobalError::MsgType errorType() const { return ErrorType; }
cd7bbc47
DK
87 GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
88 {
89 if (ErrorType == newValue) return ErrorType;
90 else {
91 GlobalError::MsgType const &oldValue = ErrorType;
92 ErrorType = newValue;
93 return oldValue;
94 }
d3e8fbb3 95 }
cd7bbc47 96
70e706ad
DK
97 /*}}}*/
98protected:
99 bool ShowError;
cd7bbc47 100 GlobalError::MsgType ErrorType;
70e706ad 101}; /*}}}*/
15fc8636
DK
102class PackageContainerInterface { /*{{{*/
103/** \class PackageContainerInterface
104
105 * Interface ensuring that all operations can be executed on the yet to
106 * define concrete PackageContainer - access to all methods is possible,
107 * but in general the wrappers provided by the PackageContainer template
108 * are nicer to use.
109
110 * This class mostly protects use from the need to write all implementation
111 * of the methods working on containers in the template */
112public:
113 class const_iterator { /*{{{*/
e1dbde8d 114 public:
15fc8636
DK
115 virtual pkgCache::PkgIterator getPkg() const = 0;
116 operator pkgCache::PkgIterator(void) const { return getPkg(); }
117
118 inline const char *Name() const {return getPkg().Name(); }
119 inline std::string FullName(bool const Pretty) const { return getPkg().FullName(Pretty); }
120 inline std::string FullName() const { return getPkg().FullName(); }
121 inline const char *Section() const {return getPkg().Section(); }
122 inline bool Purge() const {return getPkg().Purge(); }
123 inline const char *Arch() const {return getPkg().Arch(); }
124 inline pkgCache::GrpIterator Group() const { return getPkg().Group(); }
125 inline pkgCache::VerIterator VersionList() const { return getPkg().VersionList(); }
126 inline pkgCache::VerIterator CurrentVer() const { return getPkg().CurrentVer(); }
127 inline pkgCache::DepIterator RevDependsList() const { return getPkg().RevDependsList(); }
128 inline pkgCache::PrvIterator ProvidesList() const { return getPkg().ProvidesList(); }
129 inline pkgCache::PkgIterator::OkState State() const { return getPkg().State(); }
130 inline const char *CandVersion() const { return getPkg().CandVersion(); }
131 inline const char *CurVersion() const { return getPkg().CurVersion(); }
d3e8fbb3
DK
132 inline pkgCache *Cache() const { return getPkg().Cache(); }
133 inline unsigned long Index() const {return getPkg().Index();}
78c32596 134 // we have only valid iterators here
d3e8fbb3 135 inline bool end() const { return false; }
e1dbde8d 136
d3e8fbb3 137 inline pkgCache::Package const * operator->() const {return &*getPkg();}
15fc8636
DK
138 };
139 /*}}}*/
140
141 virtual bool insert(pkgCache::PkgIterator const &P) = 0;
142 virtual bool empty() const = 0;
143 virtual void clear() = 0;
144
b9179170 145 enum Constructor { UNKNOWN, REGEX, TASK, FNMATCH };
15fc8636
DK
146 virtual void setConstructor(Constructor const &con) = 0;
147 virtual Constructor getConstructor() const = 0;
148
149 static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
150 static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
151 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
b9179170 152 static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
2f0d4029 153 static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
15fc8636
DK
154 static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
155 static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
156
157 struct Modifier {
158 enum Position { NONE, PREFIX, POSTFIX };
159 unsigned short ID;
160 const char * const Alias;
161 Position Pos;
d3e8fbb3 162 Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {}
15fc8636
DK
163 };
164
165 static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
166 pkgCacheFile &Cache, const char * cmdline,
167 std::list<Modifier> const &mods, CacheSetHelper &helper);
168};
169 /*}}}*/
170template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/
171/** \class APT::PackageContainer
e1dbde8d 172
15fc8636
DK
173 Simple wrapper around a container class like std::set to provide a similar
174 interface to a set of packages as to the complete set of all packages in the
175 pkgCache. */
176 Container _cont;
177public: /*{{{*/
178 /** \brief smell like a pkgCache::PkgIterator */
c4cca791
DK
179 class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/
180 public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {
15fc8636
DK
181 typename Container::const_iterator _iter;
182 public:
183 const_iterator(typename Container::const_iterator i) : _iter(i) {}
184 pkgCache::PkgIterator getPkg(void) const { return *_iter; }
d3e8fbb3 185 inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
15fc8636 186 operator typename Container::const_iterator(void) const { return _iter; }
63b70b21
DK
187 inline const_iterator& operator++() { ++_iter; return *this; }
188 inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
d3e8fbb3
DK
189 inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
190 inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
15fc8636 191 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
e1dbde8d 192 };
c4cca791
DK
193 class iterator : public PackageContainerInterface::const_iterator,
194 public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
195 typename Container::iterator _iter;
196 public:
197 iterator(typename Container::iterator i) : _iter(i) {}
198 pkgCache::PkgIterator getPkg(void) const { return *_iter; }
d3e8fbb3 199 inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
c4cca791 200 operator typename Container::iterator(void) const { return _iter; }
bce0e0ff 201 operator typename PackageContainer<Container>::const_iterator() { return typename PackageContainer<Container>::const_iterator(_iter); }
63b70b21
DK
202 inline iterator& operator++() { ++_iter; return *this; }
203 inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
d3e8fbb3
DK
204 inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
205 inline bool operator==(iterator const &i) const { return _iter == i._iter; }
206 inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
207 inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
c4cca791
DK
208 friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
209 };
dc0f01f7 210 /*}}}*/
ffee1c2b 211
d3e8fbb3
DK
212 bool insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; _cont.insert(P); return true; }
213 template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); }
214 void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
c4cca791 215
d3e8fbb3
DK
216 bool empty() const { return _cont.empty(); }
217 void clear() { return _cont.clear(); }
5eb9a474 218 //FIXME: on ABI break, replace the first with the second without bool
d3e8fbb3
DK
219 void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
220 iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
221 size_t erase(const pkgCache::PkgIterator x) { return _cont.erase(x); }
222 void erase(iterator first, iterator last) { _cont.erase(first, last); }
223 size_t size() const { return _cont.size(); }
15fc8636 224
d3e8fbb3
DK
225 const_iterator begin() const { return const_iterator(_cont.begin()); }
226 const_iterator end() const { return const_iterator(_cont.end()); }
227 iterator begin() { return iterator(_cont.begin()); }
228 iterator end() { return iterator(_cont.end()); }
229 const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); }
15fc8636 230
d3e8fbb3
DK
231 void setConstructor(Constructor const &by) { ConstructedBy = by; }
232 Constructor getConstructor() const { return ConstructedBy; }
15fc8636 233
d3e8fbb3
DK
234 PackageContainer() : ConstructedBy(UNKNOWN) {}
235 PackageContainer(Constructor const &by) : ConstructedBy(by) {}
c45f2d19 236
dc0f01f7
DK
237 /** \brief returns all packages in the cache who belong to the given task
238
239 A simple helper responsible for search for all members of a task
240 in the cache. Optional it prints a a notice about the
241 packages chosen cause of the given task.
242 \param Cache the packages are in
243 \param pattern name of the task
c8db3fff 244 \param helper responsible for error and message handling */
15fc8636
DK
245 static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
246 PackageContainer cont(TASK);
247 PackageContainerInterface::FromTask(&cont, Cache, pattern, helper);
248 return cont;
249 }
250 static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) {
70e706ad 251 CacheSetHelper helper;
446bbcf4 252 return FromTask(Cache, pattern, helper);
dc0f01f7
DK
253 }
254
ffee1c2b
DK
255 /** \brief returns all packages in the cache whose name matchs a given pattern
256
257 A simple helper responsible for executing a regular expression on all
258 package names in the cache. Optional it prints a a notice about the
259 packages chosen cause of the given package.
260 \param Cache the packages are in
261 \param pattern regular expression for package names
c8db3fff 262 \param helper responsible for error and message handling */
15fc8636
DK
263 static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
264 PackageContainer cont(REGEX);
265 PackageContainerInterface::FromRegEx(&cont, Cache, pattern, helper);
266 return cont;
267 }
268
269 static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
70e706ad 270 CacheSetHelper helper;
446bbcf4 271 return FromRegEx(Cache, pattern, helper);
ffee1c2b
DK
272 }
273
b9179170
MV
274 static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
275 PackageContainer cont(FNMATCH);
276 PackageContainerInterface::FromFnmatch(&cont, Cache, pattern, helper);
277 return cont;
278 }
279 static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) {
280 CacheSetHelper helper;
281 return FromFnmatch(Cache, pattern, helper);
282 }
283
15fc8636 284 /** \brief returns a package specified by a string
856d3b06 285
15fc8636
DK
286 \param Cache the package is in
287 \param pattern String the package name should be extracted from
c8db3fff 288 \param helper responsible for error and message handling */
15fc8636
DK
289 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
290 return PackageContainerInterface::FromName(Cache, pattern, helper);
291 }
292 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) {
70e706ad 293 CacheSetHelper helper;
15fc8636 294 return PackageContainerInterface::FromName(Cache, pattern, helper);
856d3b06
DK
295 }
296
15fc8636 297 /** \brief returns all packages specified by a string
bd631595 298
15fc8636
DK
299 \param Cache the packages are in
300 \param pattern String the package name(s) should be extracted from
c8db3fff 301 \param helper responsible for error and message handling */
15fc8636
DK
302 static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
303 PackageContainer cont;
304 PackageContainerInterface::FromString(&cont, Cache, pattern, helper);
305 return cont;
306 }
307 static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) {
bd631595 308 CacheSetHelper helper;
15fc8636 309 return FromString(Cache, pattern, helper);
bd631595
DK
310 }
311
78c32596
DK
312 /** \brief returns all packages specified on the commandline
313
314 Get all package names from the commandline and executes regex's if needed.
315 No special package command is supported, just plain names.
316 \param Cache the packages are in
317 \param cmdline Command line the package names should be extracted from
c8db3fff 318 \param helper responsible for error and message handling */
15fc8636
DK
319 static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
320 PackageContainer cont;
321 PackageContainerInterface::FromCommandLine(&cont, Cache, cmdline, helper);
322 return cont;
323 }
324 static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
70e706ad 325 CacheSetHelper helper;
446bbcf4 326 return FromCommandLine(Cache, cmdline, helper);
78c32596 327 }
9cc83a6f 328
c8db3fff
DK
329 /** \brief group packages by a action modifiers
330
331 At some point it is needed to get from the same commandline
332 different package sets grouped by a modifier. Take
333 apt-get install apt awesome-
334 as an example.
335 \param Cache the packages are in
336 \param cmdline Command line the package names should be extracted from
337 \param mods list of modifiers the method should accept
338 \param fallback the default modifier group for a package
339 \param helper responsible for error and message handling */
15fc8636
DK
340 static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
341 pkgCacheFile &Cache,
342 const char **cmdline,
343 std::list<Modifier> const &mods,
344 unsigned short const &fallback,
345 CacheSetHelper &helper) {
346 std::map<unsigned short, PackageContainer> pkgsets;
347 for (const char **I = cmdline; *I != 0; ++I) {
348 unsigned short modID = fallback;
349 PackageContainer pkgset;
350 PackageContainerInterface::FromModifierCommandLine(modID, &pkgset, Cache, *I, mods, helper);
351 pkgsets[modID].insert(pkgset);
352 }
353 return pkgsets;
354 }
355 static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
356 pkgCacheFile &Cache,
357 const char **cmdline,
358 std::list<Modifier> const &mods,
359 unsigned short const &fallback) {
70e706ad 360 CacheSetHelper helper;
446bbcf4 361 return GroupedFromCommandLine(Cache, cmdline,
70e706ad 362 mods, fallback, helper);
9cc83a6f 363 }
c8db3fff
DK
364 /*}}}*/
365private: /*{{{*/
366 Constructor ConstructedBy;
d4489d49
DK
367 /*}}}*/
368}; /*}}}*/
c4cca791
DK
369
370template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
371 for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
372 _cont.push_back(*p);
d3e8fbb3 373}
c4cca791
DK
374// these two are 'inline' as otherwise the linker has problems with seeing these untemplated
375// specializations again and again - but we need to see them, so that library users can use them
376template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
377 if (P.end() == true)
378 return false;
379 _cont.push_back(P);
380 return true;
d3e8fbb3 381}
c4cca791
DK
382template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
383 for (const_iterator p = begin; p != end; ++p)
384 _cont.push_back(*p);
d3e8fbb3 385}
15fc8636 386typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet;
c4cca791 387typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList;
78c32596 388
15fc8636
DK
389class VersionContainerInterface { /*{{{*/
390/** \class APT::VersionContainerInterface
391
392 Same as APT::PackageContainerInterface, just for Versions */
393public:
d4489d49 394 /** \brief smell like a pkgCache::VerIterator */
15fc8636 395 class const_iterator { /*{{{*/
d4489d49 396 public:
15fc8636
DK
397 virtual pkgCache::VerIterator getVer() const = 0;
398 operator pkgCache::VerIterator(void) { return getVer(); }
399
d3e8fbb3
DK
400 inline pkgCache *Cache() const { return getVer().Cache(); }
401 inline unsigned long Index() const {return getVer().Index();}
402 inline int CompareVer(const pkgCache::VerIterator &B) const { return getVer().CompareVer(B); }
403 inline const char *VerStr() const { return getVer().VerStr(); }
404 inline const char *Section() const { return getVer().Section(); }
405 inline const char *Arch() const { return getVer().Arch(); }
406 inline pkgCache::PkgIterator ParentPkg() const { return getVer().ParentPkg(); }
407 inline pkgCache::DescIterator DescriptionList() const { return getVer().DescriptionList(); }
408 inline pkgCache::DescIterator TranslatedDescription() const { return getVer().TranslatedDescription(); }
409 inline pkgCache::DepIterator DependsList() const { return getVer().DependsList(); }
410 inline pkgCache::PrvIterator ProvidesList() const { return getVer().ProvidesList(); }
411 inline pkgCache::VerFileIterator FileList() const { return getVer().FileList(); }
412 inline bool Downloadable() const { return getVer().Downloadable(); }
413 inline const char *PriorityType() const { return getVer().PriorityType(); }
414 inline std::string RelStr() const { return getVer().RelStr(); }
415 inline bool Automatic() const { return getVer().Automatic(); }
416 inline pkgCache::VerFileIterator NewestFile() const { return getVer().NewestFile(); }
d4489d49 417 // we have only valid iterators here
d3e8fbb3 418 inline bool end() const { return false; }
d4489d49 419
d3e8fbb3 420 inline pkgCache::Version const * operator->() const { return &*getVer(); }
d4489d49 421 };
dc0f01f7 422 /*}}}*/
78c32596 423
15fc8636
DK
424 virtual bool insert(pkgCache::VerIterator const &V) = 0;
425 virtual bool empty() const = 0;
426 virtual void clear() = 0;
c45f2d19 427
856d3b06
DK
428 /** \brief specifies which version(s) will be returned if non is given */
429 enum Version {
430 /** All versions */
431 ALL,
432 /** Candidate and installed version */
433 CANDANDINST,
434 /** Candidate version */
435 CANDIDATE,
436 /** Installed version */
437 INSTALLED,
438 /** Candidate or if non installed version */
439 CANDINST,
440 /** Installed or if non candidate version */
441 INSTCAND,
442 /** Newest version */
443 NEWEST
444 };
445
15fc8636
DK
446 struct Modifier {
447 enum Position { NONE, PREFIX, POSTFIX };
448 unsigned short ID;
449 const char * const Alias;
450 Position Pos;
451 Version SelectVersion;
452 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
453 Version const &select) : ID(id), Alias(alias), Pos(pos),
d3e8fbb3 454 SelectVersion(select) {}
15fc8636
DK
455 };
456
457 static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
458 const char **cmdline, Version const &fallback,
459 CacheSetHelper &helper);
460
461 static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
462 std::string pkg, Version const &fallback, CacheSetHelper &helper,
463 bool const onlyFromName = false);
464
465 static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
466 pkgCache::PkgIterator const &P, Version const &fallback,
467 CacheSetHelper &helper);
468
469 static bool FromModifierCommandLine(unsigned short &modID,
470 VersionContainerInterface * const vci,
471 pkgCacheFile &Cache, const char * cmdline,
472 std::list<Modifier> const &mods,
473 CacheSetHelper &helper);
474
c4cca791
DK
475
476 static bool FromDependency(VersionContainerInterface * const vci,
477 pkgCacheFile &Cache,
478 pkgCache::DepIterator const &D,
479 Version const &selector,
480 CacheSetHelper &helper);
481
15fc8636
DK
482protected: /*{{{*/
483
484 /** \brief returns the candidate version of the package
485
486 \param Cache to be used to query for information
255c9e4b
DK
487 \param Pkg we want the candidate version from this package
488 \param helper used in this container instance */
15fc8636
DK
489 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
490 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
491
492 /** \brief returns the installed version of the package
493
494 \param Cache to be used to query for information
255c9e4b
DK
495 \param Pkg we want the installed version from this package
496 \param helper used in this container instance */
15fc8636
DK
497 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
498 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
499 /*}}}*/
500};
501 /*}}}*/
502template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/
c4cca791 503/** \class APT::VersionContainer
15fc8636
DK
504
505 Simple wrapper around a container class like std::set to provide a similar
506 interface to a set of versions as to the complete set of all versions in the
507 pkgCache. */
508 Container _cont;
509public: /*{{{*/
510 /** \brief smell like a pkgCache::VerIterator */
511 class const_iterator : public VersionContainerInterface::const_iterator,
512 public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {/*{{{*/
513 typename Container::const_iterator _iter;
514 public:
515 const_iterator(typename Container::const_iterator i) : _iter(i) {}
516 pkgCache::VerIterator getVer(void) const { return *_iter; }
d3e8fbb3 517 inline pkgCache::VerIterator operator*(void) const { return *_iter; }
15fc8636 518 operator typename Container::const_iterator(void) const { return _iter; }
63b70b21
DK
519 inline const_iterator& operator++() { ++_iter; return *this; }
520 inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
d3e8fbb3
DK
521 inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
522 inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
15fc8636
DK
523 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
524 };
c4cca791
DK
525 class iterator : public VersionContainerInterface::const_iterator,
526 public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
527 typename Container::iterator _iter;
528 public:
529 iterator(typename Container::iterator i) : _iter(i) {}
530 pkgCache::VerIterator getVer(void) const { return *_iter; }
d3e8fbb3 531 inline pkgCache::VerIterator operator*(void) const { return *_iter; }
c4cca791 532 operator typename Container::iterator(void) const { return _iter; }
bce0e0ff 533 operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); }
63b70b21
DK
534 inline iterator& operator++() { ++_iter; return *this; }
535 inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
d3e8fbb3
DK
536 inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
537 inline bool operator==(iterator const &i) const { return _iter == i._iter; }
538 inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
539 inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
c4cca791
DK
540 friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
541 };
15fc8636
DK
542 /*}}}*/
543
d3e8fbb3
DK
544 bool insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; _cont.insert(V); return true; }
545 template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); }
546 void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
547 bool empty() const { return _cont.empty(); }
548 void clear() { return _cont.clear(); }
5eb9a474 549 //FIXME: on ABI break, replace the first with the second without bool
d3e8fbb3
DK
550 void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
551 iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
552 size_t erase(const pkgCache::VerIterator x) { return _cont.erase(x); }
553 void erase(iterator first, iterator last) { _cont.erase(first, last); }
554 size_t size() const { return _cont.size(); }
555
556 const_iterator begin() const { return const_iterator(_cont.begin()); }
557 const_iterator end() const { return const_iterator(_cont.end()); }
558 iterator begin() { return iterator(_cont.begin()); }
559 iterator end() { return iterator(_cont.end()); }
560 const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); }
15fc8636 561
856d3b06
DK
562 /** \brief returns all versions specified on the commandline
563
564 Get all versions from the commandline, uses given default version if
565 non specifically requested and executes regex's if needed on names.
566 \param Cache the packages and versions are in
567 \param cmdline Command line the versions should be extracted from
255c9e4b 568 \param fallback version specification
c8db3fff 569 \param helper responsible for error and message handling */
15fc8636
DK
570 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
571 Version const &fallback, CacheSetHelper &helper) {
572 VersionContainer vercon;
573 VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper);
574 return vercon;
575 }
576 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
577 Version const &fallback) {
70e706ad 578 CacheSetHelper helper;
446bbcf4 579 return FromCommandLine(Cache, cmdline, fallback, helper);
856d3b06 580 }
15fc8636 581 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
446bbcf4 582 return FromCommandLine(Cache, cmdline, CANDINST);
856d3b06 583 }
55c59998 584
15fc8636
DK
585 static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
586 Version const &fallback, CacheSetHelper &helper,
587 bool const onlyFromName = false) {
588 VersionContainer vercon;
589 VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper);
590 return vercon;
591 }
592 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
593 Version const &fallback) {
70e706ad 594 CacheSetHelper helper;
446bbcf4 595 return FromString(Cache, pkg, fallback, helper);
55c59998 596 }
15fc8636 597 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) {
446bbcf4 598 return FromString(Cache, pkg, CANDINST);
55c59998
DK
599 }
600
fb83c1d0
DK
601 /** \brief returns all versions specified for the package
602
603 \param Cache the package and versions are in
604 \param P the package in question
605 \param fallback the version(s) you want to get
606 \param helper the helper used for display and error handling */
15fc8636
DK
607 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
608 Version const &fallback, CacheSetHelper &helper) {
609 VersionContainer vercon;
610 VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper);
611 return vercon;
612 }
613 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
614 Version const &fallback) {
c8db3fff 615 CacheSetHelper helper;
446bbcf4 616 return FromPackage(Cache, P, fallback, helper);
c8db3fff 617 }
15fc8636 618 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
c4cca791 619 return FromPackage(Cache, P, CANDIDATE);
c8db3fff 620 }
fb83c1d0 621
15fc8636
DK
622 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
623 pkgCacheFile &Cache,
624 const char **cmdline,
625 std::list<Modifier> const &mods,
626 unsigned short const fallback,
627 CacheSetHelper &helper) {
628 std::map<unsigned short, VersionContainer> versets;
629 for (const char **I = cmdline; *I != 0; ++I) {
630 unsigned short modID = fallback;
631 VersionContainer verset;
632 VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper);
633 versets[modID].insert(verset);
634 }
635 return versets;
55c59998 636
15fc8636
DK
637 }
638 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
55c59998 639 pkgCacheFile &Cache, const char **cmdline,
15fc8636
DK
640 std::list<Modifier> const &mods,
641 unsigned short const fallback) {
70e706ad 642 CacheSetHelper helper;
446bbcf4 643 return GroupedFromCommandLine(Cache, cmdline,
70e706ad 644 mods, fallback, helper);
55c59998 645 }
c4cca791
DK
646
647 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
648 Version const &selector, CacheSetHelper &helper) {
649 VersionContainer vercon;
650 VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper);
651 return vercon;
652 }
653 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
654 Version const &selector) {
655 CacheSetHelper helper;
656 return FromPackage(Cache, D, selector, helper);
657 }
658 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) {
659 return FromPackage(Cache, D, CANDIDATE);
660 }
856d3b06 661 /*}}}*/
d4489d49 662}; /*}}}*/
c4cca791
DK
663
664template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
665 for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
666 _cont.push_back(*v);
d3e8fbb3 667}
c4cca791
DK
668// these two are 'inline' as otherwise the linker has problems with seeing these untemplated
669// specializations again and again - but we need to see them, so that library users can use them
670template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
671 if (V.end() == true)
672 return false;
673 _cont.push_back(V);
674 return true;
d3e8fbb3 675}
c4cca791
DK
676template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
677 for (const_iterator v = begin; v != end; ++v)
678 _cont.push_back(*v);
d3e8fbb3 679}
15fc8636 680typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet;
c4cca791 681typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList;
e1dbde8d
DK
682}
683#endif