-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.C
More file actions
136 lines (113 loc) · 3.21 KB
/
filter.C
File metadata and controls
136 lines (113 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "filter.H"
#include "args.H"
#include "error.H"
#include <string.h>
#if defined(_WIN32)
#include <shlwapi.h>
#endif
#if defined(_UNIX)
#include <fnmatch.h>
#endif
#if defined(_AIX)
// No FNM_CASEFOLD on AIX
#define FNM_CASEFOLD 0
#endif
Filter::Filter(Args const & args)
: m_args(args)
{
// Build content filters
std::list<String>::const_iterator it = m_args.includeContent().begin();
for (; it != m_args.includeContent().end(); ++it) {
m_inContent.push_back(Regex::Ptr(new Regex(*it, args.grammar())));
}
it = m_args.excludeContent().begin();
for (; it != m_args.excludeContent().end(); ++it) {
m_exContent.push_back(Regex::Ptr(new Regex(*it, args.grammar())));
}
}
Filter::~Filter()
{
m_inContent.clear();
m_exContent.clear();
}
bool Filter::matchDir(std::string const & name) const
{
bool match = m_args.includeDirs().empty();
// Include filters
std::list<String>::const_iterator it = m_args.includeDirs().begin();
for (; !match && it != m_args.includeDirs().end(); ++it) {
match = fnmatch(*it, name, it->noCase());
}
return match;
}
bool Filter::excludeDir(std::string const & name) const
{
bool match = false;
// Exclude filters
std::list<String>::const_iterator it = m_args.excludeDirs().begin();
for (; !match && it != m_args.excludeDirs().end(); ++it) {
match = fnmatch(*it, name, it->noCase());
}
return match;
}
bool Filter::matchFile(std::string const & name) const
{
bool match = m_args.includeFiles().empty();
// Include filters
std::list<String>::const_iterator it = m_args.includeFiles().begin();
for (; !match && it != m_args.includeFiles().end(); ++it) {
match = fnmatch(*it, name, it->noCase());
}
// Exclude filters
it = m_args.excludeFiles().begin();
for (; match && it != m_args.excludeFiles().end(); ++it) {
match = !fnmatch(*it, name, it->noCase());
}
return match;
}
bool Filter::hasContentFilters() const
{
return !m_inContent.empty();
}
bool Filter::hasExcludeContentFilters() const
{
return !m_exContent.empty();
}
bool Filter::printContent() const
{
return !m_inContent.empty() && m_args.allContent();
}
bool Filter::matchContent(std::string const & line, Match * pmatch) const
{
bool match = m_inContent.empty();
// Include filters
Regex::PtrList::const_iterator it = m_inContent.begin();
for (; !match && it != m_inContent.end(); ++it) {
match = (*it)->match(line, pmatch);
}
return match;
}
bool Filter::excludeContent(std::string const & line) const
{
bool exclude = false;
// Exclude filters
Regex::PtrList::const_iterator it = m_exContent.begin();
for (; !exclude && it != m_exContent.end(); ++it) {
exclude = (*it)->match(line);
}
return exclude;
}
bool Filter::fnmatch(std::string const & pattern, std::string const & string, bool icase)
{
#if defined(_UNIX)
// POSIX fnmatch
int const rval = ::fnmatch(pattern.c_str(), string.c_str(), icase ? FNM_CASEFOLD : 0);
if (rval != 0 && rval != FNM_NOMATCH) {
THROW_ERROR("Invalid pattern \"{}\" : {}", pattern, strerror(errno));
}
return (rval == 0);
#endif
#if defined(_WIN32)
return PathMatchSpecA(string.c_str(), pattern.c_str());
#endif
}