use class UserDB (user-info storage)

This commit is contained in:
SASANO Takayoshi 2020-03-07 16:52:11 +09:00
parent 9b15454f96
commit b7f5fdd8a2
1 changed files with 16 additions and 85 deletions

View File

@ -30,7 +30,6 @@ CThread(),
m_filename(filename),
m_reloadTime(reloadTime),
m_table(),
m_mutex(),
m_stop(false)
{
}
@ -41,7 +40,7 @@ CDMRLookup::~CDMRLookup()
bool CDMRLookup::read()
{
bool ret = load();
bool ret = m_table.load(m_filename);
if (m_reloadTime > 0U)
run();
@ -61,7 +60,7 @@ void CDMRLookup::entry()
timer.clock();
if (timer.hasExpired()) {
load();
m_table.load(m_filename);
timer.start();
}
}
@ -88,20 +87,20 @@ 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)
@ -109,90 +108,22 @@ 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;
}