fix some errors in CMDList

This commit is contained in:
Laila van Reenen 2024-04-05 22:39:13 +02:00
parent 69c6f63251
commit ec42dc5d3d
2 changed files with 21 additions and 11 deletions

View File

@ -1,7 +1,9 @@
#include "CMDList.h" #include "CMDList.h"
#include <stdlib.h>
#include <string.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()
{ {
CMDList_t *list = malloc(sizeof(CMDList_t)); CMDList_t *list = malloc(sizeof(CMDList_t));
@ -12,11 +14,11 @@ CMDList_t* CMDList_init()
// free up the full tree from memory // free up the full tree from memory
int CMDList_deinit(CMDList_t *list) int CMDList_deinit(CMDList_t *list)
{ {
void* chars[26] = list; CMDList_t* chars[26]; //TODO: cast list to array
for (int i = 0; i < 26; i++) for (int i = 0; i < 26; i++)
{ {
if (chars[i] != null) if (chars[i] != NULL)
{ {
CMDList_deinit(chars[i]); CMDList_deinit(chars[i]);
} }
@ -27,31 +29,31 @@ int CMDList_deinit(CMDList_t *list)
} }
// add a command in the command tree // add a command in the command tree
int CMDList_add(CMDList_t *list, void* cmd) int CMDList_add(CMDList_t *list, CMD_t* cmd)
{ {
char* read_p = cmd; char* read_p = cmd;
CMDList_t* list_p = list; CMDList_t* list_p = list;
while ( while (
(read_p != null) (read_p != NULL)
&& (*read_p != ' ') // space ends the command && (*read_p != ' ') // space ends the command
&& (*read_p != '\n') // end of line && (*read_p != '\n') // end of line
&& (*read_p != '\r') // end of line && (*read_p != '\r') // end of line
&& (*read_p != 0) // end of string && (*read_p != 0) // end of string
&& (read_p - cmd < 500) && (read_p - (char*)cmd < 500)
) )
{ {
char c = *read_p & (~0x20); // to uppercase char c = *read_p & (~0x20); // to uppercase
if ((*read_p >= 'A') && (*read_p <= 'Z')) if ((*read_p >= 'A') && (*read_p <= 'Z'))
{ {
read_p += (read_p - 'A') * sizeof(CMDList_t*); read_p += (*read_p - 'A') * sizeof(CMDList_t*);
if (*((CMDList_t**)read_p) == null) if (*((CMDList_t**)read_p) == NULL)
{ {
read_p = malloc(sizeof(CMDList_t)); read_p = malloc(sizeof(CMDList_t));
} }
else else
{ {
read_p = *((CMDList_t**)read_p) read_p = *((CMDList_t**)read_p);
} }
} }
} }

View File

@ -1,6 +1,11 @@
#ifndef CMDLIST_h #ifndef CMDLIST_h
#define CMDLIST_h #define CMDLIST_h
typedef struct CMD_t {
char* cmd;
void (*fn)(char* line);
} CMD_t;
typedef struct CMDList_s { typedef struct CMDList_s {
void* a; void* a;
void* b; void* b;
@ -28,9 +33,12 @@ typedef struct CMDList_s {
void* x; void* x;
void* y; void* y;
void* z; void* z;
CMD_t cmd; CMD_t* cmd;
} CMDList_t; } CMDList_t;
// initilises a CMDList_t with all null pointers // initilises a CMDList_t with all null pointers
extern CMDList_t* CMDList_init(); extern CMDList_t* CMDList_init();
@ -38,7 +46,7 @@ extern CMDList_t* CMDList_init();
extern int CMDList_deinit(CMDList_t *list); extern int CMDList_deinit(CMDList_t *list);
// add a command in the command tree // add a command in the command tree
extern int CMDList_add(CMDList_t *list, void* 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 int CMDList_get(CMDList_t *list, char* cmd);