Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for regex in unitTagsFile #764

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions docs/CONFIGURE.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions trunk-recorder/call_concluder/call_concluder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ Call_Data_t Call_Concluder::create_call_data(Call *call, System *sys, Config con
call_info.stop_time = t.stop_time;
}

UnitTag *unit_tag = sys->find_unit_tag(t.source);
std::string tag = (unit_tag == NULL || unit_tag->tag.empty() ? "" : unit_tag->tag);
std::string tag = sys->find_unit_tag(t.source);
Call_Source call_source = {t.source, t.start_time, total_length, false, "", tag};
Call_Error call_error = {t.start_time, total_length, t.length, t.error_count, t.spike_count};
call_info.transmission_source_list.push_back(call_source);
Expand Down
2 changes: 1 addition & 1 deletion trunk-recorder/systems/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class System {
virtual void set_source(Source *) = 0;
virtual Talkgroup *find_talkgroup(long tg) = 0;
virtual Talkgroup *find_talkgroup_by_freq(double freq) = 0;
virtual UnitTag *find_unit_tag(long unitID) = 0;
virtual std::string find_unit_tag(long unitID) = 0;
virtual void set_talkgroups_file(std::string) = 0;
virtual void set_channel_file(std::string channel_file) = 0;
virtual bool has_channel_file() = 0;
Expand Down
2 changes: 1 addition & 1 deletion trunk-recorder/systems/system_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ Talkgroup *System_impl::find_talkgroup(long tg_number) {
Talkgroup *System_impl::find_talkgroup_by_freq(double freq) {
return talkgroups->find_talkgroup_by_freq(sys_num, freq);
}
UnitTag *System_impl::find_unit_tag(long unitID) {
std::string System_impl::find_unit_tag(long unitID) {
return unit_tags->find_unit_tag(unitID);
}

Expand Down
2 changes: 1 addition & 1 deletion trunk-recorder/systems/system_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class System_impl : public System {
void set_source(Source *);
Talkgroup *find_talkgroup(long tg);
Talkgroup *find_talkgroup_by_freq(double freq);
UnitTag *find_unit_tag(long unitID);
std::string find_unit_tag(long unitID);
void set_talkgroups_file(std::string);
void set_channel_file(std::string channel_file);
bool has_channel_file();
Expand Down
5 changes: 3 additions & 2 deletions trunk-recorder/unit_tag.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "unit_tag.h"
#include <boost/regex.hpp>

UnitTag::UnitTag(long num, std::string t) {
number = num;
UnitTag::UnitTag(std::string p, std::string t) {
pattern = p;
tag = t;
}
6 changes: 3 additions & 3 deletions trunk-recorder/unit_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <iostream>
#include <stdio.h>
#include <string>
//#include <sstream>
#include <boost/regex.hpp>

class UnitTag {
public:
long number;
boost::regex pattern;
std::string tag;

UnitTag(long num, std::string t);
UnitTag(std::string p, std::string t);
};

#endif // UNIT_TAG_H
27 changes: 19 additions & 8 deletions trunk-recorder/unit_tags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <boost/intrusive_ptr.hpp>
#include <boost/log/trivial.hpp>
#include <boost/tokenizer.hpp>
#include <boost/regex.hpp>

#include "csv_helper.h"
#include <cstdio>
Expand Down Expand Up @@ -60,7 +61,7 @@ void UnitTags::load_unit_tags(std::string filename) {
continue;
}

add(atoi(vec[0].c_str()), vec[1].c_str());
add(vec[0].c_str(), vec[1].c_str());

lines_pushed++;
}
Expand All @@ -75,21 +76,31 @@ void UnitTags::load_unit_tags(std::string filename) {
}
}

UnitTag *UnitTags::find_unit_tag(long tg_number) {
UnitTag *tg_match = NULL;
std::string UnitTags::find_unit_tag(long tg_number) {
std::string tg_num_str = std::to_string(tg_number);
std::string tag = "";

for (std::vector<UnitTag *>::iterator it = unit_tags.begin(); it != unit_tags.end(); ++it) {
UnitTag *tg = (UnitTag *)*it;

if (tg->number == tg_number) {
tg_match = tg;
if (regex_match(tg_num_str, tg->pattern)) {
tag = regex_replace(tg_num_str, tg->pattern, tg->tag, boost::regex_constants::format_no_copy);
break;
}
}
return tg_match;

return tag;
}

void UnitTags::add(long num, std::string tag) {
UnitTag *unit_tag = new UnitTag(num, tag);
void UnitTags::add(std::string pattern, std::string tag) {
// If the pattern is like /someregex/
if (pattern.substr(0, 1).compare("/") == 0 && pattern.substr(pattern.length()-1, 1).compare("/") == 0) {
// then remove the / at the beginning and end
pattern = pattern.substr(1, pattern.length()-2);
} else {
// otherwise add ^ and $ to the pattern e.g. ^123$ to make a regex for simple IDs
pattern = "^" + pattern + "$";
}
UnitTag *unit_tag = new UnitTag(pattern, tag);
unit_tags.push_back(unit_tag);
}
4 changes: 2 additions & 2 deletions trunk-recorder/unit_tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UnitTags {
public:
UnitTags();
void load_unit_tags(std::string filename);
UnitTag *find_unit_tag(long unitID);
void add(long num, std::string tag);
std::string find_unit_tag(long unitID);
void add(std::string pattern, std::string tag);
};
#endif // UNIT_TAGS_H