#include "history.h" #include #include #include History_t* History_init() { History_t* history = malloc(sizeof(History_t)); if (history != NULL) { history->FirstEl_p = NULL; history->LastEl_p = NULL; history->CurrEl_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) { History_element_t* nextEl = el->nextEl_p; 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->prevEl_p = NULL; history->FirstEl_p->nextEl_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->line = line; 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; } int History_getNext(History_t* history, char** line) { int retCode; if (history->CurrEl_p != NULL) { history->CurrEl_p = history->CurrEl_p->nextEl_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; }