Afegides comprobacions de valors per al fitxer de configuració

This commit is contained in:
2025-02-28 11:34:51 +01:00
parent 366fe404ca
commit 3992fc08bf
3 changed files with 115 additions and 34 deletions

View File

@@ -53,8 +53,13 @@ bool loadOptionsFromFile(const std::string &file_path)
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());
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] == '#')
@@ -107,7 +112,6 @@ bool loadOptionsFromFile(const std::string &file_path)
return success;
}
// Guarda las opciones en un fichero
bool saveOptionsToFile(const std::string &file_path)
{
@@ -169,36 +173,105 @@ bool saveOptionsToFile(const std::string &file_path)
return success;
}
// Establece las opciones
bool setOptions(const std::string &var, const std::string &value)
{
static const std::unordered_map<std::string, std::function<void(std::string)>> optionHandlers = {
{"version", [](std::string v)
static const std::unordered_map<std::string, std::function<void(const std::string &)>> optionHandlers = {
{"version", [](const std::string &v)
{ options.version = v; }},
{"keys", [](std::string v)
{ options.keys = static_cast<ControlScheme>(safeStoi(v, static_cast<int>(ControlScheme::CURSOR))); }},
{"window.zoom", [](std::string v)
{ options.window.zoom = safeStoi(v, 1); }},
{"video.mode", [](std::string v)
{ options.video.mode = safeStoi(v, 0); }},
{"video.filter", [](std::string v)
{ options.video.filter = static_cast<ScreenFilter>(safeStoi(v, static_cast<int>(DEFAULT_VIDEO_FILTER))); }},
{"video.shaders", [](std::string v)
{"keys", [](const std::string &v)
{
int val = safeStoi(v, static_cast<int>(DEFAULT_CONTROL_SCHEME));
if (val == static_cast<int>(ControlScheme::CURSOR) || val == static_cast<int>(ControlScheme::OPQA) || val == static_cast<int>(ControlScheme::WASD))
{
options.keys = static_cast<ControlScheme>(val);
}
else
{
options.keys = DEFAULT_CONTROL_SCHEME;
}
}},
{"window.zoom", [](const std::string &v)
{
int val = safeStoi(v, DEFAULT_WINDOW_ZOOM);
if (val > 0)
{
options.window.zoom = val;
}
else
{
options.window.zoom = DEFAULT_WINDOW_ZOOM;
}
}},
{"video.mode", [](const std::string &v)
{
int val = safeStoi(v, 0);
if (val == 0 || val == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
options.video.mode = val;
}
else
{
options.video.mode = 0;
}
}},
{"video.filter", [](const std::string &v)
{
int val = safeStoi(v, static_cast<int>(DEFAULT_VIDEO_FILTER));
if (val == static_cast<int>(ScreenFilter::NEAREST) || val == static_cast<int>(ScreenFilter::LINEAR))
{
options.video.filter = static_cast<ScreenFilter>(val);
}
else
{
options.video.filter = DEFAULT_VIDEO_FILTER;
}
}},
{"video.shaders", [](const std::string &v)
{ options.video.shaders = stringToBool(v); }},
{"video.vertical_sync", [](std::string v)
{"video.vertical_sync", [](const std::string &v)
{ options.video.vertical_sync = stringToBool(v); }},
{"video.integer_scale", [](std::string v)
{"video.integer_scale", [](const std::string &v)
{ options.video.integer_scale = stringToBool(v); }},
{"video.keep_aspect", [](std::string v)
{"video.keep_aspect", [](const std::string &v)
{ options.video.keep_aspect = stringToBool(v); }},
{"video.border.enabled", [](std::string v)
{"video.border.enabled", [](const std::string &v)
{ options.video.border.enabled = stringToBool(v); }},
{"video.border.width", [](std::string v)
{ options.video.border.width = safeStoi(v, 32); }},
{"video.border.height", [](std::string v)
{ options.video.border.height = safeStoi(v, 24); }},
{"video.palette", [](std::string v)
{ options.video.palette = static_cast<Palette>(safeStoi(v, static_cast<int>(DEFAULT_PALETTE))); }}};
{"video.border.width", [](const std::string &v)
{
int val = safeStoi(v, DEFAULT_BORDER_WIDTH);
if (val > 0)
{
options.video.border.width = val;
}
else
{
options.video.border.width = DEFAULT_BORDER_WIDTH;
}
}},
{"video.border.height", [](const std::string &v)
{
int val = safeStoi(v, DEFAULT_BORDER_HEIGHT);
if (val > 0)
{
options.video.border.height = val;
}
else
{
options.video.border.height = DEFAULT_BORDER_HEIGHT;
}
}},
{"video.palette", [](const std::string &v)
{
int val = safeStoi(v, static_cast<int>(DEFAULT_PALETTE));
if (val == static_cast<int>(Palette::ZXSPECTRUM) || val == static_cast<int>(Palette::ZXARNE))
{
options.video.palette = static_cast<Palette>(val);
}
else
{
options.video.palette = DEFAULT_PALETTE;
}
}}};
auto it = optionHandlers.find(var);
if (it != optionHandlers.end())
@@ -207,4 +280,4 @@ bool setOptions(const std::string &var, const std::string &value)
return true;
}
return false;
}
}