create inital version of history
This commit is contained in:
parent
c49ed49a0b
commit
a1fef05168
123
History/History.c
Normal file
123
History/History.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include "FIFOBuffChar.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
History_t* History_init()
|
||||
{
|
||||
History_t* history = malloc(sizeof(FIFOBuffChar_t));
|
||||
|
||||
if (history != NULL) {
|
||||
history->FirstEl_p = NULL;
|
||||
history->LastEl_p = NULL;
|
||||
history->size = 0;
|
||||
}
|
||||
|
||||
return history;
|
||||
}
|
||||
|
||||
|
||||
int History_deinit(History_t* history)
|
||||
{
|
||||
// Check if the buffer there is something in the buffer
|
||||
if (history->FirstEl_p == NULL)
|
||||
{
|
||||
// Delete all elements in the buffer
|
||||
History_element_t* el = history->FirstEl_p;
|
||||
while (el != NULL)
|
||||
{
|
||||
FIFOBuffChar_element_t* nextEl = el->nextEl;
|
||||
free(el);
|
||||
el = nextEl;
|
||||
}
|
||||
}
|
||||
|
||||
free(history);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int History_put(History_t *history, char* line)
|
||||
{
|
||||
int retCode;
|
||||
if ((history->LastEl_p == NULL) && (history->FirstEl_p == NULL))
|
||||
{
|
||||
// buffer is empty. add first element
|
||||
history->FirstEl_p = malloc(sizeof(History_element_t));
|
||||
if (history->FirstEl_p != NULL)
|
||||
{
|
||||
history->FirstEl_p->line = line;
|
||||
history->FirstEl_p->prev_p = NULL;
|
||||
history->FirstEl_p->next_p = NULL;
|
||||
|
||||
history->LastEl_p = history->FirstEl_p;
|
||||
history->size = 1;
|
||||
retCode = 0;
|
||||
}
|
||||
else
|
||||
{ // failt to allocate memory
|
||||
retCode = -1;
|
||||
}
|
||||
}
|
||||
else if ((history->LastEl_p != NULL) && (history->FirstEl_p != NULL))
|
||||
{
|
||||
// add element to exsiting buffer
|
||||
History_element_t* el = malloc(sizeof(History_element_t));
|
||||
if (el != NULL)
|
||||
{
|
||||
el->character = i; // "value" to "character"
|
||||
el->PrevEl_p = history->LastEl_p;
|
||||
el->nextEl_p = NULL;
|
||||
history->LastEl_p->nextEl_p = el;
|
||||
history->LastEl_p = el;
|
||||
history->size++;
|
||||
retCode = 0;
|
||||
}
|
||||
else
|
||||
{ // failt to allocate memory
|
||||
retCode = -2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
retCode = -3;
|
||||
}
|
||||
|
||||
if (retCode >= 0)
|
||||
{
|
||||
// reset current pointer
|
||||
history->currEl_p = NULL;
|
||||
}
|
||||
|
||||
return retCode;
|
||||
}
|
||||
|
||||
int History_getPrev(History_t* history, char** line)
|
||||
{
|
||||
int retCode;
|
||||
if (history->currEl_p != NULL)
|
||||
{
|
||||
history->currEl_p = history->LastEl_p;
|
||||
}
|
||||
else
|
||||
{
|
||||
history->currEl_p = history->currEl_p->PrevEl_p;
|
||||
}
|
||||
|
||||
if (history->currEl_p != NULL)
|
||||
{
|
||||
*line = history->currEl_p->line;
|
||||
retCode = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*line = NULL;
|
||||
retCode = -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int History_getSize(History_t *history)
|
||||
{
|
||||
return history->size;
|
||||
}
|
||||
36
History/history.h
Normal file
36
History/history.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef HISTORY_H
|
||||
#define HISTORY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
// one element in the buffer
|
||||
typedef struct History_element_s {
|
||||
void* prevEl_p;
|
||||
char* line;
|
||||
void* nextEl_p;
|
||||
} History_element_t;
|
||||
|
||||
// defines all vars for the buffer to operate
|
||||
typedef struct History_s {
|
||||
History_element_t* FirstEl_p;
|
||||
History_element_t* LastEl_p;
|
||||
History_element_t* CurrEl_p;
|
||||
unsigned char size;
|
||||
} History_t;
|
||||
|
||||
// create a fifo buffer and initilizes it with zeros
|
||||
extern History_t* History_init();
|
||||
|
||||
// destroy a fifo buffer (free up its space)
|
||||
extern int History_deinit(History_t* history);
|
||||
|
||||
// put value i in buffer if there is still memory avaliable
|
||||
extern int History_put(History_t *history, char* line);
|
||||
|
||||
// get value from buffer and writes it to *line
|
||||
extern int History_getPrev(History_t* history, char** line);
|
||||
extern int History_getNext(History_t* history, char** line);
|
||||
|
||||
extern unsigned int History_getSize(History_t *history);
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user