1
// rak - Rakshasa's toolbox
2
// Copyright (C) 2005, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
// This is a hacked up whole string pattern matching. Replace with
38
// TR1's regex when that becomes widely available. It is intended for
44
#include <sys/types.h>
52
class regex : public std::unary_function<std::string, bool> {
55
regex(const std::string& p) : m_pattern(p) {}
57
const std::string& pattern() const { return m_pattern; }
59
bool operator () (const std::string& p) const;
62
std::string m_pattern;
65
// This isn't optimized, or very clean. A simple hack that should work.
67
regex::operator () (const std::string& text) const {
68
if (m_pattern.empty() ||
70
(m_pattern[0] != '*' && m_pattern[0] != text[0]))
73
// Replace with unordered_vector?
74
std::list<unsigned int> paths;
77
for (std::string::const_iterator itrText = ++text.begin(), lastText = text.end(); itrText != lastText; ++itrText) {
79
for (std::list<unsigned int>::iterator itrPaths = paths.begin(), lastPaths = paths.end(); itrPaths != lastPaths; ) {
81
unsigned int next = *itrPaths + 1;
83
if (m_pattern[*itrPaths] != '*')
84
itrPaths = paths.erase(itrPaths);
88
// When we reach the end of 'm_pattern', we don't have a whole
90
if (next == m_pattern.size())
93
// Push to the back so that '*' will match zero length strings.
94
if (m_pattern[next] == '*')
95
paths.push_back(next);
97
if (m_pattern[next] == *itrText)
98
paths.push_front(next);
105
return std::find(paths.begin(), paths.end(), m_pattern.size() - 1) != paths.end();
111
// rak - Rakshasa's toolbox
112
// Copyright (C) 2005, Jari Sundell
114
// This program is free software; you can redistribute it and/or modify
115
// it under the terms of the GNU General Public License as published by
116
// the Free Software Foundation; either version 2 of the License, or
117
// (at your option) any later version.
119
// This program is distributed in the hope that it will be useful,
120
// but WITHOUT ANY WARRANTY; without even the implied warranty of
121
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
122
// GNU General Public License for more details.
124
// You should have received a copy of the GNU General Public License
125
// along with this program; if not, write to the Free Software
126
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
128
// In addition, as a special exception, the copyright holders give
129
// permission to link the code of portions of this program with the
130
// OpenSSL library under certain conditions as described in each
131
// individual source file, and distribute linked combinations
132
// including the two.
134
// You must obey the GNU General Public License in all respects for
135
// all of the code used other than OpenSSL. If you modify file(s)
136
// with this exception, you may extend this exception to your version
137
// of the file(s), but you are not obligated to do so. If you do not
138
// wish to do so, delete this exception statement from your version.
139
// If you delete this exception statement from all source files in the
140
// program, then also delete it here.
142
// Contact: Jari Sundell <jaris@ifi.uio.no>
145
// 3185 Skoppum, NORWAY
147
// This is a hacked up whole string pattern matching. Replace with
148
// TR1's regex when that becomes widely available. It is intended for
154
#include <sys/types.h>
156
#include <functional>
162
class regex : public std::unary_function<std::string, bool> {
165
regex(const std::string& p) : m_pattern(p) {}
167
const std::string& pattern() const { return m_pattern; }
169
bool operator () (const std::string& p) const;
172
std::string m_pattern;
175
// This isn't optimized, or very clean. A simple hack that should work.
177
regex::operator () (const std::string& text) const {
178
if (m_pattern.empty() ||
180
(m_pattern[0] != '*' && m_pattern[0] != text[0]))
183
// Replace with unordered_vector?
184
std::list<unsigned int> paths;
187
for (std::string::const_iterator itrText = ++text.begin(), lastText = text.end(); itrText != lastText; ++itrText) {
189
for (std::list<unsigned int>::iterator itrPaths = paths.begin(), lastPaths = paths.end(); itrPaths != lastPaths; ) {
191
unsigned int next = *itrPaths + 1;
193
if (m_pattern[*itrPaths] != '*')
194
itrPaths = paths.erase(itrPaths);
198
// When we reach the end of 'm_pattern', we don't have a whole
200
if (next == m_pattern.size())
203
// Push to the back so that '*' will match zero length strings.
204
if (m_pattern[next] == '*')
205
paths.push_back(next);
207
if (m_pattern[next] == *itrText)
208
paths.push_front(next);
215
return std::find(paths.begin(), paths.end(), m_pattern.size() - 1) != paths.end();