All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Regexp.cpp
Go to the documentation of this file.
1 /*
2  * Regex.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  * This uses the POSIX regex instead of the ANSI C++11 one, because not all the
22  * implementations for regex are ready within the current (as the creation date)
23  * compiler versions.
24  */
25 
26 #include <HSEP/Regexp.h>
27 
28 #include <regex.h>
29 
30 namespace HSEP {
31 
32  void Regexp::init(const char* pRegExpDefinition) {
33 
34  int vResult;
35  aHandle = nullptr; // lets eclipse to be happy
36 
37  // uses the Posix extended syntax to be more similar to the Perl one
38  //
39  aHandle = new regex_t();
40 
41  vResult = regcomp((regex_t*)aHandle,pRegExpDefinition,REG_EXTENDED);
42 
43  if (0 == vResult) {
44  setValid();
45  aText = pRegExpDefinition;
46  }
47  else {
48  setInvalid();
49 
50  switch (vResult) {
51  case REG_BADBR:
52  setLastError("Invalid use of back reference operator.");
53  break;
54 
55  case REG_BADPAT:
56  setLastError("Invalid use of pattern operators such as group or list.");
57  break;
58 
59  case REG_BADRPT:
60  setLastError("Invalid use of repetition operators such as using '*' as the first character.");
61  break;
62 
63  case REG_EBRACE:
64  setLastError("Un-matched brace interval operators.");
65  break;
66 
67  case REG_EBRACK:
68  setLastError("Un-matched bracket list operators.");
69  break;
70 
71  case REG_ECOLLATE:
72  setLastError("Invalid collating element.");
73  break;
74 
75  case REG_ECTYPE:
76  setLastError("Unknown character class name.");
77  break;
78 
79  case REG_EEND:
80  setLastError("Non specific error. This is not defined by POSIX.2.");
81  break;
82 
83  case REG_EESCAPE:
84  setLastError("Trailing backslash.");
85  break;
86  case REG_EPAREN:
87  setLastError("Un-matched parenthesis group operators.");
88  break;
89 
90  case REG_ERANGE:
91  setLastError("Invalid use of the range operator, e.g., the ending point of the range occurs prior to the starting point.");
92  break;
93 
94  case REG_ESIZE:
95  setLastError("Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2.");
96  break;
97 
98  case REG_ESPACE:
99  setLastError("The regex routines ran out of memory.");
100  break;
101 
102  case REG_ESUBREG:
103  setLastError("Invalid back reference to a subexpression.");
104  break;
105  }
106 
107  }
108 
109  // eval vResult;
110  } // Regexp::init
111 
112  Regexp::Regexp(string& pRegExpDefinition) : HSEPObject("Regexp") {
113  init(pRegExpDefinition.c_str());
114  } // constructor
115 
116  Regexp::Regexp(const char* pRegExpDefinition) : HSEPObject("Regexp") {
117  init(pRegExpDefinition);
118  } // constructor
119 
121  regfree((regex_t*)aHandle);
122  delete (regex_t*) aHandle;
123  }
124 
125  bool Regexp::match(string pValue) {
126 
127  bool vResult = false;
128 
129  vResult = (regexec((regex_t*)aHandle,pValue.c_str(),0,nullptr,0) == 0);
130 
131  return vResult;
132  }
133 
134 } // HSEP namespace