From ee4f92b225e57bfffa4b7e5d00a85ab2c71b791f Mon Sep 17 00:00:00 2001 From: Dennis Boekholtz <1017359@hr.nl> Date: Fri, 5 Apr 2024 21:25:41 +0200 Subject: [PATCH 1/3] verder opschonen --- FIFOBuff/FIFOBuffChar.c | 109 ++++++++++++++++++---------------------- FIFOBuff/FIFOBuffChar.h | 8 +-- 2 files changed, 53 insertions(+), 64 deletions(-) diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index 6ace37a..2c95216 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -1,77 +1,67 @@ #include #include - #include - #include "FIFOBuffChar.h" -// implementation for a FIFO-buffer with ints -// this very simple FIFO-buffer can only hold one int -#define FIFO_SIZE 8 - -typedef struct FIFO_element_s { - char character; // "char character" replaced "int value" because the elements are going to be characters - void* nextElement; -} FIFO_element_t; bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration { bool ok = false; - if ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL)) + if ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL)) { // buffer is empty. add first element - FIFO_FirstElement_p = malloc(sizeof(FIFO_element_t)); - if (FIFO_FirstElement_p != NULL) + fifo->FirstEl_p = malloc(sizeof(FIFOBuffChar_element_t)); + if (fifo->FirstEl_p != NULL) { - FIFO_FirstElement_p->character = i; // "value" to "character" - FIFO_FirstElement_p->nextElement = NULL; + fifo->FirstEl_p->character = i; // "value" to "character" + fifo->FirstEl_p->nextElement = NULL; - FIFO_LastElement_p = FIFO_FirstElement_p; - FIFO_count = 1; + fifo->LastEl_p = fifo->FirstEl_p; + fifo->size = 1; ok = true; } else { - FIFO_Full = true; + fifo->full = true; ok = false; } } - else if ((FIFO_LastElement_p != NULL) && (FIFO_FirstElement_p != NULL)) + else if ((fifo->LastEl_p != NULL) && (fifo->FirstEl_p != NULL)) { // add element to exsiting buffer - FIFO_element_t* el = malloc(sizeof(FIFO_element_t)); + FIFOBuffChar_element_t* el = malloc(sizeof(FIFOBuffChar_element_t)); if (el != NULL) { el->character = i; // "value" to "character" el->nextElement = NULL; - FIFO_LastElement_p->nextElement = el; - FIFO_LastElement_p = el; - FIFO_count++; + fifo->LastEl_p->nextElement = el; + fifo->LastEl_p = el; + fifo->size++; ok = true; } else { - FIFO_Full = true; + fifo->full = true; ok = false; } } else { // buffer is unhealthy. try to clear it and reinit - if (FIFO_FirstElement_p == NULL) + if (fifo->FirstEl_p == NULL) { printf("buffer_dyn - buffer_put(): ERROR: first pointer is NULL\n"); } //TODO: this while loop is unsave - while (FIFO_FirstElement_p != NULL) + while (fifo->FirstEl_p != NULL) { buffer_get(NULL); } - if (FIFO_LastElement_p != NULL) + if (fifo->LastEl_p != NULL) { - free(FIFO_LastElement_p); - FIFO_LastElement_p = NULL; + free(fifo->LastEl_p); + fifo->LastEl_p = NULL; printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n"); } buffer_put(i); @@ -83,37 +73,37 @@ bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function param bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function parameters to match the declaration { bool ok = false; - if (FIFO_FirstElement_p != NULL) + if (fifo->FirstEl_p != NULL) { - *p = FIFO_FirstElement_p->character; // "value" to "character" - FIFO_element_t* next = FIFO_FirstElement_p->nextElement; - free(FIFO_FirstElement_p); - // FIFO_FirstElement_p = NULL; - FIFO_count--; - FIFO_Full = false; + *p = fifo->FirstEl_p->character; // "value" to "character" + FIFOBuffChar_element_t* next = fifo->FirstEl_p->nextElement; + free(fifo->FirstEl_p); + // fifo->FirstEl_p = NULL; + fifo->size--; + fifo->full = false; if (next == NULL) { // this was the last element in the buffer - if (FIFO_LastElement_p == FIFO_FirstElement_p) + if (fifo->LastEl_p == fifo->FirstEl_p) { // clear last element becouse it's the same - FIFO_LastElement_p = NULL; - FIFO_FirstElement_p = NULL; - FIFO_count = 0; + fifo->LastEl_p = NULL; + fifo->FirstEl_p = NULL; + fifo->size = 0; ok = true; } - else if (FIFO_LastElement_p != NULL) + else if (fifo->LastEl_p != NULL) { // buffer chain is broken; skip to last element - FIFO_FirstElement_p = FIFO_LastElement_p; + fifo->FirstEl_p = fifo->LastEl_p; printf("buffer_dyn - buffer_get(): ERROR: next element is NULL, but it's not the last element. skip to last element\n"); ok = false; } else { // somehow the last element is NULL - FIFO_count = 0; - FIFO_FirstElement_p = NULL; + fifo->size = 0; + fifo->FirstEl_p = NULL; printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n"); ok = false; } @@ -121,7 +111,7 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para else { // set first element pointer to next element - FIFO_FirstElement_p = next; + fifo->FirstEl_p = next; ok = true; } } @@ -132,19 +122,19 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para return ok; } -bool FIFOBuffChar_is_full(void) +bool FIFOBuffChar_is_full(FIFOBuffChar_t *fifo) { - return FIFO_Full; + return fifo->full; } -bool FIFOBuffChar_is_empty(void) +bool FIFOBuffChar_is_empty(FIFOBuffChar_t *fifo) { - // printf("empty %s: %s; FIFO_LastElement_p = %i; FIFO_FirstElement_p = %i\n", - // (FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL) ? "true" : "false", - // (FIFO_LastElement_p == FIFO_FirstElement_p) ? "same" : "diff", - // (int) FIFO_LastElement_p, - // (int) FIFO_FirstElement_p); - return ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL)); + // printf("empty %s: %s; fifo->LastEl_p = %i; fifo->FirstEl_p = %i\n", + // (fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL) ? "true" : "false", + // (fifo->LastEl_p == fifo->FirstEl_p) ? "same" : "diff", + // (int) fifo->LastEl_p, + // (int) fifo->FirstEl_p); + return ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL)); } @@ -173,21 +163,20 @@ FIFOBuffChar_t* FIFOBuffChar_create(void) } - bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo) { // Check if the buffer is already empty - if (FIFO_FirstElement_p == NULL) + if (fifo->FirstEl_p == NULL) { return false; } // Delete all elements in the buffer - FIFO_element_t* currentElement = FIFO_FirstElement_p; + FIFOBuffChar_element_t* currentElement = fifo->FirstEl_p; while (currentElement != NULL) { - FIFO_element_t* nextElement = currentElement->nextElement; + FIFOBuffChar_element_t* nextElement = currentElement->nextElement; free(currentElement); currentElement = nextElement; } @@ -196,7 +185,7 @@ bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo) } -unsigned int number_of_elements_in_buffer() +unsigned int FIFOBuffChar_getSize(FIFOBuffChar_t *fifo) { - return FIFO_count; + return fifo->size; } diff --git a/FIFOBuff/FIFOBuffChar.h b/FIFOBuff/FIFOBuffChar.h index 7f34907..b6eb1b3 100644 --- a/FIFOBuff/FIFOBuffChar.h +++ b/FIFOBuff/FIFOBuffChar.h @@ -6,10 +6,10 @@ // interface for a buffer with chars's // one element in the buffer -struct FIFOBuffChar_element_s { - char value; - void* nextEl; -}; +typedef struct FIFOBuffChar_element_s { + char character; // "char character" replaced "int value" because the elements are going to be characters + void* nextElement; +} FIFOBuffChar_element_t; // defines all vars for the buffer to operate typedef struct FIFOBuffChar_s { From 5e3bb0ca4553ac614e270a20201596d1cd993ac3 Mon Sep 17 00:00:00 2001 From: Dennis Boekholtz <1017359@hr.nl> Date: Sat, 6 Apr 2024 19:27:07 +0200 Subject: [PATCH 2/3] buff put & get vergeten te veranderen --- FIFOBuff/FIFOBuffChar.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index 2c95216..401f2c5 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -56,7 +56,7 @@ bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function param //TODO: this while loop is unsave while (fifo->FirstEl_p != NULL) { - buffer_get(NULL); + FIFOBuffChar_get(fifo, NULL); // Replace buffer_get with FIFOBuffChar_get } if (fifo->LastEl_p != NULL) { @@ -64,7 +64,7 @@ bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function param fifo->LastEl_p = NULL; printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n"); } - buffer_put(i); + FIFOBuffChar_put(fifo, i); // Replace buffer_put with FIFOBuffChar_put ok = false; } return ok; From 8669bdcf432529d5546eb280444b70a03be7096a Mon Sep 17 00:00:00 2001 From: Dennis Boekholtz <1017359@hr.nl> Date: Sat, 6 Apr 2024 21:11:13 +0200 Subject: [PATCH 3/3] buffer verwijderen aangepast --- FIFOBuff/FIFOBuffChar.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index 401f2c5..5756401 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -169,18 +169,17 @@ bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo) // Check if the buffer is already empty if (fifo->FirstEl_p == NULL) { - return false; + // Delete all elements in the buffer + FIFOBuffChar_element_t* currentElement = fifo->FirstEl_p; + while (currentElement != NULL) + { + FIFOBuffChar_element_t* nextElement = currentElement->nextElement; + free(currentElement); + currentElement = nextElement; + } } - - // Delete all elements in the buffer - FIFOBuffChar_element_t* currentElement = fifo->FirstEl_p; - while (currentElement != NULL) - { - FIFOBuffChar_element_t* nextElement = currentElement->nextElement; - free(currentElement); - currentElement = nextElement; - } - + + free(fifo); return true; }