From 06bc40019e90dec735de57acdd10edc1a65089b4 Mon Sep 17 00:00:00 2001 From: Rompelhd <75935831+rompelhd@users.noreply.github.com> Date: Mon, 8 Apr 2024 18:36:59 +0200 Subject: [PATCH] Create config_utils.cpp - Modularize the src and change the name of the function as it will load more variables from the config file --- src/config_utils.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/config_utils.cpp diff --git a/src/config_utils.cpp b/src/config_utils.cpp new file mode 100644 index 0000000..7d78425 --- /dev/null +++ b/src/config_utils.cpp @@ -0,0 +1,31 @@ +#include "config_utils.hpp" +#include +#include + +std::string ConfigLoad(const std::string& filename) { + std::ifstream configFile(filename); + if (!configFile) { + std::cerr << "Error opening the config file: " << filename << std::endl; + return ""; + } + + std::string line; + while (std::getline(configFile, line)) { + if (line.empty() || line[0] == '#') continue; + + auto pos = line.find('='); + if (pos != std::string::npos) { + std::string key = line.substr(0, pos); + std::string value = line.substr(pos + 1); + key.erase(0, key.find_first_not_of(" \t")); + key.erase(key.find_last_not_of(" \t") + 1); + value.erase(0, value.find_first_not_of(" \t")); + value.erase(value.find_last_not_of(" \t") + 1); + if (key == "languageCode") { + return value; + } + } + } + std::cerr << "Error: 'languageCode' not found in the config file" << std::endl; + return "en"; // Default language +}