Main Page   Namespace List   Compound List   File List   Compound Members   File Members  

pcre++.cc

Go to the documentation of this file.
00001 /*
00002  *
00003  *  This file  is part of the PCRE++ Class Library.
00004  *
00005  *  By  accessing  this software,  PCRE++, you  are  duly informed
00006  *  of and agree to be  bound  by the  conditions  described below
00007  *  in this notice:
00008  *
00009  *  This software product,  PCRE++,  is developed by Thomas Linden
00010  *  and copyrighted (C) 2002-2003 by Thomas Linden,with all rights 
00011  *  reserved.
00012  *
00013  *  There  is no charge for PCRE++ software.  You can redistribute
00014  *  it and/or modify it under the terms of the GNU  Lesser General
00015  *  Public License, which is incorporated by reference herein.
00016  *
00017  *  PCRE++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,
00018  *  OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that
00019  *  the use of it will not infringe on any third party's intellec-
00020  *  tual property rights.
00021  *
00022  *  You should have received a copy of the GNU Lesser General Public
00023  *  License along with PCRE++.  Copies can also be obtained from:
00024  *
00025  *    http://www.gnu.org/licenses/lgpl.txt
00026  *
00027  *  or by writing to:
00028  *
00029  *  Free Software Foundation, Inc.
00030  *  59 Temple Place, Suite 330
00031  *  Boston, MA 02111-1307
00032  *  USA
00033  *
00034  *  Or contact:
00035  *
00036  *   "Thomas Linden" <tom@daemon.de>
00037  *
00038  *
00039  */
00040 
00041 
00042 #include "pcre++.h"
00043 
00044 using namespace std;
00045 using namespace pcrepp;
00046 
00047 /*
00048  * CONSTRUCTORS and stuff
00049  */
00050 Pcre::Pcre(const string& expression) {
00051   _expression   = expression;
00052   _flags        = 0;
00053   case_t = global_t = false;
00054   zero();
00055   Compile(0);
00056 }
00057 
00058 Pcre::Pcre(const string& expression, const string& flags) {
00059   _expression   = expression;
00060   unsigned int FLAG = 0;
00061 
00062   for(unsigned int flag=0; flag<flags.length(); flag++) {
00063     switch(flags[flag]) {
00064     case 'i': FLAG |= PCRE_CASELESS;  case_t = true;   break;
00065     case 'm': FLAG |= PCRE_MULTILINE;                  break;
00066     case 's': FLAG |= PCRE_DOTALL;                     break;
00067     case 'x': FLAG |= PCRE_EXTENDED;                   break;
00068     case 'g':                         global_t = true; break;
00069     }
00070   }
00071 
00072   _flags = FLAG;
00073 
00074   zero();
00075   Compile(FLAG);
00076 }
00077 
00078 Pcre::Pcre(const string& expression, unsigned int flags) {
00079   _expression = expression;
00080   _flags = flags;
00081 
00082   if((_flags & PCRE_CASELESS) != 0)
00083     case_t = true;
00084 
00085   if((_flags & PCRE_GLOBAL) != 0) {
00086     global_t = true;
00087     _flags = _flags - PCRE_GLOBAL; /* remove pcre++ flag before feeding _flags to pcre */
00088   }
00089 
00090   zero();
00091   Compile(_flags);
00092 }
00093 
00094 Pcre::Pcre(const Pcre &P) {
00095   _expression = P._expression;
00096   _flags      = P._flags;
00097   case_t      = P.case_t;
00098   global_t    = P.global_t;
00099   zero();
00100   Compile(_flags);
00101 }
00102 
00103 Pcre::Pcre() {
00104   zero();
00105 }
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 /*
00114  * Destructor
00115  */
00116 Pcre::~Pcre() {
00117   /* avoid deleting of uninitialized pointers */
00118   if (p_pcre != NULL) {
00119     pcre_free(p_pcre);
00120   }
00121   if (p_pcre_extra != NULL) {
00122     pcre_free(p_pcre_extra);
00123   }
00124   if(sub_vec != NULL) {
00125     delete[] sub_vec;
00126   }
00127   if(resultset != NULL) {
00128     delete resultset;
00129   }
00130 }
00131 
00132 
00133 
00134 
00135 /*
00136  * operator= definitions
00137  */
00138 const Pcre& Pcre::operator = (const string& expression) {
00139   /* reset the object and re-intialize it */
00140   reset();
00141   _expression = expression;
00142   _flags      = 0;
00143   case_t = global_t = false;
00144   Compile(0);
00145   return *this;
00146 }
00147 
00148 
00149 const Pcre& Pcre::operator = (const Pcre &P) {
00150   reset();
00151   _expression = P._expression;
00152   _flags      = P._flags;
00153   case_t      = P.case_t;
00154   global_t    = P.global_t;
00155   zero();
00156   Compile(_flags);
00157   return *this;
00158 }
00159 
00160 
00161 
00162 
00163 
00164 
00165 /*
00166  * mem resetting methods
00167  */
00168 void Pcre::zero() {
00169   /* what happens if p_pcre is already allocated? hm ... */
00170   p_pcre_extra = NULL;
00171   p_pcre       = NULL;
00172   sub_vec      = NULL;
00173   resultset    = NULL;
00174   err_str      = NULL;
00175   num_matches  = -1;
00176   tables       = NULL;
00177 }
00178 
00179 void Pcre::reset() {
00180   did_match   = false;
00181   num_matches = -1;
00182 }
00183 
00184 
00185 
00186 
00187 
00188 /*
00189  * accessing the underlying implementation
00190  */
00191 pcre* Pcre::get_pcre() {
00192   return p_pcre;
00193 }
00194 
00195 pcre_extra* Pcre::get_pcre_extra() {
00196   return p_pcre_extra;
00197 }
00198 
00199 
00200 
00201 
00202 
00203 /*
00204  * support stuff
00205  */
00206 void Pcre::study() {
00207   p_pcre_extra = pcre_study(p_pcre, 0, (const char **)(&err_str));
00208   if(err_str != NULL)
00209     throw exception("pcre_study(..) failed: " + string(err_str));
00210 }
00211 
00212 
00213 /*
00214  * setting current locale
00215  */
00216 bool Pcre::setlocale(const char* locale) {
00217   if (std::setlocale(LC_CTYPE, locale) == NULL) return false;
00218   tables = pcre_maketables();
00219   return true;
00220 }

Generated on Wed Jun 16 00:22:32 2004 for PCRE++ by doxygen1.3-rc3