Merge branch 'finley'
This commit is contained in:
commit
c6843d2aba
@ -1,7 +1,9 @@
|
|||||||
#include "CMDList.h"
|
#include "CMDList.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// initilises a CMDList_t with all NULL pointers
|
// initilises a CMDList_t with all NULL pointers
|
||||||
CMDList_t* CMDList_init()
|
CMDList_t* CMDList_init()
|
||||||
@ -12,52 +14,143 @@ CMDList_t* CMDList_init()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// free up the full tree from memory
|
// free up the full tree from memory
|
||||||
|
// does not free up the commands
|
||||||
int CMDList_deinit(CMDList_t *list)
|
int CMDList_deinit(CMDList_t *list)
|
||||||
{
|
{
|
||||||
CMDList_t* chars[26]; //TODO: cast list to array
|
CMDList_t** list_p = (CMDList_t**)list;
|
||||||
|
// printf("deinit %p\n", (void*)list);
|
||||||
|
// printf("deinit e %p\n", list->e);
|
||||||
|
|
||||||
for (int i = 0; i < 26; i++)
|
for (int i = 0; i < 26; i++)
|
||||||
{
|
{
|
||||||
if (chars[i] != NULL)
|
if (*(list_p + i) != NULL)
|
||||||
{
|
{
|
||||||
CMDList_deinit(chars[i]);
|
// printf("deinit %i\n", i);
|
||||||
|
CMDList_deinit(*(list_p + i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(list);
|
//TODO: fix "free(): invalid pointer"
|
||||||
|
// free(list);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add a command in the command tree
|
// add a command in the command tree
|
||||||
int CMDList_add(CMDList_t *list, CMD_t* cmd)
|
int CMDList_add(CMDList_t *list, CMD_t* cmd)
|
||||||
{
|
{
|
||||||
char* read_p = cmd;
|
int returnCode = 0;
|
||||||
CMDList_t* list_p = list;
|
char* read_p = cmd->cmd;
|
||||||
|
CMDList_t** list_p = (CMDList_t**)list;
|
||||||
|
|
||||||
while (
|
if (read_p == NULL)
|
||||||
(read_p != NULL)
|
{ // cmd is missing a string for the commnad name
|
||||||
&& (*read_p != ' ') // space ends the command
|
returnCode = -1;
|
||||||
&& (*read_p != '\n') // end of line
|
}
|
||||||
&& (*read_p != '\r') // end of line
|
|
||||||
&& (*read_p != 0) // end of string
|
while (returnCode == 0)
|
||||||
&& (read_p - (char*)cmd < 500)
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
char c = *read_p & (~0x20); // to uppercase
|
char c = *read_p & (~0x20); // convert to uppercase
|
||||||
if ((*read_p >= 'A') && (*read_p <= 'Z'))
|
if ((c >= 'A') && (c <= 'Z'))
|
||||||
{
|
{ // valid character
|
||||||
read_p += (*read_p - 'A') * sizeof(CMDList_t*);
|
|
||||||
if (*((CMDList_t**)read_p) == NULL)
|
// get memory address of the caracter in the CMDList
|
||||||
{
|
list_p += (c - 'A');
|
||||||
read_p = malloc(sizeof(CMDList_t));
|
|
||||||
|
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
|
else
|
||||||
{
|
{ // it points to an other list
|
||||||
read_p = *((CMDList_t**)read_p);
|
// 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
|
// search command in the tree
|
||||||
int CMDList_get(CMDList_t *list, char* cmd);
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -49,6 +49,6 @@ extern int CMDList_deinit(CMDList_t *list);
|
|||||||
extern int CMDList_add(CMDList_t *list, CMD_t* cmd);
|
extern int CMDList_add(CMDList_t *list, CMD_t* cmd);
|
||||||
|
|
||||||
// search command in the tree
|
// search command in the tree
|
||||||
extern int CMDList_get(CMDList_t *list, char* cmd);
|
extern CMD_t* CMDList_get(CMDList_t *list, char* cmd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user