libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
cfg.hpp
Go to the documentation of this file.
1/**
2 * @file cfg.hpp
3 * @brief INI-style configuration file reader/writer.
4 *
5 * ConfigFile parses simple section/key/value configuration files and
6 * provides random-access retrieval and modification of settings.
7 */
8 #ifndef CFG_H_X
9#define CFG_H_X
10
11#include<iostream>
12#include<fstream>
13#include<string>
14#include<unordered_map>
15#include<vector>
16
17namespace mx {
18
19/**
20 * @class Item
21 * @brief A single key/value pair read from a configuration file.
22 */
23 class Item {
24 public:
25 std::string key; ///< Configuration key name.
26 std::string value; ///< Value associated with the key.
27 };
28
29/**
30 * @class ConfigFile
31 * @brief Parses and manages an INI-style configuration file.
32 *
33 * Sections are denoted by [SectionName] headers. Items within a section
34 * are stored as key=value pairs. The file can be loaded, queried,
35 * modified in memory, and saved back to disk.
36 */
37 class ConfigFile {
38 std::unordered_map<std::string, std::unordered_map<std::string, Item>> values;
39 std::string filename;
40 public:
41 /** @brief Default constructor — no file loaded. */
42 ConfigFile() = default;
43
44 /**
45 * @brief Open and parse a configuration file.
46 * @param filex Path to the configuration file.
47 */
48 ConfigFile(const std::string &filex);
49
50 /** @brief Destructor — closes any open file resources. */
52
53 ConfigFile(const ConfigFile&) = delete;
54 ConfigFile& operator=(const ConfigFile&) = 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 * @return The matching Item, or a default-constructed one if not found.
63 */
64 Item itemAtKey(const std::string &section, const std::string &key);
65
66 /**
67 * @brief Insert or update a configuration item.
68 * @param section Section name.
69 * @param key Key name.
70 * @param value New value string.
71 */
72 void setItem(const std::string &section, const std::string &key, const std::string value);
73
74 /**
75 * @brief Load (or reload) a configuration file from disk.
76 * @param f Path to the file.
77 */
78 void loadFile(const std::string &f);
79
80 /**
81 * @brief Save the current configuration to a file.
82 * @param f2 Destination file path.
83 */
84 void saveFile(const std::string &f2);
85
86 /**
87 * @brief Split a string on comma delimiters.
88 * @param str Source string.
89 * @return Vector of substrings between commas.
90 */
91 std::vector<std::string> splitByComma(const std::string &str);
92 };
93}
94
95#endif
ConfigFile & operator=(const ConfigFile &)=delete
std::vector< std::string > splitByComma(const std::string &str)
Split a string on comma delimiters.
Definition cfg.cpp:90
void loadFile(const std::string &f)
Load (or reload) a configuration file from disk.
Definition cfg.cpp:12
Item itemAtKey(const std::string &section, const std::string &key)
Retrieve a configuration item by section and key.
Definition cfg.cpp:77
void setItem(const std::string &section, const std::string &key, const std::string value)
Insert or update a configuration item.
Definition cfg.cpp:86
std::string filename
Definition cfg.hpp:39
void saveFile(const std::string &f2)
Save the current configuration to a file.
Definition cfg.cpp:46
ConfigFile()=default
Default constructor — no file loaded.
ConfigFile(const ConfigFile &)=delete
ConfigFile(ConfigFile &&)=delete
~ConfigFile()
Destructor — closes any open file resources.
Definition cfg.cpp:108
std::unordered_map< std::string, std::unordered_map< std::string, Item > > values
Definition cfg.hpp:38
ConfigFile & operator=(ConfigFile &&)=delete
A single key/value pair read from a configuration file.
Definition cfg.hpp:23
std::string key
Configuration key name.
Definition cfg.hpp:25
std::string value
Value associated with the key.
Definition cfg.hpp:26
Definition cfg.cpp:11