DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
AddressTable_search.cpp
Go to the documentation of this file.
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
9std::vector<std::string>
11{
12 std::vector<std::string> names;
13 for (std::map<std::string, Item*>::iterator it = nameItemMap.begin(); it != nameItemMap.end(); it++) {
14 names.push_back(it->first);
15 }
16 return names;
17}
18
19void
20ReplaceStringInPlace(std::string& subject, const std::string& search, const std::string& replace)
21{
22 size_t pos = 0;
23 while ((pos = subject.find(search, pos)) != std::string::npos) {
24 subject.replace(pos, search.length(), replace);
25 pos += replace.length();
26 }
27}
28std::vector<std::string>
29AddressTable::GetNames(std::string const& regex)
30{
31 std::vector<std::string> names;
32 // Fix regex
33 std::string rx = regex;
34 std::transform(rx.begin(), rx.end(), rx.begin(), ::toupper);
35 if (rx.size() > 6 && rx.substr(0, 5) == "PERL:") {
36 printf("Using PERL-style regex unchanged\n");
37 rx = rx.substr(5);
38 } else {
39 ReplaceStringInPlace(rx, ".", "#");
40 ReplaceStringInPlace(rx, "*", ".*");
41 ReplaceStringInPlace(rx, "#", "\\.");
42 }
43
44 // Create regex match
45 boost::regex re;
46 try {
47 re = boost::regex(rx);
48 } catch (std::exception& e) {
49 BUException::BAD_REGEX e2;
50 e2.Append("In GetNames: (");
51 e2.Append(rx.c_str());
52 e2.Append(") ");
53 e2.Append(regex.c_str());
54 throw e2;
55 }
56 boost::cmatch match;
57 for (std::map<std::string, Item*>::iterator it = nameItemMap.begin(); it != nameItemMap.end(); it++) {
58 if (regex_match(it->first.c_str(), match, re)) {
59 names.push_back(it->first);
60 }
61 }
62 return names;
63}
64
65std::vector<std::string>
66AddressTable::GetAddresses(uint16_t lower, uint16_t upper)
67{
68 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 std::map<uint32_t, std::vector<Item*>>::iterator itAddress = addressItemMap.lower_bound(lower);
71 for (; itAddress != addressItemMap.end(); itAddress++) {
72 // loop over all following address keys
73
74 if (itAddress->first < upper) {
75 // Address key is less than uppper, so add its entries to names
76 std::vector<Item*>& items = itAddress->second;
77 for (size_t iItem = 0; iItem < items.size(); iItem++) {
78 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 return names;
86}
87
88std::vector<std::string>
89AddressTable::GetTables(std::string const& regex)
90{
91
92 // Fix regex
93 std::string rx = regex;
94 // std::transform( rx.begin(), rx.end(), rx.begin(), ::toupper);
95 if (rx.size() > 6 && rx.substr(0, 5) == "PERL:") {
96 printf("Using PERL-style regex unchanged\n");
97 rx = rx.substr(5);
98 } else {
99 ReplaceStringInPlace(rx, ".", "#");
100 ReplaceStringInPlace(rx, "*", ".*");
101 ReplaceStringInPlace(rx, "#", "\\.");
102 }
103
104 // Create regex match
105 boost::regex re;
106 try {
107 re = boost::regex(rx);
108 } catch (std::exception& e) {
109 BUException::BAD_REGEX e2;
110 e2.Append("In GetTables: (");
111 e2.Append(rx.c_str());
112 e2.Append(") ");
113 e2.Append(regex.c_str());
114 throw e2;
115 }
116 std::set<std::string> tableSearch;
117 boost::cmatch match;
118 for (std::map<std::string, Item*>::iterator it = nameItemMap.begin(); it != nameItemMap.end(); it++) {
119 // Check if this item has a table entry
120 if (it->second->user.find("Table") != it->second->user.end()) {
121 std::string const& tableName = it->second->user.find("Table")->second;
122 // check if this table isn't already in the set
123 if (tableSearch.find(tableName) == tableSearch.end()) {
124 // Check if we should add it
125 if (regex_match(tableName.c_str(), match, re)) {
126 // Add this table to the set
127 tableSearch.insert(tableName);
128 }
129 }
130 }
131 }
132 std::vector<std::string> tables(tableSearch.begin(), tableSearch.end());
133 return tables;
134}
135
136std::vector<const Item*>
137AddressTable::GetTagged(std::string const& tag)
138{
139 std::vector<const Item*> matches;
140 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 if (it->second->user.find(tag) != it->second->user.end()) {
143 matches.push_back(it->second);
144 }
145 }
146 return matches;
147}
void ReplaceStringInPlace(std::string &subject, const std::string &search, const std::string &replace)
std::map< uint32_t, std::vector< Item * > > addressItemMap
std::vector< std::string > GetAddresses(uint16_t lower, uint16_t upper)
std::vector< std::string > GetNames()
std::vector< Item const * > GetTagged(std::string const &tag)
std::map< std::string, Item * > nameItemMap
std::vector< std::string > GetTables(std::string const &regex)