Line data Source code
1 : #include "wibmod/WIB1/AddressTable.hh"
2 : #include "wibmod/WIB1/AddressTableException.hh"
3 : #include <boost/algorithm/string/case_conv.hpp> //to_upper
4 : #include <boost/regex.hpp> //regex
5 : #include <boost/tokenizer.hpp> //tokenizer
6 : #include <fstream>
7 : #include <stdlib.h> //strtoul & getenv
8 :
9 : std::vector<std::string>
10 0 : AddressTable::GetNames()
11 : {
12 0 : std::vector<std::string> names;
13 0 : for (std::map<std::string, Item*>::iterator it = nameItemMap.begin(); it != nameItemMap.end(); it++) {
14 0 : names.push_back(it->first);
15 : }
16 0 : return names;
17 0 : }
18 :
19 : void
20 0 : ReplaceStringInPlace(std::string& subject, const std::string& search, const std::string& replace)
21 : {
22 0 : size_t pos = 0;
23 0 : while ((pos = subject.find(search, pos)) != std::string::npos) {
24 0 : subject.replace(pos, search.length(), replace);
25 0 : pos += replace.length();
26 : }
27 0 : }
28 : std::vector<std::string>
29 0 : AddressTable::GetNames(std::string const& regex)
30 : {
31 0 : std::vector<std::string> names;
32 : // Fix regex
33 0 : std::string rx = regex;
34 0 : std::transform(rx.begin(), rx.end(), rx.begin(), ::toupper);
35 0 : if (rx.size() > 6 && rx.substr(0, 5) == "PERL:") {
36 0 : printf("Using PERL-style regex unchanged\n");
37 0 : rx = rx.substr(5);
38 : } else {
39 0 : ReplaceStringInPlace(rx, ".", "#");
40 0 : ReplaceStringInPlace(rx, "*", ".*");
41 0 : ReplaceStringInPlace(rx, "#", "\\.");
42 : }
43 :
44 : // Create regex match
45 0 : boost::regex re;
46 0 : try {
47 0 : re = boost::regex(rx);
48 0 : } catch (std::exception& e) {
49 0 : BUException::BAD_REGEX e2;
50 0 : e2.Append("In GetNames: (");
51 0 : e2.Append(rx.c_str());
52 0 : e2.Append(") ");
53 0 : e2.Append(regex.c_str());
54 0 : throw e2;
55 0 : }
56 0 : boost::cmatch match;
57 0 : for (std::map<std::string, Item*>::iterator it = nameItemMap.begin(); it != nameItemMap.end(); it++) {
58 0 : if (regex_match(it->first.c_str(), match, re)) {
59 0 : names.push_back(it->first);
60 : }
61 : }
62 0 : return names;
63 0 : }
64 :
65 : std::vector<std::string>
66 0 : AddressTable::GetAddresses(uint16_t lower, uint16_t upper)
67 : {
68 0 : std::vector<std::string> names;
69 : // Get an iterator into our map of addresses to vectors of items that is the first entry that is not less than lower
70 0 : std::map<uint32_t, std::vector<Item*>>::iterator itAddress = addressItemMap.lower_bound(lower);
71 0 : for (; itAddress != addressItemMap.end(); itAddress++) {
72 : // loop over all following address keys
73 :
74 0 : if (itAddress->first < upper) {
75 : // Address key is less than uppper, so add its entries to names
76 0 : std::vector<Item*>& items = itAddress->second;
77 0 : for (size_t iItem = 0; iItem < items.size(); iItem++) {
78 0 : names.push_back(items[iItem]->name);
79 : }
80 : } else {
81 : // address key is greater than or equal to upper, so stop
82 : break;
83 : }
84 : }
85 0 : return names;
86 0 : }
87 :
88 : std::vector<std::string>
89 0 : AddressTable::GetTables(std::string const& regex)
90 : {
91 :
92 : // Fix regex
93 0 : std::string rx = regex;
94 : // std::transform( rx.begin(), rx.end(), rx.begin(), ::toupper);
95 0 : if (rx.size() > 6 && rx.substr(0, 5) == "PERL:") {
96 0 : printf("Using PERL-style regex unchanged\n");
97 0 : rx = rx.substr(5);
98 : } else {
99 0 : ReplaceStringInPlace(rx, ".", "#");
100 0 : ReplaceStringInPlace(rx, "*", ".*");
101 0 : ReplaceStringInPlace(rx, "#", "\\.");
102 : }
103 :
104 : // Create regex match
105 0 : boost::regex re;
106 0 : try {
107 0 : re = boost::regex(rx);
108 0 : } catch (std::exception& e) {
109 0 : BUException::BAD_REGEX e2;
110 0 : e2.Append("In GetTables: (");
111 0 : e2.Append(rx.c_str());
112 0 : e2.Append(") ");
113 0 : e2.Append(regex.c_str());
114 0 : throw e2;
115 0 : }
116 0 : std::set<std::string> tableSearch;
117 0 : boost::cmatch match;
118 0 : for (std::map<std::string, Item*>::iterator it = nameItemMap.begin(); it != nameItemMap.end(); it++) {
119 : // Check if this item has a table entry
120 0 : if (it->second->user.find("Table") != it->second->user.end()) {
121 0 : std::string const& tableName = it->second->user.find("Table")->second;
122 : // check if this table isn't already in the set
123 0 : if (tableSearch.find(tableName) == tableSearch.end()) {
124 : // Check if we should add it
125 0 : if (regex_match(tableName.c_str(), match, re)) {
126 : // Add this table to the set
127 0 : tableSearch.insert(tableName);
128 : }
129 : }
130 : }
131 : }
132 0 : std::vector<std::string> tables(tableSearch.begin(), tableSearch.end());
133 0 : return tables;
134 0 : }
135 :
136 : std::vector<const Item*>
137 0 : AddressTable::GetTagged(std::string const& tag)
138 : {
139 0 : std::vector<const Item*> matches;
140 0 : for (std::map<std::string, Item*>::iterator it = nameItemMap.begin(); it != nameItemMap.end(); it++) {
141 : // Check if this item has the tag as an entry
142 0 : if (it->second->user.find(tag) != it->second->user.end()) {
143 0 : matches.push_back(it->second);
144 : }
145 : }
146 0 : return matches;
147 0 : }
|