apt @VERSION@
cacheset.h
Go to the documentation of this file.
00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00008                                                                         /*}}}*/
00009 #ifndef APT_CACHESET_H
00010 #define APT_CACHESET_H
00011 // Include Files                                                        /*{{{*/
00012 #include <iostream>
00013 #include <fstream>
00014 #include <list>
00015 #include <map>
00016 #include <set>
00017 #include <string>
00018 
00019 #include <apt-pkg/cachefile.h>
00020 #include <apt-pkg/pkgcache.h>
00021                                                                         /*}}}*/
00022 namespace APT {
00023 class PackageSet;
00024 class VersionSet;
00025 class CacheSetHelper {                                                  /*{{{*/
00034 public:                                                                 /*{{{*/
00035         CacheSetHelper(bool const &ShowError = true,
00036                 GlobalError::MsgType ErrorType = GlobalError::ERROR) :
00037                         ShowError(ShowError), ErrorType(ErrorType) {};
00038         virtual ~CacheSetHelper() {};
00039 
00040         virtual void showTaskSelection(PackageSet const &pkgset, string const &pattern) {};
00041         virtual void showRegExSelection(PackageSet const &pkgset, string const &pattern) {};
00042         virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
00043                                  string const &ver, bool const &verIsRel) {};
00044 
00045         virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
00046         virtual PackageSet canNotFindTask(pkgCacheFile &Cache, std::string pattern);
00047         virtual PackageSet canNotFindRegEx(pkgCacheFile &Cache, std::string pattern);
00048         virtual PackageSet canNotFindPackage(pkgCacheFile &Cache, std::string const &str);
00049         virtual VersionSet canNotFindAllVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
00050         virtual VersionSet canNotFindInstCandVer(pkgCacheFile &Cache,
00051                                 pkgCache::PkgIterator const &Pkg);
00052         virtual VersionSet canNotFindCandInstVer(pkgCacheFile &Cache,
00053                                 pkgCache::PkgIterator const &Pkg);
00054         virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
00055                                 pkgCache::PkgIterator const &Pkg);
00056         virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
00057                                 pkgCache::PkgIterator const &Pkg);
00058         virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
00059                                 pkgCache::PkgIterator const &Pkg);
00060 
00061         bool showErrors() const { return ShowError; };
00062         bool showErrors(bool const &newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); };
00063         GlobalError::MsgType errorType() const { return ErrorType; };
00064         GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
00065         {
00066                 if (ErrorType == newValue) return ErrorType;
00067                 else {
00068                         GlobalError::MsgType const &oldValue = ErrorType;
00069                         ErrorType = newValue;
00070                         return oldValue;
00071                 }
00072         };
00073 
00074                                                                         /*}}}*/
00075 protected:
00076         bool ShowError;
00077         GlobalError::MsgType ErrorType;
00078 };                                                                      /*}}}*/
00079 class PackageSet : public std::set<pkgCache::PkgIterator> {             /*{{{*/
00085 public:                                                                 /*{{{*/
00087         class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/
00088         public:
00089                 const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
00090                          std::set<pkgCache::PkgIterator>::const_iterator(x) {}
00091 
00092                 operator pkgCache::PkgIterator(void) { return **this; }
00093 
00094                 inline const char *Name() const {return (**this).Name(); }
00095                 inline std::string FullName(bool const &Pretty) const { return (**this).FullName(Pretty); }
00096                 inline std::string FullName() const { return (**this).FullName(); }
00097                 inline const char *Section() const {return (**this).Section(); }
00098                 inline bool Purge() const {return (**this).Purge(); }
00099                 inline const char *Arch() const {return (**this).Arch(); }
00100                 inline pkgCache::GrpIterator Group() const { return (**this).Group(); }
00101                 inline pkgCache::VerIterator VersionList() const { return (**this).VersionList(); }
00102                 inline pkgCache::VerIterator CurrentVer() const { return (**this).CurrentVer(); }
00103                 inline pkgCache::DepIterator RevDependsList() const { return (**this).RevDependsList(); }
00104                 inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); }
00105                 inline pkgCache::PkgIterator::OkState State() const { return (**this).State(); }
00106                 inline const char *CandVersion() const { return (**this).CandVersion(); }
00107                 inline const char *CurVersion() const { return (**this).CurVersion(); }
00108                 inline pkgCache *Cache() const { return (**this).Cache(); };
00109                 inline unsigned long Index() const {return (**this).Index();};
00110                 // we have only valid iterators here
00111                 inline bool end() const { return false; };
00112 
00113                 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
00114 
00115                 inline pkgCache::Package const * operator->() const {
00116                         return &***this;
00117                 };
00118         };
00119         // 103. set::iterator is required to be modifiable, but this allows modification of keys
00120         typedef APT::PackageSet::const_iterator iterator;
00121                                                                         /*}}}*/
00122 
00123         using std::set<pkgCache::PkgIterator>::insert;
00124         inline void insert(pkgCache::PkgIterator const &P) { if (P.end() == false) std::set<pkgCache::PkgIterator>::insert(P); };
00125         inline void insert(PackageSet const &pkgset) { insert(pkgset.begin(), pkgset.end()); };
00126 
00135         static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
00136         static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) {
00137                 CacheSetHelper helper;
00138                 return FromTask(Cache, pattern, helper);
00139         }
00140 
00149         static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
00150         static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
00151                 CacheSetHelper helper;
00152                 return FromRegEx(Cache, pattern, helper);
00153         }
00154 
00160         static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
00161         static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
00162                 CacheSetHelper helper;
00163                 return FromString(Cache, string, helper);
00164         }
00165 
00171         static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
00172         static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string) {
00173                 CacheSetHelper helper;
00174                 return FromName(Cache, string, helper);
00175         }
00176 
00184         static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
00185         static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
00186                 CacheSetHelper helper;
00187                 return FromCommandLine(Cache, cmdline, helper);
00188         }
00189 
00190         struct Modifier {
00191                 enum Position { NONE, PREFIX, POSTFIX };
00192                 unsigned short ID;
00193                 const char * const Alias;
00194                 Position Pos;
00195                 Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {};
00196         };
00197 
00209         static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
00210                 pkgCacheFile &Cache, const char **cmdline,
00211                 std::list<PackageSet::Modifier> const &mods,
00212                 unsigned short const &fallback, CacheSetHelper &helper);
00213         static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
00214                 pkgCacheFile &Cache, const char **cmdline,
00215                 std::list<PackageSet::Modifier> const &mods,
00216                 unsigned short const &fallback) {
00217                 CacheSetHelper helper;
00218                 return GroupedFromCommandLine(Cache, cmdline,
00219                                 mods, fallback, helper);
00220         }
00221 
00222         enum Constructor { UNKNOWN, REGEX, TASK };
00223         Constructor getConstructor() const { return ConstructedBy; };
00224 
00225         PackageSet() : ConstructedBy(UNKNOWN) {};
00226         PackageSet(Constructor const &by) : ConstructedBy(by) {};
00227                                                                         /*}}}*/
00228 private:                                                                /*{{{*/
00229         Constructor ConstructedBy;
00230                                                                         /*}}}*/
00231 };                                                                      /*}}}*/
00232 class VersionSet : public std::set<pkgCache::VerIterator> {             /*{{{*/
00238 public:                                                                 /*{{{*/
00240         class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
00241         public:
00242                 const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
00243                          std::set<pkgCache::VerIterator>::const_iterator(x) {}
00244 
00245                 operator pkgCache::VerIterator(void) { return **this; }
00246 
00247                 inline pkgCache *Cache() const { return (**this).Cache(); };
00248                 inline unsigned long Index() const {return (**this).Index();};
00249                 // we have only valid iterators here
00250                 inline bool end() const { return false; };
00251 
00252                 inline pkgCache::Version const * operator->() const {
00253                         return &***this;
00254                 };
00255 
00256                 inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
00257                 inline const char *VerStr() const { return (**this).VerStr(); };
00258                 inline const char *Section() const { return (**this).Section(); };
00259                 inline const char *Arch() const { return (**this).Arch(); };
00260                 inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
00261                 inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
00262                 inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
00263                 inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
00264                 inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
00265                 inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
00266                 inline bool Downloadable() const { return (**this).Downloadable(); };
00267                 inline const char *PriorityType() const { return (**this).PriorityType(); };
00268                 inline string RelStr() const { return (**this).RelStr(); };
00269                 inline bool Automatic() const { return (**this).Automatic(); };
00270                 inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
00271         };
00272                                                                         /*}}}*/
00273         // 103. set::iterator is required to be modifiable, but this allows modification of keys
00274         typedef APT::VersionSet::const_iterator iterator;
00275 
00276         using std::set<pkgCache::VerIterator>::insert;
00277         inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
00278         inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
00279 
00281         enum Version {
00283                 ALL,
00285                 CANDANDINST,
00287                 CANDIDATE,
00289                 INSTALLED,
00291                 CANDINST,
00293                 INSTCAND,
00295                 NEWEST
00296         };
00297 
00305         static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
00306                         APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
00307         static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
00308                         APT::VersionSet::Version const &fallback) {
00309                 CacheSetHelper helper;
00310                 return FromCommandLine(Cache, cmdline, fallback, helper);
00311         }
00312         static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
00313                 return FromCommandLine(Cache, cmdline, CANDINST);
00314         }
00315 
00316         static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
00317                         APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
00318                         bool const &onlyFromName = false);
00319         static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
00320                         APT::VersionSet::Version const &fallback) {
00321                 CacheSetHelper helper;
00322                 return FromString(Cache, pkg, fallback, helper);
00323         }
00324         static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
00325                 return FromString(Cache, pkg, CANDINST);
00326         }
00327 
00334         static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
00335                 VersionSet::Version const &fallback, CacheSetHelper &helper);
00336         static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
00337                         APT::VersionSet::Version const &fallback) {
00338                 CacheSetHelper helper;
00339                 return FromPackage(Cache, P, fallback, helper);
00340         }
00341         static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
00342                 return FromPackage(Cache, P, CANDINST);
00343         }
00344 
00345         struct Modifier {
00346                 enum Position { NONE, PREFIX, POSTFIX };
00347                 unsigned short ID;
00348                 const char * const Alias;
00349                 Position Pos;
00350                 VersionSet::Version SelectVersion;
00351                 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
00352                           VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
00353                          SelectVersion(select) {};
00354         };
00355 
00356         static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
00357                 pkgCacheFile &Cache, const char **cmdline,
00358                 std::list<VersionSet::Modifier> const &mods,
00359                 unsigned short const &fallback, CacheSetHelper &helper);
00360         static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
00361                 pkgCacheFile &Cache, const char **cmdline,
00362                 std::list<VersionSet::Modifier> const &mods,
00363                 unsigned short const &fallback) {
00364                 CacheSetHelper helper;
00365                 return GroupedFromCommandLine(Cache, cmdline,
00366                                 mods, fallback, helper);
00367         }
00368                                                                         /*}}}*/
00369 protected:                                                              /*{{{*/
00370 
00375         static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
00376                 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
00377 
00382         static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
00383                 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
00384                                                                         /*}}}*/
00385 };                                                                      /*}}}*/
00386 }
00387 #endif