diff options
Diffstat (limited to 'mask.cc')
-rw-r--r-- | mask.cc | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/mask.cc b/mask.cc new file mode 100644 index 00000000..a7352d12 --- /dev/null +++ b/mask.cc @@ -0,0 +1,49 @@ +#include "mask.h" +#include "debug.h" + +#include <pcre.h> + +mask_t::mask_t(const std::string& pat) : exclude(false) +{ + const char * p = pat.c_str(); + if (*p == '-') { + exclude = true; + p++; + while (std::isspace(*p)) + p++; + } + else if (*p == '+') { + p++; + while (std::isspace(*p)) + p++; + } + pattern = p; + + const char *error; + int erroffset; + regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS, + &error, &erroffset, NULL); + if (! regexp) + throw mask_error(std::string("Failed to compile regexp '") + pattern + "'"); +} + +mask_t::mask_t(const mask_t& m) : exclude(m.exclude), pattern(m.pattern) +{ + const char *error; + int erroffset; + regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS, + &error, &erroffset, NULL); + assert(regexp); +} + +mask_t::~mask_t() { + pcre_free((pcre *)regexp); +} + +bool mask_t::match(const std::string& str) const +{ + static int ovec[30]; + int result = pcre_exec((pcre *)regexp, NULL, + str.c_str(), str.length(), 0, 0, ovec, 30); + return result >= 0 && ! exclude; +} |