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
00112 const unsigned char *tables;
00113
00114 bool did_match;
00115 int num_matches;
00117
00118 void reset();
00119
00120
00121 void Compile(int flags);
00122
00123
00124 bool dosearch(const std::string& stuff, int OffSet);
00125
00126
00127 std::vector<std::string> _split(const std::string& piece, int limit, int start_offset, int end_offset);
00128
00129
00130 std::string _replace_vars(const std::string& piece);
00131
00132
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
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 }
00599
00600 #endif // HAVE_PCRE_PP_H