Google

Main Page   Namespace List   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 PCRE++ Class Library.
00006  *
00007  *  By  accessing  this software,  PCRE++, 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,  PCRE++,  is developed by Thomas Linden
00012  *  and  copyrighted (C) 2002  by  Thomas Linden,  with all rights 
00013  *  reserved.
00014  *
00015  *  There  is no charge for PCRE++ software.  You can redistribute
00016  *  it and/or modify it under the terms of the GNU  Lesser General
00017  *  Public License, which is incorporated by reference herein.
00018  *
00019  *  PCRE++ 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 Lesser General Public
00025  *  License along with PCRE++.  Copies can also be obtained from:
00026  *
00027  *    http://www.gnu.org/licenses/lgpl.txt
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@daemon.de>
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 
00056 using namespace std;
00057 
00059 typedef vector<string> Array;
00060 
00062 typedef vector<string>::iterator ArrayIterator;
00063 
00091 class Pcre {
00092  private:
00093   string _expression;        /* the given regular expression */
00094   unsigned int _flags;       /* the given flags, 0 if not defined */
00095   bool case_t, global_t;     /* internal compile flags, used by replace() and split() */
00096   pcre *p_pcre;              /* pcre object pointer */
00097   pcre_extra *p_pcre_extra;  /* stuff required by pcre lib */
00098   int sub_len;
00099   int *sub_vec;
00100   int erroffset;
00101   char *err_str;
00102   Array *resultset;          /* store substrings, if any */
00103 
00104   /* reset all counters and free objects, prepare for another search */
00105   void reset();
00106 
00107   /* compile the pattern */
00108   void Compile(int flags);
00109 
00110   /* do the actual search, will be called by the public ::search(..) methods */
00111   bool dosearch(const string& stuff, int OffSet);
00112 
00113   /* do the actual split() job, called by the various wrapper split() methods */
00114   Array _split(const string& piece, int limit, int start_offset, int end_offset);
00115   
00116   /* replace $1 .. $n with the corresponding substring, used by replace() */
00117   string _replace_vars(const string& piece);
00118 
00119   /* init pointers with NULL */
00120   void Pcre::zero();
00121 
00122  public:
00123 
00141   class exception : public runtime_error {
00142   private:
00143     string translate(int num) {
00144       string msg;
00145       switch(num) {
00146       case -1: msg = "PCRE_ERROR_NOMATCH";      break;
00147       case -2: msg = "PCRE_ERROR_NULL";         break;
00148       case -3: msg = "PCRE_ERROR_BADOPTION";    break;
00149       case -4: msg = "PCRE_ERROR_BADMAGIC";     break;
00150       case -5: msg = "PCRE_ERROR_UNKNOWN_NODE"; break;
00151       case -6: msg = "PCRE_ERROR_NOMEMORY";     break;
00152       case -7: msg = "PCRE_ERROR_NOSUBSTRING";  break;
00153       }
00154       return msg;
00155     }
00156   public:
00157     exception(const string & msg) : runtime_error(msg) { }
00158     exception(int num) : runtime_error(translate(num)) { }
00159   };
00160 
00161 
00162   bool did_match;            
00163   int  num_matches;          
00176   Pcre();
00177 
00186   Pcre(const string& expression);
00187 
00213   Pcre(const string& expression, const string& flags);
00214 
00222   Pcre(const Pcre &P);
00223 
00234   const Pcre& operator = (const string& expression); 
00235 
00248   const Pcre& operator = (const Pcre &P);
00249 
00255   ~Pcre();
00256 
00263   bool search(const string& stuff);
00264 
00272   bool search(const string& stuff, int OffSet);
00273 
00278   Array* get_sub_strings();
00279 
00294   string get_match(int pos);
00295 
00316   int get_match_start(int pos);
00317 
00338   int get_match_end(int pos);
00339 
00340 
00341 
00342 
00361   int get_match_start();
00362 
00382   int get_match_end();
00383 
00384 
00385 
00386 
00394   size_t get_match_length(int pos);
00395 
00400   bool matched() { return did_match; };
00401 
00405   int  matches() { return num_matches; };
00406 
00419   Array split(const string& piece);
00420 
00434   Array split(const string& piece, int limit);
00435 
00450   Array split(const string& piece, int limit, int start_offset);
00451 
00467   Array split(const string& piece, int limit, int start_offset, int end_offset);
00468 
00482   Array split(const string& piece, vector<int> positions);
00483 
00492   string replace(const string& piece, const string& with);
00493 }; 
00494 
00495 #endif

Generated on Tue Jul 16 22:14:38 2002 for PCRE++ by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001