EMS31_submodules/FIFOBuff/FIFOBuffChar.c
2024-04-13 23:17:47 +02:00

234 lines
6.7 KiB
C

#include "C:\Users\denbo\git\ems31_2023-2024_groep_09_ccs\ ems31_2023-2024_groep_09_submodules_2\FIFOBuff\FIFOBuffChar.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration
{
bool ok = false;
if ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL))
{
// buffer is empty. add first element
fifo->FirstEl_p = malloc(sizeof(FIFOBuffChar_element_t));
if (fifo->FirstEl_p != NULL)
{
fifo->FirstEl_p->character = i; // "value" to "character"
fifo->FirstEl_p->nextElement = NULL;
fifo->LastEl_p = fifo->FirstEl_p;
fifo->size = 1;
ok = true;
}
else
{
fifo->full = true;
ok = false;
}
}
else if ((fifo->LastEl_p != NULL) && (fifo->FirstEl_p != NULL))
{
// add element to exsiting buffer
FIFOBuffChar_element_t* el = malloc(sizeof(FIFOBuffChar_element_t));
if (el != NULL)
{
el->character = i; // "value" to "character"
el->nextElement = NULL;
fifo->LastEl_p->nextElement = el;
fifo->LastEl_p = el;
fifo->size++;
ok = true;
}
else
{
fifo->full = true;
ok = false;
}
}
else
{
// buffer is unhealthy. try to clear it and reinit
if (fifo->FirstEl_p == NULL)
{
printf("buffer_dyn - buffer_put(): ERROR: first pointer is NULL\n");
}
//TODO: this while loop is unsave
while (fifo->FirstEl_p != NULL)
{
FIFOBuffChar_get(fifo, NULL); // Replace buffer_get with FIFOBuffChar_get
}
if (fifo->LastEl_p != NULL)
{
free(fifo->LastEl_p);
fifo->LastEl_p = NULL;
printf("buffer_dyn - buffer_put(): ERROR: last pointer is NULL\n");
}
FIFOBuffChar_put(fifo, i); // Replace buffer_put with FIFOBuffChar_put
ok = false;
}
return ok;
}
bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function parameters to match the declaration
{
bool ok = false;
if (fifo->FirstEl_p != NULL)
{
*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->LastEl_p == fifo->FirstEl_p)
{
// clear last element becouse it's the same
fifo->LastEl_p = NULL;
fifo->FirstEl_p = NULL;
fifo->size = 0;
ok = true;
}
else if (fifo->LastEl_p != NULL)
{
// buffer chain is broken; skip to last element
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->size = 0;
fifo->FirstEl_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->FirstEl_p = next;
ok = true;
}
}
else
{
ok = false;
}
return ok;
}
bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo)
{
bool ok = true;
switch (fifo->size){
case 0: // buffer is empty
ok = false;
break;
case 1: // singel item in buffer
// make buffer empty
free(fifo->FirstEl_p);
fifo->FirstEl_p = NULL;
fifo->LastEl_p = NULL;
fifo->size = 0;
ok = true;
break;
default: // buffer is at least 2 element big
// find the second last element
{
FIFOBuffChar_element_t* secondLastEl = fifo->FirstEl_p;
while (
secondLastEl->nextElement != fifo->LastEl_p
&& ((FIFOBuffChar_element_t*)secondLastEl->nextElement)->nextElement != NULL
){
secondLastEl = secondLastEl->nextElement;
}
// remove last element
free(fifo->LastEl_p);
// update chain
secondLastEl->nextElement = NULL;
fifo->LastEl_p = secondLastEl;
fifo->size--;
ok = true;
break;
}
}
return ok;
}
bool FIFOBuffChar_is_full(FIFOBuffChar_t *fifo)
{
return fifo->full;
}
bool FIFOBuffChar_is_empty(FIFOBuffChar_t *fifo)
{
// 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));
}
FIFOBuffChar_t* FIFOBuffChar_create(void)
{
// 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
// Point the last element to the first one
// Initialize other fields of the FIFOBuffChar_t struct
fifo->FirstEl_p = NULL;
fifo->LastEl_p = NULL;
fifo->size = 0;
fifo->full = false;
return fifo;
}
bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo)
{
// Check if the buffer is already empty
if (fifo->FirstEl_p == NULL)
{
// 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;
}
unsigned int FIFOBuffChar_getSize(FIFOBuffChar_t *fifo)
{
return fifo->size;
}