]>
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 <libc>-<kernel>-<cpu> specification /*{{{*/
72 //----------------------------------------------------------------------
73 /* The complete architecture, consisting of <libc>-<kernel>-<cpu>. */
74 static std::string
CompleteArch(std::string
const &arch
, bool const isPattern
) {
75 auto const found
= arch
.find('-');
76 if (found
!= std::string::npos
)
78 // ensure that only -any- is replaced and not something like company-
79 std::string complete
= std::string("-").append(arch
).append("-");
81 char const * const search
= "-any-";
82 auto const search_len
= strlen(search
) - 2;
83 while((pos
= complete
.find(search
, pos
)) != std::string::npos
) {
84 complete
.replace(pos
+ 1, search_len
, "*");
87 complete
= complete
.substr(1, complete
.size()-2);
88 if (arch
.find('-', found
+1) != std::string::npos
)
89 // <libc>-<kernel>-<cpu> format
91 // <kernel>-<cpu> format
93 return "*-" + complete
;
95 return "gnu-" + complete
;
97 else if (arch
== "any")
100 return "*-linux-" + arch
;
102 return "gnu-linux-" + arch
;
104 PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string
const &pattern
, bool const pisPattern
) :
105 literal(pattern
), complete(CompleteArch(pattern
, pisPattern
)), isPattern(pisPattern
) {
107 bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch
) {
108 if (strcmp(literal
.c_str(), arch
) == 0 ||
109 strcmp(complete
.c_str(), arch
) == 0)
111 std::string
const pkgarch
= CompleteArch(arch
, !isPattern
);
112 if (isPattern
== true)
113 return fnmatch(complete
.c_str(), pkgarch
.c_str(), 0) == 0;
114 return fnmatch(pkgarch
.c_str(), complete
.c_str(), 0) == 0;
116 bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator
const &Pkg
) {
117 return (*this)(Pkg
.Arch());
119 PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() {
122 // Package is new install /*{{{*/
123 PackageIsNewInstall::PackageIsNewInstall(pkgCacheFile
* const Cache
) : Cache(Cache
) {}
124 APT_PURE
bool PackageIsNewInstall::operator() (pkgCache::PkgIterator
const &Pkg
) {
125 return (*Cache
)[Pkg
].NewInstall();
127 PackageIsNewInstall::~PackageIsNewInstall() {}
129 // Generica like True, False, NOT, AND, OR /*{{{*/
130 APT_CONST
bool TrueMatcher::operator() (pkgCache::PkgIterator
const &) { return true; }
131 APT_CONST
bool TrueMatcher::operator() (pkgCache::GrpIterator
const &) { return true; }
132 APT_CONST
bool TrueMatcher::operator() (pkgCache::VerIterator
const &) { return true; }
134 APT_CONST
bool FalseMatcher::operator() (pkgCache::PkgIterator
const &) { return false; }
135 APT_CONST
bool FalseMatcher::operator() (pkgCache::GrpIterator
const &) { return false; }
136 APT_CONST
bool FalseMatcher::operator() (pkgCache::VerIterator
const &) { return false; }
138 NOTMatcher::NOTMatcher(Matcher
* const matcher
) : matcher(matcher
) {}
139 bool NOTMatcher::operator() (pkgCache::PkgIterator
const &Pkg
) { return ! (*matcher
)(Pkg
); }
140 bool NOTMatcher::operator() (pkgCache::GrpIterator
const &Grp
) { return ! (*matcher
)(Grp
); }
141 bool NOTMatcher::operator() (pkgCache::VerIterator
const &Ver
) { return ! (*matcher
)(Ver
); }
142 NOTMatcher::~NOTMatcher() { delete matcher
; }
144 ANDMatcher::ANDMatcher() {}
145 ANDMatcher::ANDMatcher(Matcher
* const matcher1
) {
148 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
) {
149 AND(matcher1
).AND(matcher2
);
151 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
) {
152 AND(matcher1
).AND(matcher2
).AND(matcher3
);
154 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
) {
155 AND(matcher1
).AND(matcher2
).AND(matcher3
).AND(matcher4
);
157 ANDMatcher::ANDMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
, Matcher
* const matcher5
) {
158 AND(matcher1
).AND(matcher2
).AND(matcher3
).AND(matcher4
).AND(matcher5
);
160 ANDMatcher
& ANDMatcher::AND(Matcher
* const matcher
) { matchers
.push_back(matcher
); return *this; }
161 bool ANDMatcher::operator() (pkgCache::PkgIterator
const &Pkg
) {
162 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
163 if ((**M
)(Pkg
) == false)
167 bool ANDMatcher::operator() (pkgCache::GrpIterator
const &Grp
) {
168 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
169 if ((**M
)(Grp
) == false)
173 bool ANDMatcher::operator() (pkgCache::VerIterator
const &Ver
) {
174 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
175 if ((**M
)(Ver
) == false)
179 ANDMatcher::~ANDMatcher() {
180 for (std::vector
<Matcher
*>::iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
184 ORMatcher::ORMatcher() {}
185 ORMatcher::ORMatcher(Matcher
* const matcher1
) {
188 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
) {
189 OR(matcher1
).OR(matcher2
);
191 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
) {
192 OR(matcher1
).OR(matcher2
).OR(matcher3
);
194 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
) {
195 OR(matcher1
).OR(matcher2
).OR(matcher3
).OR(matcher4
);
197 ORMatcher::ORMatcher(Matcher
* const matcher1
, Matcher
* const matcher2
, Matcher
* const matcher3
, Matcher
* const matcher4
, Matcher
* const matcher5
) {
198 OR(matcher1
).OR(matcher2
).OR(matcher3
).OR(matcher4
).OR(matcher5
);
200 ORMatcher
& ORMatcher::OR(Matcher
* const matcher
) { matchers
.push_back(matcher
); return *this; }
201 bool ORMatcher::operator() (pkgCache::PkgIterator
const &Pkg
) {
202 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
203 if ((**M
)(Pkg
) == true)
207 bool ORMatcher::operator() (pkgCache::GrpIterator
const &Grp
) {
208 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
209 if ((**M
)(Grp
) == true)
213 bool ORMatcher::operator() (pkgCache::VerIterator
const &Ver
) {
214 for (std::vector
<Matcher
*>::const_iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)
215 if ((**M
)(Ver
) == true)
219 ORMatcher::~ORMatcher() {
220 for (std::vector
<Matcher
*>::iterator M
= matchers
.begin(); M
!= matchers
.end(); ++M
)