Merge branch 'Dennis_buffer'

This commit is contained in:
Dennis Boekholtz 2024-04-06 21:14:03 +02:00
commit 2e66ebe1f7
2 changed files with 63 additions and 75 deletions

View File

@ -1,80 +1,70 @@
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include "FIFOBuffChar.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 {
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 FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration
{ {
bool ok = false; 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 // buffer is empty. add first element
FIFO_FirstElement_p = malloc(sizeof(FIFO_element_t)); fifo->FirstEl_p = malloc(sizeof(FIFOBuffChar_element_t));
if (FIFO_FirstElement_p != NULL) if (fifo->FirstEl_p != NULL)
{ {
FIFO_FirstElement_p->character = i; // "value" to "character" fifo->FirstEl_p->character = i; // "value" to "character"
FIFO_FirstElement_p->nextElement = NULL; fifo->FirstEl_p->nextElement = NULL;
FIFO_LastElement_p = FIFO_FirstElement_p; fifo->LastEl_p = fifo->FirstEl_p;
FIFO_count = 1; fifo->size = 1;
ok = true; ok = true;
} }
else else
{ {
FIFO_Full = true; fifo->full = true;
ok = false; 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 // 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) if (el != NULL)
{ {
el->character = i; // "value" to "character" el->character = i; // "value" to "character"
el->nextElement = NULL; el->nextElement = NULL;
FIFO_LastElement_p->nextElement = el; fifo->LastEl_p->nextElement = el;
FIFO_LastElement_p = el; fifo->LastEl_p = el;
FIFO_count++; fifo->size++;
ok = true; ok = true;
} }
else else
{ {
FIFO_Full = true; fifo->full = true;
ok = false; ok = false;
} }
} }
else else
{ {
// buffer is unhealthy. try to clear it and reinit // 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"); printf("buffer_dyn - buffer_put(): ERROR: first pointer is NULL\n");
} }
//TODO: this while loop is unsave //TODO: this while loop is unsave
while (FIFO_FirstElement_p != NULL) while (fifo->FirstEl_p != NULL)
{ {
buffer_get(NULL); FIFOBuffChar_get(fifo, NULL); // Replace buffer_get with FIFOBuffChar_get
} }
if (FIFO_LastElement_p != NULL) if (fifo->LastEl_p != NULL)
{ {
free(FIFO_LastElement_p); free(fifo->LastEl_p);
FIFO_LastElement_p = NULL; fifo->LastEl_p = NULL;
printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n"); 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; ok = false;
} }
return ok; return ok;
@ -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 FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function parameters to match the declaration
{ {
bool ok = false; bool ok = false;
if (FIFO_FirstElement_p != NULL) if (fifo->FirstEl_p != NULL)
{ {
*p = FIFO_FirstElement_p->character; // "value" to "character" *p = fifo->FirstEl_p->character; // "value" to "character"
FIFO_element_t* next = FIFO_FirstElement_p->nextElement; FIFOBuffChar_element_t* next = fifo->FirstEl_p->nextElement;
free(FIFO_FirstElement_p); free(fifo->FirstEl_p);
// FIFO_FirstElement_p = NULL; // fifo->FirstEl_p = NULL;
FIFO_count--; fifo->size--;
FIFO_Full = false; fifo->full = false;
if (next == NULL) if (next == NULL)
{ {
// this was the last element in the buffer // 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 // clear last element becouse it's the same
FIFO_LastElement_p = NULL; fifo->LastEl_p = NULL;
FIFO_FirstElement_p = NULL; fifo->FirstEl_p = NULL;
FIFO_count = 0; fifo->size = 0;
ok = true; ok = true;
} }
else if (FIFO_LastElement_p != NULL) else if (fifo->LastEl_p != NULL)
{ {
// buffer chain is broken; skip to last element // 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"); printf("buffer_dyn - buffer_get(): ERROR: next element is NULL, but it's not the last element. skip to last element\n");
ok = false; ok = false;
} }
else else
{ {
// somehow the last element is NULL // somehow the last element is NULL
FIFO_count = 0; fifo->size = 0;
FIFO_FirstElement_p = NULL; fifo->FirstEl_p = NULL;
printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n"); printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n");
ok = false; ok = false;
} }
@ -121,7 +111,7 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para
else else
{ {
// set first element pointer to next element // set first element pointer to next element
FIFO_FirstElement_p = next; fifo->FirstEl_p = next;
ok = true; ok = true;
} }
} }
@ -132,19 +122,19 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para
return ok; 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", // printf("empty %s: %s; fifo->LastEl_p = %i; fifo->FirstEl_p = %i\n",
// (FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL) ? "true" : "false", // (fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL) ? "true" : "false",
// (FIFO_LastElement_p == FIFO_FirstElement_p) ? "same" : "diff", // (fifo->LastEl_p == fifo->FirstEl_p) ? "same" : "diff",
// (int) FIFO_LastElement_p, // (int) fifo->LastEl_p,
// (int) FIFO_FirstElement_p); // (int) fifo->FirstEl_p);
return ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL)); return ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL));
} }
@ -173,30 +163,28 @@ FIFOBuffChar_t* FIFOBuffChar_create(void)
} }
bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo) bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo)
{ {
// Check if the buffer is already empty // 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
} FIFOBuffChar_element_t* currentElement = fifo->FirstEl_p;
while (currentElement != NULL)
// Delete all elements in the buffer {
FIFO_element_t* currentElement = FIFO_FirstElement_p; FIFOBuffChar_element_t* nextElement = currentElement->nextElement;
while (currentElement != NULL) free(currentElement);
{ currentElement = nextElement;
FIFO_element_t* nextElement = currentElement->nextElement; }
free(currentElement);
currentElement = nextElement;
} }
free(fifo);
return true; return true;
} }
unsigned int number_of_elements_in_buffer() unsigned int FIFOBuffChar_getSize(FIFOBuffChar_t *fifo)
{ {
return FIFO_count; return fifo->size;
} }

View File

@ -6,10 +6,10 @@
// interface for a buffer with chars's // interface for a buffer with chars's
// one element in the buffer // one element in the buffer
struct FIFOBuffChar_element_s { typedef struct FIFOBuffChar_element_s {
char value; char character; // "char character" replaced "int value" because the elements are going to be characters
void* nextEl; void* nextElement;
}; } FIFOBuffChar_element_t;
// defines all vars for the buffer to operate // defines all vars for the buffer to operate
typedef struct FIFOBuffChar_s { typedef struct FIFOBuffChar_s {