From cf49d20afa71c47d3edc314c49afa3b755c35204 Mon Sep 17 00:00:00 2001 From: TwinFan Date: Mon, 22 Jan 2024 23:15:06 +0100 Subject: [PATCH] Fix/Config: Allow for multi-word values --- src/Utilities.cpp | 51 ++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/Utilities.cpp b/src/Utilities.cpp index cf17dcd..203f3c4 100755 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -55,7 +55,7 @@ XPMPPlaneID GlobVars::NextPlaneId () /// @details Goal is to provide some few configuration options independend /// of the plugin using XPMP2. This is useful e.g. when configuring /// non-standard network settings for remote connections.\n -/// For even greate flexibility the function will try a plugin-specific +/// For even greater flexibility the function will try a plugin-specific /// file first, named "XPMP2..prf" (with spaces replaced /// with underscores) and if that is not found /// it tries the generic file "XPMP2.prf".\n @@ -87,7 +87,6 @@ void GlobVars::ReadConfigFile () } // Read from the file - std::vector ln; std::string lnBuf; int errCnt = 0; while (fIn && errCnt <= 3) { @@ -96,43 +95,45 @@ void GlobVars::ReadConfigFile () lnBuf[0] == '#') continue; - // otherwise should be 2 tokens - ln = str_tokenize(lnBuf, " "); - if (ln.size() != 2) { - // wrong number of words in that line - LOG_MSG(logWARN,"Expected two words (key, value) in config file '%s', line '%s' -> ignored", - cfgFileName.c_str(), lnBuf.c_str()); + // otherwise should be 2 tokens, separated by the first space + size_t spcPos = lnBuf.find(" "); + if (spcPos == std::string::npos || spcPos == 0 || spcPos == lnBuf.length()-1) { + // Too few words in that line + LOG_MSG(logWARN, "Expected at least two words (key, value) in config file '%s', line '%s' -> ignored", + cfgFileName.c_str(), lnBuf.c_str()); errCnt++; continue; } + const std::string sKey = lnBuf.substr(0, spcPos); + std::string sVal = lnBuf.substr(spcPos + 1); // *** Process actual configuration entries *** int iVal = 0; - try {iVal = (int) std::stol(ln[1]); } + try {iVal = (int) std::stol(sVal); } catch (...) { iVal = 0; } - if (ln[0] == "logLvl") logLvl = (logLevelTy) clamp(iVal, int(logDEBUG), int(logFATAL)); - else if (ln[0] == "defaultICAO") defaultICAO = ln[1]; - else if (ln[0] == "carIcaoType") carIcaoType = ln[1]; - else if (ln[0] == "remoteSupport") { - str_tolower(ln[1]); - if (ln[1] == "on") glob.remoteCfg = glob.remoteCfgFromIni = REMOTE_CFG_ON; - else if (ln[1] == "auto") glob.remoteCfg = glob.remoteCfgFromIni = REMOTE_CFG_AUTO; - else if (ln[1] == "off") glob.remoteCfg = glob.remoteCfgFromIni = REMOTE_CFG_OFF; + if (sKey == "logLvl") logLvl = (logLevelTy) clamp(iVal, int(logDEBUG), int(logFATAL)); + else if (sKey == "defaultICAO") defaultICAO = sVal; + else if (sKey == "carIcaoType") carIcaoType = sVal; + else if (sKey == "remoteSupport") { + str_tolower(sVal); + if (sVal == "on") glob.remoteCfg = glob.remoteCfgFromIni = REMOTE_CFG_ON; + else if (sVal == "auto") glob.remoteCfg = glob.remoteCfgFromIni = REMOTE_CFG_AUTO; + else if (sVal == "off") glob.remoteCfg = glob.remoteCfgFromIni = REMOTE_CFG_OFF; else { LOG_MSG(logWARN, "Ignored unknown value '%s' for 'remoteSupport' in file '%s'", - ln[1].c_str(), cfgFileName.c_str()); + sVal.c_str(), cfgFileName.c_str()); } } - else if (ln[0] == "remoteMCGroup") remoteMCGroup = ln[1]; - else if (ln[0] == "remotePort") remotePort = iVal; - else if (ln[0] == "remoteSendIntf") remoteSendIntf = ln[1]; - else if (ln[0] == "remoteTTL") remoteTTL = iVal; - else if (ln[0] == "remoteBufSize") remoteBufSize = (size_t)iVal; - else if (ln[0] == "remoteTxfFrequ") remoteTxfFrequ = iVal; + else if (sKey == "remoteMCGroup") remoteMCGroup = sVal; + else if (sKey == "remotePort") remotePort = iVal; + else if (sKey == "remoteSendIntf") remoteSendIntf = sVal; + else if (sKey == "remoteTTL") remoteTTL = iVal; + else if (sKey == "remoteBufSize") remoteBufSize = (size_t)iVal; + else if (sKey == "remoteTxfFrequ") remoteTxfFrequ = iVal; else { LOG_MSG(logWARN, "Ignored unknown config item '%s' from file '%s'", - ln[0].c_str(), cfgFileName.c_str()); + sKey.c_str(), cfgFileName.c_str()); } }