~ubuntu-branches/ubuntu/maverick/newsbeuter/maverick

« back to all changes in this revision

Viewing changes to src/regexmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nico Golde
  • Date: 2008-09-02 13:56:35 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20080902135635-728j1jk0wvt7kswp
Tags: 1.2-1
* New upstream release.
  - Fix incomplete quoting fix for article URLs by replacing '
    with the proper encoding in hex (Closes: #497495).
* Update rss_parser.cpp part of the use_system-wide-libs for the
  new download-timeout option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <regexmanager.h>
 
2
#include <logger.h>
 
3
#include <utils.h>
 
4
#include <cstring>
 
5
 
 
6
namespace newsbeuter {
 
7
 
 
8
regexmanager::regexmanager() {
 
9
        // this creates the entries in the map. we need them there to have the "all" location work.
 
10
        locations["article"];
 
11
        locations["articlelist"];
 
12
        locations["feedlist"];
 
13
}
 
14
 
 
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) {
 
20
                                delete *it;
 
21
                        }
 
22
                }
 
23
        }
 
24
}
 
25
 
 
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;
 
30
 
 
31
                std::string location = params[0];
 
32
                if (location != "all" && location != "article" && location != "articlelist" && location != "feedlist")
 
33
                        return AHS_INVALID_PARAMS;
 
34
 
 
35
                regex_t * rx = new regex_t;
 
36
                if (regcomp(rx, params[1].c_str(), REG_EXTENDED | REG_ICASE) != 0) {
 
37
                        delete rx;
 
38
                        return AHS_INVALID_PARAMS;
 
39
                }
 
40
                std::string colorstr;
 
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]);
 
46
                }
 
47
                if (params.size() > 2) {
 
48
                        if (params[3] != "default") {
 
49
                                if (colorstr.length() > 0)
 
50
                                        colorstr.append(",");
 
51
                                colorstr.append("bg=");
 
52
                                if (!utils::is_valid_color(params[3]))
 
53
                                        return AHS_INVALID_PARAMS;
 
54
                                colorstr.append(params[3]);
 
55
                        }
 
56
                        for (unsigned int i=4;i<params.size();++i) {
 
57
                                if (params[i] != "default") {
 
58
                                        if (colorstr.length() > 0)
 
59
                                                colorstr.append(",");
 
60
                                        colorstr.append("attr=");
 
61
                                        if (!utils::is_valid_attribute(params[i]))
 
62
                                                return AHS_INVALID_PARAMS;
 
63
                                        colorstr.append(params[i]);
 
64
                                }
 
65
                        }
 
66
                }
 
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);
 
72
                } else {
 
73
                        delete rx;
 
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());
 
77
                                rx = new regex_t;
 
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);
 
82
                        }
 
83
                }
 
84
                return AHS_OK;
 
85
        } else
 
86
                return AHS_INVALID_COMMAND;
 
87
}
 
88
 
 
89
void regexmanager::quote_and_highlight(std::string& str, const std::string& location) {
 
90
        std::vector<regex_t *>& regexes = locations[location].first;
 
91
 
 
92
        unsigned int len = str.length();
 
93
        for (unsigned int i=0;i<len;++i) {
 
94
                if (str[i] == '<') {
 
95
                        str.insert(i+1, ">");
 
96
                        ++len;
 
97
                }
 
98
        }
 
99
        unsigned int i = 0;
 
100
        for (std::vector<regex_t *>::iterator it=regexes.begin();it!=regexes.end();++it, ++i) {
 
101
                regmatch_t pmatch;
 
102
                unsigned int offset = 0;
 
103
                int err = regexec(*it, str.c_str(), 1, &pmatch, 0);
 
104
                while (err == 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);
 
113
                }
 
114
        }
 
115
}
 
116
 
 
117
}