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