]> git.saurik.com Git - apt.git/blame - apt-pkg/cachefilter.cc
add missing "free(buffer) for allocated buffer
[apt.git] / apt-pkg / cachefilter.cc
CommitLineData
9ba5aa3b
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/** \file cachefilter.h
4 Collection of functor classes */
5 /*}}}*/
6// Include Files /*{{{*/
ea542140
DK
7#include <config.h>
8
9ba5aa3b
DK
9#include <apt-pkg/cachefilter.h>
10#include <apt-pkg/error.h>
11#include <apt-pkg/pkgcache.h>
424ff669 12#include <apt-pkg/strutl.h>
9ba5aa3b 13
9ba5aa3b
DK
14#include <string>
15
16#include <regex.h>
424ff669 17#include <fnmatch.h>
a00a9b44
DK
18
19#include <apti18n.h>
9ba5aa3b
DK
20 /*}}}*/
21namespace APT {
22namespace CacheFilter {
dcaa1185 23PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) : d(NULL) {/*{{{*/
9ba5aa3b
DK
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 /*}}}*/
36bool 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 /*}}}*/
43bool 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 /*}}}*/
50PackageNameMatchesRegEx::~PackageNameMatchesRegEx() { /*{{{*/
51 if (pattern == NULL)
52 return;
53 regfree(pattern);
54 delete pattern;
55}
56 /*}}}*/
424ff669
DK
57
58// CompleteArch to <kernel>-<cpu> tuple /*{{{*/
59//----------------------------------------------------------------------
60/* The complete architecture, consisting of <kernel>-<cpu>. */
61static std::string CompleteArch(std::string const &arch) {
62 if (arch.find('-') != std::string::npos) {
63 // ensure that only -any- is replaced and not something like company-
64 std::string complete = std::string("-").append(arch).append("-");
65 complete = SubstVar(complete, "-any-", "-*-");
66 complete = complete.substr(1, complete.size()-2);
67 return complete;
68 }
424ff669
DK
69 else if (arch == "any") return "*-*";
70 else return "linux-" + arch;
71}
72 /*}}}*/
73PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string const &pattern, bool const isPattern) :/*{{{*/
69c2ecbd 74 literal(pattern), complete(CompleteArch(pattern)), isPattern(isPattern), d(NULL) {
424ff669
DK
75}
76 /*}}}*/
77bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch) {/*{{{*/
78 if (strcmp(literal.c_str(), arch) == 0 ||
79 strcmp(complete.c_str(), arch) == 0)
80 return true;
81 std::string const pkgarch = CompleteArch(arch);
82 if (isPattern == true)
83 return fnmatch(complete.c_str(), pkgarch.c_str(), 0) == 0;
84 return fnmatch(pkgarch.c_str(), complete.c_str(), 0) == 0;
85}
86 /*}}}*/
87bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
88 return (*this)(Pkg.Arch());
89}
90 /*}}}*/
91bool PackageArchitectureMatchesSpecification::operator() (pkgCache::VerIterator const &Ver) {/*{{{*/
92 return (*this)(Ver.ParentPkg());
93}
94 /*}}}*/
95PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() { /*{{{*/
96}
97 /*}}}*/
98
9ba5aa3b
DK
99}
100}