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

#include <ConfigParser.hpp>

Public Member Functions

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

Private Member Functions

bool readJson ()
 
void writeJson () const
 

Private Attributes

std::filesystem::path _configPath
 
std::unordered_map< std::string, std::string > _configMap
 
bool _isValid = false
 
std::string _lastError
 

Detailed Description

Parses configuration files

Definition at line 11 of file ConfigParser.hpp.

Constructor & Destructor Documentation

◆ 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]configPathPath to the configuration file

Definition at line 81 of file ConfigParser.cpp.

81: _configPath(std::move(configPath)) { load(); }
std::filesystem::path _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 83 of file ConfigParser.cpp.

84{
85 auto itr = _configMap.find(key);
86 return itr == _configMap.end() ? "" : itr->second;
87}
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 54 of file ConfigParser.hpp.

54{ return _configMap; }
Here is the caller graph for this function:

◆ 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.

39{ return _lastError; }
std::string _lastError
Here is the caller graph for this function:

◆ 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.

33{ return _isValid; }
Here is the caller graph for this function:

◆ 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.

96{
97 _configMap.clear();
99 return _isValid;
100}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readJson()

bool ConfigParser::readJson ( )
private

Definition at line 22 of file ConfigParser.cpp.

23{
24 std::ifstream inFile(_configPath);
25 if (!inFile.is_open())
26 {
27 _lastError = std::format("{} {}", "Can't open config file:", _configPath.string());
28 return false;
29 }
30
31 // Scope the wrapper to ensure proper destruction order
32 rapidjson::Document doc;
33 {
34 rapidjson::IStreamWrapper fStreamWrapper(inFile);
35 doc.ParseStream(fStreamWrapper);
36 }
37
38 // Check for parse errors
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 // Check is there any data
46 if (doc.IsNull() || !doc.IsObject())
47 {
48 _lastError = "Config is empty or not a JSON object";
49 return false;
50 }
51
52 // Parse the configuration file
53 for (const auto &entry : doc.GetObject())
54 {
55 _configMap[entry.name.GetString()] =
56 entry.value.IsString() ? entry.value.GetString() : stringifyRapidjson(entry.value);
57 }
58
59 return true;
60}
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 91 of file ConfigParser.cpp.

91{ _configMap.erase(key); }

◆ save()

void ConfigParser::save ( ) const

Save the configuration to the file

Definition at line 93 of file ConfigParser.cpp.

93{ 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 89 of file ConfigParser.cpp.

89{ _configMap[key] = value; }

◆ writeJson()

void ConfigParser::writeJson ( ) const
private

Definition at line 62 of file ConfigParser.cpp.

63{
64 std::ofstream outFile(_configPath);
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}
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 14 of file ConfigParser.hpp.

◆ _configPath

std::filesystem::path ConfigParser::_configPath
private

Definition at line 13 of file ConfigParser.hpp.

◆ _isValid

bool ConfigParser::_isValid = false
private

Definition at line 15 of file ConfigParser.hpp.

◆ _lastError

std::string ConfigParser::_lastError
private

Definition at line 16 of file ConfigParser.hpp.


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