You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

684 lines
31 KiB

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