Anope IRC Services  Version 2.0
m_regex_posix.cpp
Go to the documentation of this file.
1 #include "module.h"
2 #include <sys/types.h>
3 #include <regex.h>
4 
5 class POSIXRegex : public Regex
6 {
7  regex_t regbuf;
8 
9  public:
10  POSIXRegex(const Anope::string &expr) : Regex(expr)
11  {
12  int err = regcomp(&this->regbuf, expr.c_str(), REG_EXTENDED | REG_NOSUB);
13  if (err)
14  {
15  char buf[BUFSIZE];
16  regerror(err, &this->regbuf, buf, sizeof(buf));
17  regfree(&this->regbuf);
18  throw RegexException("Error in regex " + expr + ": " + buf);
19  }
20  }
21 
23  {
24  regfree(&this->regbuf);
25  }
26 
27  bool Matches(const Anope::string &str)
28  {
29  return regexec(&this->regbuf, str.c_str(), 0, NULL, 0) == 0;
30  }
31 };
32 
34 {
35  public:
36  POSIXRegexProvider(Module *creator) : RegexProvider(creator, "regex/posix") { }
37 
39  {
40  return new POSIXRegex(expression);
41  }
42 };
43 
44 class ModuleRegexPOSIX : public Module
45 {
47 
48  public:
49  ModuleRegexPOSIX(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR),
50  posix_regex_provider(this)
51  {
52  this->SetPermanent(true);
53  }
54 };
55 
ModuleRegexPOSIX(const Anope::string &modname, const Anope::string &creator)
bool Matches(const Anope::string &str)
POSIXRegex(const Anope::string &expr)
#define BUFSIZE
Definition: services.h:18
Definition: regexpr.h:28
#define anope_override
Definition: services.h:56
#define MODULE_INIT(x)
Definition: modules.h:45
const char * c_str() const
Definition: anope.h:117
Regex * Compile(const Anope::string &expression) anope_override
void SetPermanent(bool state)
Definition: module.cpp:84
POSIXRegexProvider posix_regex_provider
POSIXRegexProvider(Module *creator)
Definition: modules.h:163
regex_t regbuf