#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, bool freeLines) { // 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; if (freeLines) { free(el->line); } 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 if (history->CurrEl_p->prevEl_p != NULL) { history->CurrEl_p = history->CurrEl_p->prevEl_p; } else { // no previus line retCode = -2; } if (history->CurrEl_p != NULL) { *line = history->CurrEl_p->line; retCode = 0; } else { // no items in history? *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; } int History_getFullHistory(History_t* history, char*** list) { int retCode = 0; *list = malloc(sizeof(char**) * (History_getSize(history) + 1)); History_element_t* el = history->FirstEl_p; int i; for (i = 0; (i < History_getSize(history)-1) && (retCode >= 0); i++) { *((*list) + i) = el->line; if (el->nextEl_p == NULL) { retCode = -1; } else { el = (History_element_t*)el->nextEl_p; } } if (retCode >= 0) { // indicate end of list *((*list) + History_getSize(history)) = NULL; } return retCode; } int History_getCurrPos(History_t* history) { int retCode = 0; if (history->CurrEl_p != NULL) { History_element_t* el = history->FirstEl_p; int i; for (i = 0; (el != history->CurrEl_p) && (retCode >= 0); i++) { if (el->nextEl_p == NULL) { retCode = 2; } else { el = (History_element_t*)el->nextEl_p; } } } else { retCode = History_getSize(history); } return retCode; } unsigned int History_getSize(History_t *history) { return history->size; }