]>
git.saurik.com Git - apt.git/blob - apt-pkg/cachefilter.cc
1 // -*- mode: cpp; mode: fold -*-
3 /** \file cachefilter.h
4 Collection of functor classes */
6 // Include Files /*{{{*/
9 #include <apt-pkg/cachefile.h>
10 #include <apt-pkg/cachefilter.h>
11 #include <apt-pkg/error.h>
12 #include <apt-pkg/pkgcache.h>
13 #include <apt-pkg/cacheiterators.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/macros.h>
25 namespace CacheFilter
{
26 APT_CONST
Matcher::~Matcher() {}
27 APT_CONST
PackageMatcher::~PackageMatcher() {}
29 // Name matches RegEx /*{{{*/
30 PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string
const &Pattern
) {
31 pattern
= new regex_t
;
32 int const Res
= regcomp(pattern
, Pattern
.c_str(), REG_EXTENDED
| REG_ICASE
| REG_NOSUB
);
39 regerror(Res
, pattern
, Error
, sizeof(Error
));
40 _error
->Error(_("Regex compilation error - %s"), Error
);
42 bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator
const &Pkg
) {
43 if (unlikely(pattern
== NULL
))
46 return regexec(pattern
, Pkg
.Name(), 0, 0, 0) == 0;
48 bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator
const &Grp
) {
49 if (unlikely(pattern
== NULL
))
52 return regexec(pattern
, Grp
.Name(), 0, 0, 0) == 0;
54 PackageNameMatchesRegEx::~PackageNameMatchesRegEx() {
61 // Name matches Fnmatch /*{{{*/
62 PackageNameMatchesFnmatch::PackageNameMatchesFnmatch(std::string
const &Pattern
) :
64 bool PackageNameMatchesFnmatch::operator() (pkgCache::PkgIterator
const &Pkg
) {
65 return fnmatch(Pattern
.c_str(), Pkg
.Name(), FNM_CASEFOLD
) == 0;
67 bool PackageNameMatchesFnmatch::operator() (pkgCache::GrpIterator
const &Grp
) {
68 return fnmatch(Pattern
.c_str(), Grp
.Name(), FNM_CASEFOLD
) == 0;
71 // Architecture matches <kernel>-<cpu> specification /*{{{*/
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);
82 else if (arch
== "any") return "*-*";
83 else return "linux-" + arch
;
85 PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string
const &pattern
, bool const isPattern
) :
86 literal(pattern
), complete(CompleteArch(pattern
)), isPattern(isPattern
) {
88 bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch
) {
89 if (strcmp(literal
.c_str(), arch
) == 0 ||
90 strcmp(complete
.c_str(), arch
) == 0)
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;
97 bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator
const &Pkg
) {
98 return (*this)(Pkg
.Arch());
100 PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() {
103 // Package is new install /*{{{*/
104 PackageIsNewInstall::PackageIsNewInstall(pkgCacheFile
* const Cache
) : Cache(Cache
) {}
105 APT_PURE
bool PackageIsNewInstall::operator() (pkgCache::PkgIterator
const &Pkg
) {
106 return (*Cache
)[Pkg
].NewInstall();
108 PackageIsNewInstall::~PackageIsNewInstall() {}
110 // Generica like True, False, NOT, AND, OR /*{{{*/
111 APT_CONST
bool TrueMatcher::operator() (pkgCache::PkgIterator
const &) { return true; }
112 APT_CONST
bool TrueMatcher::operator() (pkgCache::GrpIterator
const &) { return true; }
113 APT_CONST
bool TrueMatcher::operator() (pkgCache::VerIterator
const &) { return true; }
115 APT_CONST
bool FalseMatcher::operator() (pkgCache::PkgIterator
const &) { return false; }
116 APT_CONST
bool FalseMatcher::operator() (pkgCache::GrpIterator
const &) { return false; }
117 APT_CONST
bool FalseMatcher::operator() (pkgCache::VerIterator
const &) { return false; }
119 NOTMatcher::NOTMatcher(Matcher
* const matcher
) : matcher(matcher
) {}
120 bool NOTMatcher::operator() (pkgCache::PkgIterator
const &Pkg
) { return ! (*matcher
)(Pkg
); }
121 bool NOTMatcher::operator() (pkgCache::GrpIterator
const &Grp
) { return ! (*matcher
)(Grp
); }
122 bool NOTMatcher::operator() (pkgCache::VerIterator
const &Ver
) { return ! (*matcher
)(Ver
); }
123 NOTMatcher::~NOTMatcher() { delete matcher
; }
125 ANDMatcher::ANDMatcher() {}
126 ANDMatcher::ANDMatcher(Matcher
* const matcher1
) {
129 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
) {
130 AND(matcher1
).AND(matcher2
);
132 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
) {
133 AND(matcher1
).AND(matcher2
).AND(matcher3
);
135 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
) {
136 AND(matcher1
).AND(matcher2
).AND(matcher3
).AND(matcher4
);
138 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
, Matcher
* const matcher5
) {
139 AND(matcher1
).AND(matcher2
).AND(matcher3
).AND(matcher4
).AND(matcher5
);
141 ANDMatcher
& ANDMatcher::AND(Matcher
* const matcher
) { matchers
.push_back(matcher
); return *this; }
142 bool ANDMatcher::operator() (pkgCache::PkgIterator
const &Pkg
) {
143 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
144 if ((**M
)(Pkg
) == false)
148 bool ANDMatcher::operator() (pkgCache::GrpIterator
const &Grp
) {
149 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
150 if ((**M
)(Grp
) == false)
154 bool ANDMatcher::operator() (pkgCache::VerIterator
const &Ver
) {
155 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
156 if ((**M
)(Ver
) == false)
160 ANDMatcher::~ANDMatcher() {
161 for (std::vector
<Matcher
*>::iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
165 ORMatcher::ORMatcher() {}
166 ORMatcher::ORMatcher(Matcher
* const matcher1
) {
169 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
) {
170 OR(matcher1
).OR(matcher2
);
172 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
) {
173 OR(matcher1
).OR(matcher2
).OR(matcher3
);
175 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
) {
176 OR(matcher1
).OR(matcher2
).OR(matcher3
).OR(matcher4
);
178 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
, Matcher
* const matcher5
) {
179 OR(matcher1
).OR(matcher2
).OR(matcher3
).OR(matcher4
).OR(matcher5
);
181 ORMatcher
& ORMatcher::OR(Matcher
* const matcher
) { matchers
.push_back(matcher
); return *this; }
182 bool ORMatcher::operator() (pkgCache::PkgIterator
const &Pkg
) {
183 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
184 if ((**M
)(Pkg
) == true)
188 bool ORMatcher::operator() (pkgCache::GrpIterator
const &Grp
) {
189 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
190 if ((**M
)(Grp
) == true)
194 bool ORMatcher::operator() (pkgCache::VerIterator
const &Ver
) {
195 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
196 if ((**M
)(Ver
) == true)
200 ORMatcher::~ORMatcher() {
201 for (std::vector
<Matcher
*>::iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)