netex/src/exp_list.c
2023-07-30 23:01:41 +02:00

78 lines
1.6 KiB
C

#include "stdlib.h"
#include "string.h"
#include "cexp.h"
#include "netex.h"
#define INCREMENT_SIZE 5
int alloc_list();
int increment_list();
static int list_index_count = 0;
static int list_index_total = 0;
static struct exp_data* list = NULL;
void list_add(char* name, char* command, int (*exp_func) (void*))
{
if (alloc_list() != 0)
{ TRACE_ERROR("Unable to allocate memory!"); return; }
list[list_index_count].name = name;
list[list_index_count].command = command;
list[list_index_count].func_ptr = exp_func;
list_index_count++;
}
int alloc_list()
{
if (list == NULL)
{
list = (struct exp_data*)calloc(sizeof(struct exp_data), INCREMENT_SIZE);
if (list == NULL)
return -1;
list_index_total += INCREMENT_SIZE;
}
if (list_index_total <= list_index_count)
return increment_list();
return 0;
}
int increment_list()
{
list_index_total += INCREMENT_SIZE;
list = realloc(list, list_index_total * sizeof(struct exp_data));
if (list == NULL)
return -1;
return 0;
}
void print_list_items()
{
for (size_t i = 0; i < list_index_count; i++)
{
PRINT_LINE("%lu. [%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 || index < 0)
return NULL;
return &list[index];
}
void clear_list()
{
if (list != NULL)
free(list);
return;
}