#include <ConfigParser.hpp>
|
| | ConfigParser (std::filesystem::path configPath) |
| |
| bool | isValid () const |
| |
| const std::string & | getLastError () const |
| |
| std::string | get (const std::string &key) const |
| |
| const std::unordered_map< std::string, std::string > & | getConfigMap () const |
| |
| void | set (const std::string &key, const std::string_view &value) |
| |
| void | remove (const std::string &key) |
| |
| void | save () const |
| |
| bool | load () |
| |
Parses configuration files
Definition at line 11 of file ConfigParser.hpp.
◆ ConfigParser()
| ConfigParser::ConfigParser |
( |
std::filesystem::path | configPath | ) |
|
|
explicit |
Construct a new Config Parser object from config file. Currently only json files are supported
- Parameters
-
| [in] | configPath | Path to the configuration file |
Definition at line 81 of file ConfigParser.cpp.
std::filesystem::path _configPath
◆ get()
| std::string ConfigParser::get |
( |
const std::string & | key | ) |
const |
Get the value of a key from the configuration file. If the configuration file modified from outside the program, the changes will not be reflected. You should call load() to refresh the configuration see the changes.
- Parameters
-
- Returns
- std::string Value of the key
Definition at line 83 of file ConfigParser.cpp.
84{
86 return itr ==
_configMap.end() ?
"" : itr->second;
87}
std::unordered_map< std::string, std::string > _configMap
◆ getConfigMap()
| const std::unordered_map< std::string, std::string > & ConfigParser::getConfigMap |
( |
| ) |
const |
|
inline |
Get the configuration map
- Returns
- std::unordered_map<std::string, std::string> Configuration map
Definition at line 54 of file ConfigParser.hpp.
◆ getLastError()
| const std::string & ConfigParser::getLastError |
( |
| ) |
const |
|
inline |
Get the last error message
- Returns
- std::string Error message
Definition at line 39 of file ConfigParser.hpp.
◆ isValid()
| bool ConfigParser::isValid |
( |
| ) |
const |
|
inline |
Check if the configuration was loaded successfully
- Returns
- bool True if valid, false otherwise
Definition at line 33 of file ConfigParser.hpp.
◆ load()
| bool ConfigParser::load |
( |
| ) |
|
Load the configuration from the file
- Returns
- bool True if valid, false otherwise
Definition at line 95 of file ConfigParser.cpp.
◆ readJson()
| bool ConfigParser::readJson |
( |
| ) |
|
|
private |
Definition at line 22 of file ConfigParser.cpp.
23{
25 if (!inFile.is_open())
26 {
28 return false;
29 }
30
31
32 rapidjson::Document doc;
33 {
34 rapidjson::IStreamWrapper fStreamWrapper(inFile);
35 doc.ParseStream(fStreamWrapper);
36 }
37
38
39 if (doc.HasParseError())
40 {
41 _lastError = std::format(
"{} {}",
"JSON parse error at offset", std::to_string(doc.GetErrorOffset()));
42 return false;
43 }
44
45
46 if (doc.IsNull() || !doc.IsObject())
47 {
48 _lastError =
"Config is empty or not a JSON object";
49 return false;
50 }
51
52
53 for (const auto &entry : doc.GetObject())
54 {
56 entry.value.IsString() ? entry.value.GetString() : stringifyRapidjson(entry.value);
57 }
58
59 return true;
60}
◆ remove()
| void ConfigParser::remove |
( |
const std::string & | key | ) |
|
Remove a key from the configuration file. If the key does not exist, nothing will happen If you don't save to the file, the changes will be lost.
- Parameters
-
Definition at line 91 of file ConfigParser.cpp.
◆ save()
| void ConfigParser::save |
( |
| ) |
const |
◆ set()
| void ConfigParser::set |
( |
const std::string & | key, |
|
|
const std::string_view & | value ) |
Set the value of a key in the configuration file. If the key does not exist, it will be created If you don't save to the file, the changes will be lost.
- Parameters
-
| [in] | key | Key to set |
| [in] | value | Value to set |
Definition at line 89 of file ConfigParser.cpp.
◆ writeJson()
| void ConfigParser::writeJson |
( |
| ) |
const |
|
private |
Definition at line 62 of file ConfigParser.cpp.
63{
65 rapidjson::OStreamWrapper fStreamWrapper(outFile);
66
67 rapidjson::Document doc;
68 doc.SetObject();
69
70 for (
const auto &[keyVal, valueVal] :
_configMap)
71 {
72 rapidjson::Value key(keyVal.c_str(), doc.GetAllocator());
73 rapidjson::Value value(valueVal.c_str(), doc.GetAllocator());
74 doc.AddMember(key, value, doc.GetAllocator());
75 }
76
77 rapidjson::Writer<rapidjson::OStreamWrapper> writer(fStreamWrapper);
78 doc.Accept(writer);
79}
◆ _configMap
| std::unordered_map<std::string, std::string> ConfigParser::_configMap |
|
private |
◆ _configPath
| std::filesystem::path ConfigParser::_configPath |
|
private |
◆ _isValid
| bool ConfigParser::_isValid = false |
|
private |
◆ _lastError
| std::string ConfigParser::_lastError |
|
private |
The documentation for this class was generated from the following files: