36 #include <sys/types.h> 55 Regex::init(
const char *t)
57 DBG( cerr <<
"Regex::init() - BEGIN" << endl);
59 DBG( cerr <<
"Regex::init() - creating new regex..." << endl);
60 d_preg =
static_cast<void*
>(
new regex_t);
62 DBG( cerr <<
"Regex::init() - Calling regcomp()..." << endl);
63 int result = regcomp(static_cast<regex_t*>(d_preg), t, REG_EXTENDED);
66 DBG( cerr <<
"Regex::init() - Call to regcomp FAILED" << endl);
67 DBG( cerr <<
"Regex::init() - Calling regerror()..." << endl);
68 size_t msg_len = regerror(result, static_cast<regex_t*>(d_preg),
69 static_cast<char*>(NULL),
70 static_cast<size_t>(0));
72 DBG( cerr <<
"Regex::init() - Creating message" << endl);
73 vector<char> msg(msg_len+1);
75 DBG( cerr <<
"Regex::init() - Calling regerror() again..." << endl);
76 regerror(result, static_cast<regex_t*>(d_preg), &msg[0], msg_len);
77 DBG( cerr <<
"Regex::init() - Throwing libdap::Error" << endl);
78 throw Error(
string(
"Regex error: ") +
string(&msg[0]));
82 DBG( cerr <<
"Regex::init() - Call to regcomp() SUCCEEDED" << endl);
83 DBG( cerr <<
"Regex::init() - END" << endl);
88 regfree(static_cast<regex_t*>(d_preg));
89 delete static_cast<regex_t*
>(d_preg); d_preg = 0;
96 Regex::Regex(
const char* t)
103 Regex::Regex(
const char* t,
int)
115 Regex::match(
const char* s,
int len,
int pos)
120 regmatch_t *pmatch =
new regmatch_t[len+1];
123 int result = regexec(static_cast<regex_t*>(d_preg),
124 ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
126 if (result == REG_NOMATCH)
129 matchnum = pmatch[0].rm_eo - pmatch[0].rm_so;
131 delete[] pmatch; pmatch = 0;
147 Regex::search(
const char* s,
int len,
int& matchlen,
int pos)
150 if (!
size_ok(
sizeof(regmatch_t), len+1))
161 regmatch_t *pmatch =
new regmatch_t[len+1];
164 int result = regexec(static_cast<regex_t*>(d_preg),
165 ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
166 if (result == REG_NOMATCH) {
167 delete[] pmatch; pmatch = 0;
173 for (
int i = 1; i < len; ++i)
174 if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
177 matchlen = pmatch[m].rm_eo - pmatch[m].rm_so;
178 int matchpos = pmatch[m].rm_so;
180 delete[] pmatch; pmatch = 0;
bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array...
top level DAP object to house generic methods