00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _Basics_RegExp
00022 #define _Basics_RegExp
00023
00024 #include <stddef.h>
00025 #include <regex.h>
00026
00027 #include "Basics.H"
00028
00029 namespace Basics
00030 {
00031
00032
00033 class RegExp
00034 {
00035 private:
00036
00037 regex_t parsed;
00038 public:
00039
00040 struct ParseError
00041 {
00042 const int code;
00043 const char *msg;
00044 const char *re;
00045 ParseError(int code, const char *msg, const char *re)
00046 : code(code), msg(msg), re(re) { }
00047 };
00048
00049 RegExp(const char *re,
00050
00051
00052
00053 int cflags = (REG_EXTENDED | REG_ICASE | REG_NOSUB))
00054 throw(RegExp::ParseError)
00055 {
00056 constructor_inner(re, cflags);
00057 }
00058
00059 RegExp(const Text &re,
00060 int cflags = (REG_EXTENDED | REG_ICASE | REG_NOSUB))
00061 throw(RegExp::ParseError)
00062 {
00063 constructor_inner(re.cchars(), cflags);
00064 }
00065
00066 ~RegExp()
00067 {
00068 regfree(&(this->parsed));
00069 }
00070
00071 inline bool match(const char *string,
00072
00073
00074
00075 size_t nmatch = 0, regmatch_t *pmatch = 0,
00076
00077
00078
00079 int eflags = 0)
00080 {
00081 return regexec(&(this->parsed), string, nmatch, pmatch, eflags) == 0;
00082 }
00083
00084 inline bool match(const Text &text,
00085 size_t nmatch = 0, regmatch_t *pmatch = 0,
00086 int eflags = 0)
00087 {
00088 return match(text.cchars(), nmatch, pmatch, eflags);
00089 }
00090
00091 inline bool operator()(const char *string,
00092 size_t nmatch = 0, regmatch_t *pmatch = 0,
00093 int eflags = 0)
00094 {
00095 return match(string, nmatch, pmatch, eflags);
00096 }
00097
00098 inline bool operator()(const Text &text,
00099 size_t nmatch = 0, regmatch_t *pmatch = 0,
00100 int eflags = 0)
00101 {
00102 return match(text.cchars(), nmatch, pmatch, eflags);
00103 }
00104
00105 inline size_t nsubs(void)
00106 {
00107 return parsed.re_nsub;
00108 }
00109
00110 private:
00111
00112
00113 RegExp(const RegExp &);
00114 void operator=(const RegExp &);
00115
00116
00117
00118 void constructor_inner(const char *re, int cflags)
00119 throw(RegExp::ParseError);
00120 };
00121 }
00122
00123 #endif