00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
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;   
00102   unsigned int _flags;       
00103   bool case_t, global_t;     
00104   pcre *p_pcre;              
00105   pcre_extra *p_pcre_extra;  
00106   int sub_len;
00107   int *sub_vec;
00108   int erroffset;
00109   char *err_str;
00110   std::vector<std::string> *resultset;          
00111   bool _have_paren;          
00112 
00113   const unsigned char *tables; 
00114 
00115   bool did_match;            
00116   int  num_matches;          
00118   
00119   void reset();
00120 
00121   
00122   void Compile(int flags);
00123 
00124   
00125   bool dosearch(const std::string& stuff, int OffSet);
00126 
00127   
00128   std::vector<std::string> _split(const std::string& piece, int limit, int start_offset, int end_offset);
00129   
00130   
00131   std::string _replace_vars(const std::string& piece);
00132 
00133   
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         
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 } 
00600 
00601 #endif // HAVE_PCRE_PP_H