Merge commit 'd69605b3a6172bcd5845c394aa7f8395364f584a'
This commit is contained in:
commit
e46dc50f83
@ -3,24 +3,19 @@
|
||||
|
||||
#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;
|
||||
|
||||
// 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 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 +24,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 +43,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 +80,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 +132,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,17 +147,55 @@ bool buffer_is_empty(void)
|
||||
return ((FIFO_LastElement_p == NULL) && (FIFO_FirstElement_p == NULL));
|
||||
}
|
||||
|
||||
bool FIFOBuffChar_create(int ){
|
||||
|
||||
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(int ){
|
||||
|
||||
|
||||
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;
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user