summaryrefslogtreecommitdiff
path: root/apt-pkg
diff options
context:
space:
mode:
authorDavid Kalnischkies <kalnischkies@gmail.com>2010-05-30 23:12:41 +0200
committerDavid Kalnischkies <kalnischkies@gmail.com>2010-05-30 23:12:41 +0200
commitffee1c2bed4accfe25b2ac9e9f0ab9a0ebae9b5b (patch)
tree300268d8dc2bec4b4537dfa7f106395290be44e9 /apt-pkg
parente1dbde8dd006ca0ed6b7a6c383af7bbc60014912 (diff)
move regex magic from apt-get to new FromRegEx method
Diffstat (limited to 'apt-pkg')
-rw-r--r--apt-pkg/cacheiterators.h2
-rw-r--r--apt-pkg/makefile2
-rw-r--r--apt-pkg/packageset.cc62
-rw-r--r--apt-pkg/packageset.h24
4 files changed, 87 insertions, 3 deletions
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<typename Str, typename Itr> 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 <apt-pkg/error.h>
+#include <apt-pkg/packageset.h>
+#include <apt-pkg/strutl.h>
+
+#include <apti18n.h>
+
+#include <regex.h>
+ /*}}}*/
+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 <iostream>
+#include <fstream>
+#include <set>
#include <string>
+
#include <apt-pkg/pkgcache.h>
/*}}}*/
namespace APT {
@@ -22,6 +26,8 @@ public: /*{{{*/
const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
std::set<pkgCache::PkgIterator>::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);
+ }
+
/*}}}*/
};
/*}}}*/