This commit is contained in:
2025-10-27 18:35:53 +01:00
parent b1dca32a5b
commit 3179a08dac
63 changed files with 686 additions and 693 deletions

View File

@@ -7,6 +7,7 @@
#include <fstream> // Para basic_ostream, operator<<, basic_ofstream
#include <functional> // Para function
#include <iostream> // Para cout, cerr
#include <ranges>
#include <sstream> // Para basic_istringstream
#include <string> // Para char_traits, string, operator<<, hash
#include <unordered_map> // Para unordered_map, operator==, _Node_const_i...
@@ -16,11 +17,11 @@
namespace Options {
// 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);
auto setOptions(const std::string& var, const std::string& value) -> bool;
auto trimLine(const std::string& line) -> std::string;
auto isCommentOrEmpty(const std::string& line) -> bool;
auto processConfigLine(const std::string& line) -> bool;
auto readConfigFile(const std::string& file_path) -> bool;
// Crea e inicializa las opciones del programa
void init() {
@@ -32,19 +33,19 @@ void init() {
}
// 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);
auto trimLine(const std::string& line) -> std::string {
auto start = std::ranges::find_if(line, [](int ch) { return !std::isspace(ch); });
auto end = std::ranges::find_if(std::ranges::reverse_view(line), [](int ch) { return !std::isspace(ch); }).base();
return {start, end};
}
// Verifica si una línea es comentario o está vacía
bool isCommentOrEmpty(const std::string& line) {
auto isCommentOrEmpty(const std::string& line) -> bool {
return line.empty() || line[0] == '#';
}
// Procesa una línea de configuración individual
bool processConfigLine(const std::string& line) {
auto processConfigLine(const std::string& line) -> bool {
std::istringstream iss(line);
std::string key;
std::string value;
@@ -53,7 +54,7 @@ bool processConfigLine(const std::string& line) {
if (!setOptions(key, value)) {
if (console) {
std::cout << "Warning: file config.txt\n";
std::cout << "unknown parameter " << key << std::endl;
std::cout << "unknown parameter " << key << '\n';
}
return false;
}
@@ -62,7 +63,7 @@ bool processConfigLine(const std::string& line) {
}
// Lee y procesa el fichero de configuración
bool readConfigFile(const std::string& file_path) {
auto readConfigFile(const std::string& file_path) -> bool {
std::ifstream file(file_path);
if (!file.good()) {
return false;
@@ -95,7 +96,7 @@ bool readConfigFile(const std::string& file_path) {
}
// Carga las opciones desde un fichero
bool loadFromFile(const std::string& file_path) {
auto loadFromFile(const std::string& file_path) -> bool {
// Versión actual del fichero
const std::string CONFIG_VERSION = version;
version = "";
@@ -122,7 +123,7 @@ bool loadFromFile(const std::string& file_path) {
}
// Guarda las opciones en un fichero
bool saveToFile(const std::string& file_path) {
auto saveToFile(const std::string& file_path) -> bool {
// Crea y abre el fichero de texto
std::ofstream file(file_path);
bool success = file.is_open(); // Verifica si el archivo se abrió correctamente
@@ -130,13 +131,13 @@ bool saveToFile(const std::string& file_path) {
if (!success) // Si no se pudo abrir el archivo, muestra un mensaje de error y devuelve false
{
if (console) {
std::cerr << "Error: Unable to open file " << file_path << " for writing." << std::endl;
std::cerr << "Error: Unable to open file " << file_path << " for writing." << '\n';
}
return false;
}
if (console) {
std::cout << file_path << " open for writing" << std::endl;
std::cout << file_path << " open for writing" << '\n';
}
// Escribe en el fichero
@@ -179,7 +180,7 @@ bool saveToFile(const std::string& file_path) {
return success;
}
bool setOptions(const std::string& var, const std::string& value) {
auto setOptions(const std::string& var, const std::string& value) -> bool {
static const std::unordered_map<std::string, std::function<void(const std::string&)>> OPTION_HANDLERS = {
{"version", [](const std::string& v) { version = v; }},
{"keys", [](const std::string& v) {