OPAL (Object Oriented Parallel Accelerator Library)
2024.2
OPAL
RegularExpression.cpp
Go to the documentation of this file.
1
// ------------------------------------------------------------------------
2
// $RCSfile: RegularExpression.cpp,v $
3
// ------------------------------------------------------------------------
4
// $Revision: 1.1.1.1 $
5
// ------------------------------------------------------------------------
6
// Copyright: see Copyright.readme
7
// ------------------------------------------------------------------------
8
//
9
// Class: RegularExpression.
10
// This class provides a simple interface to the POSIX-compliant
11
// regcomp/regexec package.
12
//
13
// ------------------------------------------------------------------------
14
//
15
// $Date: 2000/03/27 09:33:48 $
16
// $Author: Andreas Adelmann $
17
//
18
// ------------------------------------------------------------------------
19
20
#include "
Utilities/RegularExpression.h
"
21
#include "
Utilities/OpalException.h
"
22
#include <cstdlib>
23
#include <regex.h>
24
#include <cstring>
25
#include <algorithm>
26
#include <iomanip>
27
#include <iostream>
28
#include <new>
29
#include <cstring>
30
#include <functional>
31
32
// Class RegularExpression::Expression
33
// ------------------------------------------------------------------------
34
35
class
RegularExpression::Expression
:
public
regex_t {};
36
37
38
// Class RegularExpression
39
// ------------------------------------------------------------------------
40
41
RegularExpression::RegularExpression
(
const
std::string &pattern,
bool
ignore):
42
patt
(pattern),
caseIgnore
(ignore) {
43
init
();
44
}
45
46
47
RegularExpression::RegularExpression
(
const
RegularExpression
&rhs):
48
patt
(rhs.
patt
),
caseIgnore
(rhs.
caseIgnore
) {
49
init
();
50
}
51
52
53
RegularExpression::~RegularExpression
() {
54
::regfree(
expr
);
55
}
56
57
58
bool
RegularExpression::match
(
const
std::string &s)
const
{
59
int
rc =
state
;
60
61
if
(rc == 0) {
62
int
nmatch = 0;
63
regmatch_t *pmatch = 0;
64
int
flags = 0;
65
rc = regexec(
expr
, s.data(), nmatch, pmatch, flags);
66
if
(rc == 0) {
67
return
true
;
68
}
else
if
(rc == REG_NOMATCH) {
69
return
false
;
70
}
71
}
72
73
char
buffer[80];
74
regerror(rc,
expr
, buffer, 80);
75
throw
OpalException
(
"RegularExpression::match()"
, buffer);
76
}
77
78
79
bool
RegularExpression::OK
()
const
{
80
return
state
== 0 &&
expr
!= 0;
81
}
82
83
84
void
RegularExpression::init
() {
85
expr
= (
Expression
*) malloc(
sizeof
(regex_t));
86
int
flags = REG_NOSUB;
87
if
(
caseIgnore
) flags |= REG_ICASE;
88
state
= regcomp(
expr
,
patt
.c_str(), flags);
89
}
RegularExpression.h
OpalException.h
OpalException
The base class for all OPAL exceptions.
Definition
OpalException.h:28
RegularExpression::Expression
Definition
RegularExpression.cpp:35
RegularExpression::match
bool match(const std::string &s) const
Match a string against the pattern.
Definition
RegularExpression.cpp:58
RegularExpression::RegularExpression
RegularExpression(const std::string &pattern, bool ignore=false)
Constructor.
Definition
RegularExpression.cpp:41
RegularExpression::state
int state
Definition
RegularExpression.h:68
RegularExpression::patt
const std::string patt
Definition
RegularExpression.h:58
RegularExpression::OK
bool OK() const
Check the regular expression for sanity.
Definition
RegularExpression.cpp:79
RegularExpression::caseIgnore
bool caseIgnore
Definition
RegularExpression.h:61
RegularExpression::init
void init()
Definition
RegularExpression.cpp:84
RegularExpression::expr
Expression * expr
Definition
RegularExpression.h:65
RegularExpression::~RegularExpression
~RegularExpression()
Definition
RegularExpression.cpp:53