1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include "../libpcre++/pcre++.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
using namespace pcrepp;
class failure {
private:
string message;
public:
failure(const string& msg) {
message = msg;
}
string what() {
return message;
}
};
map<string, vector<string> > read_data(const char* file) {
ifstream data(file);
if(data) {
vector<string> content;
map<string, vector<string> > hash;
string line, regex;
char zeichen;
bool is_regex=false;
Pcre find_end("\\/[gimsxADEGIMNSUX8L\\+]{0,2}$");
while (data) {
data.get(zeichen);
if(zeichen == '\n') {
if(line[0] == '/') {
regex = line;
if(find_end.search(line))
is_regex = false;
else
is_regex = true;
}
else if(is_regex) {
regex += "\n" + line;
if(find_end.search(line))
is_regex = false;
else
is_regex = true;
}
else if(line[0] == ' ') {
line.replace(0, 4, ""); // remove leading whitespaces
content.push_back(line);
}
else if(line.empty()) {
hash[regex] = content;
content.clear();
}
line = "";
}
else
line += zeichen;
}
data.close();
return hash;
}
else {
throw failure("Could not open file " + string(file));
}
}
int main(int argc, char *argv[]) {
try {
if(argc < 2)
throw failure("Usage: " + string(argv[0]) + " <datafile>");
else {
map<string, vector<string> > hash = read_data(argv[1]);
typedef map<string, vector<string> >::iterator map_iter;
typedef vector<string>::iterator vec_iter;
Pcre *reg;
string expression, flags;
for(map_iter mp = hash.begin(); mp != hash.end(); ++mp) {
string regex = mp->first;
vector<string> content = mp->second;
unsigned int pos = regex.find_last_of("/", string::npos);
if (pos == regex.size()) {
flags = "";
expression = regex.substr(1, regex.size() - 1);
}
else {
flags = regex.substr(pos + 1, regex.size());
expression = regex.substr(1, pos - 1);
}
cout << "/" << expression << "/" << flags << endl;
try {
if(flags.empty())
reg = new Pcre(expression);
else {
reg = new Pcre(expression, flags);
flags = "";
}
}
catch(Pcre::exception &e) {
cout << e.what() << endl;
continue;
}
for(vec_iter vp = content.begin(); vp != content.end(); ++vp) {
string data = *vp;
if(reg->search(data))
cout << " 1: ";
else
cout << " 0: ";
cout << data << endl;
}
cout << endl;
delete reg;
}
}
}
catch(exception &e) {
cerr << e.what() << endl;
return 1;
}
catch(failure &f) {
cerr << f.what() << endl;
return 1;
}
catch(...) {
cerr << "unknown failure" << endl;
return 1;
}
return 0;
}
|