Skip to content

Commit

Permalink
use class UserDB (user-info storage)
Browse files Browse the repository at this point in the history
  • Loading branch information
jg1uaa committed Mar 7, 2020
1 parent 9b15454 commit b7f5fdd
Showing 1 changed file with 16 additions and 85 deletions.
101 changes: 16 additions & 85 deletions DMRLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ CThread(),
m_filename(filename),
m_reloadTime(reloadTime),
m_table(),
m_mutex(),
m_stop(false)
{
}
Expand All @@ -41,7 +40,7 @@ CDMRLookup::~CDMRLookup()

bool CDMRLookup::read()
{
bool ret = load();
bool ret = m_table.load(m_filename);

if (m_reloadTime > 0U)
run();
Expand All @@ -61,7 +60,7 @@ void CDMRLookup::entry()

timer.clock();
if (timer.hasExpired()) {
load();
m_table.load(m_filename);
timer.start();
}
}
Expand All @@ -88,111 +87,43 @@ std::string CDMRLookup::findWithName(unsigned int id)
if (id == 0xFFFFFFU)
return std::string("ALL");

m_mutex.lock();
class CUserDBentry entry;
if (m_table.lookup(id, &entry)) {
// callsign is always available
callsign = entry.get(keyCALLSIGN);
if (!entry.get(keyFIRST_NAME).empty())
callsign += " " + entry.get(keyFIRST_NAME);

try {
callsign = m_table.at(id);
LogDebug("FindWithName =%s",callsign.c_str());

} catch (...) {
} else {
char text[10U];
::sprintf(text, "%u", id);
::snprintf(text, sizeof(text), "%u", id);
callsign = std::string(text);
}

m_mutex.unlock();

return callsign;
}
std::string CDMRLookup::find(unsigned int id)
{
std::string callsign;
std::string b;


if (id == 0xFFFFFFU)
return std::string("ALL");

m_mutex.lock();

try {
b = m_table.at(id);
size_t n = b.find(" ");
if (n > 0) {
callsign = b.substr(0,n);

} else {
LogDebug("b=%s",b.c_str());
callsign = b;
}

} catch (...) {
class CUserDBentry entry;
if (m_table.lookup(id, &entry)) {
callsign = entry.get(keyCALLSIGN);
} else {
char text[10U];
::sprintf(text, "%u", id);
::snprintf(text, sizeof(text), "%u", id);
callsign = std::string(text);
}

m_mutex.unlock();

return callsign;
}

bool CDMRLookup::exists(unsigned int id)
{
m_mutex.lock();

bool found = m_table.count(id) == 1U;

m_mutex.unlock();

return found;
return m_table.lookup(id, NULL);
}

bool CDMRLookup::load()
{
FILE* fp = ::fopen(m_filename.c_str(), "rt");
if (fp == NULL) {
LogWarning("Cannot open the DMR Id lookup file - %s", m_filename.c_str());
return false;
}

m_mutex.lock();

// Remove the old entries
m_table.clear();

char buffer[100U];
while (::fgets(buffer, 100U, fp) != NULL) {
if (buffer[0U] == '#')
continue;

char* p1 = ::strtok(buffer, " \t\r\n");
char* p2 = ::strtok(NULL, " \r\n"); // tokenize to eol to capture name as well

if (p1 != NULL && p2 != NULL) {
unsigned int id = (unsigned int)::atoi(p1);
for (char* p = p2; *p != 0x00U; p++) {

if(*p == 0x09U)
*p = 0x20U;

else
*p = ::toupper(*p);

}
m_table[id] = std::string(p2);
}
}

m_mutex.unlock();

::fclose(fp);

size_t size = m_table.size();
if (size == 0U)
return false;

LogInfo("Loaded %u Ids to the DMR callsign lookup table", size);

return true;
}

0 comments on commit b7f5fdd

Please sign in to comment.