OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 
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 
48  patt(rhs.patt), caseIgnore(rhs.caseIgnore) {
49  init();
50 }
51 
52 
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 
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 }
The base class for all OPAL exceptions.
Definition: OpalException.h:28
A regular expression.
bool match(const std::string &s) const
Match a string against the pattern.
RegularExpression(const std::string &pattern, bool ignore=false)
Constructor.
const std::string patt
bool OK() const
Check the regular expression for sanity.