This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
match.cc
98 lines (80 loc) · 1.9 KB
/
match.cc
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
/*
* match - match/filter a string according regex rules - implementation
* Copyright(c) 2004-2005 of wave++ (Yuri D'Elia)
* Distributed under GNU LGPL without ANY warranty.
*/
// interface
#include "match.hh"
using std::string;
using std::vector;
// system headers
#include <fstream>
using std::ifstream;
using std::getline;
#include <stdexcept>
using std::runtime_error;
// c system headers
#include <stdlib.h>
// compile a regular expression with error checking
void
BMatch::compile(regex_t& data, const char* regex, int flags)
{
int res;
if((res = regcomp(&data, regex, flags | REG_NOSUB)))
{
char buf[128];
regerror(res, &data, buf, sizeof(buf));
throw runtime_error(string("regcomp: ") + buf);
}
}
void
BMatch::compInsert(std::vector<regex_t>& v, const char* regex, int flags)
{
regex_t buf;
compile(buf, regex, flags);
v.push_back(buf);
}
void
BMatch::load(const char* file)
{
ifstream in(file);
if(!in)
throw runtime_error(string("can't open: ") + file);
string line;
char buf[16];
for(size_t num = 1; getline(in, line); ++num)
{
if(!line.size() || line[0] == '#')
continue;
if(line[0] == '+')
include(line.substr(1).c_str());
else if(line[0] == '-')
exclude(line.substr(1).c_str());
else
{
snprintf(buf, sizeof(buf), ":%lu", num);
throw runtime_error(string("syntax error in ") + file + buf);
}
}
}
bool
BMatch::operator()(const char* string, int flags)
{
bool retIncl(!incl.size());
for(vector<regex_t>::const_iterator it = incl.begin(); it != incl.end(); ++it)
if(!regexec(&*it, string, 0, NULL, flags))
{
retIncl = true;
break;
}
if(!retIncl)
return false;
bool retExcl(true);
for(vector<regex_t>::const_iterator it = excl.begin(); it != excl.end(); ++it)
if(!regexec(&*it, string, 0, NULL, flags))
{
retExcl = false;
break;
}
return retExcl;
}