From d5e889c9a890b425cc737b420f1416030d9fca5d Mon Sep 17 00:00:00 2001 From: Dennis Boekholtz <1017359@hr.nl> Date: Sat, 30 Mar 2024 18:28:49 +0100 Subject: [PATCH 1/3] begin functie create --- FIFOBuff/FIFOBuffChar.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index ba0fdb4..acbb63f 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -153,7 +153,30 @@ bool buffer_is_empty(void) } bool FIFOBuffChar_create(int ){ + + bool ok = false; + + // Initialize the FIFO buffer + FIFO_FirstElement_p = NULL; + FIFO_LastElement_p = NULL; + FIFO_count = 0; + FIFO_Full = false; + FIFO_FirstElement_p = malloc(sizeof(FIFO_element_t)); + if (FIFO_FirstElement_p == NULL) + { + // Memory allocation failed + FIFO_Full = true; + return false; + } + // Initialize the first element + FIFO_FirstElement_p->value = 0; + FIFO_FirstElement_p->nextElement = NULL; + + // Point the last element to the first one + FIFO_LastElement_p = FIFO_FirstElement_p; + + return true; } From 04bd4397e8ab25172e9b950684201dad0c743248 Mon Sep 17 00:00:00 2001 From: Dennis Boekholtz <1017359@hr.nl> Date: Tue, 2 Apr 2024 21:49:31 +0200 Subject: [PATCH 2/3] add delete buff en toevoegen .h en .c comments --- FIFOBuff/FIFOBuffChar.c | 94 +++++++++++++++++++++++++++-------------- FIFOBuff/FIFOBuffChar.h | 4 +- 2 files changed, 65 insertions(+), 33 deletions(-) diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index acbb63f..3fd3bc2 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -3,14 +3,14 @@ #include -#include "buffer.h" +#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 { - int value; + char character; // "char character" replaced "int value" because the elements are going to be characters void* nextElement; } FIFO_element_t; @@ -20,7 +20,7 @@ static FIFO_element_t* FIFO_LastElement_p = NULL; static unsigned int FIFO_count = 0; bool FIFO_Full = false; -bool buffer_put(int i) +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)) @@ -29,7 +29,7 @@ bool buffer_put(int i) FIFO_FirstElement_p = malloc(sizeof(FIFO_element_t)); if (FIFO_FirstElement_p != NULL) { - FIFO_FirstElement_p->value = i; + FIFO_FirstElement_p->character = i; // "value" to "character" FIFO_FirstElement_p->nextElement = NULL; FIFO_LastElement_p = FIFO_FirstElement_p; @@ -48,7 +48,7 @@ bool buffer_put(int i) FIFO_element_t* el = malloc(sizeof(FIFO_element_t)); if (el != NULL) { - el->value = i; + el->character = i; // "value" to "character" el->nextElement = NULL; FIFO_LastElement_p->nextElement = el; FIFO_LastElement_p = el; @@ -85,12 +85,12 @@ bool buffer_put(int i) return ok; } -bool buffer_get(int *p) +bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function parameters to match the declaration { bool ok = false; if (FIFO_FirstElement_p != NULL) { - *p = FIFO_FirstElement_p->value; + *p = FIFO_FirstElement_p->character; // "value" to "character" FIFO_element_t* next = FIFO_FirstElement_p->nextElement; free(FIFO_FirstElement_p); // FIFO_FirstElement_p = NULL; @@ -137,12 +137,12 @@ bool buffer_get(int *p) return ok; } -bool buffer_is_full(void) +bool FIFOBuffChar_is_full(void) { return FIFO_Full; } -bool buffer_is_empty(void) +bool FIFOBuffChar_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", @@ -152,39 +152,71 @@ bool buffer_is_empty(void) return ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL)); } -bool FIFOBuffChar_create(int ){ + +FIFOBuffChar_t* FIFOBuffChar_create(void) +{ - bool ok = false; + // Allocate memory for a new FIFOBuffChar_t + FIFOBuffChar_t* fifo = malloc(sizeof(FIFOBuffChar_t)); + + if (fifo == NULL) { + // Memory allocation failed + return NULL; + } // Initialize the FIFO buffer + fifo->FirstEl_p = malloc(sizeof(struct FIFOBuffChar_element_s)); + + if (fifo->FirstEl_p == NULL) { + // Memory allocation failed + free(fifo); + return NULL; + } + + // Initialize the first element + fifo->FirstEl_p->value = 0; // "value" of "char" + fifo->FirstEl_p->nextEl = NULL; + + // Point the last element to the first one + fifo->LastEl_p = fifo->FirstEl_p; + + // Initialize other fields of the FIFOBuffChar_t struct + fifo->size = 1; + fifo->full = false; + + return fifo; + +} + + + +bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo) +{ + + // Check if the buffer is already empty + if (FIFO_FirstElement_p == NULL) + { + return false; + } + + // Delete all elements in the buffer + FIFO_element_t* currentElement = FIFO_FirstElement_p; + while (currentElement != NULL) + { + FIFO_element_t* nextElement = currentElement->nextElement; + free(currentElement); + currentElement = nextElement; + } + + // Reset the FIFO buffer FIFO_FirstElement_p = NULL; FIFO_LastElement_p = NULL; FIFO_count = 0; FIFO_Full = false; - FIFO_FirstElement_p = malloc(sizeof(FIFO_element_t)); - if (FIFO_FirstElement_p == NULL) - { - // Memory allocation failed - FIFO_Full = true; - return false; - } - // Initialize the first element - FIFO_FirstElement_p->value = 0; - FIFO_FirstElement_p->nextElement = NULL; - - // Point the last element to the first one - FIFO_LastElement_p = FIFO_FirstElement_p; - return true; - } -bool FIFOBuffChar_delete(int ){ - -} - - unsigned int number_of_elements_in_buffer() { diff --git a/FIFOBuff/FIFOBuffChar.h b/FIFOBuff/FIFOBuffChar.h index 7663692..7f34907 100644 --- a/FIFOBuff/FIFOBuffChar.h +++ b/FIFOBuff/FIFOBuffChar.h @@ -28,11 +28,11 @@ extern bool FIFOBuffChar_delete(FIFOBuffChar_t* fifo); // put value i in buffer if there is still memory avaliable // returns true on success or false otherways -extern bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i); +extern bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i); //moet dit "char" zijn of "uint8_t" // get value from buffer and writes it to *p if buffer not empty // returns true on success or false otherways -extern bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p); +extern bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p); //moet dit "char" zijn of "uint8_t" // returns false if last put fails to gain memory and no get is called afterwards or true otherwise extern bool FIFOBuffChar_is_full(FIFOBuffChar_t *fifo); From 51b2329ba572addb9f5b43f71c168ed81c29dc5f Mon Sep 17 00:00:00 2001 From: Dennis Boekholtz <1017359@hr.nl> Date: Fri, 5 Apr 2024 20:57:41 +0200 Subject: [PATCH 3/3] overbodinge dingen weg gegooid --- FIFOBuff/FIFOBuffChar.c | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index 3fd3bc2..6ace37a 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -14,11 +14,6 @@ typedef struct FIFO_element_s { 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 FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration { @@ -165,23 +160,12 @@ FIFOBuffChar_t* FIFOBuffChar_create(void) } // Initialize the FIFO buffer - fifo->FirstEl_p = malloc(sizeof(struct FIFOBuffChar_element_s)); - - if (fifo->FirstEl_p == NULL) { - // Memory allocation failed - free(fifo); - return NULL; - } - - // Initialize the first element - fifo->FirstEl_p->value = 0; // "value" of "char" - fifo->FirstEl_p->nextEl = NULL; - // Point the last element to the first one - fifo->LastEl_p = fifo->FirstEl_p; - // Initialize other fields of the FIFOBuffChar_t struct - fifo->size = 1; + + fifo->FirstEl_p = NULL; + fifo->LastEl_p = NULL; + fifo->size = 0; fifo->full = false; return fifo; @@ -208,12 +192,6 @@ bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo) currentElement = nextElement; } - // Reset the FIFO buffer - FIFO_FirstElement_p = NULL; - FIFO_LastElement_p = NULL; - FIFO_count = 0; - FIFO_Full = false; - return true; }