forked from jaildesigner-jailgames/jaildoctors_dilemma
linter
This commit is contained in:
@@ -15,8 +15,12 @@
|
||||
#include "utils/utils.hpp" // Para stringToBool, boolToString, safeStoi
|
||||
|
||||
namespace Options {
|
||||
// Declaración de función interna
|
||||
// Declaración de funciones internas
|
||||
bool setOptions(const std::string& var, const std::string& value);
|
||||
std::string trimLine(const std::string& line);
|
||||
bool isCommentOrEmpty(const std::string& line);
|
||||
bool processConfigLine(const std::string& line);
|
||||
bool readConfigFile(const std::string& file_path);
|
||||
|
||||
// Crea e inicializa las opciones del programa
|
||||
void init() {
|
||||
@@ -27,62 +31,82 @@ void init() {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Elimina espacios en blanco al inicio y final de una línea
|
||||
std::string trimLine(const std::string& line) {
|
||||
auto start = std::find_if(line.begin(), line.end(), [](int ch) { return !std::isspace(ch); });
|
||||
auto end = std::find_if(line.rbegin(), line.rend(), [](int ch) { return !std::isspace(ch); }).base();
|
||||
return std::string(start, end);
|
||||
}
|
||||
|
||||
// Verifica si una línea es comentario o está vacía
|
||||
bool isCommentOrEmpty(const std::string& line) {
|
||||
return line.empty() || line[0] == '#';
|
||||
}
|
||||
|
||||
// Procesa una línea de configuración individual
|
||||
bool processConfigLine(const std::string& line) {
|
||||
std::istringstream iss(line);
|
||||
std::string key;
|
||||
std::string value;
|
||||
|
||||
if (iss >> key >> value) {
|
||||
if (!setOptions(key, value)) {
|
||||
if (console) {
|
||||
std::cout << "Warning: file config.txt\n";
|
||||
std::cout << "unknown parameter " << key << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Lee y procesa el fichero de configuración
|
||||
bool readConfigFile(const std::string& file_path) {
|
||||
std::ifstream file(file_path);
|
||||
if (!file.good()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool success = true;
|
||||
if (console) {
|
||||
std::cout << "Reading file config.txt\n";
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
line = trimLine(line);
|
||||
|
||||
if (isCommentOrEmpty(line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!processConfigLine(line)) {
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (console) {
|
||||
std::cout << "Closing file config.txt\n\n";
|
||||
}
|
||||
file.close();
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Carga las opciones desde un fichero
|
||||
bool loadFromFile(const std::string& file_path) {
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
// Versión actual del fichero
|
||||
const std::string CONFIG_VERSION = version;
|
||||
version = "";
|
||||
|
||||
// Variables para manejar el fichero
|
||||
std::ifstream file(file_path);
|
||||
// Intenta leer el fichero
|
||||
bool success = readConfigFile(file_path);
|
||||
|
||||
// Si el fichero se puede abrir
|
||||
if (file.good()) {
|
||||
// Procesa el fichero línea a línea
|
||||
if (console) {
|
||||
std::cout << "Reading file config.txt\n";
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
// Elimina espacios en blanco iniciales y finales
|
||||
line = std::string(std::find_if(line.begin(), line.end(), [](int ch) { return !std::isspace(ch); }),
|
||||
line.end());
|
||||
line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch) { return !std::isspace(ch); })
|
||||
.base(),
|
||||
line.end());
|
||||
|
||||
// Ignora líneas vacías o comentarios
|
||||
if (line.empty() || line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Usa un stringstream para dividir la línea en dos partes
|
||||
std::istringstream iss(line);
|
||||
std::string key;
|
||||
std::string value;
|
||||
|
||||
if (iss >> key >> value) {
|
||||
if (!setOptions(key, value)) {
|
||||
if (console) {
|
||||
std::cout << "Warning: file config.txt\n";
|
||||
std::cout << "unknown parameter " << key << std::endl;
|
||||
}
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
if (console) {
|
||||
std::cout << "Closing file config.txt\n\n";
|
||||
}
|
||||
file.close();
|
||||
} else {
|
||||
// Crea el fichero con los valores por defecto
|
||||
// Si no se pudo leer, crea el fichero con valores por defecto
|
||||
if (!success) {
|
||||
saveToFile(file_path);
|
||||
success = true;
|
||||
}
|
||||
|
||||
// Si la versión de fichero no coincide, crea un fichero nuevo con los valores por defecto
|
||||
|
||||
Reference in New Issue
Block a user