#include <ConfigParser.hpp>
|
| ConfigParser (std::string configPath) |
|
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 |
|
void | load () |
|
Parses configuration files
Definition at line 10 of file ConfigParser.hpp.
◆ ConfigParser()
ConfigParser::ConfigParser |
( |
std::string | 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 67 of file ConfigParser.cpp.
◆ 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 69 of file ConfigParser.cpp.
70{
72 return itr ==
_configMap.end() ?
"" : itr->second;
73}
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 39 of file ConfigParser.hpp.
◆ load()
void ConfigParser::load |
( |
| ) |
|
◆ readJson()
void ConfigParser::readJson |
( |
| ) |
|
|
private |
Definition at line 21 of file ConfigParser.cpp.
22{
24 if (!inFile.is_open())
25 {
26 throw std::invalid_argument("Can't open config file");
27 }
28
29 rapidjson::IStreamWrapper fStreamWrapper(inFile);
30
31 rapidjson::Document doc;
32 doc.ParseStream(fStreamWrapper);
33
34
35 if (doc.IsNull())
36 {
37 throw std::invalid_argument("Read config is empty or invalid JSON format");
38 }
39
40
41 for (const auto &entry : doc.GetObject())
42 {
44 entry.value.IsString() ? entry.value.GetString() : stringifyRapidjson(entry.value);
45 }
46}
◆ 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 77 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 75 of file ConfigParser.cpp.
◆ writeJson()
void ConfigParser::writeJson |
( |
| ) |
const |
|
private |
Definition at line 48 of file ConfigParser.cpp.
49{
51 rapidjson::OStreamWrapper fStreamWrapper(outFile);
52
53 rapidjson::Document doc;
54 doc.SetObject();
55
56 for (
const auto &[keyVal, valueVal] :
_configMap)
57 {
58 rapidjson::Value key(keyVal.c_str(), doc.GetAllocator());
59 rapidjson::Value value(valueVal.c_str(), doc.GetAllocator());
60 doc.AddMember(key, value, doc.GetAllocator());
61 }
62
63 rapidjson::Writer<rapidjson::OStreamWrapper> writer(fStreamWrapper);
64 doc.Accept(writer);
65}
◆ _configMap
std::unordered_map<std::string, std::string> ConfigParser::_configMap |
|
private |
◆ _configPath
std::string ConfigParser::_configPath |
|
private |
The documentation for this class was generated from the following files: