First commit
[anna.git] / source / core / util / RegularExpression.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 // Local
38 #include <anna/core/util/RegularExpression.hpp>
39
40 #include <anna/core/util/Tokenizer.hpp>
41 #include <anna/core/functions.hpp>
42
43
44 //using namespace anna;
45
46 // private
47
48 void anna::RegularExpression::freeRegex() throw() {
49   if(a_compiled) {
50     regfree(&a_preg);
51     a_compiled = false;
52   }
53 }
54
55 void anna::RegularExpression::compile() throw(anna::RuntimeException) {
56   if(a_compiled) return;
57
58   int ret;
59
60   if((ret = regcomp(&a_preg, a_pattern.c_str(), REG_EXTENDED)) != 0) {
61     char err[256];
62     std::string msg("anna::RegularExpression::setPattern | ");
63     msg += " | Pattern: ";
64     msg += a_pattern;
65     msg += " | ";
66
67     if(regerror(ret, &a_preg, err, sizeof(err)))
68       msg += err;
69     else
70       msg += "Invalid pattern provided";
71
72     throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
73   }
74
75   a_compiled = true;
76 }
77
78 // public
79
80 //------------------------------------------------------------------------------
81 //---------------------------------------------- RegularExpression::setPattern()
82 //------------------------------------------------------------------------------
83 void anna::RegularExpression::setPattern(const std::string & pattern) throw() {
84   if(pattern == a_pattern) return;
85
86   freeRegex();
87   a_pattern = pattern;
88 }
89
90
91 //------------------------------------------------------------------------------
92 //-------------------------------------------------- RegularExpression::isLike()
93 //------------------------------------------------------------------------------
94 bool anna::RegularExpression::isLike(const std::string & value) throw() {
95   compile();
96   const bool result = (regexec(&a_preg, value.c_str(), 0, NULL, 0) == 0) ? true : false;
97   return result;
98 }
99
100
101 //------------------------------------------------------------------------------
102 //------------------------------------------------ RegularExpression::operator==
103 //------------------------------------------------------------------------------
104 bool anna::RegularExpression::operator == (const RegularExpression & re) const {
105   return (getPattern() == re.getPattern());
106 }
107
108
109 //------------------------------------------------------------------------------
110 //------------------------------------------------- RegularExpression::operator<
111 //------------------------------------------------------------------------------
112 bool anna::RegularExpression::operator < (const RegularExpression & re) const {
113   return (getPattern() < re.getPattern());
114 }
115