Add thin wrapper around pcre.h

This commit is contained in:
Ai Lin Chia 2017-02-07 16:37:41 +01:00
parent 484d4e6ae7
commit 1def0b382a
3 changed files with 101 additions and 1 deletions

68
GbRegex.cpp Normal file

@ -0,0 +1,68 @@
#include "GbRegex.h"
#include "Log.h"
#include <string.h>
GbRegex::GbRegex(const char *pattern, int options, int study_options)
: m_pattern(pattern)
, m_pcre(NULL)
, m_extra(NULL)
, m_compile_options(options)
, m_compile_error(NULL)
, m_compile_error_offset(0)
, m_study_options(study_options)
, m_study_error(NULL) {
m_pcre = pcre_compile(m_pattern.c_str(), m_compile_options, &m_compile_error, &m_compile_error_offset, NULL);
if (m_pcre) {
m_extra = pcre_study(m_pcre, m_study_options, &m_study_error);
if (m_study_error) {
log(LOG_INFO, "GbRegex: pcre_study error='%s'", m_study_error);
}
} else {
if (m_compile_error) {
log(LOG_INFO, "GbRegex: pcre_compile error='%s'", m_compile_error);
}
}
}
GbRegex::GbRegex(const GbRegex& rhs) {
m_pattern = rhs.m_pattern;
m_compile_options = rhs.m_compile_options;
m_study_options = rhs.m_study_options;
m_pcre = pcre_compile(m_pattern.c_str(), m_compile_options, &m_compile_error, &m_compile_error_offset, NULL);
if (m_pcre) {
m_extra = pcre_study(m_pcre, m_study_options, &m_study_error);
if (m_study_error) {
log(LOG_INFO, "GbRegex: pcre_study error='%s'", m_study_error);
}
} else {
if (m_compile_error) {
log(LOG_INFO, "GbRegex: pcre_compile error='%s'", m_compile_error);
}
}
}
GbRegex::~GbRegex() {
if (m_extra) {
pcre_free_study(m_extra);
}
if (m_pcre) {
pcre_free(m_pcre);
}
}
bool GbRegex::match(const char *subject, int options) const {
if (hasError()) {
log(LOG_WARN, "GbRegex: has error during compilation(%s) or study(%s)", m_compile_error, m_study_error);
return false;
}
return (pcre_exec(m_pcre, m_extra, subject, strlen(subject), 0, options, NULL, 0) >= 0);
}
bool GbRegex::hasError() const {
return (m_compile_error != NULL || m_study_error != NULL);
}

31
GbRegex.h Normal file

@ -0,0 +1,31 @@
#ifndef GB_GBREGEX_H
#define GB_GBREGEX_H
#include <pcre.h>
#include <string>
class GbRegex {
public:
GbRegex(const char *pattern, int options = 0, int study_options = 0);
GbRegex(const GbRegex &rhs);
~GbRegex();
bool match(const char *subject, int options = 0) const;
bool hasError() const;
private:
std::string m_pattern;
pcre *m_pcre;
pcre_extra *m_extra;
int m_compile_options;
const char *m_compile_error;
int m_compile_error_offset;
int m_study_options;
const char *m_study_error;
};
#endif //GB_GBREGEX_H

@ -73,6 +73,7 @@ OBJS_O3 = \
GbMakePath.o \
GbUtil.o \
GbCompress.o \
GbRegex.o \
OBJS = $(OBJS_O0) $(OBJS_O1) $(OBJS_O2) $(OBJS_O3)
@ -222,7 +223,7 @@ CPPFLAGS += -Wno-format-nonliteral
endif
LIBS = -lm -lpthread -lssl -lcrypto -lz
LIBS = -lm -lpthread -lssl -lcrypto -lz -lpcre
# to build static libiconv.a do a './configure --enable-static' then 'make' in the iconv directory