From b08f1afe7f9445cf1ea0d758971f8efa41a3089d Mon Sep 17 00:00:00 2001 From: FReenen Date: Mon, 8 Apr 2024 14:36:53 +0200 Subject: [PATCH] add pop function in FIFOBuff --- FIFOBuff/FIFOBuffChar.c | 28 ++++++++++++++++++++++++++++ FIFOBuff/FIFOBuffChar.h | 5 ++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/FIFOBuff/FIFOBuffChar.c b/FIFOBuff/FIFOBuffChar.c index 5756401..9b5fa98 100644 --- a/FIFOBuff/FIFOBuffChar.c +++ b/FIFOBuff/FIFOBuffChar.c @@ -122,6 +122,34 @@ bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p) // Change the function para return ok; } +bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo) +{ + bool ok = true; + + if (fifo->FirstEl_p == NULL) + { // buffer is empty + ok = false; + } + else + { // there is data in the buffer + // find the second last element + FIFOBuffChar_element_t* secondLastEl; = fifo->FirstEl_p; + while (secondLastEl->nextElement != fifo->LastEl_p) + { + secondLastEl = secondLastEl->nextElement; + } + + // remove last element + free(fifo->LastEl_p); + + // update chain + secondLastEl->nextElement == NULL; + fifo->LastEl_p = secondLastEl; + } + + return ok; +} + bool FIFOBuffChar_is_full(FIFOBuffChar_t *fifo) { return fifo->full; diff --git a/FIFOBuff/FIFOBuffChar.h b/FIFOBuff/FIFOBuffChar.h index b6eb1b3..9671b9d 100644 --- a/FIFOBuff/FIFOBuffChar.h +++ b/FIFOBuff/FIFOBuffChar.h @@ -32,7 +32,10 @@ extern bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i); // 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); //moet dit "char" zijn of "uint8_t" +extern bool FIFOBuffChar_get(FIFOBuffChar_t *fifo, char *p); + +// pop last added value from the buffer +extern bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo); // 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);