Implentación de control de entrada. Arregla #2

Signed-off-by: somebody_master <somebody_master@somebodyserver.mooo.com>

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.
This commit is contained in:
Somebody Master 2020-10-28 16:12:21 +01:00
parent 67c4c4d4d5
commit 4e3bab68de
Signed by: somebody_master
GPG Key ID: 78315CFDF0B25505
8 changed files with 54 additions and 10 deletions

View File

@ -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_

View File

@ -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_

View File

@ -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_

View File

@ -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);
}

View File

@ -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<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems);
std::vector<std::string> showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems, long title_size);
std::vector<std::string> showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems, const int8_t *color_error);
std::vector<std::string> showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems, long title_size, const int8_t *color_error);
void showCenterMensaje(std::vector<std::string> *mensaje, const int8_t *menu_color);
void showTopTitle(const std::vector<std::string> *titulo);
} // namespace ui

View File

@ -88,7 +88,7 @@ void userInterface(const int8_t *num_menu, vector<string> *mensaje, Config *conf
vector<string> textos_init = {txt::g_confTodo + cf::g_configFile, "", ""};
vector<string> elemens = {txt::g_confHilosInput , txt::g_confTiempoInput};
ui::showTopTitle(&TITULO_PRINCIPAL);
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size());
vector<string> 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<string> *mensaje, Config *conf
case CONFIGURAR_HILOS: {
vector<string> textos_init = {txt::g_confHilos + to_string(config->getNumHilos()), "", ""};
vector<string> elemens = {txt::g_confHilosInput};
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size());
vector<string> 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<string> *mensaje, Config *conf
case CONFIGURAR_TIEMPO: {
vector<string> textos_init = {txt::g_confTiempo + config->getTiempo(), "", ""};
vector<string> elemens = {txt::g_confTiempoInput};
vector<string> entrada = ui::showCentralInputBox(&textos_init, &CONFIGURAR_TODO, &elemens, TITULO_PRINCIPAL.size());
vector<string> 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);

View File

@ -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;
}
}

View File

@ -1,6 +1,7 @@
#include "ui.hpp"
#include "configFile.hpp"
#include "textos.hpp"
#include <regex>
using namespace std;
@ -51,11 +52,11 @@ void ui::showCenterMensaje(vector<string> *mensaje, const int8_t *menu_color) {
g_mensaje = false;
}
void ui::showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems) {
ui::showCentralInputBox(textos, num_box, elems, 0);
vector<string> ui::showCentralInputBox(std::vector<std::string> *textos, const int8_t *num_box, std::vector<std::string> *elems, const int8_t *color_error) {
return ui::showCentralInputBox(textos, num_box, elems, 0, color_error);
}
vector<string> ui::showCentralInputBox(vector<string> *textos, const int8_t *num_box, vector<string> *elems, long title_size) {
vector<string> ui::showCentralInputBox(vector<string> *textos, const int8_t *num_box, vector<string> *elems, long title_size, const int8_t *color_error) {
vector<string> entrada;
int start_ver_window = 1;
@ -75,12 +76,36 @@ vector<string> ui::showCentralInputBox(vector<string> *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);
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();