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 
00052 extern "C" {
00053   #include <pcre.h>
00054   #include <locale.h>
00055 }
00056 
00057 namespace pcrepp {
00058 
00059 #ifdef DEBUG
00060 #define __pcredebug cerr << "(pcre++ DEBUG) " << __LINE__ << ": " 
00061 #else
00062 #define __pcredebug if(0) cerr 
00063 #endif
00064 
00068 #define PCRE_GLOBAL 0x10000
00069 
00070 
00099 class Pcre {
00100  private:
00101   std::string _expression;   /* the given regular expression */
00102   unsigned int _flags;       /* the given flags, 0 if not defined */
00103   bool case_t, global_t;     /* internal compile flags, used by replace() and split() */
00104   pcre *p_pcre;              /* pcre object pointer */
00105   pcre_extra *p_pcre_extra;  /* stuff required by pcre lib */
00106   int sub_len;
00107   int *sub_vec;
00108   int erroffset;
00109   char *err_str;
00110   std::vector<std::string> *resultset;          /* store substrings, if any */
00111 
00112   const unsigned char *tables; /* locale tables */
00113 
00114   bool did_match;            
00115   int  num_matches;          
00117   /* reset all counters and free objects, prepare for another search */
00118   void reset();
00119 
00120   /* compile the pattern */
00121   void Compile(int flags);
00122 
00123   /* do the actual search, will be called by the public ::search(..) methods */
00124   bool dosearch(const std::string& stuff, int OffSet);
00125 
00126   /* do the actual split() job, called by the various wrapper split() methods */
00127   std::vector<std::string> _split(const std::string& piece, int limit, int start_offset, int end_offset);
00128   
00129   /* replace $1 .. $n with the corresponding substring, used by replace() */
00130   std::string _replace_vars(const std::string& piece);
00131 
00132   /* init pointers with NULL */
00133   void zero();
00134 
00135   std::map<std::string,std::string> info();
00136   std::string info(int what);
00137 
00138  public:
00139 
00157   class exception : public std::runtime_error {
00158   private:
00159     std::string translate(int num) {
00160       std::string msg;
00161       switch(num) {
00162       case -1: msg = "PCRE_ERROR_NOMATCH";      break;
00163       case -2: msg = "PCRE_ERROR_NULL";         break;
00164       case -3: msg = "PCRE_ERROR_BADOPTION";    break;
00165       case -4: msg = "PCRE_ERROR_BADMAGIC";     break;
00166       case -5: msg = "PCRE_ERROR_UNKNOWN_NODE"; break;
00167       case -6: msg = "PCRE_ERROR_NOMEMORY";     break;
00168       case -7: msg = "PCRE_ERROR_NOSUBSTRING";  break;
00169         // pcre4-HINT: add PCRE_ERROR_MATCHLIMIT support
00170       }
00171       return msg;
00172     }
00173   public:
00174     exception(const std::string & msg) : runtime_error(msg) { }
00175     exception(int num) : runtime_error(translate(num)) { }
00176   };
00177 
00178 
00190   Pcre();
00191 
00201   Pcre(const std::string& expression);
00202 
00229   Pcre(const std::string& expression, const std::string& flags);
00230 
00256   Pcre(const std::string& expression, unsigned int flags);
00257 
00265   Pcre(const Pcre &P);
00266 
00277   const Pcre& operator = (const std::string& expression); 
00278 
00291   const Pcre& operator = (const Pcre &P);
00292 
00298   ~Pcre();
00299 
00306   bool search(const std::string& stuff);
00307 
00315   bool search(const std::string& stuff, int OffSet);
00316 
00321   std::vector<std::string>* get_sub_strings();
00322 
00337   std::string get_match(int pos);
00338 
00359   int get_match_start(int pos);
00360 
00381   int get_match_end(int pos);
00382 
00383 
00384 
00385 
00404   int get_match_start();
00405 
00425   int get_match_end();
00426 
00427 
00428 
00429 
00437   size_t get_match_length(int pos);
00438 
00443   bool matched() { return did_match; };
00444 
00448   int  matches() { return num_matches; }
00449 
00450 
00463   std::vector<std::string> split(const std::string& piece);
00464 
00478   std::vector<std::string> split(const std::string& piece, int limit);
00479 
00494   std::vector<std::string> split(const std::string& piece, int limit, int start_offset);
00495 
00511   std::vector<std::string> split(const std::string& piece, int limit, int start_offset, int end_offset);
00512 
00526   std::vector<std::string> split(const std::string& piece, std::vector<int> positions);
00527 
00536   std::string replace(const std::string& piece, const std::string& with);
00537 
00549   pcre* get_pcre();
00550 
00558   pcre_extra* get_pcre_extra();
00559 
00566   void study();
00567 
00575   bool setlocale(const char* locale);
00576 
00593   std::string operator[](int index) {
00594     return get_match(index);
00595   }
00596 }; 
00597 
00598 } // end namespace pcre
00599 
00600 #endif // HAVE_PCRE_PP_H

Generated on Wed Jun 16 00:22:32 2004 for PCRE++ by doxygen1.3-rc3