#include "regex-pcre.h" #include PCRERegex::PCRERegex(const QString & str) : _compiled(NULL), _extra(NULL), _has_matched(false) { pcre * _p; int options = 0; const char *errptr; int erroffset; unsigned char *tableptr; _p = pcre_compile(str.toAscii(), options, &errptr, &erroffset, NULL); if (_p == NULL) return; /* pcre compilation failure is not critical */ _extra = pcre_study(_p,0,&errptr); //printf("pcre_compile('%s') => %p\n", (const char*)str.toAscii(), _p); _compiled = _p; } PCRERegex::~PCRERegex() { if (_compiled) pcre_free(_compiled); } /* See $PRELUDE_LML/plugins/pcre/rule-regex.c */ int PCRERegex::match(const QString & text) { if (_compiled == NULL) return -1; int rc; rc = pcre_exec(_compiled, _extra, text.toAscii(), text.length(), 0, /* start at offset 0 */ 0, /* default options */ ovector, /* vector of integers for substring information */ 90); /* number of elements (NOT size in bytes) */ if (rc == -1) /* no match */ return -1; if (rc < 0) { /* other errors */ printf("ERROR pcre_exec(%s) => %d\n", (const char*)text.toAscii(), rc); return -1; } _has_matched = true; //printf("pcre_exec(%s) => %d\n", (const char*)text.toAscii(), rc); //printf("\tcomplete match: %d -> %d\n", ovector[0], ovector[1]); for (int i=1; i<= rc-1; i++) { int start = ovector[2*i]; int end = ovector[(2*i)+1]; QString m = text.mid(start,end-start); //printf("\tmatch %d: %d -> %d [%s]\n", i, start, end, (const char*)m.toAscii()); } return rc; }