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 Pcre::Pcre(const string& expression) {
00051   _have_paren    = false;
00052   _expression   = expression;
00053   _flags        = 0;
00054   case_t = global_t = false;
00055   zero();
00056   Compile(0);
00057 }
00058 
00059 Pcre::Pcre(const string& expression, const string& flags) {
00060   _have_paren    = false;
00061   _expression   = expression;
00062   unsigned int FLAG = 0;
00063 
00064   for(unsigned int flag=0; flag<flags.length(); flag++) {
00065     switch(flags[flag]) {
00066     case 'i': FLAG |= PCRE_CASELESS;  case_t = true;   break;
00067     case 'm': FLAG |= PCRE_MULTILINE;                  break;
00068     case 's': FLAG |= PCRE_DOTALL;                     break;
00069     case 'x': FLAG |= PCRE_EXTENDED;                   break;
00070     case 'g':                         global_t = true; break;
00071     }
00072   }
00073 
00074   _flags = FLAG;
00075 
00076   zero();
00077   Compile(FLAG);
00078 }
00079 
00080 Pcre::Pcre(const string& expression, unsigned int flags) {
00081   _have_paren    = false;
00082   _expression = expression;
00083   _flags = flags;
00084 
00085   if((_flags & PCRE_CASELESS) != 0)
00086     case_t = true;
00087 
00088   if((_flags & PCRE_GLOBAL) != 0) {
00089     global_t = true;
00090     _flags = _flags - PCRE_GLOBAL; 
00091   }
00092 
00093   zero();
00094   Compile(_flags);
00095 }
00096 
00097 Pcre::Pcre(const Pcre &P) {
00098   _have_paren = P._have_paren;
00099   _expression = P._expression;
00100   _flags      = P._flags;
00101   case_t      = P.case_t;
00102   global_t    = P.global_t;
00103   zero();
00104   Compile(_flags);
00105 }
00106 
00107 Pcre::Pcre() {
00108   zero();
00109 }
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 
00120 Pcre::~Pcre() {
00121   
00122   if (p_pcre != NULL) {
00123     pcre_free(p_pcre);
00124   }
00125   if (p_pcre_extra != NULL) {
00126     pcre_free(p_pcre_extra);
00127   }
00128   if(sub_vec != NULL) {
00129     delete[] sub_vec;
00130   }
00131   if(resultset != NULL) {
00132     delete resultset;
00133   }
00134 }
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 const Pcre& Pcre::operator = (const string& expression) {
00143   
00144   reset();
00145   _expression = expression;
00146   _flags      = 0;
00147   case_t = global_t = false;
00148   Compile(0);
00149   return *this;
00150 }
00151 
00152 
00153 const Pcre& Pcre::operator = (const Pcre &P) {
00154   reset();
00155   _expression = P._expression;
00156   _flags      = P._flags;
00157   case_t      = P.case_t;
00158   global_t    = P.global_t;
00159   zero();
00160   Compile(_flags);
00161   return *this;
00162 }
00163 
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 void Pcre::zero() {
00173   
00174   p_pcre_extra = NULL;
00175   p_pcre       = NULL;
00176   sub_vec      = NULL;
00177   resultset    = NULL;
00178   err_str      = NULL;
00179   num_matches  = -1;
00180   tables       = NULL;
00181 }
00182 
00183 void Pcre::reset() {
00184   did_match   = false;
00185   num_matches = -1;
00186 }
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 pcre* Pcre::get_pcre() {
00196   return p_pcre;
00197 }
00198 
00199 pcre_extra* Pcre::get_pcre_extra() {
00200   return p_pcre_extra;
00201 }
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 void Pcre::study() {
00211   p_pcre_extra = pcre_study(p_pcre, 0, (const char **)(&err_str));
00212   if(err_str != NULL)
00213     throw exception("pcre_study(..) failed: " + string(err_str));
00214 }
00215 
00216 
00217 
00218 
00219 
00220 bool Pcre::setlocale(const char* locale) {
00221   if (std::setlocale(LC_CTYPE, locale) == NULL) return false;
00222   tables = pcre_maketables();
00223   return true;
00224 }