154 lines
2.7 KiB
C
154 lines
2.7 KiB
C
#include "CMDList.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <stdio.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
|
|
// does not free up the commands
|
|
int CMDList_deinit(CMDList_t* list)
|
|
{
|
|
CMDList_t** list_p = (CMDList_t**)list;
|
|
|
|
int i;
|
|
for (i = 0; i < 26; i++)
|
|
{
|
|
if (*(list_p + i) != NULL)
|
|
{
|
|
CMDList_deinit(*(list_p + i));
|
|
}
|
|
}
|
|
|
|
free(list);
|
|
return 0;
|
|
}
|
|
|
|
// add a command in the command tree
|
|
int CMDList_add(CMDList_t *list, CMD_t* cmd, char* cmdName)
|
|
{
|
|
int returnCode = 0;
|
|
char* read_p = cmdName;
|
|
CMDList_t** list_p = (CMDList_t**)list;
|
|
|
|
if (read_p == NULL)
|
|
{
|
|
read_p = cmd->cmd;
|
|
}
|
|
|
|
while (returnCode >= 0)
|
|
{
|
|
char c = *read_p & (~0x20); // convert to uppercase
|
|
if ((c >= 'A') && (c <= 'Z'))
|
|
{ // valid character
|
|
|
|
// get memory address of the caracter in the CMDList
|
|
list_p += (c - 'A');
|
|
|
|
if (*list_p == NULL)
|
|
{ // it does not point to another list
|
|
// allocate a new list for it
|
|
*list_p = CMDList_init();
|
|
if (list_p == NULL)
|
|
{ // allocation faild (memory full?)
|
|
returnCode = -2;
|
|
}
|
|
|
|
// update list_p for the next list
|
|
list_p = (CMDList_t**)(*list_p);
|
|
}
|
|
else
|
|
{ // it points to an other list
|
|
// update pointer to this list
|
|
list_p = (CMDList_t**)(*list_p);
|
|
}
|
|
}
|
|
else
|
|
{ // invalid caracter
|
|
break;
|
|
}
|
|
|
|
// read next character
|
|
read_p++;
|
|
}
|
|
|
|
if (returnCode >= 0)
|
|
{ // no errors
|
|
|
|
if (((CMDList_t*)list_p)->cmd != NULL)
|
|
{ // replacing a command
|
|
returnCode = 1;
|
|
}
|
|
// add command in it's spot
|
|
((CMDList_t*)list_p)->cmd = cmd;
|
|
}
|
|
|
|
|
|
return returnCode;
|
|
}
|
|
|
|
// search command in the tree
|
|
CMD_t* CMDList_get(CMDList_t *list, char* cmd)
|
|
{
|
|
bool found = true;
|
|
CMD_t* cmd_object = NULL;
|
|
char* read_p = cmd;
|
|
CMDList_t** list_p = (CMDList_t**)list;
|
|
|
|
while (found)
|
|
{
|
|
//TODO: mayby make a copy before converting to upper case
|
|
char c = *read_p & (~0x20); // convert to uppercase
|
|
if ((c >= 'A') && (c <= 'Z'))
|
|
{ // valid character
|
|
|
|
// get memory address of the caracter in the CMDList
|
|
list_p += (c - 'A');
|
|
|
|
if (*list_p == NULL)
|
|
{ // no command found
|
|
found = false;
|
|
}
|
|
else
|
|
{ // it points to an other list
|
|
// update pointer to this list
|
|
list_p = (CMDList_t**)(*list_p);
|
|
}
|
|
}
|
|
else
|
|
{ // invalid caracter
|
|
break;
|
|
}
|
|
|
|
// read next character
|
|
read_p++;
|
|
}
|
|
|
|
if (found)
|
|
{ // list found for command
|
|
if (((CMDList_t*)list_p)->cmd == NULL)
|
|
{ // command incomplete
|
|
found = false;
|
|
cmd_object = NULL;
|
|
}
|
|
else
|
|
{
|
|
cmd_object = ((CMDList_t*)list_p)->cmd;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cmd_object = NULL;
|
|
}
|
|
|
|
return cmd_object;
|
|
}
|