Args gets functions directly from list. Some small changes

This commit is contained in:
Max 2023-02-01 13:49:31 +01:00
parent d508ffc81b
commit 815ce0c6c4
3 changed files with 22 additions and 14 deletions

View File

@ -1,4 +1,5 @@
#include "stdlib.h" #include "stdlib.h"
#include "string.h"
#include "cexp.h" #include "cexp.h"
#define INCREMENT_SIZE 5 #define INCREMENT_SIZE 5
@ -44,14 +45,25 @@ int increment_list()
} }
void print_list_items() 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); 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) struct exp_data* get_from_list(int index)
{ {
if (index > list_index_count) if (index > list_index_count || index < 0)
return NULL; return NULL;
return &list[index]; return &list[index];
} }

View File

@ -12,6 +12,7 @@ struct exp_data{
// Exp list functions // Exp list functions
void list_add(char* name, char* command, int (*exp_func) (void*)); void list_add(char* name, char* command, int (*exp_func) (void*));
void print_list_items(); void print_list_items();
struct exp_data* get_command(char* command);
struct exp_data* get_from_list(int index); struct exp_data* get_from_list(int index);
void clear_list(); void clear_list();

View File

@ -13,8 +13,8 @@ void app_cleanup();
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
signal(SIGINT, app_cleanup); signal(SIGINT, app_cleanup);
list_add("Socket server", "sock:server", start_server); list_add("Socket server", "server", start_server);
list_add("Socket client", "sock:client", clientstart); list_add("Socket client", "client", clientstart);
int result; int result;
printf(" _____ \n / ____| \n | | _____ ___ __ \n | | / _ \\ \\/ / '_ \\ \n | |___| __/> <| |_) |\n \\_____\\___/_/\\_\\ .__/ \n | | \n |_| \n"); printf(" _____ \n / ____| \n | | _____ ___ __ \n | | / _ \\ \\/ / '_ \\ \n | |___| __/> <| |_) |\n \\_____\\___/_/\\_\\ .__/ \n | | \n |_| \n");
@ -32,15 +32,10 @@ int checkarg(int argc, char* argv[])
{ {
if (argc == 2) if (argc == 2)
{ {
if (strcmp(argv[1], "server") == 0) struct exp_data* command = get_command(argv[1]);
{ if (command == NULL)
return start_server(); return -1;
} command->func_ptr(NULL);
if (strcmp(argv[1], "client") == 0)
{
clientstart();
return 0;
}
} }
return -1; return -1;
} }
@ -63,7 +58,7 @@ int handleoptions()
return -1; 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; return data->func_ptr != NULL ? data->func_ptr(NULL) : -1;
} }