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 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;
00100 unsigned int _flags;
00101 bool case_t, global_t;
00102 pcre *p_pcre;
00103 pcre_extra *p_pcre_extra;
00104 int sub_len;
00105 int *sub_vec;
00106 int erroffset;
00107 char *err_str;
00108 std::vector<std::string> *resultset;
00109
00110 bool did_match;
00111 int num_matches;
00113
00114 void reset();
00115
00116
00117 void Compile(int flags);
00118
00119
00120 bool dosearch(const std::string& stuff, int OffSet);
00121
00122
00123 std::vector<std::string> _split(const std::string& piece, int limit, int start_offset, int end_offset);
00124
00125
00126 std::string _replace_vars(const std::string& piece);
00127
00128
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
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 }
00586
00587 #endif // HAVE_PCRE_PP_H