-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrMatcher.cpp
112 lines (95 loc) · 2.45 KB
/
strMatcher.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
#include <string.h>
#include "pcre.h"
#include "strMatcher.hpp"
#define LG_LBUF 1024
strMatcher::strMatcher(char *Masks, bool unique, int o) {
char *pc;
char *lBuf;
int i;
this->strmOptions = o;
this->pcreOptions = 0;
this->rc = 0;
if ( this->strmOptions & STRM_OPTION_CASELESS ) {
this->pcreOptions |= PCRE_CASELESS;
}
reTable = 0;
if ( *Masks == 0 ) {
this->nbRE = 0;
return;
}
lBuf = new char[strlen(Masks) + 10];
strcpy(lBuf, Masks);
pc = lBuf;
this->nbRE = 1;
if ( !unique ) {
while ( *pc != 0 ) {
if ( (*pc == ';') || (*pc == ',') ) {
*pc = 0;
if ( *(pc + 1) != 0 ) {
++this->nbRE;
}
}
++pc;
}
}
this->reTable = new TregExp[nbRE];
memset(this->reTable, 0, sizeof(TregExp) * nbRE);
pc = lBuf;
for ( i = 0; i < nbRE; i++ ) {
if ( (this->reTable[i].regExp = pcre_compile(pc, this->pcreOptions, &this->reTable[i].reErrMes, &this->reTable[i].reErrOffset, 0)) != 0 ) {
this->reTable[i].regExpExtra = pcre_study(this->reTable[i].regExp, 0, &this->reTable[i].reeErrMes);
}
else {
--i;
--this->nbRE;
}
if ( *pc == 0 )
++pc;
while ( *pc )
++pc;
++pc;
}
delete[] lBuf;
}
strMatcher::~strMatcher() {
int i;
if ( reTable != 0) {
for ( i = 0; i < nbRE; i++ ) {
if ( this->reTable[i].regExp != 0 ) {
free(this->reTable[i].regExp);
}
if ( this->reTable[i].regExpExtra != 0 ) {
free(this->reTable[i].regExpExtra);
}
}
delete reTable;
}
}
int strMatcher::doesStrMatchMask(const char *str) {
int i, lg;
this->rc = 0;
lg = strlen(str);
for ( i = 0; i < nbRE; i++ ) {
if ( (this->rc = pcre_exec(this->reTable[i].regExp, this->reTable[i].regExpExtra, str, lg, 0, 0, this->ovector, LG_OVECTOR)) > 0 ) {
return 1;
}
}
return 0;
}
int strMatcher::getOffsetOfCaptured(int c) {
if ( this->rc ) {
if ( (c >= 0) && (c < this->rc) )
return this->ovector[c * 2];
}
return -1;
}
int strMatcher::getLengthOfCaptured(int c) {
if ( this->rc ) {
if ( (c >= 0) && (c < this->rc) )
return this->ovector[c * 2 + 1] - this->ovector[c * 2];
}
return -1;
}
int strMatcher::getOffsetOfMatchingStr() {
return this->getOffsetOfCaptured(0);
}