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.hpp
Go to the documentation of this file.
1/**
2 * @file mxvk_cfg.hpp
3 * @brief INI-style configuration file reader/writer.
4 *
5 * VK_Config parses simple section/key/value configuration files and
6 * provides random-access retrieval and modification of settings.
7 */
8#pragma once
9
10#include <fstream>
11#include <iostream>
12#include <string>
13#include <unordered_map>
14#include <vector>
15
16namespace mxvk {
17
18 /**
19 * @class VK_ConfigItem
20 * @brief A single key/value pair read from a configuration file.
21 */
23 public:
24 std::string key; ///< Configuration key name.
25 std::string value; ///< Value associated with the key.
26 };
27
28 /**
29 * @class VK_Config
30 * @brief Parses and manages an INI-style configuration file.
31 *
32 * Sections are denoted by [SectionName] headers. Items within a section
33 * are stored as key=value pairs. The file can be loaded, queried,
34 * modified in memory, and saved back to disk.
35 */
36 class VK_Config {
37 std::unordered_map<std::string, std::unordered_map<std::string, VK_ConfigItem>> values;
38 std::string file_name;
39
40 public:
41 /** @brief Default constructor — no file loaded. */
42 VK_Config() = default;
43
44 /**
45 * @brief Open and parse a configuration file.
46 * @param filePath Path to the configuration file.
47 */
48 explicit VK_Config(const std::string &filePath);
49
50 /** @brief Destructor — closes any open file resources. */
51 ~VK_Config();
52
53 VK_Config(const VK_Config &) = delete;
54 VK_Config &operator=(const VK_Config &) = delete;
55 VK_Config(VK_Config &&) = delete;
57
58 /**
59 * @brief Retrieve a configuration item by section and key.
60 * @param section Section name (text inside [] brackets).
61 * @param key Key name within that section.
62 * @param default_value Value returned when the key is not present.
63 * @return The matching Item, or a default-constructed one if not found.
64 */
65 VK_ConfigItem itemAtKey(const std::string &section, const std::string &key, const std::string &default_value = "") const;
66
67 /**
68 * @brief Insert or update a configuration item.
69 * @param section Section name.
70 * @param key Key name.
71 * @param value New value string.
72 */
73 void setItem(const std::string &section, const std::string &key, const std::string &value);
74
75 /**
76 * @brief Load (or reload) a configuration file from disk.
77 *
78 * If the file does not exist, an empty file is created first.
79 * @param f Path to the file.
80 */
81 void loadFile(const std::string &f);
82
83 /**
84 * @brief Save the current configuration to a file.
85 * @param f2 Destination file path.
86 */
87 void saveFile(const std::string &f2);
88
89 /**
90 * @brief Split a string on comma delimiters.
91 * @param str Source string.
92 * @return Vector of substrings between commas.
93 */
94 std::vector<std::string> splitByComma(const std::string &str) const;
95 };
96
97 // Backward-compatible aliases for older call sites.
100} // 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
Parses and manages an INI-style configuration file.
Definition mxvk_cfg.hpp:36
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
VK_Config(const VK_Config &)=delete
void loadFile(const std::string &f)
Load (or reload) a configuration file from disk.
Definition mxvk_cfg.cpp:14
VK_Config & operator=(VK_Config &&)=delete
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(VK_Config &&)=delete
VK_Config & operator=(const VK_Config &)=delete
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
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
VK_Config ConfigFile
Definition mxvk_cfg.hpp:99
VK_ConfigItem Item
Definition mxvk_cfg.hpp:98