#include #include #include #include "re.h" #define OVECCOUNT 30 /* should be a multiple of 3 */ regex::regex() { found = false; } regex::~regex() { } regex& regex::operator=(const regex& re) { if (this == &re) return *this; for(unsigned int i = 0; i < re.matches.size(); i++ ) { struct pcre_res result; result.pos = re.matches[i].pos; result.len = re.matches[i].len;; result.sub = re.matches[i].sub; matches.push_back(result); } return *this; } regex::regex(const regex& re) { for(unsigned int i = 0; i < re.matches.size(); i++ ) { matches.push_back(re.matches[i]); } } regex pcre_match(string pat, string tar ) { return pcre_match_num(pat, tar, false); } regex pcre_match_num(string p, string t, bool global ) { const char* pat = p.c_str(); const char* tar = t.c_str(); char* argv[2]; argv[0] = "regex"; regex results; pcre *re; const char *error; char* pattern; char* subject = (char*)malloc(strlen(tar) + 1); strcpy(subject,tar); string subject_s = subject; unsigned char *name_table; int erroffset; int find_all; int namecount; int name_entry_size; int ovector[OVECCOUNT]; int subject_length; int rc, i; find_all = 0; if(global) { find_all = 1; } pattern = (char*)pat; subject_length = (int)strlen(subject); re = pcre_compile( pattern, PCRE_DOTALL, &error, &erroffset, NULL); if (re == NULL) { return results; } rc = pcre_exec( re, NULL, subject, subject_length, 0, 0, ovector, OVECCOUNT); if (rc < 0) { switch(rc) { case PCRE_ERROR_NOMATCH: ; break; default: printf("Matching error %d\n", rc); break; } return results; } if (rc == 0) { rc = OVECCOUNT/3; } for (i = 0; i < rc; i++) { int substring_length = ovector[2*i+1] - ovector[2*i]; pcre_res this_res; this_res.pos = ovector[2*i]; this_res.len = substring_length; this_res.sub = ""; if(this_res.len > 0) { this_res.sub = subject_s.substr(this_res.pos, this_res.len); } if(i == 0) { results.global_matches.push_back(this_res); } results.found = true; results.matches.push_back(this_res); } (void)pcre_fullinfo(re, NULL, PCRE_INFO_NAMECOUNT, &namecount); if (namecount <= 0) {;} else { unsigned char *tabptr; (void)pcre_fullinfo( re, NULL, PCRE_INFO_NAMETABLE, &name_table); (void)pcre_fullinfo( re, NULL, PCRE_INFO_NAMEENTRYSIZE, &name_entry_size); tabptr = name_table; for (i = 0; i < namecount; i++) { tabptr += name_entry_size; } } if (!find_all) { return results; } for (;;) { //printf("loop\n"); int options = 0; /* Normally no options */ int start_offset = ovector[1]; /* Start at end of previous match */ if (ovector[0] == ovector[1]) { if (ovector[0] == subject_length) { break; } options = PCRE_NOTEMPTY | PCRE_ANCHORED; } rc = pcre_exec( re, NULL, subject, subject_length, start_offset, options, ovector, OVECCOUNT); if (rc == PCRE_ERROR_NOMATCH) { if (options == 0) { break; } ovector[1] = start_offset + 1; //printf("nomatch\n"); continue; /* Go round the loop again */ } if (rc < 0) { return results; } if (rc == 0) { rc = OVECCOUNT/3; } for (i = 0; i < rc; i++) { int substring_length = ovector[2*i+1] - ovector[2*i]; pcre_res this_res; this_res.pos = ovector[2*i]; this_res.len = substring_length; this_res.sub = ""; //printf("pos is %d\n", this_res.pos); if(this_res.len > 0) { this_res.sub = subject_s.substr(this_res.pos, this_res.len); } if(i == 0) { //global_results.matches.push_back(this_res); results.global_matches.push_back(this_res); } /* results.matches.push_back(this_res); */ } if (namecount <= 0) { //printf(" "); } else { //printf("found\n"); unsigned char *tabptr = name_table; for (i = 0; i < namecount; i++) { tabptr += name_entry_size; } } } free(subject); return results; } string pcre_replace(string pattern, string target, string replacement) { //printf("pcre\n"); regex re = pcre_match_num(pattern.c_str(), target.c_str(), true ); for(unsigned int i=0;i