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/error.h>
00020 #include <apt-pkg/pkgcache.h>
00021                                                                         /*}}}*/
00022 
00023 class pkgCacheFile;
00024 
00025 namespace APT {
00026 class PackageSet;
00027 class VersionSet;
00028 class CacheSetHelper {                                                  /*{{{*/
00037 public:                                                                 /*{{{*/
00038         CacheSetHelper(bool const &ShowError = true,
00039                 GlobalError::MsgType ErrorType = GlobalError::ERROR) :
00040                         ShowError(ShowError), ErrorType(ErrorType) {};
00041         virtual ~CacheSetHelper() {};
00042 
00043         virtual void showTaskSelection(PackageSet const &pkgset, std::string const &pattern) {};
00044         virtual void showRegExSelection(PackageSet const &pkgset, std::string const &pattern) {};
00045         virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
00046                                  std::string const &ver, bool const &verIsRel) {};
00047 
00048         virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
00049         virtual PackageSet canNotFindTask(pkgCacheFile &Cache, std::string pattern);
00050         virtual PackageSet canNotFindRegEx(pkgCacheFile &Cache, std::string pattern);
00051         virtual PackageSet canNotFindPackage(pkgCacheFile &Cache, std::string const &str);
00052         virtual VersionSet canNotFindAllVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
00053         virtual VersionSet canNotFindInstCandVer(pkgCacheFile &Cache,
00054                                 pkgCache::PkgIterator const &Pkg);
00055         virtual VersionSet canNotFindCandInstVer(pkgCacheFile &Cache,
00056                                 pkgCache::PkgIterator const &Pkg);
00057         virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
00058                                 pkgCache::PkgIterator const &Pkg);
00059         virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
00060                                 pkgCache::PkgIterator const &Pkg);
00061         virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
00062                                 pkgCache::PkgIterator const &Pkg);
00063 
00064         bool showErrors() const { return ShowError; };
00065         bool showErrors(bool const &newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); };
00066         GlobalError::MsgType errorType() const { return ErrorType; };
00067         GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
00068         {
00069                 if (ErrorType == newValue) return ErrorType;
00070                 else {
00071                         GlobalError::MsgType const &oldValue = ErrorType;
00072                         ErrorType = newValue;
00073                         return oldValue;
00074                 }
00075         };
00076 
00077                                                                         /*}}}*/
00078 protected:
00079         bool ShowError;
00080         GlobalError::MsgType ErrorType;
00081 };                                                                      /*}}}*/
00082 class PackageSet : public std::set<pkgCache::PkgIterator> {             /*{{{*/
00088 public:                                                                 /*{{{*/
00090         class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/
00091         public:
00092                 const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
00093                          std::set<pkgCache::PkgIterator>::const_iterator(x) {}
00094 
00095                 operator pkgCache::PkgIterator(void) { return **this; }
00096 
00097                 inline const char *Name() const {return (**this).Name(); }
00098                 inline std::string FullName(bool const &Pretty) const { return (**this).FullName(Pretty); }
00099                 inline std::string FullName() const { return (**this).FullName(); }
00100                 inline const char *Section() const {return (**this).Section(); }
00101                 inline bool Purge() const {return (**this).Purge(); }
00102                 inline const char *Arch() const {return (**this).Arch(); }
00103                 inline pkgCache::GrpIterator Group() const { return (**this).Group(); }
00104                 inline pkgCache::VerIterator VersionList() const { return (**this).VersionList(); }
00105                 inline pkgCache::VerIterator CurrentVer() const { return (**this).CurrentVer(); }
00106                 inline pkgCache::DepIterator RevDependsList() const { return (**this).RevDependsList(); }
00107                 inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); }
00108                 inline pkgCache::PkgIterator::OkState State() const { return (**this).State(); }
00109                 inline const char *CandVersion() const { return (**this).CandVersion(); }
00110                 inline const char *CurVersion() const { return (**this).CurVersion(); }
00111                 inline pkgCache *Cache() const { return (**this).Cache(); };
00112                 inline unsigned long Index() const {return (**this).Index();};
00113                 // we have only valid iterators here
00114                 inline bool end() const { return false; };
00115 
00116                 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
00117 
00118                 inline pkgCache::Package const * operator->() const {
00119                         return &***this;
00120                 };
00121         };
00122         // 103. set::iterator is required to be modifiable, but this allows modification of keys
00123         typedef APT::PackageSet::const_iterator iterator;
00124                                                                         /*}}}*/
00125 
00126         using std::set<pkgCache::PkgIterator>::insert;
00127         inline void insert(pkgCache::PkgIterator const &P) { if (P.end() == false) std::set<pkgCache::PkgIterator>::insert(P); };
00128         inline void insert(PackageSet const &pkgset) { insert(pkgset.begin(), pkgset.end()); };
00129 
00138         static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
00139         static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) {
00140                 CacheSetHelper helper;
00141                 return FromTask(Cache, pattern, helper);
00142         }
00143 
00152         static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
00153         static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
00154                 CacheSetHelper helper;
00155                 return FromRegEx(Cache, pattern, helper);
00156         }
00157 
00163         static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
00164         static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
00165                 CacheSetHelper helper;
00166                 return FromString(Cache, string, helper);
00167         }
00168 
00174         static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
00175         static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string) {
00176                 CacheSetHelper helper;
00177                 return FromName(Cache, string, helper);
00178         }
00179 
00187         static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
00188         static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
00189                 CacheSetHelper helper;
00190                 return FromCommandLine(Cache, cmdline, helper);
00191         }
00192 
00193         struct Modifier {
00194                 enum Position { NONE, PREFIX, POSTFIX };
00195                 unsigned short ID;
00196                 const char * const Alias;
00197                 Position Pos;
00198                 Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {};
00199         };
00200 
00212         static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
00213                 pkgCacheFile &Cache, const char **cmdline,
00214                 std::list<PackageSet::Modifier> const &mods,
00215                 unsigned short const &fallback, CacheSetHelper &helper);
00216         static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
00217                 pkgCacheFile &Cache, const char **cmdline,
00218                 std::list<PackageSet::Modifier> const &mods,
00219                 unsigned short const &fallback) {
00220                 CacheSetHelper helper;
00221                 return GroupedFromCommandLine(Cache, cmdline,
00222                                 mods, fallback, helper);
00223         }
00224 
00225         enum Constructor { UNKNOWN, REGEX, TASK };
00226         Constructor getConstructor() const { return ConstructedBy; };
00227 
00228         PackageSet() : ConstructedBy(UNKNOWN) {};
00229         PackageSet(Constructor const &by) : ConstructedBy(by) {};
00230                                                                         /*}}}*/
00231 private:                                                                /*{{{*/
00232         Constructor ConstructedBy;
00233                                                                         /*}}}*/
00234 };                                                                      /*}}}*/
00235 class VersionSet : public std::set<pkgCache::VerIterator> {             /*{{{*/
00241 public:                                                                 /*{{{*/
00243         class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
00244         public:
00245                 const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
00246                          std::set<pkgCache::VerIterator>::const_iterator(x) {}
00247 
00248                 operator pkgCache::VerIterator(void) { return **this; }
00249 
00250                 inline pkgCache *Cache() const { return (**this).Cache(); };
00251                 inline unsigned long Index() const {return (**this).Index();};
00252                 // we have only valid iterators here
00253                 inline bool end() const { return false; };
00254 
00255                 inline pkgCache::Version const * operator->() const {
00256                         return &***this;
00257                 };
00258 
00259                 inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
00260                 inline const char *VerStr() const { return (**this).VerStr(); };
00261                 inline const char *Section() const { return (**this).Section(); };
00262                 inline const char *Arch() const { return (**this).Arch(); };
00263                 inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
00264                 inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
00265                 inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
00266                 inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
00267                 inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
00268                 inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
00269                 inline bool Downloadable() const { return (**this).Downloadable(); };
00270                 inline const char *PriorityType() const { return (**this).PriorityType(); };
00271                 inline std::string RelStr() const { return (**this).RelStr(); };
00272                 inline bool Automatic() const { return (**this).Automatic(); };
00273                 inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
00274         };
00275                                                                         /*}}}*/
00276         // 103. set::iterator is required to be modifiable, but this allows modification of keys
00277         typedef APT::VersionSet::const_iterator iterator;
00278 
00279         using std::set<pkgCache::VerIterator>::insert;
00280         inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
00281         inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
00282 
00284         enum Version {
00286                 ALL,
00288                 CANDANDINST,
00290                 CANDIDATE,
00292                 INSTALLED,
00294                 CANDINST,
00296                 INSTCAND,
00298                 NEWEST
00299         };
00300 
00308         static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
00309                         APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
00310         static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
00311                         APT::VersionSet::Version const &fallback) {
00312                 CacheSetHelper helper;
00313                 return FromCommandLine(Cache, cmdline, fallback, helper);
00314         }
00315         static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
00316                 return FromCommandLine(Cache, cmdline, CANDINST);
00317         }
00318 
00319         static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
00320                         APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
00321                         bool const &onlyFromName = false);
00322         static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
00323                         APT::VersionSet::Version const &fallback) {
00324                 CacheSetHelper helper;
00325                 return FromString(Cache, pkg, fallback, helper);
00326         }
00327         static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
00328                 return FromString(Cache, pkg, CANDINST);
00329         }
00330 
00337         static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
00338                 VersionSet::Version const &fallback, CacheSetHelper &helper);
00339         static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
00340                         APT::VersionSet::Version const &fallback) {
00341                 CacheSetHelper helper;
00342                 return FromPackage(Cache, P, fallback, helper);
00343         }
00344         static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
00345                 return FromPackage(Cache, P, CANDINST);
00346         }
00347 
00348         struct Modifier {
00349                 enum Position { NONE, PREFIX, POSTFIX };
00350                 unsigned short ID;
00351                 const char * const Alias;
00352                 Position Pos;
00353                 VersionSet::Version SelectVersion;
00354                 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
00355                           VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
00356                          SelectVersion(select) {};
00357         };
00358 
00359         static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
00360                 pkgCacheFile &Cache, const char **cmdline,
00361                 std::list<VersionSet::Modifier> const &mods,
00362                 unsigned short const &fallback, CacheSetHelper &helper);
00363         static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
00364                 pkgCacheFile &Cache, const char **cmdline,
00365                 std::list<VersionSet::Modifier> const &mods,
00366                 unsigned short const &fallback) {
00367                 CacheSetHelper helper;
00368                 return GroupedFromCommandLine(Cache, cmdline,
00369                                 mods, fallback, helper);
00370         }
00371                                                                         /*}}}*/
00372 protected:                                                              /*{{{*/
00373 
00378         static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
00379                 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
00380 
00385         static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
00386                 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
00387                                                                         /*}}}*/
00388 };                                                                      /*}}}*/
00389 }
00390 #endif