19 throw mxvk::Exception(std::format(
"mxvk: Could not create configuration file: {}", f));
28 std::string line, currentSection;
29 while (std::getline(in, line)) {
30 if (line.empty() || line[0] ==
';' || line[0] ==
'#') {
34 if (line.front() ==
'[' && line.back() ==
']') {
35 currentSection = line.substr(1, line.size() - 2);
39 size_t pos = line.find(
'=');
40 if (pos != std::string::npos && !currentSection.empty()) {
41 std::string key = line.substr(0, pos);
42 std::string value = line.substr(pos + 1);
48 values[currentSection][key] = item;
55 std::ofstream out(f2);
60 std::vector<std::string> sectionNames;
61 for (
const auto §ion : values) {
62 sectionNames.push_back(section.first);
64 std::ranges::sort(sectionNames);
66 for (
const auto §ion : sectionNames) {
67 out <<
"[" << section <<
"]\n";
69 std::vector<std::string> keys;
70 for (
const auto &pair : values[section]) {
71 keys.push_back(pair.first);
73 std::ranges::sort(keys);
75 for (
const auto &key : keys) {
76 out << key <<
"=" << values[section][key].value <<
"\n";
86 const auto sectionIt = values.find(section);
87 if (sectionIt == values.end()) {
88 return {key, default_value};
90 const auto keyIt = sectionIt->second.find(key);
91 if (keyIt == sectionIt->second.end()) {
92 throw mxvk::Exception(std::format(
"mxvk: Could not find key '{}' in section '{}'.", key, section));
97 void VK_Config::setItem(
const std::string §ion,
const std::string &key,
const std::string &value) {
98 values[section][key] = {key, value};
102 std::vector<std::string> result;
103 size_t start = 0, end = 0;
105 while ((end = str.find(
',', start)) != std::string::npos) {
106 result.push_back(str.substr(start, end - start));
110 result.push_back(str.substr(start));
120 if (!file_name.empty()) {
A single key/value pair read from a configuration file.
std::string value
Value associated with the key.
std::string key
Configuration key name.
std::vector< std::string > splitByComma(const std::string &str) const
Split a string on comma delimiters.
void saveFile(const std::string &f2)
Save the current configuration to a file.
void loadFile(const std::string &f)
Load (or reload) a configuration file from disk.
VK_ConfigItem itemAtKey(const std::string §ion, const std::string &key, const std::string &default_value="") const
Retrieve a configuration item by section and key.
~VK_Config()
Destructor — closes any open file resources.
VK_Config()=default
Default constructor — no file loaded.
void setItem(const std::string §ion, const std::string &key, const std::string &value)
Insert or update a configuration item.
INI-style configuration file reader/writer.
Utilities for loading and saving PNG images.