dropshell/src/utils/envmanager.hpp
2025-04-23 21:50:04 +12:00

50 lines
1.7 KiB
C++

#ifndef ENV_MANAGER_HPP
#define ENV_MANAGER_HPP
#include <string>
#include <map>
namespace dropshell {
// envmanager is a class that manages the environment files for the application.
// it is responsible for loading the environment files, and providing a class to access the variables.
// it can also save the environment files.
class envmanager {
public:
envmanager(std::string path);
~envmanager();
// load all variables from the environment file
bool load();
// save all variables to the environment file
void save();
// get variables from the environment files. Trim whitespace from the values.
// keys are case-sensitive.
std::string get_variable(std::string key) const;
void get_all_variables(std::map<std::string, std::string>& variables) const;
// get variables, but replace patterns ${var} and $var with the actual environment variable in the returned string.
// trim whitespace from the values.
std::string get_variable_substituted(std::string key) const;
void get_all_variables_substituted(std::map<std::string, std::string>& variables) const;
// add variables to the environment files.
// trim whitespace from the values.
void add_variables(std::map<std::string, std::string> variables);
void set_variable(std::string key, std::string value);
void clear_variables();
private:
std::string trim(std::string str) const;
std::string expand_patterns(std::string str) const;
private:
std::string m_path;
std::map<std::string, std::string> m_variables;
};
} // namespace dropshell
#endif