Convert to warning
[anna.git] / source / core / util / RegularExpression.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Local
10 #include <anna/core/util/RegularExpression.hpp>
11
12 #include <anna/core/util/Tokenizer.hpp>
13 #include <anna/core/functions.hpp>
14
15
16 //using namespace anna;
17
18 // private
19
20 void anna::RegularExpression::freeRegex() throw() {
21   if(a_compiled) {
22     regfree(&a_preg);
23     a_compiled = false;
24   }
25 }
26
27 void anna::RegularExpression::compile() throw(anna::RuntimeException) {
28   if(a_compiled) return;
29
30   int ret;
31
32   if((ret = regcomp(&a_preg, a_pattern.c_str(), REG_EXTENDED)) != 0) {
33     char err[256];
34     std::string msg("anna::RegularExpression::setPattern | ");
35     msg += " | Pattern: ";
36     msg += a_pattern;
37     msg += " | ";
38
39     if(regerror(ret, &a_preg, err, sizeof(err)))
40       msg += err;
41     else
42       msg += "Invalid pattern provided";
43
44     throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
45   }
46
47   a_compiled = true;
48 }
49
50 // public
51
52 //------------------------------------------------------------------------------
53 //---------------------------------------------- RegularExpression::setPattern()
54 //------------------------------------------------------------------------------
55 void anna::RegularExpression::setPattern(const std::string & pattern) throw() {
56   if(pattern == a_pattern) return;
57
58   freeRegex();
59   a_pattern = pattern;
60 }
61
62
63 //------------------------------------------------------------------------------
64 //-------------------------------------------------- RegularExpression::isLike()
65 //------------------------------------------------------------------------------
66 bool anna::RegularExpression::isLike(const std::string & value) throw() {
67   compile();
68   const bool result = (regexec(&a_preg, value.c_str(), 0, NULL, 0) == 0) ? true : false;
69   return result;
70 }
71
72
73 //------------------------------------------------------------------------------
74 //------------------------------------------------ RegularExpression::operator==
75 //------------------------------------------------------------------------------
76 bool anna::RegularExpression::operator == (const RegularExpression & re) const {
77   return (getPattern() == re.getPattern());
78 }
79
80
81 //------------------------------------------------------------------------------
82 //------------------------------------------------- RegularExpression::operator<
83 //------------------------------------------------------------------------------
84 bool anna::RegularExpression::operator < (const RegularExpression & re) const {
85   return (getPattern() < re.getPattern());
86 }
87