add delete buff en toevoegen .h en .c comments

This commit is contained in:
Dennis Boekholtz
2024-04-02 21:49:31 +02:00
parent d5e889c9a8
commit 04bd4397e8
2 changed files with 65 additions and 33 deletions

View File

@@ -3,14 +3,14 @@
#include <stdio.h>
#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()
{

View File

@@ -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);