BESRegex.cc
Go to the documentation of this file.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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <config.h>
00036
00037 #ifndef WIN32
00038 #include <alloca.h>
00039 #endif
00040
00041 #include <sys/types.h>
00042 #include <regex.h>
00043
00044 #include <cstdlib>
00045 #include <new>
00046 #include <string>
00047 #include <stdexcept>
00048
00049 #include "BESRegex.h"
00050 #include "BESInternalError.h"
00051 #include "BESScrub.h"
00052
00053 using namespace std;
00054
00055 void
00056 BESRegex::init(const char *t)
00057 {
00058 d_preg = static_cast<void*>(new regex_t);
00059 int result = regcomp(static_cast<regex_t*>(d_preg), t, REG_EXTENDED);
00060
00061 if (result != 0) {
00062 size_t msg_len = regerror(result, static_cast<regex_t*>(d_preg),
00063 static_cast<char*>(NULL),
00064 static_cast<size_t>(0));
00065 char *msg = new char[msg_len+1];
00066 regerror(result, static_cast<regex_t*>(d_preg), msg, msg_len);
00067 string err = string( "BESRegex error: " ) + string( msg ) ;
00068 BESInternalError e( err, __FILE__, __LINE__ ) ;
00069 delete[] msg;
00070 throw e;
00071 }
00072 }
00073
00074 BESRegex::~BESRegex()
00075 {
00076 regfree(static_cast<regex_t*>(d_preg));
00077 delete static_cast<regex_t*>(d_preg); d_preg = 0;
00078
00079 }
00080
00084 BESRegex::BESRegex(const char* t)
00085 {
00086 init(t);
00087 }
00088
00091 BESRegex::BESRegex(const char* t, int)
00092 {
00093 init(t);
00094 }
00095
00102 int
00103 BESRegex::match(const char* s, int len, int pos)
00104 {
00105 regmatch_t pmatch[len];
00106 string ss = s;
00107
00108 int result = regexec(static_cast<regex_t*>(d_preg),
00109 ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
00110 if (result == REG_NOMATCH)
00111 return -1;
00112
00113 return pmatch[0].rm_eo - pmatch[0].rm_so;
00114 }
00115
00126 int
00127 BESRegex::search(const char* s, int len, int& matchlen, int pos)
00128 {
00129
00130 if (!BESScrub::size_ok(sizeof(regmatch_t), len+1))
00131 return -1;
00132
00133
00134 regmatch_t *pmatch = new regmatch_t[len+1];
00135 string ss = s;
00136
00137 int result = regexec(static_cast<regex_t*>(d_preg),
00138 ss.substr(pos, len-pos).c_str(), len, pmatch, 0);
00139 if (result == REG_NOMATCH) {
00140 delete[] pmatch; pmatch = 0;
00141 return -1;
00142 }
00143
00144
00145 int m = 0;
00146 for (int i = 1; i < len; ++i)
00147 if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
00148 m = i;
00149
00150 matchlen = pmatch[m].rm_eo - pmatch[m].rm_so;
00151 int matchpos = pmatch[m].rm_so;
00152
00153 delete[] pmatch; pmatch = 0;
00154 return matchpos;
00155 }
00156