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   bool _have_paren;          /* indicate wether we have already parentesis applied in replace */
00112 
00113   const unsigned char *tables; /* locale tables */
00114 
00115   bool did_match;            
00116   int  num_matches;          
00118   /* reset all counters and free objects, prepare for another search */
00119   void reset();
00120 
00121   /* compile the pattern */
00122   void Compile(int flags);
00123 
00124   /* do the actual search, will be called by the public ::search(..) methods */
00125   bool dosearch(const std::string& stuff, int OffSet);
00126 
00127   /* do the actual split() job, called by the various wrapper split() methods */
00128   std::vector<std::string> _split(const std::string& piece, int limit, int start_offset, int end_offset);
00129   
00130   /* replace $1 .. $n with the corresponding substring, used by replace() */
00131   std::string _replace_vars(const std::string& piece);
00132 
00133   /* init pointers with NULL */
00134   void zero();
00135 
00136   std::map<std::string,std::string> info();
00137   std::string info(int what);
00138 
00139  public:
00140 
00158   class exception : public std::runtime_error {
00159   private:
00160     std::string translate(int num) {
00161       std::string msg;
00162       switch(num) {
00163       case -1: msg = "PCRE_ERROR_NOMATCH";      break;
00164       case -2: msg = "PCRE_ERROR_NULL";         break;
00165       case -3: msg = "PCRE_ERROR_BADOPTION";    break;
00166       case -4: msg = "PCRE_ERROR_BADMAGIC";     break;
00167       case -5: msg = "PCRE_ERROR_UNKNOWN_NODE"; break;
00168       case -6: msg = "PCRE_ERROR_NOMEMORY";     break;
00169       case -7: msg = "PCRE_ERROR_NOSUBSTRING";  break;
00170         // pcre4-HINT: add PCRE_ERROR_MATCHLIMIT support
00171       }
00172       return msg;
00173     }
00174   public:
00175     exception(const std::string & msg) : runtime_error(msg) { }
00176     exception(int num) : runtime_error(translate(num)) { }
00177   };
00178 
00179 
00191   Pcre();
00192 
00202   Pcre(const std::string& expression);
00203 
00230   Pcre(const std::string& expression, const std::string& flags);
00231 
00257   Pcre(const std::string& expression, unsigned int flags);
00258 
00266   Pcre(const Pcre &P);
00267 
00278   const Pcre& operator = (const std::string& expression); 
00279 
00292   const Pcre& operator = (const Pcre &P);
00293 
00299   ~Pcre();
00300 
00307   bool search(const std::string& stuff);
00308 
00316   bool search(const std::string& stuff, int OffSet);
00317 
00322   std::vector<std::string>* get_sub_strings() const;
00323 
00338   std::string get_match(int pos) const;
00339 
00360   int get_match_start(int pos) const;
00361 
00382   int get_match_end(int pos) const;
00383 
00384 
00385 
00386 
00405   int get_match_start() const;
00406 
00426   int get_match_end() const;
00427 
00428 
00429 
00430 
00438   size_t get_match_length(int pos) const;
00439 
00444   bool matched() const { return did_match; };
00445 
00449   int  matches() const { return num_matches; }
00450 
00451 
00464   std::vector<std::string> split(const std::string& piece);
00465 
00479   std::vector<std::string> split(const std::string& piece, int limit);
00480 
00495   std::vector<std::string> split(const std::string& piece, int limit, int start_offset);
00496 
00512   std::vector<std::string> split(const std::string& piece, int limit, int start_offset, int end_offset);
00513 
00527   std::vector<std::string> split(const std::string& piece, std::vector<int> positions);
00528 
00537   std::string replace(const std::string& piece, const std::string& with);
00538 
00550   pcre* get_pcre();
00551 
00559   pcre_extra* get_pcre_extra();
00560 
00567   void study();
00568 
00576   bool setlocale(const char* locale);
00577 
00594   std::string operator[](int index) {
00595     return get_match(index);
00596   }
00597 }; 
00598 
00599 } // end namespace pcre
00600 
00601 #endif // HAVE_PCRE_PP_H

Generated on Wed Aug 25 01:38:04 2004 for PCRE++ by doxygen1.3-rc3