64 lines
1.2 KiB
C

#include "CMDList.h"
#include <stdlib.h>
#include <string.h>
// initilises a CMDList_t with all NULL pointers
CMDList_t* CMDList_init()
{
CMDList_t *list = malloc(sizeof(CMDList_t));
memset(list, 0, sizeof(CMDList_t));
return list;
}
// free up the full tree from memory
int CMDList_deinit(CMDList_t *list)
{
CMDList_t* chars[26]; //TODO: cast list to array
for (int i = 0; i < 26; i++)
{
if (chars[i] != NULL)
{
CMDList_deinit(chars[i]);
}
}
free(list);
return 0;
}
// add a command in the command tree
int CMDList_add(CMDList_t *list, CMD_t* cmd)
{
char* read_p = cmd;
CMDList_t* list_p = list;
while (
(read_p != NULL)
&& (*read_p != ' ') // space ends the command
&& (*read_p != '\n') // end of line
&& (*read_p != '\r') // end of line
&& (*read_p != 0) // end of string
&& (read_p - (char*)cmd < 500)
)
{
char c = *read_p & (~0x20); // to uppercase
if ((*read_p >= 'A') && (*read_p <= 'Z'))
{
read_p += (*read_p - 'A') * sizeof(CMDList_t*);
if (*((CMDList_t**)read_p) == NULL)
{
read_p = malloc(sizeof(CMDList_t));
}
else
{
read_p = *((CMDList_t**)read_p);
}
}
}
}
// search command in the tree
int CMDList_get(CMDList_t *list, char* cmd);