All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RegexpChecker.cpp
Go to the documentation of this file.
1 /*
2  * RegexpChecker.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 
22 #include <HSEP/RegexpChecker.h>
23 
24 namespace HSEP {
25 
26  string RegexpChecker::NOTFOUND = "NOT FOUND";
27 
28  RegexpChecker::RegexpChecker(StringDictionary& pDefinitionsRef) : HSEPObject("TypeChecker") {
29 
30  pDefinitionsRef.forEach([&](string& xKey, string* xValue){
31 
32  string& vValue = *xValue;
33 
34  Regexp* vNewExpression = new Regexp(vValue);
35 
36  if (vNewExpression->valid()) {
37  aRegexps.put(xKey,vNewExpression);
38  }
39  else {
40  delete vNewExpression;
41  }
42 
43  });
44 
45  } // RegexpChecker constructor
46 
47  bool RegexpChecker::match(const char* pType, string pDataStr) {
48  bool vResult = false;
49 
50  if (aRegexps.has(pType)) {
51  return aRegexps.ref(pType).match(pDataStr);
52  }
53 
54  return vResult;
55  } // RegexpChecker::match
56 
57  string& RegexpChecker::key(const char* pSample) {
58 
59  string& vResult = NOTFOUND;
60  string& vDefault = NOTFOUND;
61 
62  bool vFound = false;
63  aRegexps.forEach([&](string& xKey, Regexp* xValue) {
64 
65  if (!vFound) { // only work if necessary
66 
67  if (xValue->text() == "DEFAULT") {
68  vDefault = xKey;
69  }
70  else {
71  if (xValue->match(pSample)) {
72  vResult = xKey;
73  vFound = true;
74  }
75  }
76  }
77 
78  });
79 
80  if (!vFound) {
81  vResult = vDefault;
82  }
83 
84  return vResult;
85 
86  } // RegexpChecker::key
87 
88 } // HSEP namespace
89 
90