pcre++.cc Source File

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   _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; /* remove pcre++ flag before feeding _flags to pcre */
    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  * Destructor
    00119  */
    00120 Pcre::~Pcre() {
    00121   /* avoid deleting of uninitialized pointers */
    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  * operator= definitions
    00141  */
    00142 const Pcre& Pcre::operator = (const string& expression) {
    00143   /* reset the object and re-intialize it */
    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  * mem resetting methods
    00171  */
    00172 void Pcre::zero() {
    00173   /* what happens if p_pcre is already allocated? hm ... */
    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  * accessing the underlying implementation
    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  * support stuff
    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  * setting current locale
    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 }