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

search.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  * the search interface to pcre
00049  */
00050 
00051 
00052 /*
00053  * compile the expression
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     /* umh, that's odd, the parser should not fail at all */
00061     string Error = err_str;
00062     throw exception("pcre_compile(..) failed: " + Error + " at: " + _expression.substr(erroffset));
00063   }
00064 
00065   /* calculate the number of substrings we are willing to catch */
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; /* see "man pcre" for the exact formula */
00070   }
00071   else {
00072     throw exception(info);
00073   }
00074   reset();
00075 }
00076 
00077 
00078 
00079 
00080 /*
00081  * API methods
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     /* no match at all */
00104     __pcredebug << " - no match" << endl;
00105     return false;
00106   }
00107   else if(num == 0) {
00108     /* vector too small, there were too many substrings in stuff */
00109     __pcredebug << " - too many substrings" << endl;
00110     return false;
00111   }
00112   else if(num == 1) {
00113     /* we had a match, but without substrings */
00114     __pcredebug << " - match without substrings" << endl;
00115     did_match = true;
00116     num_matches = 0;
00117     return true;
00118   }
00119   else if(num > 1) {
00120     /* we had matching substrings */
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     /* some other uncommon error occured */
00146     __pcredebug << " - uncommon error" << endl;
00147     return false;
00148   }
00149 }

Generated on Wed Aug 25 01:38:04 2004 for PCRE++ by doxygen1.3-rc3