Some minor code beautification

This commit is contained in:
phl0 2018-11-18 23:35:06 +01:00
parent 3a44ac4aa6
commit 334d5eb08c
No known key found for this signature in database
GPG key ID: 48EA1E640798CA9A
4 changed files with 103 additions and 106 deletions

176
Conf.cpp
View file

@ -61,99 +61,99 @@ CConf::~CConf()
bool CConf::read()
{
FILE* fp = ::fopen(m_file.c_str(), "rt");
if (fp == NULL) {
::fprintf(stderr, "Couldn't open the .ini file - %s\n", m_file.c_str());
return false;
}
SECTION section = SECTION_NONE;
char buffer[BUFFER_SIZE];
while (::fgets(buffer, BUFFER_SIZE, fp) != NULL) {
if (buffer[0U] == '#')
continue;
if (buffer[0U] == '[') {
if (::strncmp(buffer, "[General]", 9U) == 0)
section = SECTION_GENERAL;
else if (::strncmp(buffer, "[Log]", 5U) == 0)
section = SECTION_LOG;
else if (::strncmp(buffer, "[DAPNET]", 8U) == 0)
section = SECTION_DAPNET;
else
section = SECTION_NONE;
continue;
}
char* key = ::strtok(buffer, " \t=\r\n");
if (key == NULL)
continue;
char* value = ::strtok(NULL, "\r\n");
if (section == SECTION_GENERAL) {
if (::strcmp(key, "Callsign") == 0) {
for (unsigned int i = 0U; value[i] != '\0'; i++) {
if (!::isspace(value[i]))
m_callsign.insert(m_callsign.end(), 1, value[i]);
}
}
else if (::strcmp(key, "WhiteList") == 0) {
char* p = ::strtok(value, ",\r\n");
while (p != NULL) {
unsigned int ric = (unsigned int)::atoi(p);
if (ric > 0U)
m_whiteList.push_back(ric);
p = ::strtok(NULL, ",\r\n");
}
} else if (::strcmp(key,"BlacklistRegexfile") == 0)
m_blacklistRegexfile = value;
else if (::strcmp(key,"WhitelistRegexfile") == 0)
m_whitelistRegexfile = value;
else if (::strcmp(key, "RptAddress") == 0)
m_rptAddress = value;
else if (::strcmp(key, "RptPort") == 0)
m_rptPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "LocalAddress") == 0)
m_myAddress = value;
else if (::strcmp(key, "LocalPort") == 0)
m_myPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "Daemon") == 0)
m_daemon = ::atoi(value) == 1;
} else if (section == SECTION_LOG) {
if (::strcmp(key, "FilePath") == 0)
m_logFilePath = value;
else if (::strcmp(key, "FileRoot") == 0)
m_logFileRoot = value;
else if (::strcmp(key, "FileLevel") == 0)
m_logFileLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "DisplayLevel") == 0)
m_logDisplayLevel = (unsigned int)::atoi(value);
} else if (section == SECTION_DAPNET) {
if (::strcmp(key, "Address") == 0)
m_dapnetAddress = value;
else if (::strcmp(key, "Port") == 0)
m_dapnetPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "AuthKey") == 0) {
for (unsigned int i = 0U; value[i] != '\0'; i++) {
if (!::isspace(value[i]))
m_dapnetAuthKey.insert(m_dapnetAuthKey.end(), 1, value[i]);
}
}
else if (::strcmp(key, "Debug") == 0)
m_dapnetDebug = ::atoi(value) == 1;
FILE* fp = ::fopen(m_file.c_str(), "rt");
if (fp == NULL) {
::fprintf(stderr, "Couldn't open the .ini file - %s\n", m_file.c_str());
return false;
}
}
::fclose(fp);
SECTION section = SECTION_NONE;
return true;
char buffer[BUFFER_SIZE];
while (::fgets(buffer, BUFFER_SIZE, fp) != NULL) {
if (buffer[0U] == '#')
continue;
if (buffer[0U] == '[') {
if (::strncmp(buffer, "[General]", 9U) == 0)
section = SECTION_GENERAL;
else if (::strncmp(buffer, "[Log]", 5U) == 0)
section = SECTION_LOG;
else if (::strncmp(buffer, "[DAPNET]", 8U) == 0)
section = SECTION_DAPNET;
else
section = SECTION_NONE;
continue;
}
char* key = ::strtok(buffer, " \t=\r\n");
if (key == NULL)
continue;
char* value = ::strtok(NULL, "\r\n");
if (section == SECTION_GENERAL) {
if (::strcmp(key, "Callsign") == 0) {
for (unsigned int i = 0U; value[i] != '\0'; i++) {
if (!::isspace(value[i]))
m_callsign.insert(m_callsign.end(), 1, value[i]);
}
}
else if (::strcmp(key, "WhiteList") == 0) {
char* p = ::strtok(value, ",\r\n");
while (p != NULL) {
unsigned int ric = (unsigned int)::atoi(p);
if (ric > 0U)
m_whiteList.push_back(ric);
p = ::strtok(NULL, ",\r\n");
}
} else if (::strcmp(key,"BlacklistRegexfile") == 0)
m_blacklistRegexfile = value;
else if (::strcmp(key,"WhitelistRegexfile") == 0)
m_whitelistRegexfile = value;
else if (::strcmp(key, "RptAddress") == 0)
m_rptAddress = value;
else if (::strcmp(key, "RptPort") == 0)
m_rptPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "LocalAddress") == 0)
m_myAddress = value;
else if (::strcmp(key, "LocalPort") == 0)
m_myPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "Daemon") == 0)
m_daemon = ::atoi(value) == 1;
} else if (section == SECTION_LOG) {
if (::strcmp(key, "FilePath") == 0)
m_logFilePath = value;
else if (::strcmp(key, "FileRoot") == 0)
m_logFileRoot = value;
else if (::strcmp(key, "FileLevel") == 0)
m_logFileLevel = (unsigned int)::atoi(value);
else if (::strcmp(key, "DisplayLevel") == 0)
m_logDisplayLevel = (unsigned int)::atoi(value);
} else if (section == SECTION_DAPNET) {
if (::strcmp(key, "Address") == 0)
m_dapnetAddress = value;
else if (::strcmp(key, "Port") == 0)
m_dapnetPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "AuthKey") == 0) {
for (unsigned int i = 0U; value[i] != '\0'; i++) {
if (!::isspace(value[i]))
m_dapnetAuthKey.insert(m_dapnetAuthKey.end(), 1, value[i]);
}
}
else if (::strcmp(key, "Debug") == 0)
m_dapnetDebug = ::atoi(value) == 1;
}
}
::fclose(fp);
return true;
}
std::string CConf::getCallsign() const
{
return m_callsign;
return m_callsign;
}
std::vector<uint32_t> CConf::getWhiteList() const
@ -208,12 +208,12 @@ unsigned int CConf::getLogFileLevel() const
std::string CConf::getLogFilePath() const
{
return m_logFilePath;
return m_logFilePath;
}
std::string CConf::getLogFileRoot() const
{
return m_logFileRoot;
return m_logFileRoot;
}
std::string CConf::getDAPNETAddress() const

View file

@ -275,8 +275,8 @@ int CDAPNETGateway::run()
regexBlacklist = m_regexBlacklist->get();
m_regexWhitelist = new CREGEX(m_conf.getwhitelistRegexfile());
if (m_regexWhitelist->load())
regexWhitelist = m_regexWhitelist->get();
if (m_regexWhitelist->load())
regexWhitelist = m_regexWhitelist->get();
@ -318,7 +318,7 @@ int CDAPNETGateway::run()
// If we have a white list of RICs, use it.
if (!whiteList.empty())
found = std::find(whiteList.begin(), whiteList.end(), message->m_ric) != whiteList.end();
std::string messageBody(reinterpret_cast<char*>(message->m_message));
//If we have a list of blacklist REGEXes, use them
if (!regexBlacklist.empty()) {
@ -333,15 +333,14 @@ int CDAPNETGateway::run()
}
if(!regexWhitelist.empty() && !blacklistRegexmatch) {
for (std::regex regex : regexWhitelist) {
bool ret = std::regex_match(messageBody,regex);
//If the regex does not match the message body, don't send the message
if (!ret) {
whitelistRegexmatch = false;
LogDebug("No whitelist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, messageBody.c_str());
}
}
for (std::regex regex : regexWhitelist) {
bool ret = std::regex_match(messageBody,regex);
//If the regex does not match the message body, don't send the message
if (!ret) {
whitelistRegexmatch = false;
LogDebug("No whitelist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, messageBody.c_str());
}
}
}
if (found && !blacklistRegexmatch && whitelistRegexmatch) {

View file

@ -49,8 +49,8 @@ private:
bool m_allSlots;
unsigned int m_currentSlot;
unsigned int m_sentCodewords;
CREGEX* m_regexBlacklist;
CREGEX* m_regexWhitelist;
CREGEX* m_regexBlacklist;
CREGEX* m_regexWhitelist;
bool m_mmdvmFree;
@ -61,8 +61,6 @@ private:
void loadSchedule();
bool sendMessage(CPOCSAGMessage* message) const;
// std::vector<std::regex> m_regexBlacklist;
// std::vector<std::regex> m_regexWhitelist;
};
#endif

View file

@ -36,8 +36,8 @@ public:
private:
std::string m_regexFile;
std::vector<std::regex> m_regex;
std::string m_regexFile;
std::vector<std::regex> m_regex;
};
#endif