17 using namespace swift::misc::simulation::xplane::qtfreeutils;
23 CConfig::~CConfig() {}
27 std::ifstream configFile(m_filePath);
28 if (!configFile.is_open()) {
return; }
31 for (
int lineNo = 1; std::getline(configFile, line); ++lineNo)
33 line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
34 if (line.empty() || line[0] ==
'#') {
continue; }
36 auto delimiterPos = line.find(
"=");
37 if (delimiterPos == std::string::npos)
39 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Skipping invalid line!");
43 std::string key = line.substr(0, delimiterPos);
44 std::string value = line.substr(delimiterPos + 1);
46 if (key.empty() || value.empty())
48 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Skipping invalid line!");
60 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Unknown variable " + key +
"!");
66 WARNING_LOG(
"xswiftbus.conf line " + std::to_string(lineNo) +
": Skipping invalid line!");
75 DEBUG_LOG(
"DBus mode: " + dbusModeToString(m_dbusMode));
76 DEBUG_LOG(
"DBus server address: " + m_dbusAddress);
77 DEBUG_LOG(
"DBus server port: " + std::to_string(m_dbusPort));
80 bool CConfig::writeConfig(
bool tcas,
bool debug)
84 return writeConfigFile();
87 bool CConfig::writeConfigFile()
const
89 std::ofstream configFile(m_filePath, std::ofstream::out | std::ofstream::trunc);
90 if (!configFile.is_open()) {
return false; }
93 configFile <<
"# DBus Mode - Options: p2p, session" << std::endl;
94 configFile <<
"dbusMode = " <<
toLower(dbusModeToString(m_dbusMode)) << std::endl;
95 configFile << std::endl;
96 configFile <<
"# DBus server address - relevant for P2P mode only" << std::endl;
97 configFile <<
"dbusAddress = " << m_dbusAddress << std::endl;
98 configFile << std::endl;
99 configFile <<
"# DBus server port - relevant for P2P mode only" << std::endl;
100 configFile <<
"dbusPort = " << m_dbusPort << std::endl;
101 configFile << std::endl;
102 configFile <<
"# Render phase debugging - to help diagnose crashes" << std::endl;
103 configFile <<
"debug = " <<
boolToOnOff(m_debug) << std::endl;
104 configFile << std::endl;
105 configFile <<
"# TCAS traffic - to disable in case of crashes" << std::endl;
106 configFile <<
"tcas = " <<
boolToOnOff(m_tcas) << std::endl;
109 const auto clockNow = std::chrono::system_clock::now();
110 const time_t now = std::chrono::system_clock::to_time_t(clockNow);
113 localtime_s(&tms, &now);
115 localtime_r(&now, &tms);
117 configFile << std::endl;
118 configFile <<
"# Updated by xswiftbus plugin " << std::put_time(&tms,
"%T");
119 configFile << std::endl;
124 bool CConfig::parseDBusMode(
const std::string &value)
128 m_dbusMode = CConfig::DBusSession;
133 m_dbusMode = CConfig::DBusP2P;
136 else {
return false; }
139 bool CConfig::parseDBusAddress(
const std::string &value)
141 m_dbusAddress = value;
145 bool CConfig::parseDBusPort(
const std::string &value)
150 port = std::stoi(value);
157 if (port < 0 || port > 65535) {
return false; }
162 bool CConfig::parseDebug(
const std::string &value)
165 else { m_debug =
false; }
169 bool CConfig::parseTcas(
const std::string &value)
175 std::string CConfig::dbusModeToString(DBusMode mode)
179 case DBusSession:
return "Session";
180 case DBusP2P:
return "P2P";
187 if (on) {
return "on"; }
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.