Ref T709, move case insensitive compare to utils (so it can be used elsewhere)

This commit is contained in:
Klaus Basan
2019-07-25 16:05:21 +02:00
committed by Mat Sutcliffe
parent 14bb3ea249
commit cc4b8b61ae
2 changed files with 19 additions and 15 deletions

View File

@@ -129,6 +129,16 @@ namespace BlackMisc
return s; return s;
} }
//! Compare case insensitive
inline bool stringCompareCaseInsensitive(const std::string &str1, const std::string &str2)
{
if (str1.size() != str2.size()) { return false; }
return std::equal(str1.begin(), str1.end(), str2.begin(), [](const char &c1, const char &c2)
{
return (c1 == c2 || std::toupper(c1) == std::toupper(c2));
});
}
//! Trim whitespace from the beginning and end, and replace sequences of whitespace with single space characters //! Trim whitespace from the beginning and end, and replace sequences of whitespace with single space characters
inline std::string simplifyWhitespace(const std::string &s) inline std::string simplifyWhitespace(const std::string &s)
{ {

View File

@@ -8,23 +8,17 @@
#include "config.h" #include "config.h"
#include "utils.h" #include "utils.h"
#include "blackmisc/simulation/xplane/qtfreeutils.h"
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <cctype> #include <cctype>
#include <algorithm> #include <algorithm>
using namespace BlackMisc::Simulation::XPlane::QtFreeUtils;
namespace XSwiftBus namespace XSwiftBus
{ {
//! Case insensitive string compare
bool stringCompare(const std::string & str1, const std::string &str2)
{
if (str1.size() != str2.size()) { return false; }
return std::equal(str1.begin(), str1.end(), str2.begin(), [](const char & c1, const char & c2)
{
return (c1 == c2 || std::toupper(c1) == std::toupper(c2));
});
}
CConfig::CConfig() CConfig::CConfig()
{} {}
@@ -62,9 +56,9 @@ namespace XSwiftBus
} }
bool valid = true; bool valid = true;
if (stringCompare(key, "dbusMode")) { valid = parseDBusMode(value); } if (stringCompareCaseInsensitive(key, "dbusMode")) { valid = parseDBusMode(value); }
else if (stringCompare(key, "dbusAddress")) { valid = parseDBusAddress(value); } else if (stringCompareCaseInsensitive(key, "dbusAddress")) { valid = parseDBusAddress(value); }
else if (stringCompare(key, "dbusPort")) { valid = parseDBusPort(value); } else if (stringCompareCaseInsensitive(key, "dbusPort")) { valid = parseDBusPort(value); }
else else
{ {
WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Unknown variable " + value + "!"); WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Unknown variable " + value + "!");
@@ -93,8 +87,8 @@ namespace XSwiftBus
bool CConfig::parseDBusMode(const std::string &value) bool CConfig::parseDBusMode(const std::string &value)
{ {
if (stringCompare(value, "session")) { m_dbusMode = CConfig::DBusSession; return true; } if (stringCompareCaseInsensitive(value, "session")) { m_dbusMode = CConfig::DBusSession; return true; }
else if (stringCompare(value, "P2P")) { m_dbusMode = CConfig::DBusP2P; return true; } else if (stringCompareCaseInsensitive(value, "P2P")) { m_dbusMode = CConfig::DBusP2P; return true; }
else { return false; } else { return false; }
} }