Support global keys in ini

This commit is contained in:
Tixx
2025-09-16 16:10:16 +02:00
parent 5b2eb0a1a0
commit 33b2030f97

View File

@@ -14,6 +14,7 @@
#include <openssl/evp.h> #include <openssl/evp.h>
#include <regex> #include <regex>
#include <string> #include <string>
#include <variant>
#include <vector> #include <vector>
#ifdef _WIN32 #ifdef _WIN32
@@ -108,8 +109,8 @@ namespace Utils {
return result; return result;
} }
#endif #endif
inline std::map<std::string, std::map<std::string, std::string>> ParseINI(const std::string& contents) { inline std::map<std::string, std::variant<std::map<std::string, std::string>, std::string>> ParseINI(const std::string& contents) {
std::map<std::string, std::map<std::string, std::string>> ini; std::map<std::string, std::variant<std::map<std::string, std::string>, std::string>> ini;
std::string currentSection; std::string currentSection;
auto sections = Split(contents, "\n"); auto sections = Split(contents, "\n");
@@ -136,18 +137,19 @@ namespace Utils {
if (line[0] == '[') { if (line[0] == '[') {
currentSection = line.substr(1, line.find(']') - 1); currentSection = line.substr(1, line.find(']') - 1);
} else { } else {
if (currentSection.empty()) {
invalidLineLog();
continue;
}
std::string key, value; std::string key, value;
size_t pos = line.find('='); size_t pos = line.find('=');
if (pos != std::string::npos) { if (pos != std::string::npos) {
key = line.substr(0, pos); key = line.substr(0, pos);
key = key.substr(0, key.find_last_not_of(" \t") + 1);
value = line.substr(pos + 1); value = line.substr(pos + 1);
ini[currentSection][key] = value; if (currentSection.empty()) {
ini[key] = value;
} else {
std::get<std::map<std::string, std::string>>(ini[currentSection])[key] = value;
}
} else { } else {
invalidLineLog(); invalidLineLog();
continue; continue;
@@ -157,7 +159,7 @@ namespace Utils {
value.erase(0, value.find_first_not_of(" \t")); value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t") + 1); value.erase(value.find_last_not_of(" \t") + 1);
ini[currentSection][key] = value; std::get<std::map<std::string, std::string>>(ini[currentSection])[key] = value;
} }
} }