merged from lp:~donkult/apt/experimental
[ntk/apt.git] / apt-pkg / cachefilter.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /** \file cachefilter.h
4 Collection of functor classes */
5 /*}}}*/
6 // Include Files /*{{{*/
7 #include <config.h>
8
9 #include <apt-pkg/cachefilter.h>
10 #include <apt-pkg/error.h>
11 #include <apt-pkg/pkgcache.h>
12
13 #include <apti18n.h>
14
15 #include <string>
16
17 #include <regex.h>
18 /*}}}*/
19 namespace APT {
20 namespace CacheFilter {
21 PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) : d(NULL) {/*{{{*/
22 pattern = new regex_t;
23 int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
24 if (Res == 0)
25 return;
26
27 delete pattern;
28 pattern = NULL;
29 char Error[300];
30 regerror(Res, pattern, Error, sizeof(Error));
31 _error->Error(_("Regex compilation error - %s"), Error);
32 }
33 /*}}}*/
34 bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
35 if (unlikely(pattern == NULL))
36 return false;
37 else
38 return regexec(pattern, Pkg.Name(), 0, 0, 0) == 0;
39 }
40 /*}}}*/
41 bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
42 if (unlikely(pattern == NULL))
43 return false;
44 else
45 return regexec(pattern, Grp.Name(), 0, 0, 0) == 0;
46 }
47 /*}}}*/
48 PackageNameMatchesRegEx::~PackageNameMatchesRegEx() { /*{{{*/
49 if (pattern == NULL)
50 return;
51 regfree(pattern);
52 delete pattern;
53 }
54 /*}}}*/
55 }
56 }