Skip to content

Commit

Permalink
Create config_utils.cpp
Browse files Browse the repository at this point in the history
- Modularize the src and change the name of the function as it will load more variables from the config file
  • Loading branch information
rompelhd authored Apr 8, 2024
1 parent 53a52f5 commit 06bc400
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/config_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "config_utils.hpp"
#include <iostream>
#include <fstream>

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
}

0 comments on commit 06bc400

Please sign in to comment.