Merge branch 'Dennis_buffer'
This commit is contained in:
commit
2e66ebe1f7
@ -1,80 +1,70 @@
|
||||
#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))
|
||||
if ((fifo->LastEl_p == NULL) && (fifo->FirstEl_p == NULL))
|
||||
{
|
||||
// buffer is empty. add first element
|
||||
FIFO_FirstElement_p = malloc(sizeof(FIFO_element_t));
|
||||
if (FIFO_FirstElement_p != NULL)
|
||||
fifo->FirstEl_p = malloc(sizeof(FIFOBuffChar_element_t));
|
||||
if (fifo->FirstEl_p != NULL)
|
||||
{
|
||||
FIFO_FirstElement_p->character = i; // "value" to "character"
|
||||
FIFO_FirstElement_p->nextElement = NULL;
|
||||
fifo->FirstEl_p->character = i; // "value" to "character"
|
||||
fifo->FirstEl_p->nextElement = NULL;
|
||||
|
||||
FIFO_LastElement_p = FIFO_FirstElement_p;
|
||||
FIFO_count = 1;
|
||||
fifo->LastEl_p = fifo->FirstEl_p;
|
||||
fifo->size = 1;
|
||||
ok = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
FIFO_Full = true;
|
||||
fifo->full = true;
|
||||
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
|
||||
FIFO_element_t* el = malloc(sizeof(FIFO_element_t));
|
||||
FIFOBuffChar_element_t* el = malloc(sizeof(FIFOBuffChar_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++;
|
||||
fifo->LastEl_p->nextElement = el;
|
||||
fifo->LastEl_p = el;
|
||||
fifo->size++;
|
||||
ok = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
FIFO_Full = true;
|
||||
fifo->full = true;
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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");
|
||||
}
|
||||
//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);
|
||||
FIFO_LastElement_p = NULL;
|
||||
free(fifo->LastEl_p);
|
||||
fifo->LastEl_p = NULL;
|
||||
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;
|
||||
}
|
||||
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 ok = false;
|
||||
if (FIFO_FirstElement_p != NULL)
|
||||
if (fifo->FirstEl_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;
|
||||
*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_LastElement_p == FIFO_FirstElement_p)
|
||||
if (fifo->LastEl_p == fifo->FirstEl_p)
|
||||
{
|
||||
// clear last element becouse it's the same
|
||||
FIFO_LastElement_p = NULL;
|
||||
FIFO_FirstElement_p = NULL;
|
||||
FIFO_count = 0;
|
||||
fifo->LastEl_p = NULL;
|
||||
fifo->FirstEl_p = NULL;
|
||||
fifo->size = 0;
|
||||
ok = true;
|
||||
}
|
||||
else if (FIFO_LastElement_p != NULL)
|
||||
else if (fifo->LastEl_p != NULL)
|
||||
{
|
||||
// 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");
|
||||
ok = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// somehow the last element is NULL
|
||||
FIFO_count = 0;
|
||||
FIFO_FirstElement_p = NULL;
|
||||
fifo->size = 0;
|
||||
fifo->FirstEl_p = NULL;
|
||||
printf("buffer_dyn - buffer_get(): ERROR: last element pointer is NULL\n");
|
||||
ok = false;
|
||||
}
|
||||
@ -121,7 +111,7 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para
|
||||
else
|
||||
{
|
||||
// set first element pointer to next element
|
||||
FIFO_FirstElement_p = next;
|
||||
fifo->FirstEl_p = next;
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
@ -132,19 +122,19 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para
|
||||
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",
|
||||
// (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));
|
||||
// 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));
|
||||
}
|
||||
|
||||
|
||||
@ -173,30 +163,28 @@ FIFOBuffChar_t* FIFOBuffChar_create(void)
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool FIFOBuffChar_delete(FIFOBuffChar_t *fifo)
|
||||
{
|
||||
|
||||
// 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)
|
||||
{
|
||||
FIFOBuffChar_element_t* nextElement = currentElement->nextElement;
|
||||
free(currentElement);
|
||||
currentElement = nextElement;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
free(fifo);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
unsigned int number_of_elements_in_buffer()
|
||||
unsigned int FIFOBuffChar_getSize(FIFOBuffChar_t *fifo)
|
||||
{
|
||||
return FIFO_count;
|
||||
return fifo->size;
|
||||
}
|
||||
|
||||
@ -6,10 +6,10 @@
|
||||
// interface for a buffer with chars's
|
||||
|
||||
// one element in the buffer
|
||||
struct FIFOBuffChar_element_s {
|
||||
char value;
|
||||
void* nextEl;
|
||||
};
|
||||
typedef struct FIFOBuffChar_element_s {
|
||||
char character; // "char character" replaced "int value" because the elements are going to be characters
|
||||
void* nextElement;
|
||||
} FIFOBuffChar_element_t;
|
||||
|
||||
// defines all vars for the buffer to operate
|
||||
typedef struct FIFOBuffChar_s {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user