// Zoe AIM Away Message RSS Feed Generator // // Douglas Thrift // // $Id$ #include "Matcher.hpp" Matcher::Matcher(const ext::String& expression) : expression(NULL) { (*this)(expression); } Matcher::~Matcher() { if (expression != NULL) { pcre_free(expression); } } bool Matcher::match(const ext::String& stuff) { substrings.clear(); if (expression != NULL) { int length; pcre_fullinfo(expression, NULL, PCRE_INFO_CAPTURECOUNT, &length); int* substrings(new int[(++length *= 3)]); int count(pcre_exec(expression, NULL, stuff.c_str(), stuff.length(), 0, 0, substrings, length)); if (count > 0) { char* substring(new char[stuff.length() + 1]); for (int index(0); index < count; ++index) { pcre_copy_substring(stuff.c_str(), substrings, count, index, substring, stuff.length() + 1); this->substrings.push_back(substring); } delete [] substring; delete [] substrings; return true; } delete [] substrings; } return false; } Matcher& Matcher::operator()(const ext::String& expression) { substrings.clear(); if (this->expression != NULL) { pcre_free(this->expression); } const char* error; int offset; this->expression = pcre_compile(expression.c_str(), 0, &error, &offset, NULL); return *this; }