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