#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 &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 64 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 66 of file ConfigParser.cpp.
67{
69 return itr ==
_configMap.end() ?
"" : itr->second;
70}
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 18 of file ConfigParser.cpp.
19{
21 if (!inFile.is_open())
22 {
23 throw std::invalid_argument("Can't open config file");
24 }
25
26 rapidjson::IStreamWrapper fStreamWrapper(inFile);
27
28 rapidjson::Document doc;
29 doc.ParseStream(fStreamWrapper);
30
31
32 if (doc.IsNull())
33 {
34 throw std::invalid_argument("Read config is empty or invalid JSON format");
35 }
36
37
38 for (const auto &entry : doc.GetObject())
39 {
42 }
43}
std::string stringifyRapidjson(const T &obj)
◆ 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 74 of file ConfigParser.cpp.
◆ save()
void ConfigParser::save |
( |
| ) |
const |
◆ set()
void ConfigParser::set |
( |
const std::string & | key, |
|
|
const std::string & | 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 72 of file ConfigParser.cpp.
◆ writeJson()
void ConfigParser::writeJson |
( |
| ) |
const |
|
private |
Definition at line 45 of file ConfigParser.cpp.
46{
48 rapidjson::OStreamWrapper fStreamWrapper(outFile);
49
50 rapidjson::Document doc;
51 doc.SetObject();
52
54 {
55 rapidjson::Value key(entry.first.c_str(), doc.GetAllocator());
56 rapidjson::Value value(entry.second.c_str(), doc.GetAllocator());
57 doc.AddMember(key, value, doc.GetAllocator());
58 }
59
60 rapidjson::Writer<rapidjson::OStreamWrapper> writer(fStreamWrapper);
61 doc.Accept(writer);
62}
◆ _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: