diff options
Diffstat (limited to 'src/RegExp.cc')
-rw-r--r-- | src/RegExp.cc | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/RegExp.cc b/src/RegExp.cc new file mode 100644 index 0000000..2e991da --- /dev/null +++ b/src/RegExp.cc | |||
@@ -0,0 +1,94 @@ | |||
1 | // RegExp.cc for Fluxbox Window Manager | ||
2 | // Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
3 | // and Simon Bowden (rathnor at users.sourceforge.net) | ||
4 | // | ||
5 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
6 | // copy of this software and associated documentation files (the "Software"), | ||
7 | // to deal in the Software without restriction, including without limitation | ||
8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
9 | // and/or sell copies of the Software, and to permit persons to whom the | ||
10 | // Software is furnished to do so, subject to the following conditions: | ||
11 | // | ||
12 | // The above copyright notice and this permission notice shall be included in | ||
13 | // all copies or substantial portions of the Software. | ||
14 | // | ||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
21 | // DEALINGS IN THE SOFTWARE. | ||
22 | |||
23 | // $Id: RegExp.cc,v 1.1 2003/06/12 15:12:19 rathnor Exp $ | ||
24 | |||
25 | #include "RegExp.hh" | ||
26 | |||
27 | //use GNU extensions | ||
28 | #ifndef _GNU_SOURCE | ||
29 | #define _GNU_SOURCE | ||
30 | #endif // _GNU_SOURCE | ||
31 | |||
32 | #include <string> | ||
33 | #include <iostream> | ||
34 | |||
35 | using namespace std; | ||
36 | |||
37 | |||
38 | /******************************************************** | ||
39 | * RegExp * | ||
40 | ***********/ | ||
41 | |||
42 | // full_match is to say if we match on this regexp using the full string | ||
43 | // or just a substring. Substrings aren't supported if not HAVE_REGEXP | ||
44 | RegExp::RegExp(const std::string &str, bool full_match): | ||
45 | #ifdef USE_REGEXP | ||
46 | m_regex(0) { | ||
47 | string match; | ||
48 | if (full_match) { | ||
49 | match = "^"; | ||
50 | match.append(str); | ||
51 | match.append("$"); | ||
52 | } else { | ||
53 | match = str; | ||
54 | } | ||
55 | |||
56 | m_regex = new regex_t; | ||
57 | int ret = regcomp(m_regex, match.c_str(), REG_NOSUB | REG_EXTENDED); | ||
58 | if (ret != 0) { | ||
59 | char *errstr = 0; | ||
60 | // gives us the length of the string | ||
61 | unsigned int size = regerror(ret, m_regex, errstr, 0); | ||
62 | errstr = new char[size]; | ||
63 | |||
64 | regerror(ret, m_regex, errstr, size); | ||
65 | cerr<<"Error parsing regular expression: "<<errstr<<endl; | ||
66 | delete [] errstr; | ||
67 | delete m_regex; // I don't think I regfree a failed compile? | ||
68 | m_regex = 0; | ||
69 | } | ||
70 | } | ||
71 | #else // notdef USE_REGEXP | ||
72 | m_str(str) {} | ||
73 | #endif // USE_REGEXP | ||
74 | |||
75 | RegExp::~RegExp() { | ||
76 | #ifdef USE_REGEXP | ||
77 | if (m_regex != 0) { | ||
78 | regfree(m_regex); | ||
79 | delete m_regex; | ||
80 | } | ||
81 | #endif // USE_REGEXP | ||
82 | } | ||
83 | |||
84 | bool RegExp::match(const std::string &str) { | ||
85 | #ifdef USE_REGEXP | ||
86 | if (m_regex) | ||
87 | return (regexec(m_regex, str.c_str(), 0, 0, 0) == 0); | ||
88 | else | ||
89 | return false; | ||
90 | #else // notdef USE_REGEXP | ||
91 | return (m_str == str); | ||
92 | #endif // USE_REGEXP | ||
93 | } | ||
94 | |||