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-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 #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 #include <iostream>
00049 
00050 extern "C" {
00051   #include <pcre.h>
00052 }
00053 
00054 #ifdef DEBUG
00055 #define __pcredebug cerr << "(pcre++ DEBUG) " << __LINE__ << ": " 
00056 #else
00057 #define __pcredebug if(0) cerr 
00058 #endif
00059 
00060 
00062 typedef std::vector<std::string> Array;
00063 
00065 typedef std::vector<std::string>::iterator ArrayIterator;
00066 
00095 class Pcre {
00096  private:
00097   std::string _expression;        /* the given regular expression */
00098   unsigned int _flags;       /* the given flags, 0 if not defined */
00099   bool case_t, global_t;     /* internal compile flags, used by replace() and split() */
00100   pcre *p_pcre;              /* pcre object pointer */
00101   pcre_extra *p_pcre_extra;  /* stuff required by pcre lib */
00102   int sub_len;
00103   int *sub_vec;
00104   int erroffset;
00105   char *err_str;
00106   Array *resultset;          /* store substrings, if any */
00107 
00108   /* reset all counters and free objects, prepare for another search */
00109   void reset();
00110 
00111   /* compile the pattern */
00112   void Compile(int flags);
00113 
00114   /* do the actual search, will be called by the public ::search(..) methods */
00115   bool dosearch(const std::string& stuff, int OffSet);
00116 
00117   /* do the actual split() job, called by the various wrapper split() methods */
00118   Array _split(const std::string& piece, int limit, int start_offset, int end_offset);
00119   
00120   /* replace $1 .. $n with the corresponding substring, used by replace() */
00121   std::string _replace_vars(const std::string& piece);
00122 
00123   /* init pointers with NULL */
00124   void zero();
00125 
00126 
00127 
00128  public:
00129 
00147   class exception : public std::runtime_error {
00148   private:
00149     std::string translate(int num) {
00150       std::string msg;
00151       switch(num) {
00152       case -1: msg = "PCRE_ERROR_NOMATCH";      break;
00153       case -2: msg = "PCRE_ERROR_NULL";         break;
00154       case -3: msg = "PCRE_ERROR_BADOPTION";    break;
00155       case -4: msg = "PCRE_ERROR_BADMAGIC";     break;
00156       case -5: msg = "PCRE_ERROR_UNKNOWN_NODE"; break;
00157       case -6: msg = "PCRE_ERROR_NOMEMORY";     break;
00158       case -7: msg = "PCRE_ERROR_NOSUBSTRING";  break;
00159         // pcre4-HINT: add PCRE_ERROR_MATCHLIMIT support
00160       }
00161       return msg;
00162     }
00163   public:
00164     exception(const std::string & msg) : runtime_error(msg) { }
00165     exception(int num) : runtime_error(translate(num)) { }
00166   };
00167 
00168 
00169   bool did_match;            
00170   int  num_matches;          
00183   Pcre();
00184 
00193   Pcre(const std::string& expression);
00194 
00220   Pcre(const std::string& expression, const std::string& flags);
00221 
00222   // pcre4-HINT: add another constructor with match_limit support (pcre_extra data to pcre_exec)
00223 
00231   Pcre(const Pcre &P);
00232 
00243   const Pcre& operator = (const std::string& expression); 
00244 
00257   const Pcre& operator = (const Pcre &P);
00258 
00264   ~Pcre();
00265 
00272   bool search(const std::string& stuff);
00273 
00281   bool search(const std::string& stuff, int OffSet);
00282 
00287   Array* get_sub_strings();
00288 
00303   std::string get_match(int pos);
00304 
00325   int get_match_start(int pos);
00326 
00347   int get_match_end(int pos);
00348 
00349 
00350 
00351 
00370   int get_match_start();
00371 
00391   int get_match_end();
00392 
00393 
00394 
00395 
00403   size_t get_match_length(int pos);
00404 
00409   bool matched() { return did_match; };
00410 
00414   int  matches() { return num_matches; }
00415 
00416 
00429   Array split(const std::string& piece);
00430 
00444   Array split(const std::string& piece, int limit);
00445 
00460   Array split(const std::string& piece, int limit, int start_offset);
00461 
00477   Array split(const std::string& piece, int limit, int start_offset, int end_offset);
00478 
00492   Array split(const std::string& piece, std::vector<int> positions);
00493 
00502   std::string replace(const std::string& piece, const std::string& with);
00503 }; 
00504 
00505 #endif

Generated on Wed Jun 25 00:39:01 2003 for PCRE++ by doxygen1.3-rc3