|
a/src/utils/strmatcher.cpp |
|
b/src/utils/strmatcher.cpp |
|
... |
|
... |
14 |
* Free Software Foundation, Inc.,
|
14 |
* Free Software Foundation, Inc.,
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
*/
|
16 |
*/
|
17 |
|
17 |
|
18 |
#include "autoconfig.h"
|
18 |
#include "autoconfig.h"
|
|
|
19 |
#include "strmatcher.h"
|
19 |
|
20 |
|
20 |
#include <stdio.h>
|
21 |
#include <stdio.h>
|
21 |
#include <sys/types.h>
|
22 |
#include <sys/types.h>
|
22 |
#ifdef _WIN32
|
|
|
23 |
#include <regex>
|
|
|
24 |
#else
|
|
|
25 |
#include <regex.h>
|
|
|
26 |
#endif
|
|
|
27 |
#include <fnmatch.h>
|
23 |
#include <fnmatch.h>
|
28 |
|
24 |
|
29 |
#include <string>
|
25 |
#include <string>
|
30 |
|
26 |
|
31 |
#include "cstr.h"
|
27 |
#include "cstr.h"
|
32 |
#include "log.h"
|
28 |
#include "log.h"
|
33 |
#include "pathut.h"
|
29 |
#include "pathut.h"
|
34 |
#include "strmatcher.h"
|
|
|
35 |
|
30 |
|
36 |
using namespace std;
|
31 |
using namespace std;
|
37 |
|
32 |
|
38 |
bool StrWildMatcher::match(const string& val) const
|
33 |
bool StrWildMatcher::match(const string& val) const
|
39 |
{
|
34 |
{
|
40 |
LOGDEB2("StrWildMatcher::match: [" << (m_sexp) << "] against [" << (val) << "]\n" );
|
35 |
LOGDEB2("StrWildMatcher::match ["<< m_sexp<< "] against [" << val << "]\n");
|
41 |
int ret = fnmatch(m_sexp.c_str(), val.c_str(), FNM_NOESCAPE);
|
36 |
int ret = fnmatch(m_sexp.c_str(), val.c_str(), FNM_NOESCAPE);
|
42 |
switch (ret) {
|
37 |
switch (ret) {
|
43 |
case 0: return true;
|
38 |
case 0: return true;
|
44 |
case FNM_NOMATCH: return false;
|
39 |
case FNM_NOMATCH: return false;
|
45 |
default:
|
40 |
default:
|
46 |
LOGINFO("StrWildMatcher::match:err: e [" << (m_sexp) << "] s [" << (val) << "] (" << (url_encode(val)) << ") ret " << (ret) << "\n" );
|
41 |
LOGINFO("StrWildMatcher::match:err: e [" << m_sexp << "] s [" << val
|
|
|
42 |
<< "] (" << url_encode(val) << ") ret " << ret << "\n");
|
47 |
return false;
|
43 |
return false;
|
48 |
}
|
44 |
}
|
49 |
}
|
45 |
}
|
50 |
|
46 |
|
51 |
string::size_type StrWildMatcher::baseprefixlen() const
|
47 |
string::size_type StrWildMatcher::baseprefixlen() const
|
52 |
{
|
48 |
{
|
53 |
return m_sexp.find_first_of(cstr_wildSpecStChars);
|
49 |
return m_sexp.find_first_of(cstr_wildSpecStChars);
|
54 |
}
|
50 |
}
|
55 |
|
51 |
|
56 |
StrRegexpMatcher::StrRegexpMatcher(const string& exp)
|
52 |
StrRegexpMatcher::StrRegexpMatcher(const string& exp)
|
57 |
: StrMatcher(exp), m_compiled(0), m_errcode(0)
|
53 |
: StrMatcher(exp),
|
|
|
54 |
m_re(exp, SimpleRegexp::SRE_NOSUB)
|
58 |
{
|
55 |
{
|
59 |
setExp(exp);
|
|
|
60 |
}
|
56 |
}
|
61 |
|
57 |
|
62 |
bool StrRegexpMatcher::setExp(const string& exp)
|
58 |
bool StrRegexpMatcher::setExp(const string& exp)
|
63 |
{
|
59 |
{
|
64 |
if (m_compiled) {
|
60 |
m_re = SimpleRegexp(exp, SimpleRegexp::SRE_NOSUB);
|
65 |
#ifdef _WIN32
|
|
|
66 |
delete (regex*)m_compiled;
|
|
|
67 |
#else
|
|
|
68 |
regfree((regex_t*)m_compiled);
|
|
|
69 |
delete (regex_t*)m_compiled;
|
|
|
70 |
#endif
|
|
|
71 |
}
|
|
|
72 |
m_compiled = 0;
|
|
|
73 |
|
|
|
74 |
#ifdef _WIN32
|
|
|
75 |
try {
|
|
|
76 |
m_compiled = new regex(exp, std::regex_constants::nosubs |
|
|
|
77 |
std::regex_constants::extended);
|
|
|
78 |
} catch (...) {
|
|
|
79 |
m_reason = string("StrRegexpMatcher:regcomp failed for ")
|
|
|
80 |
+ exp + string("syntax error ?");
|
|
|
81 |
return false;
|
|
|
82 |
}
|
|
|
83 |
#else
|
|
|
84 |
m_compiled = new regex_t;
|
|
|
85 |
if ((m_errcode =
|
|
|
86 |
regcomp((regex_t*)m_compiled, exp.c_str(), REG_EXTENDED|REG_NOSUB))) {
|
|
|
87 |
char errbuf[200];
|
|
|
88 |
regerror(m_errcode, (regex_t*)m_compiled, errbuf, 199);
|
|
|
89 |
m_reason = string("StrRegexpMatcher:regcomp failed for ")
|
|
|
90 |
+ exp + string(errbuf);
|
|
|
91 |
return false;
|
|
|
92 |
}
|
|
|
93 |
#endif
|
|
|
94 |
m_sexp = exp;
|
|
|
95 |
return true;
|
61 |
return m_re.ok();
|
96 |
}
|
|
|
97 |
|
|
|
98 |
StrRegexpMatcher::~StrRegexpMatcher()
|
|
|
99 |
{
|
|
|
100 |
if (m_compiled) {
|
|
|
101 |
#ifdef _WIN32
|
|
|
102 |
delete (regex *)m_compiled;
|
|
|
103 |
#else
|
|
|
104 |
regfree((regex_t*)m_compiled);
|
|
|
105 |
delete (regex_t*)m_compiled;
|
|
|
106 |
#endif
|
|
|
107 |
}
|
|
|
108 |
}
|
62 |
}
|
109 |
|
63 |
|
110 |
bool StrRegexpMatcher::match(const string& val) const
|
64 |
bool StrRegexpMatcher::match(const string& val) const
|
111 |
{
|
65 |
{
|
112 |
if (m_errcode)
|
66 |
if (!m_re.ok())
|
113 |
return false;
|
67 |
return false;
|
114 |
#ifdef _WIN32
|
68 |
return m_re(val);
|
115 |
return regex_match(val, *((regex *)m_compiled));
|
|
|
116 |
#else
|
|
|
117 |
return regexec((regex_t*)m_compiled, val.c_str(), 0, 0, 0) != REG_NOMATCH;
|
|
|
118 |
#endif
|
|
|
119 |
}
|
69 |
}
|
120 |
|
70 |
|
121 |
string::size_type StrRegexpMatcher::baseprefixlen() const
|
71 |
string::size_type StrRegexpMatcher::baseprefixlen() const
|
122 |
{
|
72 |
{
|
123 |
return m_sexp.find_first_of(cstr_regSpecStChars);
|
73 |
return m_sexp.find_first_of(cstr_regSpecStChars);
|
124 |
}
|
74 |
}
|
125 |
|
75 |
|
126 |
bool StrRegexpMatcher::ok() const
|
76 |
bool StrRegexpMatcher::ok() const
|
127 |
{
|
77 |
{
|
128 |
return !m_errcode;
|
78 |
return m_re.ok();
|
129 |
}
|
79 |
}
|
130 |
|
80 |
|