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

pcre++.h

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  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 #ifndef HAVE_PCRE_PP_H
00042 #define HAVE_PCRE_PP_H
00043 
00044 #include <string>
00045 #include <sstream>
00046 #include <vector>
00047 #include <stdexcept>
00048 
00049 extern "C" {
00050   #include <pcre.h>
00051 }
00052 
00053 
00054 using namespace std;
00055 
00057 typedef vector<string> Array;
00058 
00060 typedef vector<string>::iterator ArrayIterator;
00061 
00089 class Pcre {
00090  private:
00091   string _expression;        /* the given regular expression */
00092   unsigned int _flags;       /* the given flags, 0 if not defined */
00093   bool case_t, global_t;     /* internal compile flags, used by replace() and split() */
00094   pcre *p_pcre;              /* pcre object pointer */
00095   pcre_extra *p_pcre_extra;  /* stuff required by pcre lib */
00096   int sub_len;
00097   int *sub_vec;
00098   int erroffset;
00099   char *err_str;
00100   Array *resultset;          /* store substrings, if any */
00101 
00102   /* reset all counters and free objects, prepare for another search */
00103   void reset();
00104 
00105   /* compile the pattern */
00106   void Compile(int flags);
00107 
00108   /* do the actual search, will be called by the public ::search(..) methods */
00109   bool dosearch(const string& stuff, int OffSet);
00110 
00111   /* do the actual split() job, called by the various wrapper split() methods */
00112   Array _split(const string& piece, int limit, int start_offset, int end_offset);
00113   
00114   /* replace $1 .. $n with the corresponding substring, used by replace() */
00115   string _replace_vars(const string& piece);
00116 
00117   /* init pointers with NULL */
00118   void zero();
00119 
00120  public:
00121 
00139   class exception : public runtime_error {
00140   private:
00141     string translate(int num) {
00142       string msg;
00143       switch(num) {
00144       case -1: msg = "PCRE_ERROR_NOMATCH";      break;
00145       case -2: msg = "PCRE_ERROR_NULL";         break;
00146       case -3: msg = "PCRE_ERROR_BADOPTION";    break;
00147       case -4: msg = "PCRE_ERROR_BADMAGIC";     break;
00148       case -5: msg = "PCRE_ERROR_UNKNOWN_NODE"; break;
00149       case -6: msg = "PCRE_ERROR_NOMEMORY";     break;
00150       case -7: msg = "PCRE_ERROR_NOSUBSTRING";  break;
00151         // pcre4-HINT: add PCRE_ERROR_MATCHLIMIT support
00152       }
00153       return msg;
00154     }
00155   public:
00156     exception(const string & msg) : runtime_error(msg) { }
00157     exception(int num) : runtime_error(translate(num)) { }
00158   };
00159 
00160 
00161   bool did_match;            
00162   int  num_matches;          
00175   Pcre();
00176 
00185   Pcre(const string& expression);
00186 
00212   Pcre(const string& expression, const string& flags);
00213 
00214   // pcre4-HINT: add another constructor with match_limit support (pcre_extra data to pcre_exec)
00215 
00223   Pcre(const Pcre &P);
00224 
00235   const Pcre& operator = (const string& expression); 
00236 
00249   const Pcre& operator = (const Pcre &P);
00250 
00256   ~Pcre();
00257 
00264   bool search(const string& stuff);
00265 
00273   bool search(const string& stuff, int OffSet);
00274 
00279   Array* get_sub_strings();
00280 
00295   string get_match(int pos);
00296 
00317   int get_match_start(int pos);
00318 
00339   int get_match_end(int pos);
00340 
00341 
00342 
00343 
00362   int get_match_start();
00363 
00383   int get_match_end();
00384 
00385 
00386 
00387 
00395   size_t get_match_length(int pos);
00396 
00401   bool matched() { return did_match; };
00402 
00406   int  matches() { return num_matches; };
00407 
00420   Array split(const string& piece);
00421 
00435   Array split(const string& piece, int limit);
00436 
00451   Array split(const string& piece, int limit, int start_offset);
00452 
00468   Array split(const string& piece, int limit, int start_offset, int end_offset);
00469 
00483   Array split(const string& piece, vector<int> positions);
00484 
00493   string replace(const string& piece, const string& with);
00494 }; 
00495 
00496 #endif

Generated on Sun Feb 23 17:57:14 2003 for PCRE++ by doxygen1.3-rc3