From 815ce0c6c4b3258f5346dac4df8a6b920af4d6c9 Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Wed, 1 Feb 2023 13:49:31 +0100 Subject: [PATCH] Args gets functions directly from list. Some small changes --- src/exp_list.c | 16 ++++++++++++++-- src/include/cexp.h | 1 + src/main.c | 19 +++++++------------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/exp_list.c b/src/exp_list.c index c754be8..75684e9 100644 --- a/src/exp_list.c +++ b/src/exp_list.c @@ -1,4 +1,5 @@ #include "stdlib.h" +#include "string.h" #include "cexp.h" #define INCREMENT_SIZE 5 @@ -44,14 +45,25 @@ int increment_list() } void print_list_items() { - for (int i = 0; i < list_index_count; i++) + for (size_t i = 0; i < list_index_count; i++) { PRINT_LINE("%i. [%s]", i, list[i].name); } } +struct exp_data* get_command(char* command) +{ + for (size_t i = 0; i < list_index_count; i++) + { + struct exp_data* item = &list[i]; + if (strcmp(item->command, command) == 0) + return item; + } + return NULL; +}; + struct exp_data* get_from_list(int index) { - if (index > list_index_count) + if (index > list_index_count || index < 0) return NULL; return &list[index]; } diff --git a/src/include/cexp.h b/src/include/cexp.h index 5c8b352..0097ed2 100644 --- a/src/include/cexp.h +++ b/src/include/cexp.h @@ -12,6 +12,7 @@ struct exp_data{ // Exp list functions void list_add(char* name, char* command, int (*exp_func) (void*)); void print_list_items(); +struct exp_data* get_command(char* command); struct exp_data* get_from_list(int index); void clear_list(); diff --git a/src/main.c b/src/main.c index b8e3698..60021b4 100644 --- a/src/main.c +++ b/src/main.c @@ -13,8 +13,8 @@ void app_cleanup(); int main(int argc, char *argv[]) { signal(SIGINT, app_cleanup); - list_add("Socket server", "sock:server", start_server); - list_add("Socket client", "sock:client", clientstart); + list_add("Socket server", "server", start_server); + list_add("Socket client", "client", clientstart); int result; printf(" _____ \n / ____| \n | | _____ ___ __ \n | | / _ \\ \\/ / '_ \\ \n | |___| __/> <| |_) |\n \\_____\\___/_/\\_\\ .__/ \n | | \n |_| \n"); @@ -32,15 +32,10 @@ int checkarg(int argc, char* argv[]) { if (argc == 2) { - if (strcmp(argv[1], "server") == 0) - { - return start_server(); - } - if (strcmp(argv[1], "client") == 0) - { - clientstart(); - return 0; - } + struct exp_data* command = get_command(argv[1]); + if (command == NULL) + return -1; + command->func_ptr(NULL); } return -1; } @@ -63,7 +58,7 @@ int handleoptions() return -1; } - PRINT_LINE("Option: [%s] chosen", data->name); + PRINT_LINE("Option: [%s] selected", data->name); return data->func_ptr != NULL ? data->func_ptr(NULL) : -1; }