From 9788056af63e073607897559997a44272b9a5cef Mon Sep 17 00:00:00 2001 From: Pixel Date: Thu, 29 Nov 2001 01:03:21 +0000 Subject: Regex done! --- include/ReadJob.h | 3 ++- include/Regex.h | 11 ++++++++++- lib/ReadJob.cc | 4 ++-- lib/Regex.cc | 26 +++++++++++++++++++++++++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/include/ReadJob.h b/include/ReadJob.h index ae0eef7..7d4bfc7 100644 --- a/include/ReadJob.h +++ b/include/ReadJob.h @@ -8,13 +8,14 @@ class ReadJob : public Task { public: - ReadJob(Handle *, Handle *); + ReadJob(Handle *, Handle *, const Regex & = empty); virtual ~ReadJob(); virtual int Do() throw (GeneralException); virtual String GetName(); private: Handle * s, * d; + const Regex * regex; }; #else diff --git a/include/Regex.h b/include/Regex.h index 6786041..1fc7197 100644 --- a/include/Regex.h +++ b/include/Regex.h @@ -3,13 +3,22 @@ #ifdef __cplusplus #include +#include + +#include class Regex : public Base { public: - + Regex(const String &, int = REG_EXTENDED | REG_ICASE, int = 0) throw (GeneralException); + ~Regex(); + bool Match(const String &) const; private: + regex_t preg; + int eflags; }; +extern Regex empty, any; + #else #error This only works with a C++ compiler #endif diff --git a/lib/ReadJob.cc b/lib/ReadJob.cc index d5cabca..a29f641 100644 --- a/lib/ReadJob.cc +++ b/lib/ReadJob.cc @@ -1,7 +1,7 @@ #include "ReadJob.h" #include "HttpServ.h" -ReadJob::ReadJob(Handle * as, Handle * ad) : s(as), d(ad) { +ReadJob::ReadJob(Handle * as, Handle * ad, const Regex & aregex) : s(as), d(ad), regex(&aregex) { s->SetNonBlock(); d->SetNonBlock(); WaitFor(s, W4_READING); @@ -31,7 +31,7 @@ int ReadJob::Do() throw (GeneralException) { Suspend(TASK_ON_HOLD); } current = 0; - if (buff == "") return TASK_DONE; + if (regex->Match(buff)) return TASK_DONE; } if (!s->IsClosed()) { diff --git a/lib/Regex.cc b/lib/Regex.cc index 2ef2c8e..9b70d68 100644 --- a/lib/Regex.cc +++ b/lib/Regex.cc @@ -1 +1,25 @@ -#include "Regex.h" \ No newline at end of file +#include "Regex.h" + +char t[1024]; + +Regex empty("$^"), any(".*"); + +Regex::Regex(const String & regex, int cflags, int aeflags) throw (GeneralException) : eflags(aeflags) { + int r; + if ((r = regcomp(&preg, regex.to_charp(), cflags | REG_NOSUB))) { + regerror(r, &preg, t, sizeof(t)); + throw GeneralException(String("Regex \"") + regex + "\" failed to compile: " + t + "\n"); + } +} + +Regex::~Regex() { + regfree(&preg); +} + +bool Regex::Match(const String & s) const { + if (regexec(&preg, s.to_charp(), 0, 0, eflags)) { + return false; + } else { + return true; + } +} -- cgit v1.2.3