MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk_cfg.cpp
Go to the documentation of this file.
1/**
2 * @file mxvk_cfg.cpp
3 * @brief Implementation of INI-style config file reader/writer (mxvk::VK_Config).
4 */
5#include "mxvk/mxvk_cfg.hpp"
6
7#include <algorithm>
8#include <format>
9#include <ranges>
10
12
13namespace mxvk {
14 void VK_Config::loadFile(const std::string &f) {
15 std::ifstream in(f);
16 if (!in.is_open()) {
17 std::ofstream out(f);
18 if (!out.is_open()) {
19 throw mxvk::Exception(std::format("mxvk: Could not create configuration file: {}", f));
20 }
21
22 values.clear();
23 return;
24 }
25
26 values.clear();
27
28 std::string line, currentSection;
29 while (std::getline(in, line)) {
30 if (line.empty() || line[0] == ';' || line[0] == '#') {
31 continue;
32 }
33
34 if (line.front() == '[' && line.back() == ']') {
35 currentSection = line.substr(1, line.size() - 2);
36 continue;
37 }
38
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);
43
44 VK_ConfigItem item;
45 item.key = key;
46 item.value = value;
47
48 values[currentSection][key] = item;
49 }
50 }
51
52 in.close();
53 }
54 void VK_Config::saveFile(const std::string &f2) {
55 std::ofstream out(f2);
56 if (!out.is_open()) {
57 return;
58 }
59
60 std::vector<std::string> sectionNames;
61 for (const auto &section : values) {
62 sectionNames.push_back(section.first);
63 }
64 std::ranges::sort(sectionNames);
65
66 for (const auto &section : sectionNames) {
67 out << "[" << section << "]\n";
68
69 std::vector<std::string> keys;
70 for (const auto &pair : values[section]) {
71 keys.push_back(pair.first);
72 }
73 std::ranges::sort(keys);
74
75 for (const auto &key : keys) {
76 out << key << "=" << values[section][key].value << "\n";
77 }
78
79 out << "\n";
80 }
81
82 out.close();
83 }
84
85 VK_ConfigItem VK_Config::itemAtKey(const std::string &section, const std::string &key, const std::string &default_value) const {
86 const auto sectionIt = values.find(section);
87 if (sectionIt == values.end()) {
88 return {key, default_value};
89 }
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));
93 }
94 return keyIt->second;
95 }
96
97 void VK_Config::setItem(const std::string &section, const std::string &key, const std::string &value) {
98 values[section][key] = {key, value};
99 }
100
101 std::vector<std::string> VK_Config::splitByComma(const std::string &str) const {
102 std::vector<std::string> result;
103 size_t start = 0, end = 0;
104
105 while ((end = str.find(',', start)) != std::string::npos) {
106 result.push_back(str.substr(start, end - start));
107 start = end + 1;
108 }
109
110 result.push_back(str.substr(start));
111
112 return result;
113 }
114
115 VK_Config::VK_Config(const std::string &filePath) : file_name(filePath) {
116 loadFile(filePath);
117 }
118
120 if (!file_name.empty()) {
121 saveFile(file_name);
122 }
123 }
124
125} // namespace mxvk
A single key/value pair read from a configuration file.
Definition mxvk_cfg.hpp:22
std::string value
Value associated with the key.
Definition mxvk_cfg.hpp:25
std::string key
Configuration key name.
Definition mxvk_cfg.hpp:24
std::vector< std::string > splitByComma(const std::string &str) const
Split a string on comma delimiters.
Definition mxvk_cfg.cpp:101
void saveFile(const std::string &f2)
Save the current configuration to a file.
Definition mxvk_cfg.cpp:54
void loadFile(const std::string &f)
Load (or reload) a configuration file from disk.
Definition mxvk_cfg.cpp:14
VK_ConfigItem itemAtKey(const std::string &section, const std::string &key, const std::string &default_value="") const
Retrieve a configuration item by section and key.
Definition mxvk_cfg.cpp:85
~VK_Config()
Destructor — closes any open file resources.
Definition mxvk_cfg.cpp:119
VK_Config()=default
Default constructor — no file loaded.
void setItem(const std::string &section, const std::string &key, const std::string &value)
Insert or update a configuration item.
Definition mxvk_cfg.cpp:97
INI-style configuration file reader/writer.
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30