]> git.saurik.com Git - apt.git/blob - apt-pkg/cachefilter.cc
add make -C test/interactive-helper/ to .travis.yml
[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 #include <apt-pkg/strutl.h>
13
14 #include <string>
15
16 #include <regex.h>
17 #include <fnmatch.h>
18
19 #include <apti18n.h>
20 /*}}}*/
21 namespace APT {
22 namespace CacheFilter {
23 PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) : d(NULL) {/*{{{*/
24 pattern = new regex_t;
25 int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
26 if (Res == 0)
27 return;
28
29 delete pattern;
30 pattern = NULL;
31 char Error[300];
32 regerror(Res, pattern, Error, sizeof(Error));
33 _error->Error(_("Regex compilation error - %s"), Error);
34 }
35 /*}}}*/
36 bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
37 if (unlikely(pattern == NULL))
38 return false;
39 else
40 return regexec(pattern, Pkg.Name(), 0, 0, 0) == 0;
41 }
42 /*}}}*/
43 bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
44 if (unlikely(pattern == NULL))
45 return false;
46 else
47 return regexec(pattern, Grp.Name(), 0, 0, 0) == 0;
48 }
49 /*}}}*/
50 PackageNameMatchesRegEx::~PackageNameMatchesRegEx() { /*{{{*/
51 if (pattern == NULL)
52 return;
53 regfree(pattern);
54 delete pattern;
55 }
56 /*}}}*/
57
58 // Fnmatch support /*{{{*/
59 //----------------------------------------------------------------------
60 bool PackageNameMatchesFnmatch::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
61 return fnmatch(Pattern.c_str(), Pkg.Name(), FNM_CASEFOLD) == 0;
62 }
63 /*}}}*/
64 bool PackageNameMatchesFnmatch::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
65 return fnmatch(Pattern.c_str(), Grp.Name(), FNM_CASEFOLD) == 0;
66 }
67 /*}}}*/
68
69 // CompleteArch to <kernel>-<cpu> tuple /*{{{*/
70 //----------------------------------------------------------------------
71 /* The complete architecture, consisting of <kernel>-<cpu>. */
72 static std::string CompleteArch(std::string const &arch) {
73 if (arch.find('-') != std::string::npos) {
74 // ensure that only -any- is replaced and not something like company-
75 std::string complete = std::string("-").append(arch).append("-");
76 complete = SubstVar(complete, "-any-", "-*-");
77 complete = complete.substr(1, complete.size()-2);
78 return complete;
79 }
80 else if (arch == "any") return "*-*";
81 else return "linux-" + arch;
82 }
83 /*}}}*/
84 PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string const &pattern, bool const isPattern) :/*{{{*/
85 literal(pattern), complete(CompleteArch(pattern)), isPattern(isPattern), d(NULL) {
86 }
87 /*}}}*/
88 bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch) {/*{{{*/
89 if (strcmp(literal.c_str(), arch) == 0 ||
90 strcmp(complete.c_str(), arch) == 0)
91 return true;
92 std::string const pkgarch = CompleteArch(arch);
93 if (isPattern == true)
94 return fnmatch(complete.c_str(), pkgarch.c_str(), 0) == 0;
95 return fnmatch(pkgarch.c_str(), complete.c_str(), 0) == 0;
96 }
97 /*}}}*/
98 bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
99 return (*this)(Pkg.Arch());
100 }
101 /*}}}*/
102 bool PackageArchitectureMatchesSpecification::operator() (pkgCache::VerIterator const &Ver) {/*{{{*/
103 return (*this)(Ver.ParentPkg());
104 }
105 /*}}}*/
106 PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() { /*{{{*/
107 }
108 /*}}}*/
109
110 }
111 }