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