From 50fd4d952f79e56fa12f10d7666733a2c7c3656b Mon Sep 17 00:00:00 2001 From: Dennis Boekholtz <1017359@hr.nl> Date: Wed, 27 Mar 2024 16:28:27 +0100 Subject: [PATCH] Test Commit --- FIFOBuff/FIFOBuffChar.c | 158 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 FIFOBuff/FIFOBuffChar.c diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c new file mode 100644 index 0000000..bb71290 --- /dev/null +++ b/FIFOBuff/FIFOBuffChar.c @@ -0,0 +1,158 @@ +#include +#include + +#include + +#include "buffer.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 { + int value; + void* nextElement; +} FIFO_element_t; + +// shared variables within this file +static FIFO_element_t* FIFO_FirstElement_p = NULL; +static FIFO_element_t* FIFO_LastElement_p = NULL; +static unsigned int FIFO_count = 0; +bool FIFO_Full = false; + +bool buffer_put(int i) +{ + bool ok = false; + if ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL)) + { + // buffer is empty. add first element + FIFO_FirstElement_p = malloc(sizeof(FIFO_element_t)); + if (FIFO_FirstElement_p != NULL) + { + FIFO_FirstElement_p->value = i; + FIFO_FirstElement_p->nextElement = NULL; + + FIFO_LastElement_p = FIFO_FirstElement_p; + FIFO_count = 1; + ok = true; + } + else + { + FIFO_Full = true; + ok = false; + } + } + else if ((FIFO_LastElement_p != NULL) && (FIFO_FirstElement_p != NULL)) + { + // add element to exsiting buffer + FIFO_element_t* el = malloc(sizeof(FIFO_element_t)); + if (el != NULL) + { + el->value = i; + el->nextElement = NULL; + FIFO_LastElement_p->nextElement = el; + FIFO_LastElement_p = el; + FIFO_count++; + ok = true; + } + else + { + FIFO_Full = true; + ok = false; + } + } + else + { + // buffer is unhealthy. try to clear it and reinit + if (FIFO_FirstElement_p == NULL) + { + printf("buffer_dyn - buffer_put(): ERROR: first pointer is NULL\n"); + } + //TODO: this while loop is unsave + while (FIFO_FirstElement_p != NULL) + { + buffer_get(NULL); + } + if (FIFO_LastElement_p != NULL) + { + free(FIFO_LastElement_p); + FIFO_LastElement_p = NULL; + printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n"); + } + buffer_put(i); + ok = false; + } + return ok; +} + +bool buffer_get(int *p) +{ + bool ok = false; + if (FIFO_FirstElement_p != NULL) + { + *p = FIFO_FirstElement_p->value; + FIFO_element_t* next = FIFO_FirstElement_p->nextElement; + free(FIFO_FirstElement_p); + // FIFO_FirstElement_p = NULL; + FIFO_count--; + FIFO_Full = false; + if (next == NULL) + { + // this was the last element in the buffer + if (FIFO_LastElement_p == FIFO_FirstElement_p) + { + // clear last element becouse it's the same + FIFO_LastElement_p = NULL; + FIFO_FirstElement_p = NULL; + FIFO_count = 0; + ok = true; + } + else if (FIFO_LastElement_p != NULL) + { + // buffer chain is broken; skip to last element + FIFO_FirstElement_p = FIFO_LastElement_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; + printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n"); + ok = false; + } + } + else + { + // set first element pointer to next element + FIFO_FirstElement_p = next; + ok = true; + } + } + else + { + ok = false; + } + return ok; +} + +bool buffer_is_full(void) +{ + return FIFO_Full; +} + +bool buffer_is_empty(void) +{ + // 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)); +} + +unsigned int number_of_elements_in_buffer() +{ + return FIFO_count; +}