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_view &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 67 of file ConfigParser.cpp.

67: _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 69 of file ConfigParser.cpp.

70{
71 auto itr = _configMap.find(key);
72 return itr == _configMap.end() ? "" : itr->second;
73}
std::unordered_map< std::string, std::string > _configMap
Here is the caller graph for this function:

◆ 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; }
Here is the caller graph for this function:

◆ load()

void ConfigParser::load ( )

Load the configuration from the file

Definition at line 81 of file ConfigParser.cpp.

82{
83 _configMap.clear();
84 readJson();
85}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readJson()

void ConfigParser::readJson ( )
private

Definition at line 21 of file ConfigParser.cpp.

22{
23 std::ifstream inFile(_configPath);
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 // Check is there any data
35 if (doc.IsNull())
36 {
37 throw std::invalid_argument("Read config is empty or invalid JSON format");
38 }
39
40 // Parse the configuration file
41 for (const auto &entry : doc.GetObject())
42 {
43 _configMap[entry.name.GetString()] =
44 entry.value.IsString() ? entry.value.GetString() : stringifyRapidjson(entry.value);
45 }
46}
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 77 of file ConfigParser.cpp.

77{ _configMap.erase(key); }

◆ save()

void ConfigParser::save ( ) const

Save the configuration to the file

Definition at line 79 of file ConfigParser.cpp.

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

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

Definition at line 75 of file ConfigParser.cpp.

75{ _configMap[key] = value; }

◆ writeJson()

void ConfigParser::writeJson ( ) const
private

Definition at line 48 of file ConfigParser.cpp.

49{
50 std::ofstream outFile(_configPath);
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}
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: