1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
// $Id: RegularExpression.cc,v 1.7 2003/10/24 15:59:34 bburton Exp $
//
// Copyright (C) 2000 Burton Computer Corporation
// ALL RIGHTS RESERVED
//
// This program is open source software; you can redistribute it
// and/or modify it under the terms of the Q Public License (QPL)
// version 1.0. Use of this software in whole or in part, including
// linking it (modified or unmodified) into other programs is
// subject to the terms of the QPL.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Q Public License for more details.
//
// You should have received a copy of the Q Public License
// along with this program; see the file LICENSE.txt. If not, visit
// the Burton Computer Corporation or CoolDevTools web site
// QPL pages at:
//
// http://www.burton-computer.com/qpl.html
//
#ifndef HAVE_REGEX_H
#error This module requires POSIX regular expressions
#endif
#include "RegularExpression.h"
#include <regex.h>
#define MAX_MATCHES 25
void dumpstr(const string &str)
{
cout << "dump: len " << dec << str.length() << ": " << str << endl;
for (string::size_type pos = 0; pos < str.length(); ++pos) {
if (pos % 20 == 0 && pos != 0) {
cout << endl;
}
cout << str[pos] << " " << hex << (unsigned)(str[pos] & 0xff) << " ";
}
cout << endl;
}
class RegularExpressionImpl
{
public:
RegularExpressionImpl()
: m_haveExpr(false)
{
}
~RegularExpressionImpl()
{
if (m_haveExpr) {
regfree(&m_expr);
}
}
void release()
{
if (m_haveExpr) {
regfree(&m_expr);
m_numSubs = 0;
m_haveExpr = false;
}
}
bool compile(const string &expr,
int num_subs,
bool ignore_case,
bool ignore_newline)
{
assert(num_subs < MAX_MATCHES);
release();
int flags = REG_EXTENDED;
if (ignore_case) {
flags |= REG_ICASE;
}
if (ignore_newline) {
flags |= REG_NEWLINE;
}
if (num_subs == 0) {
flags |= REG_NOSUB;
}
if (regcomp(&m_expr, expr.c_str(), flags)) {
return false;
}
m_numSubs = num_subs;
m_haveExpr = true;
return true;
}
bool match(const string &text,
vector<RegularExpression::MatchData> &match_vec)
{
assert(m_haveExpr);
match_vec.clear();
regmatch_t matches[MAX_MATCHES + 1];
int rc = regexec(&m_expr, to_7bits(text).c_str(), m_numSubs + 1, matches, 0);
if (rc != 0) {
return false;
}
for (int i = 0; i <= m_numSubs; ++i) {
RegularExpression::MatchData md;
if (matches[i].rm_so == -1) {
md.start_pos = 0;
md.end_pos = 0;
} else {
md.start_pos = matches[i].rm_so;
md.end_pos = matches[i].rm_eo;
}
match_vec.push_back(md);
}
return true;
}
private:
bool m_haveExpr;
int m_numSubs;
regex_t m_expr;
};
RegularExpression::RegularExpression()
{
}
RegularExpression::RegularExpression(const string &expr,
int num_subs,
bool ignore_case,
bool ignore_newline)
{
setExpression(expr, num_subs, ignore_case, ignore_newline);
}
RegularExpression::~RegularExpression()
{
}
bool RegularExpression::isValid()
{
return m_expr.get() != 0;
}
bool RegularExpression::setExpression(const string &expr,
int num_subs,
bool ignore_case,
bool ignore_newline)
{
NewPtr<RegularExpressionImpl> new_expr(new RegularExpressionImpl);
if (!new_expr->compile(expr, num_subs, ignore_case, ignore_newline)) {
return false;
}
m_expr.set(new_expr.release());
return true;
}
bool RegularExpression::match(const string &text)
{
assert(isValid());
m_text = text;
return m_expr->match(m_text, m_matches);
}
int RegularExpression::matchCount() const
{
return (int)m_matches.size();
}
void RegularExpression::removeMatch(int match_index,
string &text)
{
assert(match_index < matchCount());
MatchData *match_data = &m_matches[match_index];
text.erase(match_data->start_pos, match_data->end_pos - match_data->start_pos + 1);
}
void RegularExpression::replaceMatch(int match_index,
string &text,
const string &replace_with)
{
assert(match_index < matchCount());
MatchData *match_data = &m_matches[match_index];
text.replace(match_data->start_pos, match_data->end_pos - match_data->start_pos + 1, replace_with);
}
void RegularExpression::getMatch(int match_index,
MatchData &match_data)
{
assert(match_index < matchCount());
match_data = m_matches[match_index];
}
const string &RegularExpression::getMatch(int match_index,
string &matched_string)
{
assert(match_index < matchCount());
MatchData &md(m_matches[match_index]);
matched_string = m_text.substr(md.start_pos, md.end_pos - md.start_pos);
return matched_string;
}
const string &RegularExpression::getMatch(int match_index,
string &matched_string,
const string &default_value)
{
if (match_index < matchCount()) {
MatchData &md(m_matches[match_index]);
matched_string = m_text.substr(md.start_pos, md.end_pos - md.start_pos);
} else {
matched_string = default_value;
}
return matched_string;
}
ostream &RegularExpression::dumpMatches(ostream &out)
{
string buffer;
for (int i = 0; i < matchCount(); ++i) {
out << " " << i << ": " << getMatch(i, buffer) << endl;
}
return out;
}
|