From ffee1c2bed4accfe25b2ac9e9f0ab9a0ebae9b5b Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 30 May 2010 23:12:41 +0200 Subject: move regex magic from apt-get to new FromRegEx method --- apt-pkg/cacheiterators.h | 2 +- apt-pkg/makefile | 2 +- apt-pkg/packageset.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ apt-pkg/packageset.h | 24 ++++++++++++++++++- 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 apt-pkg/packageset.cc (limited to 'apt-pkg') diff --git a/apt-pkg/cacheiterators.h b/apt-pkg/cacheiterators.h index 3d58f7ec0..ee852f594 100644 --- a/apt-pkg/cacheiterators.h +++ b/apt-pkg/cacheiterators.h @@ -66,7 +66,7 @@ template class pkgCache::Iterator : inline operator Str const *() const {return S == OwnerPointer() ? 0 : S;}; inline Str &operator *() {return *S;}; inline Str const &operator *() const {return *S;}; - inline pkgCache *Cache() {return Owner;}; + inline pkgCache *Cache() const {return Owner;}; // Mixed stuff inline void operator =(const Itr &B) {S = B.S; Owner = B.Owner;}; diff --git a/apt-pkg/makefile b/apt-pkg/makefile index d4537859d..968275c5c 100644 --- a/apt-pkg/makefile +++ b/apt-pkg/makefile @@ -35,7 +35,7 @@ SOURCE+= pkgcache.cc version.cc depcache.cc \ srcrecords.cc cachefile.cc versionmatch.cc policy.cc \ pkgsystem.cc indexfile.cc pkgcachegen.cc acquire-item.cc \ indexrecords.cc vendor.cc vendorlist.cc cdrom.cc indexcopy.cc \ - aptconfiguration.cc + aptconfiguration.cc packageset.cc HEADERS+= algorithms.h depcache.h pkgcachegen.h cacheiterators.h \ orderlist.h sourcelist.h packagemanager.h tagfile.h \ init.h pkgcache.h version.h progress.h pkgrecords.h \ diff --git a/apt-pkg/packageset.cc b/apt-pkg/packageset.cc new file mode 100644 index 000000000..f452bc052 --- /dev/null +++ b/apt-pkg/packageset.cc @@ -0,0 +1,62 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +/* ###################################################################### + + Simple wrapper around a std::set to provide a similar interface to + a set of packages as to the complete set of all packages in the + pkgCache. + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ +#include +#include +#include + +#include + +#include + /*}}}*/ +namespace APT { +// FromRegEx - Return all packages in the cache matching a pattern /*{{{*/ +PackageSet PackageSet::FromRegEx(pkgCache &Cache, const char * const pattern, std::ostream &out) { + PackageSet pkgset; + + const char * I; + for (I = pattern; *I != 0; I++) + if (*I == '.' || *I == '?' || *I == '+' || *I == '*' || + *I == '|' || *I == '[' || *I == '^' || *I == '$') + break; + if (*I == 0) + return pkgset; + + regex_t Pattern; + int Res; + if ((Res = regcomp(&Pattern, pattern , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) { + char Error[300]; + regerror(Res, &Pattern, Error, sizeof(Error)); + _error->Error(_("Regex compilation error - %s"), Error); + return pkgset; + } + + for (pkgCache::GrpIterator Grp = Cache.GrpBegin(); Grp.end() == false; ++Grp) + { + if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0) + continue; + pkgCache::PkgIterator Pkg = Grp.FindPkg("native"); + if (unlikely(Pkg.end() == true)) + // FIXME: Fallback to different architectures here? + continue; + + ioprintf(out, _("Note, selecting %s for regex '%s'\n"), + Pkg.Name(), pattern); + + pkgset.insert(Pkg); + } + + regfree(&Pattern); + + return pkgset; +} + /*}}}*/ +} diff --git a/apt-pkg/packageset.h b/apt-pkg/packageset.h index d80536942..cd1430a2a 100644 --- a/apt-pkg/packageset.h +++ b/apt-pkg/packageset.h @@ -10,7 +10,11 @@ #ifndef APT_PACKAGESET_H #define APT_PACKAGESET_H // Include Files /*{{{*/ +#include +#include +#include #include + #include /*}}}*/ namespace APT { @@ -22,6 +26,8 @@ public: /*{{{*/ const_iterator(std::set::const_iterator x) : std::set::const_iterator(x) {} + operator pkgCache::PkgIterator(void) { return **this; } + inline const char *Name() const {return (*this)->Name(); } inline std::string FullName(bool const &Pretty) const { return (*this)->FullName(Pretty); } inline std::string FullName() const { return (*this)->FullName(); } @@ -36,7 +42,8 @@ public: /*{{{*/ inline pkgCache::PkgIterator::OkState State() const { return (*this)->State(); } inline const char *CandVersion() const { return (*this)->CandVersion(); } inline const char *CurVersion() const { return (*this)->CurVersion(); } - inline pkgCache *Cache() {return (*this)->Cache();}; + inline pkgCache *Cache() const { return (*this)->Cache(); }; + inline unsigned long Index() const {return (*this)->Index();}; friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); } @@ -46,6 +53,21 @@ public: /*{{{*/ }; // 103. set::iterator is required to be modifiable, but this allows modification of keys typedef typename APT::PackageSet::const_iterator iterator; + + /** \brief returns all packages in the cache whose name matchs a given pattern + + A simple helper responsible for executing a regular expression on all + package names in the cache. Optional it prints a a notice about the + packages chosen cause of the given package. + \param Cache the packages are in + \param pattern regular expression for package names + \param out stream to print the notice to */ + static APT::PackageSet FromRegEx(pkgCache &Cache, const char *pattern, std::ostream &out); + static APT::PackageSet FromRegEx(pkgCache &Cache, const char *pattern) { + std::ostream out (std::ofstream("/dev/null").rdbuf()); + return APT::PackageSet::FromRegEx(Cache, pattern, out); + } + /*}}}*/ }; /*}}}*/ -- cgit v1.2.3