forked from e2guardian/e2guardian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegExp.hpp
91 lines (68 loc) · 2.08 KB
/
RegExp.hpp
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
// RegExp class - search text using regular expressions
// For all support, instructions and copyright go to:
// http://e2guardian.org/
// Released under the GPL v2, with the OpenSSL exception described in the README file.
#ifndef __HPP_REGEXP
#define __HPP_REGEXP
#define MAX_SUB_EXPRESSIONS 1024
// INCLUDES
#include <sys/types.h> // needed for size_t used in regex.h
#ifdef HAVE_PCRE
#include <pcreposix.h>
#else
#include <regex.h>
#endif
#include <string>
#include <deque>
// DECLARATIONS
class RegResult
{
public:
// constructor - set sensible defaults
RegResult();
// destructor - delete regexp if compiled
~RegResult();
// how many matches did the last run generate?
int numberOfMatches();
// did it generate any at all?
bool matched();
// the i'th match from the last run
std::string result(int i);
// position of the i'th match in the overall text
unsigned int offset(int i);
// length of the i'th match
unsigned int length(int i);
// the match results, their positions in the text & their lengths
std::deque<std::string> results;
std::deque<unsigned int> offsets;
std::deque<unsigned int> lengths;
// have we matched something yet?
bool imatched;
};
class RegExp
{
public:
// constructor - set sensible defaults
RegExp();
// destructor - delete regexp if compiled
~RegExp();
// copy constructor
RegExp(const RegExp &r );
// compile the given regular expression
bool comp(const char *exp);
// match the given text against the pre-compiled expression
bool match(const char *text, struct RegResult& rs);
// how many matches did the last run generate?
// did it generate any at all?
// faster equivalent of STL::Search
char *search(char *file, char *fileend, char *phrase, char *phraseend);
private:
// the expression itself
regex_t reg;
// whether it's been pre-compiled
bool wascompiled;
// the uncompiled form of the expression (checkme: is this only used
// for debugging purposes?)
std::string searchstring;
};
#endif