Main Page   Compound List   File List   Compound Members   File Members  

pcre++.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *  $Id: pcre++.h,v 1.1.1.1 2001/12/30 04:01:36 zarahg Exp $
00004  * 
00005  *  This file  is part of the  NABOU  Intrusion Detection System.
00006  *
00007  *  By  accessing  this software,  NABOU, you  are  duly informed
00008  *  of and agree to be  bound by the  conditions  described below
00009  *  in this notice:
00010  *
00011  *  This software product,  NABOU,  is developed by Thomas Linden
00012  *  and   copyrighted (C) 1999-2002   by  Thomas Linden, with all
00013  *  rights reserved.
00014  *
00015  *  There  is no charge for NABOU software.  You can redistribute
00016  *  it and/or modify it under the terms of the GNU General Public
00017  *  License, which is incorporated by reference herein.
00018  *
00019  *  NABOU is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,
00020  *  OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that
00021  *  the use of it will not infringe on any third party's intellec-
00022  *  tual property rights.
00023  *
00024  *  You should have received a copy of the GNU General Public
00025  *  License along with NABOU.  Copies can also be obtained from:
00026  *
00027  *    http://www.gnu.org/copyleft/gpl.html
00028  *
00029  *  or by writing to:
00030  *
00031  *  Free Software Foundation, Inc.
00032  *  59 Temple Place, Suite 330
00033  *  Boston, MA 02111-1307
00034  *  USA
00035  *
00036  *  Or contact:
00037  *
00038  *   "Thomas Linden" <tom@nabou.org>
00039  *
00040  *
00041  */
00042 
00043 #ifndef HAVE_PCRE_PP_H
00044 #define HAVE_PCRE_PP_H
00045 
00046 #include <string>
00047 #include <sstream>
00048 #include <vector>
00049 #include <stdexcept>
00050 
00051 extern "C" {
00052   #include <pcre.h>
00053 }
00054 
00055 #include "config.h"
00056 
00057 
00058 
00060 typedef vector<string> Array;
00061 
00063 typedef vector<string>::iterator ArrayIterator;
00064 
00092 class Pcre {
00093  private:
00094   string _expression;        /* the given regular expression */
00095   unsigned int _flags;       /* the given flags, 0 if not defined */
00096   bool case_t, global_t;     /* internal compile flags, used by replace() and split() */
00097   pcre *p_pcre;              /* pcre object pointer */
00098   pcre_extra *p_pcre_extra;  /* stuff required by pcre lib */
00099   int sub_len;
00100   int *sub_vec;
00101   int erroffset;
00102   char *err_str;
00103   Array *resultset;   /* store substrings, if any */
00104 
00105   /* reset all counters and free objects, prepare for another search */
00106   void reset();
00107 
00108   /* compile the pattern */
00109   void Compile(int flags);
00110 
00111   /* do the actual search, will be called by the public ::search(..) methods */
00112   bool dosearch(const string& stuff, int OffSet);
00113 
00114   /* do the actual split() job, called by the various wrapper split() methods */
00115   Array _split(const string& piece, int limit, int start_offset, int end_offset);
00116   
00117   /* replace $1 .. $n with the corresponding substring, used by replace() */
00118   string _replace_vars(const string& piece);
00119  public:
00120 
00138   class exception : public runtime_error {
00139   private:
00140     string translate(int num) {
00141       string msg;
00142       switch(num) {
00143       case -1: msg = "PCRE_ERROR_NOMATCH";      break;
00144       case -2: msg = "PCRE_ERROR_NULL";         break;
00145       case -3: msg = "PCRE_ERROR_BADOPTION";    break;
00146       case -4: msg = "PCRE_ERROR_BADMAGIC";     break;
00147       case -5: msg = "PCRE_ERROR_UNKNOWN_NODE"; break;
00148       case -6: msg = "PCRE_ERROR_NOMEMORY";     break;
00149       case -7: msg = "PCRE_ERROR_NOSUBSTRING";  break;
00150       }
00151       return msg;
00152     }
00153   public:
00154     exception(const string & msg) : runtime_error(msg) { }
00155     exception(int num) : runtime_error(translate(num)) { }
00156   };
00157 
00158 
00159   bool did_match;            
00160   int  num_matches;          
00170   Pcre(const string& expression);
00171 
00197   Pcre(const string& expression, const string& flags);
00198 
00206   Pcre(Pcre &P);
00207 
00218   const Pcre& operator = (const string& expression); 
00219 
00225   ~Pcre();
00226 
00233   bool search(const string& stuff);
00234 
00242   bool search(const string& stuff, int OffSet);
00243 
00248   Array* get_sub_strings();
00249 
00264   string get_match(int pos);
00265 
00284   int get_match_start(int pos);
00285 
00304   int get_match_end(int pos);
00305 
00313   size_t get_match_length(int pos);
00314 
00319   bool matched() { return did_match; };
00320 
00324   int  matches() { return num_matches; };
00325 
00338   Array split(const string& piece);
00339 
00353   Array split(const string& piece, int limit);
00354 
00369   Array split(const string& piece, int limit, int start_offset);
00370 
00386   Array split(const string& piece, int limit, int start_offset, int end_offset);
00387 
00401   Array split(const string& piece, vector<int> positions);
00402 
00411   string replace(const string& piece, const string& with);
00412 }; 
00413 
00414 #endif

Generated on Sun Jan 6 16:23:44 2002 for PCRE++ by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001