37 #include <sys/types.h>
66 Regex::init(
const char *t)
69 DBG( cerr <<
"Regex::init() - BEGIN" << endl);
71 DBG( cerr <<
"Regex::init() - creating new regex..." << endl);
72 d_preg =
static_cast<void*
>(
new regex_t);
74 DBG( cerr <<
"Regex::init() - Calling regcomp()..." << endl);
75 int result = regcomp(
static_cast<regex_t*
>(d_preg), t, REG_EXTENDED);
78 DBG( cerr <<
"Regex::init() - Call to regcomp FAILED" << endl);
79 DBG( cerr <<
"Regex::init() - Calling regerror()..." << endl);
80 size_t msg_len = regerror(result,
static_cast<regex_t*
>(d_preg),
81 static_cast<char*
>(NULL),
82 static_cast<size_t>(0));
84 DBG( cerr <<
"Regex::init() - Creating message" << endl);
85 vector<char> msg(msg_len+1);
87 DBG( cerr <<
"Regex::init() - Calling regerror() again..." << endl);
88 regerror(result,
static_cast<regex_t*
>(d_preg), &msg[0], msg_len);
89 DBG( cerr <<
"Regex::init() - Throwing libdap::Error" << endl);
90 throw Error(
string(
"Regex error: ") +
string(&msg[0]));
94 DBG( cerr <<
"Regex::init() - Call to regcomp() SUCCEEDED" << endl);
95 DBG( cerr <<
"Regex::init() - END" << endl);
103 Regex::init(
const string &t)
109 #if !USE_CPP_11_REGEX
112 regfree(
static_cast<regex_t*
>(d_preg));
113 delete static_cast<regex_t*
>(d_preg); d_preg = 0;
121 Regex::Regex(
const char* t)
128 Regex::Regex(
const char* t,
int)
141 Regex::match(
const char *s,
int len,
int pos)
const
143 #if !USE_CPP_11_REGEX
147 regmatch_t *pmatch =
new regmatch_t[len+1];
150 int result = regexec(
static_cast<regex_t*
>(d_preg),
151 ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
153 if (result == REG_NOMATCH)
156 matchnum = pmatch[0].rm_eo - pmatch[0].rm_so;
158 delete[] pmatch; pmatch = 0;
163 throw Error(
"Position exceed length in Regex::match()");
166 auto target = string(s+pos, len-pos);
167 bool found = regex_search(target, match, d_exp);
169 return (
int)match.length();
181 Regex::match(
const string &s)
const
185 bool found = regex_search(s, match, d_exp);
187 return (
int)match.length();
191 return match(s.c_str(), s.length(), 0);
206 Regex::search(
const char *s,
int len,
int& matchlen,
int pos)
const
208 #if !USE_CPP_11_REGEX
210 if (!
size_ok(
sizeof(regmatch_t), len+1))
221 regmatch_t *pmatch =
new regmatch_t[len+1];
224 int result = regexec(
static_cast<regex_t*
>(d_preg),
225 ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
226 if (result == REG_NOMATCH) {
227 delete[] pmatch; pmatch = 0;
233 for (
int i = 1; i < len; ++i)
234 if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
237 matchlen = pmatch[m].rm_eo - pmatch[m].rm_so;
238 int matchpos = pmatch[m].rm_so;
240 delete[] pmatch; pmatch = 0;
247 auto target = string(s+pos, len-pos);
248 bool found = regex_search(target, match, d_exp);
249 matchlen = (int)match.length();
251 return (
int)match.position();
264 Regex::search(
const string &s,
int& matchlen)
const
268 bool found = regex_search(s, match, d_exp);
269 matchlen = (int)match.length();
271 return (
int)match.position();
276 return search(s.c_str(), s.length(), matchlen, 0);
A class for error processing.
top level DAP object to house generic methods
bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array.