1
#include <regexmanager.h>
8
regexmanager::regexmanager() {
9
// this creates the entries in the map. we need them there to have the "all" location work.
11
locations["articlelist"];
12
locations["feedlist"];
15
regexmanager::~regexmanager() {
16
for (std::map<std::string, rc_pair>::iterator jt=locations.begin();jt!=locations.end();jt++) {
17
std::vector<regex_t *>& regexes(jt->second.first);
18
if (regexes.size() > 0) {
19
for (std::vector<regex_t *>::iterator it=regexes.begin();it!=regexes.end();++it) {
26
action_handler_status regexmanager::handle_action(const std::string& action, const std::vector<std::string>& params) {
27
if (action == "highlight") {
28
if (params.size() < 3)
29
return AHS_TOO_FEW_PARAMS;
31
std::string location = params[0];
32
if (location != "all" && location != "article" && location != "articlelist" && location != "feedlist")
33
return AHS_INVALID_PARAMS;
35
regex_t * rx = new regex_t;
36
if (regcomp(rx, params[1].c_str(), REG_EXTENDED | REG_ICASE) != 0) {
38
return AHS_INVALID_PARAMS;
41
if (params[2] != "default") {
42
colorstr.append("fg=");
43
if (!utils::is_valid_color(params[2]))
44
return AHS_INVALID_PARAMS;
45
colorstr.append(params[2]);
47
if (params.size() > 2) {
48
if (params[3] != "default") {
49
if (colorstr.length() > 0)
51
colorstr.append("bg=");
52
if (!utils::is_valid_color(params[3]))
53
return AHS_INVALID_PARAMS;
54
colorstr.append(params[3]);
56
for (unsigned int i=4;i<params.size();++i) {
57
if (params[i] != "default") {
58
if (colorstr.length() > 0)
60
colorstr.append("attr=");
61
if (!utils::is_valid_attribute(params[i]))
62
return AHS_INVALID_PARAMS;
63
colorstr.append(params[i]);
67
if (location != "all") {
68
GetLogger().log(LOG_DEBUG, "regexmanager::handle_action: adding rx = %s colorstr = %s to location %s",
69
params[1].c_str(), colorstr.c_str(), location.c_str());
70
locations[location].first.push_back(rx);
71
locations[location].second.push_back(colorstr);
74
for (std::map<std::string, rc_pair>::iterator it=locations.begin();it!=locations.end();it++) {
75
GetLogger().log(LOG_DEBUG, "regexmanager::handle_action: adding rx = %s colorstr = %s to location %s",
76
params[1].c_str(), colorstr.c_str(), it->first.c_str());
78
// we need to create a new one for each push_back, otherwise we'd have double frees.
79
regcomp(rx, params[1].c_str(), REG_EXTENDED | REG_ICASE);
80
it->second.first.push_back(rx);
81
it->second.second.push_back(colorstr);
86
return AHS_INVALID_COMMAND;
89
void regexmanager::quote_and_highlight(std::string& str, const std::string& location) {
90
std::vector<regex_t *>& regexes = locations[location].first;
92
unsigned int len = str.length();
93
for (unsigned int i=0;i<len;++i) {
100
for (std::vector<regex_t *>::iterator it=regexes.begin();it!=regexes.end();++it, ++i) {
102
unsigned int offset = 0;
103
int err = regexec(*it, str.c_str(), 1, &pmatch, 0);
105
// GetLogger().log(LOG_DEBUG, "regexmanager::quote_and_highlight: matched %s rm_so = %u rm_eo = %u", str.c_str() + offset, pmatch.rm_so, pmatch.rm_eo);
106
std::string marker = utils::strprintf("<%u>", i);
107
str.insert(offset + pmatch.rm_eo, "</>");
108
// GetLogger().log(LOG_DEBUG, "after first insert: %s", str.c_str());
109
str.insert(offset + pmatch.rm_so, marker);
110
// GetLogger().log(LOG_DEBUG, "after second insert: %s", str.c_str());
111
offset += pmatch.rm_eo + marker.length() + strlen("</>");
112
err = regexec(*it, str.c_str() + offset, 1, &pmatch, 0);