77 lines
2.3 KiB
C++
77 lines
2.3 KiB
C++
#ifndef MODULAR_CONFIGMANAGER_HPP
|
|
#define MODULAR_CONFIGMANAGER_HPP
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
#include <fstream>
|
|
|
|
/**
|
|
* @brief Manages application configuration loaded from a simple key=value file.
|
|
*
|
|
* Supported file format:
|
|
* - Lines starting with '#' are comments.
|
|
* - Empty lines are ignored.
|
|
* - Each line must be in the form: key = value
|
|
* - Leading / trailing whitespace around key and value is trimmed.
|
|
*/
|
|
class ConfigManager {
|
|
public:
|
|
/**
|
|
* @brief Load configuration from a file.
|
|
* @param path File path to the configuration file.
|
|
* @return true on success, false on failure (file not found or parse error).
|
|
*/
|
|
bool loadFrom(const std::string& path);
|
|
|
|
/**
|
|
* @brief Get a string value by key.
|
|
* @param key Configuration key.
|
|
* @param default_val Value returned if the key is not found.
|
|
* @return The value as string, or default_val.
|
|
*/
|
|
std::string getString(const std::string& key, const std::string& default_val = "") const;
|
|
|
|
/**
|
|
* @brief Get an integer value by key.
|
|
* @param key Configuration key.
|
|
* @param default_val Value returned if the key is not found or not convertible.
|
|
* @return The parsed integer, or default_val.
|
|
*/
|
|
int getInt(const std::string& key, int default_val = 0) const;
|
|
|
|
/**
|
|
* @brief Get a boolean value by key.
|
|
* @param key Configuration key.
|
|
* @param default_val Value returned if the key is not found.
|
|
* @return true if the value is "true", "1", or "yes" (case-insensitive); otherwise false.
|
|
*/
|
|
bool getBool(const std::string& key, bool default_val = false) const;
|
|
|
|
/**
|
|
* @brief Check if a key exists.
|
|
* @param key Configuration key.
|
|
* @return true if the key exists.
|
|
*/
|
|
bool hasKey(const std::string& key) const;
|
|
|
|
/**
|
|
* @brief Clear all loaded configuration data.
|
|
*/
|
|
void clear();
|
|
|
|
private:
|
|
std::unordered_map<std::string, std::string> config_;
|
|
|
|
/**
|
|
* @brief Parse a single line into key and value.
|
|
* @param line Input line.
|
|
* @param key [out] Parsed key.
|
|
* @param value [out] Parsed value.
|
|
* @return true if parsing succeeded.
|
|
*/
|
|
static bool parseLine(const std::string& line, std::string& key, std::string& value);
|
|
};
|
|
|
|
#endif // MODULAR_CONFIGMANAGER_HPP
|