Repo-Init
 
Loading...
Searching...
No Matches
ConfigParser Class Reference

#include <ConfigParser.hpp>

Public Member Functions

 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 ()
 

Private Member Functions

void readJson ()
 
void writeJson () const
 

Private Attributes

std::string _configPath
 
std::unordered_map< std::string, std::string > _configMap
 

Detailed Description

Parses configuration files

Definition at line 10 of file ConfigParser.hpp.

Constructor & Destructor Documentation

◆ ConfigParser()

ConfigParser::ConfigParser ( std::string configPath)
explicit

Construct a new Config Parser object from config file. Currently only json files are supported

Parameters
[in]configPathPath to the configuration file

Definition at line 64 of file ConfigParser.cpp.

64: _configPath(std::move(configPath)) { load(); }
std::string _configPath
Here is the call graph for this function:

Member Function Documentation

◆ 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
[in]keyKey to search for
Returns
std::string Value of the key

Definition at line 66 of file ConfigParser.cpp.

67{
68 auto itr = _configMap.find(key);
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.

39{ return _configMap; }

◆ load()

void ConfigParser::load ( )

Load the configuration from the file

Definition at line 78 of file ConfigParser.cpp.

79{
80 _configMap.clear();
81 readJson();
82}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readJson()

void ConfigParser::readJson ( )
private

Definition at line 18 of file ConfigParser.cpp.

19{
20 std::ifstream inFile(_configPath);
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 // Check is there any data
32 if (doc.IsNull())
33 {
34 throw std::invalid_argument("Read config is empty or invalid JSON format");
35 }
36
37 // Parse the configuration file
38 for (const auto &entry : doc.GetObject())
39 {
40 _configMap[entry.name.GetString()] =
41 entry.value.IsString() ? entry.value.GetString() : stringifyRapidjson(entry.value);
42 }
43}
std::string stringifyRapidjson(const T &obj)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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
[in]keyKey to remove

Definition at line 74 of file ConfigParser.cpp.

74{ _configMap.erase(key); }

◆ save()

void ConfigParser::save ( ) const

Save the configuration to the file

Definition at line 76 of file ConfigParser.cpp.

76{ writeJson(); }
void writeJson() const
Here is the call graph for this function:

◆ 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]keyKey to set
[in]valueValue to set

Definition at line 72 of file ConfigParser.cpp.

72{ _configMap[key] = value; }

◆ writeJson()

void ConfigParser::writeJson ( ) const
private

Definition at line 45 of file ConfigParser.cpp.

46{
47 std::ofstream outFile(_configPath);
48 rapidjson::OStreamWrapper fStreamWrapper(outFile);
49
50 rapidjson::Document doc;
51 doc.SetObject();
52
53 for (const auto &entry : _configMap)
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}
Here is the caller graph for this function:

Member Data Documentation

◆ _configMap

std::unordered_map<std::string, std::string> ConfigParser::_configMap
private

Definition at line 13 of file ConfigParser.hpp.

◆ _configPath

std::string ConfigParser::_configPath
private

Definition at line 12 of file ConfigParser.hpp.


The documentation for this class was generated from the following files: