62 lines
1.1 KiB
C
62 lines
1.1 KiB
C
#include "CMDList.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)
|
|
{
|
|
void* chars[26] = list;
|
|
|
|
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, void* 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 - 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);
|