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