cacheset.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <iostream>
  12. #include <fstream>
  13. #include <list>
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. #include <apt-pkg/cachefile.h>
  18. #include <apt-pkg/pkgcache.h>
  19. /*}}}*/
  20. namespace APT {
  21. class PackageSet : public std::set<pkgCache::PkgIterator> { /*{{{*/
  22. /** \class APT::PackageSet
  23. Simple wrapper around a std::set to provide a similar interface to
  24. a set of packages as to the complete set of all packages in the
  25. pkgCache. */
  26. public: /*{{{*/
  27. /** \brief smell like a pkgCache::PkgIterator */
  28. class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {
  29. public:
  30. const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
  31. std::set<pkgCache::PkgIterator>::const_iterator(x) {}
  32. operator pkgCache::PkgIterator(void) { return **this; }
  33. inline const char *Name() const {return (**this).Name(); }
  34. inline std::string FullName(bool const &Pretty) const { return (**this).FullName(Pretty); }
  35. inline std::string FullName() const { return (**this).FullName(); }
  36. inline const char *Section() const {return (**this).Section(); }
  37. inline bool Purge() const {return (**this).Purge(); }
  38. inline const char *Arch() const {return (**this).Arch(); }
  39. inline pkgCache::GrpIterator Group() const { return (**this).Group(); }
  40. inline pkgCache::VerIterator VersionList() const { return (**this).VersionList(); }
  41. inline pkgCache::VerIterator CurrentVer() const { return (**this).CurrentVer(); }
  42. inline pkgCache::DepIterator RevDependsList() const { return (**this).RevDependsList(); }
  43. inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); }
  44. inline pkgCache::PkgIterator::OkState State() const { return (**this).State(); }
  45. inline const char *CandVersion() const { return (**this).CandVersion(); }
  46. inline const char *CurVersion() const { return (**this).CurVersion(); }
  47. inline pkgCache *Cache() const { return (**this).Cache(); };
  48. inline unsigned long Index() const {return (**this).Index();};
  49. // we have only valid iterators here
  50. inline bool end() const { return false; };
  51. friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
  52. inline pkgCache::Package const * operator->() const {
  53. return &***this;
  54. };
  55. };
  56. // 103. set::iterator is required to be modifiable, but this allows modification of keys
  57. typedef APT::PackageSet::const_iterator iterator;
  58. using std::set<pkgCache::PkgIterator>::insert;
  59. inline void insert(pkgCache::PkgIterator const &P) { if (P.end() == false) std::set<pkgCache::PkgIterator>::insert(P); };
  60. /** \brief returns all packages in the cache whose name matchs a given pattern
  61. A simple helper responsible for executing a regular expression on all
  62. package names in the cache. Optional it prints a a notice about the
  63. packages chosen cause of the given package.
  64. \param Cache the packages are in
  65. \param pattern regular expression for package names
  66. \param out stream to print the notice to */
  67. static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out);
  68. static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
  69. std::ostream out (std::ofstream("/dev/null").rdbuf());
  70. return APT::PackageSet::FromRegEx(Cache, pattern, out);
  71. }
  72. /** \brief returns all packages specified by a string
  73. \param Cache the packages are in
  74. \param string String the package name(s) should be extracted from
  75. \param out stream to print various notices to */
  76. static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, std::ostream &out);
  77. static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
  78. std::ostream out (std::ofstream("/dev/null").rdbuf());
  79. return APT::PackageSet::FromString(Cache, string, out);
  80. }
  81. /** \brief returns all packages specified on the commandline
  82. Get all package names from the commandline and executes regex's if needed.
  83. No special package command is supported, just plain names.
  84. \param Cache the packages are in
  85. \param cmdline Command line the package names should be extracted from
  86. \param out stream to print various notices to */
  87. static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out);
  88. static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  89. std::ostream out (std::ofstream("/dev/null").rdbuf());
  90. return APT::PackageSet::FromCommandLine(Cache, cmdline, out);
  91. }
  92. struct Modifier {
  93. enum Position { NONE, PREFIX, POSTFIX };
  94. unsigned short ID;
  95. const char * const Alias;
  96. Position Pos;
  97. Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {};
  98. };
  99. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  100. pkgCacheFile &Cache, const char **cmdline,
  101. std::list<PackageSet::Modifier> const &mods,
  102. unsigned short const &fallback, std::ostream &out);
  103. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  104. pkgCacheFile &Cache, const char **cmdline,
  105. std::list<PackageSet::Modifier> const &mods,
  106. unsigned short const &fallback) {
  107. std::ostream out (std::ofstream("/dev/null").rdbuf());
  108. return APT::PackageSet::GroupedFromCommandLine(Cache, cmdline,
  109. mods, fallback, out);
  110. }
  111. /*}}}*/
  112. }; /*}}}*/
  113. class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
  114. /** \class APT::VersionSet
  115. Simple wrapper around a std::set to provide a similar interface to
  116. a set of versions as to the complete set of all versions in the
  117. pkgCache. */
  118. public: /*{{{*/
  119. /** \brief smell like a pkgCache::VerIterator */
  120. class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {
  121. public:
  122. const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
  123. std::set<pkgCache::VerIterator>::const_iterator(x) {}
  124. operator pkgCache::VerIterator(void) { return **this; }
  125. inline pkgCache *Cache() const { return (**this).Cache(); };
  126. inline unsigned long Index() const {return (**this).Index();};
  127. // we have only valid iterators here
  128. inline bool end() const { return false; };
  129. inline pkgCache::Version const * operator->() const {
  130. return &***this;
  131. };
  132. inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
  133. inline const char *VerStr() const { return (**this).VerStr(); };
  134. inline const char *Section() const { return (**this).Section(); };
  135. inline const char *Arch() const { return (**this).Arch(); };
  136. inline const char *Arch(bool const pseudo) const { return (**this).Arch(pseudo); };
  137. inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
  138. inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
  139. inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
  140. inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
  141. inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
  142. inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
  143. inline bool Downloadable() const { return (**this).Downloadable(); };
  144. inline const char *PriorityType() const { return (**this).PriorityType(); };
  145. inline string RelStr() const { return (**this).RelStr(); };
  146. inline bool Automatic() const { return (**this).Automatic(); };
  147. inline bool Pseudo() const { return (**this).Pseudo(); };
  148. inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
  149. };
  150. // 103. set::iterator is required to be modifiable, but this allows modification of keys
  151. typedef APT::VersionSet::const_iterator iterator;
  152. using std::set<pkgCache::VerIterator>::insert;
  153. inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
  154. /** \brief specifies which version(s) will be returned if non is given */
  155. enum Version {
  156. /** All versions */
  157. ALL,
  158. /** Candidate and installed version */
  159. CANDANDINST,
  160. /** Candidate version */
  161. CANDIDATE,
  162. /** Installed version */
  163. INSTALLED,
  164. /** Candidate or if non installed version */
  165. CANDINST,
  166. /** Installed or if non candidate version */
  167. INSTCAND,
  168. /** Newest version */
  169. NEWEST
  170. };
  171. /** \brief returns all versions specified on the commandline
  172. Get all versions from the commandline, uses given default version if
  173. non specifically requested and executes regex's if needed on names.
  174. \param Cache the packages and versions are in
  175. \param cmdline Command line the versions should be extracted from
  176. \param out stream to print various notices to */
  177. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  178. APT::VersionSet::Version const &fallback, std::ostream &out);
  179. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  180. APT::VersionSet::Version const &fallback) {
  181. std::ostream out (std::ofstream("/dev/null").rdbuf());
  182. return APT::VersionSet::FromCommandLine(Cache, cmdline, fallback, out);
  183. }
  184. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  185. return APT::VersionSet::FromCommandLine(Cache, cmdline, CANDINST);
  186. }
  187. /*}}}*/
  188. protected: /*{{{*/
  189. /** \brief returns the candidate version of the package
  190. \param Cache to be used to query for information
  191. \param Pkg we want the candidate version from this package
  192. \param AllowError add an error to the stack if not */
  193. static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
  194. pkgCache::PkgIterator const &Pkg, bool const &AllowError = false);
  195. /** \brief returns the installed version of the package
  196. \param Cache to be used to query for information
  197. \param Pkg we want the installed version from this package
  198. \param AllowError add an error to the stack if not */
  199. static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
  200. pkgCache::PkgIterator const &Pkg, bool const &AllowError = false);
  201. static bool AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
  202. pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
  203. bool const &AllowError = false);
  204. /*}}}*/
  205. }; /*}}}*/
  206. }
  207. #endif