Merge branch 'finley'

This commit is contained in:
Laila van Reenen 2024-04-06 14:24:00 +02:00
commit c6843d2aba
2 changed files with 118 additions and 25 deletions

View File

@ -1,7 +1,9 @@
#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()
@ -12,52 +14,143 @@ CMDList_t* CMDList_init()
}
// free up the full tree from memory
// does not free up the commands
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++)
{
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;
}
// 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;
int returnCode = 0;
char* read_p = cmd->cmd;
CMDList_t** list_p = (CMDList_t**)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)
)
if (read_p == NULL)
{ // cmd is missing a string for the commnad name
returnCode = -1;
}
while (returnCode == 0)
{
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));
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
{
read_p = *((CMDList_t**)read_p);
{ // 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
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;
}

View File

@ -49,6 +49,6 @@ extern int CMDList_deinit(CMDList_t *list);
extern int CMDList_add(CMDList_t *list, CMD_t* cmd);
// 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