203 lines
5.7 KiB
C
203 lines
5.7 KiB
C
#include <stdlib.h>
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.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 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->character = i; // "value" to "character"
|
|
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->character = i; // "value" to "character"
|
|
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 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->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;
|
|
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 FIFOBuffChar_is_full(void)
|
|
{
|
|
return FIFO_Full;
|
|
}
|
|
|
|
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",
|
|
// (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));
|
|
}
|
|
|
|
|
|
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_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;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
unsigned int number_of_elements_in_buffer()
|
|
{
|
|
return FIFO_count;
|
|
}
|