From c1e0df66f70bf4aa5406bfb2a5e111a657befcc3 Mon Sep 17 00:00:00 2001 From: hozlucas28 Date: Fri, 11 Oct 2024 16:45:35 -0300 Subject: [PATCH] fix: fix `getMainArguments` function Co-authored-by: Tiago Giannotti --- src/main.c | 12 ++++++------ src/utilities.c | 48 ++++++++++++++++-------------------------------- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/main.c b/src/main.c index 838855c..28afcc3 100644 --- a/src/main.c +++ b/src/main.c @@ -29,12 +29,12 @@ int main(int argc, char* argv[]) { setDefaultMainArguments(&mainArguments); getMainArguments(&mainArguments, argc, argv); - // printf("> dashboardRows --> %d\n", mainArguments.dashboardRows); - // printf("> dashboardCols --> %d\n", mainArguments.dashboardCols); - // printf("> pattern --> %s\n", mainArguments.pattern); - // printf("> maximumGeneration --> %d\n", mainArguments.maximumGeneration); - // printf("> delay --> %d\n", mainArguments.delay); - // printf("> platform --> %s\n", mainArguments.platform); + printf("> dashboardRows --> %d\n", mainArguments.dashboardRows); + printf("> dashboardCols --> %d\n", mainArguments.dashboardCols); + printf("> pattern --> \"%s\"\n", mainArguments.pattern); + printf("> maximumGeneration --> %d\n", mainArguments.maximumGeneration); + printf("> delay --> %d\n", mainArguments.delay); + printf("> platform --> \"%s\"\n", mainArguments.platform); // free(mainArguments.pattern); // free(mainArguments.platform); diff --git a/src/utilities.c b/src/utilities.c index c6af037..392667a 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -12,60 +12,44 @@ void getMainArguments(TMainArguments* pMainArguments, int argc, char* argv[]) { int i; - char argumentName[50]; + char* argumentName; char* argumentValue; + char* sep; int argumentValueInt; for (i = 1; i < argc; i++) { - argumentValue = malloc(50 * sizeof(char)); - sscanf(*(argv + i), "-- %[a-z|-] = %[-|a-z| |0-9]", argumentName, argumentValue); + argumentName = *(argv + i); + sep = strrchr(argumentName, '='); + argumentValue = sep + 1; + *sep = '\0'; - if (strcmp(argumentName, "dashboard-rows") == 0) { + if (strcmp(argumentName, "--dashboard-rows") == 0) { sscanf(argumentValue, "%d", &argumentValueInt); if (validateRows(argumentValueInt)) pMainArguments->dashboardRows = argumentValueInt; - free(argumentValue); - - } else if (strcmp(argumentName, "dashboard-cols") == 0) { + } else if (strcmp(argumentName, "--dashboard-cols") == 0) { sscanf(argumentValue, "%d", &argumentValueInt); if (validateCols(argumentValueInt)) pMainArguments->dashboardCols = argumentValueInt; - free(argumentValue); - - } else if (strcmp(argumentName, "pattern") == 0) { - if (validatePattern(argumentValue)) { - pMainArguments->pattern = argumentValue; - } else { - free(argumentValue); - }; + } else if (strcmp(argumentName, "--pattern") == 0) { + if (validatePattern(argumentValue)) pMainArguments->pattern = argumentValue; - } else if (strcmp(argumentName, "maximum-generation") == 0) { + } else if (strcmp(argumentName, "--maximum-generation") == 0) { if (validateGeneration(argumentValue)) { sscanf(argumentValue, "%d", &argumentValueInt); pMainArguments->maximumGeneration = argumentValueInt; - } - - free(argumentValue); + }; - } else if (strcmp(argumentName, "delay") == 0) { + } else if (strcmp(argumentName, "--delay") == 0) { if (validateDelay(argumentValue)) { sscanf(argumentValue, "%d", &argumentValueInt); pMainArguments->delay = argumentValueInt; }; - free(argumentValue); - - } else if (strcmp(argumentName, "platform") == 0) { - if (validatePlatform(argumentValue)) { - pMainArguments->platform = argumentValue; - } else { - free(argumentValue); - }; - - } else { - free(argumentValue); - } + } else if (strcmp(argumentName, "--platform") == 0) { + if (validatePlatform(argumentValue)) pMainArguments->platform = argumentValue; + }; } }