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 <map>
00048 #include <stdexcept>
00049 #include <iostream>
00050 
00051 extern "C" {
00052   #include <pcre.h>
00053 }
00054 
00055 namespace pcrepp {
00056 
00057 #ifdef DEBUG
00058 #define __pcredebug cerr << "(pcre++ DEBUG) " << __LINE__ << ": " 
00059 #else
00060 #define __pcredebug if(0) cerr 
00061 #endif
00062 
00066 #define PCRE_GLOBAL 0x10000
00067 
00068 
00097 class Pcre {
00098  private:
00099   std::string _expression;   /* the given regular expression */
00100   unsigned int _flags;       /* the given flags, 0 if not defined */
00101   bool case_t, global_t;     /* internal compile flags, used by replace() and split() */
00102   pcre *p_pcre;              /* pcre object pointer */
00103   pcre_extra *p_pcre_extra;  /* stuff required by pcre lib */
00104   int sub_len;
00105   int *sub_vec;
00106   int erroffset;
00107   char *err_str;
00108   std::vector<std::string> *resultset;          /* store substrings, if any */
00109 
00110   bool did_match;            
00111   int  num_matches;          
00113   /* reset all counters and free objects, prepare for another search */
00114   void reset();
00115 
00116   /* compile the pattern */
00117   void Compile(int flags);
00118 
00119   /* do the actual search, will be called by the public ::search(..) methods */
00120   bool dosearch(const std::string& stuff, int OffSet);
00121 
00122   /* do the actual split() job, called by the various wrapper split() methods */
00123   std::vector<std::string> _split(const std::string& piece, int limit, int start_offset, int end_offset);
00124   
00125   /* replace $1 .. $n with the corresponding substring, used by replace() */
00126   std::string _replace_vars(const std::string& piece);
00127 
00128   /* init pointers with NULL */
00129   void zero();
00130 
00131   std::map<std::string,std::string> info();
00132   std::string info(int what);
00133 
00134  public:
00135 
00153   class exception : public std::runtime_error {
00154   private:
00155     std::string translate(int num) {
00156       std::string msg;
00157       switch(num) {
00158       case -1: msg = "PCRE_ERROR_NOMATCH";      break;
00159       case -2: msg = "PCRE_ERROR_NULL";         break;
00160       case -3: msg = "PCRE_ERROR_BADOPTION";    break;
00161       case -4: msg = "PCRE_ERROR_BADMAGIC";     break;
00162       case -5: msg = "PCRE_ERROR_UNKNOWN_NODE"; break;
00163       case -6: msg = "PCRE_ERROR_NOMEMORY";     break;
00164       case -7: msg = "PCRE_ERROR_NOSUBSTRING";  break;
00165         // pcre4-HINT: add PCRE_ERROR_MATCHLIMIT support
00166       }
00167       return msg;
00168     }
00169   public:
00170     exception(const std::string & msg) : runtime_error(msg) { }
00171     exception(int num) : runtime_error(translate(num)) { }
00172   };
00173 
00174 
00186   Pcre();
00187 
00197   Pcre(const std::string& expression);
00198 
00225   Pcre(const std::string& expression, const std::string& flags);
00226 
00252   Pcre(const std::string& expression, unsigned int flags);
00253 
00261   Pcre(const Pcre &P);
00262 
00273   const Pcre& operator = (const std::string& expression); 
00274 
00287   const Pcre& operator = (const Pcre &P);
00288 
00294   ~Pcre();
00295 
00302   bool search(const std::string& stuff);
00303 
00311   bool search(const std::string& stuff, int OffSet);
00312 
00317   std::vector<std::string>* get_sub_strings();
00318 
00333   std::string get_match(int pos);
00334 
00355   int get_match_start(int pos);
00356 
00377   int get_match_end(int pos);
00378 
00379 
00380 
00381 
00400   int get_match_start();
00401 
00421   int get_match_end();
00422 
00423 
00424 
00425 
00433   size_t get_match_length(int pos);
00434 
00439   bool matched() { return did_match; };
00440 
00444   int  matches() { return num_matches; }
00445 
00446 
00459   std::vector<std::string> split(const std::string& piece);
00460 
00474   std::vector<std::string> split(const std::string& piece, int limit);
00475 
00490   std::vector<std::string> split(const std::string& piece, int limit, int start_offset);
00491 
00507   std::vector<std::string> split(const std::string& piece, int limit, int start_offset, int end_offset);
00508 
00522   std::vector<std::string> split(const std::string& piece, std::vector<int> positions);
00523 
00532   std::string replace(const std::string& piece, const std::string& with);
00533 
00545   pcre* get_pcre();
00546 
00554   pcre_extra* get_pcre_extra();
00555 
00562   void study();
00563 
00580   std::string operator[](int index) {
00581     return get_match(index);
00582   }
00583 }; 
00584 
00585 } // end namespace pcre
00586 
00587 #endif // HAVE_PCRE_PP_H

Generated on Sun Jul 13 13:03:00 2003 for PCRE++ by doxygen1.3-rc3