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 
00036 
00037 
00038 
00039 
00040 
00041 
00042 #include "pcre++.h"
00043 
00044 using namespace std;
00045 using namespace pcrepp;
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 void Pcre::Compile(int flags) {
00056   p_pcre       = pcre_compile((char *)_expression.c_str(), flags,
00057                               (const char **)(&err_str), &erroffset, tables);
00058 
00059   if(p_pcre == NULL) {
00060     
00061     string Error = err_str;
00062     throw exception("pcre_compile(..) failed: " + Error + " at: " + _expression.substr(erroffset));
00063   }
00064 
00065   
00066   int where;
00067   int info = pcre_fullinfo( p_pcre, p_pcre_extra, PCRE_INFO_CAPTURECOUNT, &where);
00068   if(info == 0) {
00069     sub_len = (where +2) * 3; 
00070   }
00071   else {
00072     throw exception(info);
00073   }
00074   reset();
00075 }
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 bool Pcre::search(const string& stuff, int OffSet){
00084   return dosearch(stuff, OffSet);
00085 }
00086 
00087 bool Pcre::search(const string& stuff){
00088   return dosearch(stuff, 0);
00089 }
00090 
00091 bool Pcre::dosearch(const string& stuff, int OffSet){
00092   reset();
00093   if (sub_vec != NULL)
00094     delete[] sub_vec;
00095 
00096   sub_vec = new int[sub_len];
00097   int num = pcre_exec(p_pcre, p_pcre_extra, (char *)stuff.c_str(),
00098                         (int)stuff.length(), OffSet, 0, (int *)sub_vec, sub_len);
00099 
00100   __pcredebug << "Pcre::dosearch(): pcre_exec() returned: " << num << endl;
00101 
00102   if(num < 0) {
00103     
00104     __pcredebug << " - no match" << endl;
00105     return false;
00106   }
00107   else if(num == 0) {
00108     
00109     __pcredebug << " - too many substrings" << endl;
00110     return false;
00111   }
00112   else if(num == 1) {
00113     
00114     __pcredebug << " - match without substrings" << endl;
00115     did_match = true;
00116     num_matches = 0;
00117     return true;
00118   }
00119   else if(num > 1) {
00120     
00121     if (resultset != NULL)
00122       delete resultset;
00123     resultset = new vector<string>;
00124     const char **stringlist;
00125     did_match = true;
00126     num_matches = num - 1;
00127 
00128     __pcredebug << " - match with " << num_matches << " substrings" << endl;
00129 
00130     int res = pcre_get_substring_list((char *)stuff.c_str(), sub_vec, num, &stringlist);
00131     if(res == 0) {
00132       __pcredebug << "Pcre::dosearch(): matched substrings: " << endl;
00133       for(int i=1; i<num; i++) {
00134         __pcredebug << " " << string(stringlist[i]) << endl;
00135         resultset->push_back(stringlist[i]);
00136       }
00137       pcre_free_substring_list(stringlist);
00138     }
00139     else {
00140       throw exception(res);
00141     }
00142     return true;
00143   }
00144   else {
00145     
00146     __pcredebug << " - uncommon error" << endl;
00147     return false;
00148   }
00149 }