summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Kalnischkies <kalnischkies@gmail.com>2010-07-17 19:07:00 +0200
committerDavid Kalnischkies <kalnischkies@gmail.com>2010-07-17 19:07:00 +0200
commit9ba5aa3b01f1f7c08c74f5d0a21971db221d30f1 (patch)
treed2b12bb5b382c36aeaf6872241e75a2b25923fe7
parentfe1af091b871f9af715d729e515f2e3a62802c6f (diff)
factor regex package name matches into newly created cachefilter classes
-rw-r--r--apt-pkg/cachefilter.cc54
-rw-r--r--apt-pkg/cachefilter.h29
-rw-r--r--apt-pkg/makefile5
-rw-r--r--cmdline/cacheset.cc18
4 files changed, 91 insertions, 15 deletions
diff --git a/apt-pkg/cachefilter.cc b/apt-pkg/cachefilter.cc
new file mode 100644
index 000000000..8f0725ea3
--- /dev/null
+++ b/apt-pkg/cachefilter.cc
@@ -0,0 +1,54 @@
+// -*- mode: cpp; mode: fold -*-
+// Description /*{{{*/
+/** \file cachefilter.h
+ Collection of functor classes */
+ /*}}}*/
+// Include Files /*{{{*/
+#include <apt-pkg/cachefilter.h>
+#include <apt-pkg/error.h>
+#include <apt-pkg/pkgcache.h>
+
+#include <apti18n.h>
+
+#include <string>
+
+#include <regex.h>
+ /*}}}*/
+namespace APT {
+namespace CacheFilter {
+PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) {/*{{{*/
+ pattern = new regex_t;
+ int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
+ if (Res == 0)
+ return;
+
+ delete pattern;
+ pattern = NULL;
+ char Error[300];
+ regerror(Res, pattern, Error, sizeof(Error));
+ _error->Error(_("Regex compilation error - %s"), Error);
+}
+ /*}}}*/
+bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
+ if (unlikely(pattern == NULL))
+ return false;
+ else
+ return regexec(pattern, Pkg.Name(), 0, 0, 0) == 0;
+}
+ /*}}}*/
+bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
+ if (unlikely(pattern == NULL))
+ return false;
+ else
+ return regexec(pattern, Grp.Name(), 0, 0, 0) == 0;
+}
+ /*}}}*/
+PackageNameMatchesRegEx::~PackageNameMatchesRegEx() { /*{{{*/
+ if (pattern == NULL)
+ return;
+ regfree(pattern);
+ delete pattern;
+}
+ /*}}}*/
+}
+}
diff --git a/apt-pkg/cachefilter.h b/apt-pkg/cachefilter.h
new file mode 100644
index 000000000..e7ab1723f
--- /dev/null
+++ b/apt-pkg/cachefilter.h
@@ -0,0 +1,29 @@
+// -*- mode: cpp; mode: fold -*-
+// Description /*{{{*/
+/** \file cachefilter.h
+ Collection of functor classes */
+ /*}}}*/
+#ifndef APT_CACHEFILTER_H
+#define APT_CACHEFILTER_H
+// Include Files /*{{{*/
+#include <apt-pkg/pkgcache.h>
+
+#include <string>
+
+#include <regex.h>
+ /*}}}*/
+namespace APT {
+namespace CacheFilter {
+// PackageNameMatchesRegEx /*{{{*/
+class PackageNameMatchesRegEx {
+ regex_t* pattern;
+public:
+ PackageNameMatchesRegEx(std::string const &Pattern);
+ bool operator() (pkgCache::PkgIterator const &Pkg);
+ bool operator() (pkgCache::GrpIterator const &Grp);
+ ~PackageNameMatchesRegEx();
+};
+ /*}}}*/
+}
+}
+#endif
diff --git a/apt-pkg/makefile b/apt-pkg/makefile
index 2a7958536..e9e5651b0 100644
--- a/apt-pkg/makefile
+++ b/apt-pkg/makefile
@@ -35,14 +35,15 @@ 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 cachefilter.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 \
acquire.h acquire-worker.h acquire-item.h acquire-method.h \
clean.h srcrecords.h cachefile.h versionmatch.h policy.h \
pkgsystem.h indexfile.h metaindex.h indexrecords.h vendor.h \
- vendorlist.h cdrom.h indexcopy.h aptconfiguration.h
+ vendorlist.h cdrom.h indexcopy.h aptconfiguration.h \
+ cachefilter.h
# Source code for the debian specific components
# In theory the deb headers do not need to be exported..
diff --git a/cmdline/cacheset.cc b/cmdline/cacheset.cc
index 78c9d3f6c..0b099f442 100644
--- a/cmdline/cacheset.cc
+++ b/cmdline/cacheset.cc
@@ -10,6 +10,7 @@
/*}}}*/
// Include Files /*{{{*/
#include <apt-pkg/aptconfiguration.h>
+#include <apt-pkg/cachefilter.h>
#include <apt-pkg/error.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/versionmatch.h>
@@ -97,22 +98,14 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, Cache
arch = "native";
}
- regex_t Pattern;
- int Res;
- if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
- char Error[300];
- regerror(Res, &Pattern, Error, sizeof(Error));
- _error->Error(_("Regex compilation error - %s"), Error);
- return PackageSet(REGEX);
- }
-
if (unlikely(Cache.GetPkgCache() == 0))
return PackageSet(REGEX);
+ APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
+
PackageSet pkgset(REGEX);
- for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
- {
- if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
+ for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
+ if (regexfilter(Grp) == false)
continue;
pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
if (Pkg.end() == true) {
@@ -128,7 +121,6 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, Cache
pkgset.insert(Pkg);
}
- regfree(&Pattern);
if (pkgset.empty() == true)
return helper.canNotFindRegEx(Cache, pattern);