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::VK_Config Class Reference

Parses and manages an INI-style configuration file. More...

#include <mxvk/include/mxvk/mxvk_cfg.hpp>

Public Member Functions

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.
void loadFile (const std::string &f)
 Load (or reload) a configuration file from disk.
VK_Configoperator= (const VK_Config &)=delete
VK_Configoperator= (VK_Config &&)=delete
void saveFile (const std::string &f2)
 Save the current configuration to a file.
void setItem (const std::string &section, const std::string &key, const std::string &value)
 Insert or update a configuration item.
std::vector< std::string > splitByComma (const std::string &str) const
 Split a string on comma delimiters.
 VK_Config ()=default
 Default constructor — no file loaded.
 VK_Config (const std::string &filePath)
 Open and parse a configuration file.
 VK_Config (const VK_Config &)=delete
 VK_Config (VK_Config &&)=delete
 ~VK_Config ()
 Destructor — closes any open file resources.

Detailed Description

Parses and manages an INI-style configuration file.

Sections are denoted by [SectionName] headers. Items within a section are stored as key=value pairs. The file can be loaded, queried, modified in memory, and saved back to disk.

Definition at line 36 of file mxvk_cfg.hpp.

Constructor & Destructor Documentation

◆ VK_Config() [1/4]

mxvk::VK_Config::VK_Config ( )
default

Default constructor — no file loaded.

◆ VK_Config() [2/4]

mxvk::VK_Config::VK_Config ( const std::string & filePath)
explicit

Open and parse a configuration file.

Parameters
filePathPath to the configuration file.

Definition at line 115 of file mxvk_cfg.cpp.

115 : file_name(filePath) {
116 loadFile(filePath);
117 }
void loadFile(const std::string &f)
Load (or reload) a configuration file from disk.
Definition mxvk_cfg.cpp:14

◆ ~VK_Config()

mxvk::VK_Config::~VK_Config ( )

Destructor — closes any open file resources.

Definition at line 119 of file mxvk_cfg.cpp.

119 {
120 if (!file_name.empty()) {
121 saveFile(file_name);
122 }
123 }
void saveFile(const std::string &f2)
Save the current configuration to a file.
Definition mxvk_cfg.cpp:54

◆ VK_Config() [3/4]

mxvk::VK_Config::VK_Config ( const VK_Config & )
delete

◆ VK_Config() [4/4]

mxvk::VK_Config::VK_Config ( VK_Config && )
delete

Member Function Documentation

◆ itemAtKey()

VK_ConfigItem mxvk::VK_Config::itemAtKey ( const std::string & section,
const std::string & key,
const std::string & default_value = "" ) const

Retrieve a configuration item by section and key.

Parameters
sectionSection name (text inside [] brackets).
keyKey name within that section.
default_valueValue returned when the key is not present.
Returns
The matching Item, or a default-constructed one if not found.

Definition at line 85 of file mxvk_cfg.cpp.

85 {
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 }

◆ loadFile()

void mxvk::VK_Config::loadFile ( const std::string & f)

Load (or reload) a configuration file from disk.

If the file does not exist, an empty file is created first.

Parameters
fPath to the file.

Definition at line 14 of file mxvk_cfg.cpp.

14 {
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 }

◆ operator=() [1/2]

VK_Config & mxvk::VK_Config::operator= ( const VK_Config & )
delete

◆ operator=() [2/2]

VK_Config & mxvk::VK_Config::operator= ( VK_Config && )
delete

◆ saveFile()

void mxvk::VK_Config::saveFile ( const std::string & f2)

Save the current configuration to a file.

Parameters
f2Destination file path.

Definition at line 54 of file mxvk_cfg.cpp.

54 {
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 }

◆ setItem()

void mxvk::VK_Config::setItem ( const std::string & section,
const std::string & key,
const std::string & value )

Insert or update a configuration item.

Parameters
sectionSection name.
keyKey name.
valueNew value string.

Definition at line 97 of file mxvk_cfg.cpp.

97 {
98 values[section][key] = {key, value};
99 }

◆ splitByComma()

std::vector< std::string > mxvk::VK_Config::splitByComma ( const std::string & str) const

Split a string on comma delimiters.

Parameters
strSource string.
Returns
Vector of substrings between commas.

Definition at line 101 of file mxvk_cfg.cpp.

101 {
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 }

The documentation for this class was generated from the following files: