diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index dde5b50..dd89e2b 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -31,32 +31,60 @@ int CMDList_deinit(CMDList_t *list) // add a command in the command tree int CMDList_add(CMDList_t *list, CMD_t* cmd) { - char* read_p = cmd; + int returnCode = 0; + char* read_p = cmd->cmd; CMDList_t* list_p = list; + if (read_p == NULL) + { + returnCode = -1; + } + 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) + ((read_p - cmd->cmd) < (500 * sizeof(char))) + && (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') * sizeof(CMDList_t*); + + if (*((CMDList_t**)list_p) == NULL) + { // it does not point to another list + // allocate a new list for it + *((CMDList_t**)list_p) = malloc(sizeof(CMDList_t)); + if (list_p == NULL) + { // allocation faild (memory full?) + returnCode = -2; + } } else - { - read_p = *((CMDList_t**)read_p); + { // it points to an other list + // update pointer to this list + *((CMDList_t**)list_p) = *((CMDList_t**)read_p); } + // update list_p for the next list + list_p = *((CMDList_t**)list_p); } + else + { // invalid caracter + break; + } + + // read next character + read_p += sizeof(char); } + + if (returnCode >= 0) + { // no errors + // add command in it's spot + list_p->cmd = cmd; + } + + return returnCode; } // search command in the tree