From 4e3bab68de069bba863d2757dce76aaf7aaea182 Mon Sep 17 00:00:00 2001 From: somebody_master Date: Wed, 28 Oct 2020 16:12:21 +0100 Subject: [PATCH] =?UTF-8?q?Implentaci=C3=B3n=20de=20control=20de=20entrada?= =?UTF-8?q?.=20Arregla=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: somebody_master Se ha implementado el control de entrada por teclado en la configuración para asegurar que se introducen datos adecuados. Se usan expresiones regulares, mediante la librería regex, para conseguir tal efecto. --- include/idiomas/en.hpp | 2 ++ include/idiomas/es.hpp | 2 ++ include/idiomas/gl.hpp | 2 ++ include/textos.hpp | 2 ++ include/ui.hpp | 5 +++-- src/main.cpp | 6 +++--- src/textos.cpp | 10 ++++++++++ src/ui.cpp | 35 ++++++++++++++++++++++++++++++----- 8 files changed, 54 insertions(+), 10 deletions(-) diff --git a/include/idiomas/en.hpp b/include/idiomas/en.hpp index 8e40a44..0dd2042 100644 --- a/include/idiomas/en.hpp +++ b/include/idiomas/en.hpp @@ -20,6 +20,8 @@ namespace en{ const extern std::string confTiempo = "Current execution time > "; const extern std::string confHilosInput = "- Number of threads\t> "; const extern std::string confTiempoInput = "- Execution time\t> "; + const extern std::string confHilosError = "Must introduce a number"; + const extern std::string confTiempoError = "The time format must be HH:mm:ss"; } #endif // __EN_HPP_ diff --git a/include/idiomas/es.hpp b/include/idiomas/es.hpp index 83183d6..5e6c17f 100644 --- a/include/idiomas/es.hpp +++ b/include/idiomas/es.hpp @@ -20,6 +20,8 @@ namespace es{ const extern std::string confTiempo = "Tiempo actual > "; const extern std::string confHilosInput = "- Número de hilos\t> "; const extern std::string confTiempoInput = "- Tiempo de ejecución\t> "; + const extern std::string confHilosError = "Se debe introducir un número"; + const extern std::string confTiempoError = "El formato del tiempo debe ser HH:mm:ss"; } #endif // __ES_HPP_ diff --git a/include/idiomas/gl.hpp b/include/idiomas/gl.hpp index 599db77..726d6b6 100644 --- a/include/idiomas/gl.hpp +++ b/include/idiomas/gl.hpp @@ -20,6 +20,8 @@ namespace gl{ const extern std::string confTiempo = "Tempo actual > "; const extern std::string confHilosInput = "- Número de filos\t> "; const extern std::string confTiempoInput = "- Tempo de execución\t> "; + const extern std::string confHilosError = "Debese introducir un número"; + const extern std::string confTiempoError = "O formato do tempo debe ser HH:mm:ss"; } #endif // __GL_HPP_ diff --git a/include/textos.hpp b/include/textos.hpp index 8aae4bf..a20ce02 100644 --- a/include/textos.hpp +++ b/include/textos.hpp @@ -20,6 +20,8 @@ namespace txt{ extern std::string g_confTiempo; extern std::string g_confHilosInput; extern std::string g_confTiempoInput; + extern std::string g_confHilosError; + extern std::string g_confTiempoError; void inicializarIdioma(std::string idioma); } diff --git a/include/ui.hpp b/include/ui.hpp index 7dcfbd0..40bbc3e 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -6,13 +6,14 @@ namespace ui { extern int g_maxLines, g_maxCols, g_begCol, g_begLine, g_menuWith; + //int8_t error; extern bool g_mensaje; int8_t initUI(const int8_t *menu_principal, const int8_t *menu_error); void closeUI(); - void showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems); - std::vector showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems, long title_size); + std::vector showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems, const int8_t *color_error); + std::vector showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems, long title_size, const int8_t *color_error); void showCenterMensaje(std::vector *mensaje, const int8_t *menu_color); void showTopTitle(const std::vector *titulo); } // namespace ui diff --git a/src/main.cpp b/src/main.cpp index f57f5f9..5268b21 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,7 +88,7 @@ void userInterface(const int8_t *num_menu, vector *mensaje, Config *conf vector textos_init = {txt::g_confTodo + cf::g_configFile, "", ""}; vector elemens = {txt::g_confHilosInput , txt::g_confTiempoInput}; ui::showTopTitle(&TITULO_PRINCIPAL); - vector entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size()); + vector entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size(), &MENU_ERROR); Config config2 = Config(atoi(entrada[0].c_str()), entrada[1]); config = &config2; cf::saveConfig(config); @@ -99,7 +99,7 @@ void userInterface(const int8_t *num_menu, vector *mensaje, Config *conf case CONFIGURAR_HILOS: { vector textos_init = {txt::g_confHilos + to_string(config->getNumHilos()), "", ""}; vector elemens = {txt::g_confHilosInput}; - vector entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size()); + vector entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size(), &MENU_ERROR); config->setNumHilos(atoi(entrada[0].c_str())); cf::saveConfig(config); userInterface(&MENU_CONFIGURACION, mensaje, config); @@ -109,7 +109,7 @@ void userInterface(const int8_t *num_menu, vector *mensaje, Config *conf case CONFIGURAR_TIEMPO: { vector textos_init = {txt::g_confTiempo + config->getTiempo(), "", ""}; vector elemens = {txt::g_confTiempoInput}; - vector entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size()); + vector entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size(), &MENU_ERROR); config->setTiempo(entrada[0]); cf::saveConfig(config); userInterface(&MENU_CONFIGURACION, mensaje, config); diff --git a/src/textos.cpp b/src/textos.cpp index 615c47f..0cac74b 100644 --- a/src/textos.cpp +++ b/src/textos.cpp @@ -20,6 +20,8 @@ std::string txt::g_confHilos = ""; std::string txt::g_confTiempo = ""; std::string txt::g_confHilosInput = ""; std::string txt::g_confTiempoInput = ""; +std::string txt::g_confHilosError = ""; +std::string txt::g_confTiempoError = ""; void txt::inicializarIdioma(std::string idioma){ idioma = idioma.substr(0, idioma.find("_")); @@ -43,6 +45,8 @@ void txt::inicializarIdioma(std::string idioma){ txt::g_confTiempo = confTiempo; txt::g_confHilosInput = confHilosInput; txt::g_confTiempoInput = confTiempoInput; + txt::g_confHilosError = confHilosError; + txt::g_confTiempoError = confTiempoError; }else if(idioma == "es"){ using namespace es; txt::g_noColor = noColor; @@ -62,6 +66,8 @@ void txt::inicializarIdioma(std::string idioma){ txt::g_confTiempo = confTiempo; txt::g_confHilosInput = confHilosInput; txt::g_confTiempoInput = confTiempoInput; + txt::g_confHilosError = confHilosError; + txt::g_confTiempoError = confTiempoError; }else if(idioma == "en"){ using namespace en; txt::g_noColor = noColor; @@ -81,6 +87,8 @@ void txt::inicializarIdioma(std::string idioma){ txt::g_confTiempo = confTiempo; txt::g_confHilosInput = confHilosInput; txt::g_confTiempoInput = confTiempoInput; + txt::g_confHilosError = confHilosError; + txt::g_confTiempoError = confTiempoError; }else{ using namespace en; txt::g_noColor = noColor; @@ -100,6 +108,8 @@ void txt::inicializarIdioma(std::string idioma){ txt::g_confTiempo = confTiempo; txt::g_confHilosInput = confHilosInput; txt::g_confTiempoInput = confTiempoInput; + txt::g_confHilosError = confHilosError; + txt::g_confTiempoError = confTiempoError; } } diff --git a/src/ui.cpp b/src/ui.cpp index 4615bb7..0d87f4c 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1,6 +1,7 @@ #include "ui.hpp" #include "configFile.hpp" #include "textos.hpp" +#include using namespace std; @@ -51,11 +52,11 @@ void ui::showCenterMensaje(vector *mensaje, const int8_t *menu_color) { g_mensaje = false; } -void ui::showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems) { - ui::showCentralInputBox(textos, num_box, elems, 0); +vector ui::showCentralInputBox(std::vector *textos, const int8_t *num_box, std::vector *elems, const int8_t *color_error) { + return ui::showCentralInputBox(textos, num_box, elems, 0, color_error); } -vector ui::showCentralInputBox(vector *textos, const int8_t *num_box, vector *elems, long title_size) { +vector ui::showCentralInputBox(vector *textos, const int8_t *num_box, vector *elems, long title_size, const int8_t *color_error) { vector entrada; int start_ver_window = 1; @@ -75,12 +76,36 @@ vector ui::showCentralInputBox(vector *textos, const int8_t *num echo(); nocbreak(); curs_set(1); + char input[10]; + string cleaner; + for (int i = 0; i < elems->size(); i++) { + cleaner = elems->at(i) + " "; mvwprintw(centralBOX, i + 1 + textos->size(), 1, elems->at(i).c_str()); wrefresh(centralBOX); - char input[10]; wgetnstr(centralBOX, input, 10); - entrada.push_back(input); + if (elems->at(i) == txt::g_confHilosInput){ + regex integer_expr("(\\+|-)?[[:digit:]]+"); + if(regex_match(input, integer_expr)){ + entrada.push_back(input); + } else { + mvwprintw(centralBOX, i + 1 + textos->size(), 1, cleaner.c_str()); + wattron(centralBOX, COLOR_PAIR(*color_error)); + mvwprintw(centralBOX, i + textos->size(), 1, txt::g_confHilosError.c_str()); + wattroff(centralBOX, COLOR_PAIR(*color_error)); + i--; + } + } else if(elems->at(i) == txt::g_confTiempoInput){ + if(regex_match(input,regex("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$"))){ + entrada.push_back(input); + } else { + mvwprintw(centralBOX, i + 1 + textos->size(), 1, cleaner.c_str()); + wattron(centralBOX, COLOR_PAIR(*color_error)); + mvwprintw(centralBOX, i + textos->size(), 1, txt::g_confTiempoError.c_str()); + wattroff(centralBOX, COLOR_PAIR(*color_error)); + i--; + } + } } curs_set(0); noecho();