diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index 251323e..dde5b50 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -1,7 +1,9 @@ #include "CMDList.h" +#include +#include -// initilises a CMDList_t with all null pointers +// initilises a CMDList_t with all NULL pointers CMDList_t* CMDList_init() { CMDList_t *list = malloc(sizeof(CMDList_t)); @@ -12,11 +14,11 @@ CMDList_t* CMDList_init() // free up the full tree from memory 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++) { - if (chars[i] != null) + if (chars[i] != NULL) { CMDList_deinit(chars[i]); } @@ -27,31 +29,31 @@ int CMDList_deinit(CMDList_t *list) } // 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; CMDList_t* list_p = list; while ( - (read_p != null) + (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) + && (read_p - (char*)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 += (*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) + read_p = *((CMDList_t**)read_p); } } } diff --git a/CMDList/CMDList.h b/CMDList/CMDList.h index 9732524..b19004f 100644 --- a/CMDList/CMDList.h +++ b/CMDList/CMDList.h @@ -1,6 +1,11 @@ #ifndef CMDLIST_h #define CMDLIST_h +typedef struct CMD_t { + char* cmd; + void (*fn)(char* line); +} CMD_t; + typedef struct CMDList_s { void* a; void* b; @@ -28,9 +33,12 @@ typedef struct CMDList_s { void* x; void* y; void* z; - CMD_t cmd; + CMD_t* cmd; } CMDList_t; + + + // initilises a CMDList_t with all null pointers extern CMDList_t* CMDList_init(); @@ -38,7 +46,7 @@ extern CMDList_t* CMDList_init(); extern int CMDList_deinit(CMDList_t *list); // 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 extern int CMDList_get(CMDList_t *list, char* cmd);