Fairly big refactor...

This commit is contained in:
Your Name
2025-04-23 21:50:04 +12:00
parent 50e340f1c5
commit 9a3dd18525
24 changed files with 853 additions and 660 deletions

135
src/utils/envmanager.cpp Normal file
View File

@ -0,0 +1,135 @@
#include "envmanager.hpp"
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <regex>
#include <cstdlib> // For std::getenv
namespace dropshell {
envmanager::envmanager(std::string path) : m_path(path) {
}
envmanager::~envmanager() {
}
bool envmanager::load() {
std::ifstream file(m_path);
if (!file.is_open()) {
return false;
}
m_variables.clear();
std::string line;
while (std::getline(file, line)) {
line=trim(line);
// Skip empty lines and comments
if (line.empty() || line[0] == '#') {
continue;
}
size_t pos = line.find('=');
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
// trim whitespace from the key and value
m_variables[trim(key)] = trim(value);
}
}
file.close();
return true;
}
void envmanager::save() {
std::ofstream file(m_path);
if (!file.is_open()) {
return;
}
for (const auto& pair : m_variables) {
file << pair.first << "=" << pair.second << std::endl;
}
file.close();
}
std::string envmanager::get_variable(std::string key) const {
key = trim(key);
// Use case-insensitive comparison to find the key
for (const auto& pair : m_variables) {
if (pair.first == key) {
return pair.second;
}
}
return "";
}
void envmanager::get_all_variables(std::map<std::string, std::string>& variables) const {
variables = m_variables;
}
std::string envmanager::get_variable_substituted(std::string key) const {
std::string value = get_variable(key);
return expand_patterns(value);
}
void envmanager::get_all_variables_substituted(std::map<std::string, std::string>& variables) const {
variables.clear();
for (const auto& pair : m_variables) {
variables[pair.first] = expand_patterns(pair.second);
}
}
void envmanager::add_variables(std::map<std::string, std::string> variables) {
for (auto& pair : variables) {
set_variable(pair.first, pair.second);
}
}
void envmanager::set_variable(std::string key, std::string value) {
m_variables[trim(key)] = trim(value);
}
void envmanager::clear_variables() {
m_variables.clear();
}
std::string envmanager::trim(std::string str) const {
// Trim leading whitespace
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
// Trim trailing whitespace
str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), str.end());
return str;
}
std::string envmanager::expand_patterns(std::string str) const {
// Combined regex pattern for both ${var} and $var formats
std::regex var_pattern("\\$(?:\\{([^}]+)\\}|([a-zA-Z0-9_]+))");
std::string result = str;
std::smatch match;
while (std::regex_search(result, match, var_pattern)) {
// match[1] will contain capture from ${var} format
// match[2] will contain capture from $var format
std::string var_name = match[1].matched ? match[1].str() : match[2].str();
// Get value from system environment variables
const char* env_value = std::getenv(var_name.c_str());
std::string value = env_value ? env_value : "";
result = result.replace(match.position(), match.length(), value);
}
return result;
}
} // namespace dropshell