17using namespace swift::misc::simulation::xplane::qtfreeutils;
27 std::ifstream configFile(m_filePath);
28 if (!configFile.is_open())
34 for (
int lineNo = 1; std::getline(configFile, line); ++lineNo)
36 line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
37 if (line.empty() || line[0] ==
'#')
42 auto delimiterPos = line.find(
"=");
43 if (delimiterPos == std::string::npos)
45 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Skipping invalid line!");
49 std::string key = line.substr(0, delimiterPos);
50 std::string value = line.substr(delimiterPos + 1);
52 if (key.empty() || value.empty())
54 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Skipping invalid line!");
61 valid = parseDBusMode(value);
65 valid = parseDBusAddress(value);
69 valid = parseDBusPort(value);
73 valid = parseDebug(value);
77 valid = parseTcas(value);
81 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Unknown variable " + key +
"!");
87 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Skipping invalid line!");
96 DEBUG_LOG(
"DBus mode: " + dbusModeToString(m_dbusMode));
97 DEBUG_LOG(
"DBus server address: " + m_dbusAddress);
98 DEBUG_LOG(
"DBus server port: " + std::to_string(m_dbusPort));
105 return writeConfigFile();
108 bool CConfig::writeConfigFile()
const
110 std::ofstream configFile(m_filePath, std::ofstream::out | std::ofstream::trunc);
111 if (!configFile.is_open())
117 configFile <<
"# DBus Mode - Options: p2p, session" << std::endl;
118 configFile <<
"dbusMode = " <<
toLower(dbusModeToString(m_dbusMode)) << std::endl;
119 configFile << std::endl;
120 configFile <<
"# DBus server address - relevant for P2P mode only" << std::endl;
121 configFile <<
"dbusAddress = " << m_dbusAddress << std::endl;
122 configFile << std::endl;
123 configFile <<
"# DBus server port - relevant for P2P mode only" << std::endl;
124 configFile <<
"dbusPort = " << m_dbusPort << std::endl;
125 configFile << std::endl;
126 configFile <<
"# Render phase debugging - to help diagnose crashes" << std::endl;
127 configFile <<
"debug = " <<
boolToOnOff(m_debug) << std::endl;
128 configFile << std::endl;
129 configFile <<
"# TCAS traffic - to disable in case of crashes" << std::endl;
130 configFile <<
"tcas = " <<
boolToOnOff(m_tcas) << std::endl;
133 const auto clockNow = std::chrono::system_clock::now();
134 const time_t now = std::chrono::system_clock::to_time_t(clockNow);
137 localtime_s(&tms, &now);
139 localtime_r(&now, &tms);
141 configFile << std::endl;
142 configFile <<
"# Updated by xswiftbus plugin " << std::put_time(&tms,
"%T");
143 configFile << std::endl;
148 bool CConfig::parseDBusMode(
const std::string &value)
152 m_dbusMode = CConfig::DBusSession;
157 m_dbusMode = CConfig::DBusP2P;
166 bool CConfig::parseDBusAddress(
const std::string &value)
168 m_dbusAddress = value;
172 bool CConfig::parseDBusPort(
const std::string &value)
177 port = std::stoi(value);
184 if (port < 0 || port > 65535)
192 bool CConfig::parseDebug(
const std::string &value)
205 bool CConfig::parseTcas(
const std::string &value)
211 std::string CConfig::dbusModeToString(DBusMode mode)
215 case DBusSession:
return "Session";
216 case DBusP2P:
return "P2P";
221 std::string CConfig::boolToOnOff(
bool on)
void setTcasEnabled(bool on)
Set TCAS traffic on/off.
void print()
Print the current configuration to the X-Plane log.
bool writeConfig(bool tcas, bool debug)
Update and write config file.
void parse()
Load and parse config file.
void setDebugMode(bool on)
Set debug mode.
Plugin loaded by X-Plane which publishes a DBus service.
SWIFT_MISC_EXPORT const QString & boolToOnOff(bool v)
Bool to on/off.
bool stringCompareCaseInsensitive(const std::string &str1, const std::string &str2)
Compare case insensitive.
std::string toLower(std::string s)
String to lower case.
#define DEBUG_LOG(msg)
Logger convenience macros.
#define WARNING_LOG(msg)
Logger convenience macros.