fix pop from FIFOBuff and fix warnings for CLI

This commit is contained in:
Laila van Reenen 2024-04-08 15:08:10 +02:00
parent e6d9bb1da6
commit 24738ebeea
2 changed files with 43 additions and 27 deletions

View File

@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include "../FIFOBuff/FIFOBuffChar.h" #include "../FIFOBuff/FIFOBuffChar.h"
@ -62,7 +63,6 @@ int tryExecute(FIFOBuffChar_t* fifo)
if (CLI_lineOut != NULL) if (CLI_lineOut != NULL)
{ {
(*CLI_lineOut)("\n");
(*CLI_lineOut)(">"); (*CLI_lineOut)(">");
(*CLI_lineOut)(" "); (*CLI_lineOut)(" ");
} }
@ -73,7 +73,7 @@ int tryExecute(FIFOBuffChar_t* fifo)
if (CLI_lineOut != NULL) if (CLI_lineOut != NULL)
{ {
char err[100]; char err[100];
sprintf(&err, "command not found: %s\n> ", line); sprintf(&err[0], "command not found: %s\n> ", line);
for (int i=0; err[i] != 0; i++) for (int i=0; err[i] != 0; i++)
{ {
char c[2] = {err[i], 0}; char c[2] = {err[i], 0};
@ -125,13 +125,16 @@ bool CLI_charIn(char c)
break; break;
case 127: // backspace case 127: // backspace
(*CLI_lineOut)("\x1b"); if (FIFOBuffChar_pop(FIFO))
(*CLI_lineOut)("["); {
(*CLI_lineOut)("D"); (*CLI_lineOut)("\x1b");
(*CLI_lineOut)("[");
(*CLI_lineOut)("D");
}
break; break;
case 27: // escape (start for arrow keys) case 27: // escape (start for arrow keys)
sprintf(&str, "\ninvlid char: ESC - (%i)\n", c); sprintf(&str[0], "\ninvlid char: ESC - (%i)\n", c);
for (int i=0; str[i] != 0; i++) for (int i=0; str[i] != 0; i++)
{ {
char ch[2] = {str[i], 0}; char ch[2] = {str[i], 0};
@ -140,7 +143,7 @@ bool CLI_charIn(char c)
break; break;
default: default:
sprintf(&str, "\ninvlid char: '%c' - (%i)\n", c, c); sprintf(&str[0], "\ninvlid char: '%c' - (%i)\n", c, c);
for (int i=0; str[i] != 0; i++) for (int i=0; str[i] != 0; i++)
{ {
char ch[2] = {str[i], 0}; char ch[2] = {str[i], 0};

View File

@ -1,9 +1,8 @@
#include "FIFOBuffChar.h"
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include "FIFOBuffChar.h"
bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration bool FIFOBuffChar_put(FIFOBuffChar_t *fifo, char i) // Change the function parameters to match the declaration
{ {
@ -126,25 +125,39 @@ bool FIFOBuffChar_pop(FIFOBuffChar_t *fifo)
{ {
bool ok = true; bool ok = true;
if (fifo->FirstEl_p == NULL) switch (fifo->size){
{ // buffer is empty case 0: // buffer is empty
ok = false; ok = false;
} break;
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 case 1: // singel item in buffer
free(fifo->LastEl_p); // make buffer empty
free(fifo->FirstEl_p);
fifo->FirstEl_p = NULL;
fifo->LastEl_p = NULL;
fifo->size = 0;
ok = true;
break;
// update chain default: // buffer is at least 2 element big
secondLastEl->nextElement == NULL; // find the second last element
fifo->LastEl_p = secondLastEl; FIFOBuffChar_element_t* secondLastEl = fifo->FirstEl_p;
while (
secondLastEl->nextElement != fifo->LastEl_p
&& ((FIFOBuffChar_element_t*)secondLastEl->nextElement)->nextElement != NULL
){
secondLastEl = secondLastEl->nextElement;
}
// remove last element
free(fifo->LastEl_p);
// update chain
secondLastEl->nextElement = NULL;
fifo->LastEl_p = secondLastEl;
fifo->size--;
ok = true;
break;
} }
return ok; return ok;