00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <magic/mobject.h>
00026 #include <magic/mstring.h>
00027 #include <magic/mmagisupp.h>
00028 #include <magic/mpararr.h>
00029 #include <magic/mregexp.h>
00030
00031
00032
00033
00037
00038 BEGIN_NAMESPACE (MagiC);
00039
00040 RegExp::~RegExp () {
00041 if (regt) {
00042 regfree (regt);
00043 delete regt;
00044 regt=NULL;
00045 }
00046 }
00047
00048 int RegExp::make (const char* expr) {
00049 this->~RegExp ();
00050 regt = new regex_t;
00051
00052 if ((errcode = regcomp (regt, expr, REG_EXTENDED)))
00053 throw invalid_format (format ("%%%%%%RegExp Error:%s\n", (CONSTR) geterror ()));
00054
00055 return errcode;
00056 }
00057
00058 int RegExp::match (const char* str) {
00059 return !(errcode = regexec (regt, str, 0, NULL, REG_NOSUB));
00060 }
00061
00062 int RegExp::match (const String& str, Array<String>& results) {
00063 regmatch_t matches [20];
00064 errcode = regexec (regt, str, 20, matches, 0);
00065 if (errcode) {
00066 if (errcode == REG_NOMATCH)
00067 return 0;
00068 else
00069 throw invalid_format (geterror());
00070 }
00071
00072 int rescnt=0;
00073 for (; rescnt<19; rescnt++)
00074 if (matches[rescnt].rm_so>str.length() || matches[rescnt].rm_eo>str.length() ||
00075 matches[rescnt].rm_so<0 || matches[rescnt].rm_eo<0 ||
00076 matches[rescnt].rm_so > matches[rescnt].rm_eo)
00077 break;
00078
00079 results.resize (rescnt);
00080
00081 for (int i=0; i<rescnt; i++)
00082 results[i] = str.mid (matches[i].rm_so, matches[i].rm_eo-matches[i].rm_so);
00083
00084 return 1;
00085 }
00086
00087 String RegExp::geterror () const {
00088 char errbuf [256];
00089 regerror (errcode, regt, errbuf, 256);
00090 return errbuf;
00091 }
00092
00093 END_NAMESPACE;