LoRaWANCH341 Library
Loading...
Searching...
No Matches
ConfigManager.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <string>
12#include <map>
13#include <fstream>
14#include <cjson/cJSON.h>
15
21public:
26 ConfigManager(const std::string& configFile = "config.json");
27
32
37 bool loadConfig();
38
45 std::string getString(const std::string& key, const std::string& defaultValue = "");
46
53 int getInt(const std::string& key, int defaultValue = 0);
54
61 bool getBool(const std::string& key, bool defaultValue = false);
62
69 std::string getNestedString(const std::string& path, const std::string& defaultValue = "");
70
77 int getNestedInt(const std::string& path, int defaultValue = 0);
78
85 bool getNestedBool(const std::string& path, bool defaultValue = false);
86
91 bool saveConfig();
92
98 void setString(const std::string& key, const std::string& value);
99
105 void setInt(const std::string& key, int value);
106
112 void setBool(const std::string& key, bool value);
113
114private:
115 std::string configFilePath;
116 cJSON* root;
117
123 cJSON* getNestedItem(const std::string& path);
124
128 void cleanupJSON();
129};
Manages configuration settings stored in a JSON file.
Definition ConfigManager.hpp:20
cJSON * root
The root JSON object representing the configuration.
Definition ConfigManager.hpp:116
void setBool(const std::string &key, bool value)
Sets a boolean value in the configuration.
Definition ConfigManager.cpp:199
std::string getNestedString(const std::string &path, const std::string &defaultValue="")
Retrieves a nested string value from the configuration.
Definition ConfigManager.cpp:154
bool getNestedBool(const std::string &path, bool defaultValue=false)
Retrieves a nested boolean value from the configuration.
Definition ConfigManager.cpp:172
bool loadConfig()
Loads the configuration from the JSON file.
Definition ConfigManager.cpp:37
bool saveConfig()
Saves the current configuration to the JSON file.
Definition ConfigManager.cpp:65
cJSON * getNestedItem(const std::string &path)
Retrieves a nested JSON item from the configuration.
Definition ConfigManager.cpp:132
std::string getString(const std::string &key, const std::string &defaultValue="")
Retrieves a string value from the configuration.
Definition ConfigManager.cpp:93
~ConfigManager()
Destructs the ConfigManager object.
Definition ConfigManager.cpp:26
bool getBool(const std::string &key, bool defaultValue=false)
Retrieves a boolean value from the configuration.
Definition ConfigManager.cpp:119
void setString(const std::string &key, const std::string &value)
Sets a string value in the configuration.
Definition ConfigManager.cpp:181
int getInt(const std::string &key, int defaultValue=0)
Retrieves an integer value from the configuration.
Definition ConfigManager.cpp:106
void setInt(const std::string &key, int value)
Sets an integer value in the configuration.
Definition ConfigManager.cpp:190
std::string configFilePath
The path to the configuration file.
Definition ConfigManager.hpp:115
void cleanupJSON()
Cleans up the JSON object.
Definition ConfigManager.cpp:30
int getNestedInt(const std::string &path, int defaultValue=0)
Retrieves a nested integer value from the configuration.
Definition ConfigManager.cpp:163