Anope IRC Services  Version 2.0
m_regex_pcre.cpp
Go to the documentation of this file.
1 /* RequiredLibraries: pcre */
2 /* RequiredWindowsLibraries: libpcre */
3 
4 #include "module.h"
5 #include <pcre.h>
6 
7 class PCRERegex : public Regex
8 {
9  pcre *regex;
10 
11  public:
12  PCRERegex(const Anope::string &expr) : Regex(expr)
13  {
14  const char *error;
15  int erroffset;
16  this->regex = pcre_compile(expr.c_str(), PCRE_CASELESS, &error, &erroffset, NULL);
17  if (!this->regex)
18  throw RegexException("Error in regex " + expr + " at offset " + stringify(erroffset) + ": " + error);
19  }
20 
22  {
23  pcre_free(this->regex);
24  }
25 
26  bool Matches(const Anope::string &str)
27  {
28  return pcre_exec(this->regex, NULL, str.c_str(), str.length(), 0, 0, NULL, 0) > -1;
29  }
30 };
31 
33 {
34  public:
35  PCRERegexProvider(Module *creator) : RegexProvider(creator, "regex/pcre") { }
36 
38  {
39  return new PCRERegex(expression);
40  }
41 };
42 
43 class ModuleRegexPCRE : public Module
44 {
46 
47  public:
48  ModuleRegexPCRE(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR),
49  pcre_regex_provider(this)
50  {
51  this->SetPermanent(true);
52  }
53 };
54 
ModuleRegexPCRE(const Anope::string &modname, const Anope::string &creator)
PCRERegexProvider(Module *creator)
Regex * Compile(const Anope::string &expression) anope_override
Definition: regexpr.h:28
size_type length() const
Definition: anope.h:131
#define anope_override
Definition: services.h:56
#define MODULE_INIT(x)
Definition: modules.h:45
PCRERegex(const Anope::string &expr)
PCRERegexProvider pcre_regex_provider
Anope::string stringify(const T &x)
Definition: anope.h:710
const char * c_str() const
Definition: anope.h:117
void SetPermanent(bool state)
Definition: module.cpp:84
pcre * regex
Definition: m_regex_pcre.cpp:9
Definition: modules.h:163
bool Matches(const Anope::string &str)