From 334d5eb08c5594b805838299dd076ac2f3195fe5 Mon Sep 17 00:00:00 2001 From: phl0 Date: Sun, 18 Nov 2018 23:35:06 +0100 Subject: [PATCH] Some minor code beautification --- Conf.cpp | 176 +++++++++++++++++++++++----------------------- DAPNETGateway.cpp | 23 +++--- DAPNETGateway.h | 6 +- REGEX.h | 4 +- 4 files changed, 103 insertions(+), 106 deletions(-) diff --git a/Conf.cpp b/Conf.cpp index d532d5c..acca9cd 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -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 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 diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 60b3745..b7c47ec 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -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(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) { diff --git a/DAPNETGateway.h b/DAPNETGateway.h index 3b29a3e..d3dbb17 100644 --- a/DAPNETGateway.h +++ b/DAPNETGateway.h @@ -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 m_regexBlacklist; -// std::vector m_regexWhitelist; }; #endif diff --git a/REGEX.h b/REGEX.h index a1888bb..9d34187 100644 --- a/REGEX.h +++ b/REGEX.h @@ -36,8 +36,8 @@ public: private: - std::string m_regexFile; - std::vector m_regex; + std::string m_regexFile; + std::vector m_regex; }; #endif