-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmatch.cpp
117 lines (103 loc) · 3.48 KB
/
match.cpp
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
#include "match.hh"
#include <iostream>
#include <string>
#include <vector>
// Boost libraries
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace std;
extern int gVerbosity;
//----------------------------------------------------------------
// simplifyURL(), remove duplicated '/' in the URL
string simplifyURL(const char* url)
{
const char* p = url; // current char in url
int n = 0; // number of successive '/'
string r; // resulting simplified URL
for (char c = *p; c != 0; c = *(++p)) {
n = (c == '/') ? n + 1 : 0;
if (n < 2) r += c;
}
if (gVerbosity >= 2) cerr << "Simplify url " << url << " --> " << r << endl;
return r;
}
//----------------------------------------------------------------
// decomposeURL(), decompose an URL into a vector of strings.
// Used internally by matchURL. Trailing / are removed
vector<string> decomposeURL(const string& url)
{
boost::filesystem::path U(url);
vector<string> decomposition;
for (auto n : U) {
string s = n.string();
if (s != ".") decomposition.push_back(s);
}
return decomposition;
}
//----------------------------------------------------------------
// matchURL() returns true if the url and the pattern match
// To match they must have the same number of elements
// and all these elements must be identical, or wildcards ( '*' ).
// Data contains the decomposition of the URL
bool matchURL(const string& url, const std::string& pat, vector<string>& data)
{
vector<string> U = decomposeURL(url);
vector<string> P = decomposeURL(pat);
if (P.size() == U.size()) {
for (size_t i = 0; i < P.size(); i++) {
if ((P[i] != "*") && (P[i] != U[i])) {
return false;
}
}
data = U;
if (gVerbosity >= 2) cout << "PATTERN " << pat << " MATCHES URL " << url << endl;
return true;
} else {
return false;
}
}
//----------------------------------------------------------------
// matchURL() returns true if the url and the pattern match.
// To match they must have the same number of elements
// and all these elements must be identical, or wildcards ( '*' ).
bool matchURL(const string& url, const std::string& pat)
{
vector<string> ignore;
bool r = matchURL(url, pat, ignore);
if (gVerbosity >= 2) {
if (r)
cout << "PATTERN " << pat << " MATCHES URL " << url << endl;
else
cout << "PATTERN " << pat << " DOES NOT MATCH URL " << url << endl;
}
return r;
}
bool matchExtension(const string& url, const std::string& ext)
{
size_t u = url.length();
size_t e = ext.length();
if (u > e) {
size_t p = u - e;
return url.find(ext, p) == p;
} else {
return false;
}
}
bool matchBeginURL(const string& url, const std::string& pat)
{
vector<string> U = decomposeURL(url);
vector<string> P = decomposeURL(pat);
if (P.size() <= U.size()) {
for (size_t i = 0; i < P.size(); i++) {
if ((P[i] != "*") && (P[i] != U[i])) {
if (gVerbosity >= 2) cout << "PATTERN " << pat << " DOES NOT MATCH URL " << url << endl;
return false;
}
}
if (gVerbosity >= 2) cout << "PATTERN " << pat << " MATCHES URL " << url << endl;
return true;
} else {
if (gVerbosity >= 2) cout << "PATTERN " << pat << " DOES NOT MATCH URL " << url << endl;
return false;
}
}