From b8735666e05c612eb79f7d65420d3ba5d2b7afe7 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Mon, 15 May 2017 15:02:58 +0200 Subject: [PATCH] =?UTF-8?q?Tkt=20j'ai=20tout=20test=C3=A9=20;)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_core.c | 12 ----- fish_shell/fish_core.h | 1 - fish_shell/fish_types.h | 12 +++++ fish_shell/fish_utils.c | 101 ++++++++++++++++++++++++++++++++++++++++ fish_shell/fish_utils.h | 16 +++++++ 5 files changed, 129 insertions(+), 13 deletions(-) diff --git a/fish_shell/fish_core.c b/fish_shell/fish_core.c index 268c63e..73e384c 100644 --- a/fish_shell/fish_core.c +++ b/fish_shell/fish_core.c @@ -75,18 +75,6 @@ WordArray * split(char *string, char *separator){ return tokens; } -void freeWordArray(WordArray *array) { - int i; - if (array != NULL) { - for (i = 0; i < array->size; i++) { - free(array->words[i]); - } - free(array->words); - free(array); - } - -} - char *fishReadLine() { size_t bufferSize = FISH_BUFFER_SIZE; int position = 0; diff --git a/fish_shell/fish_core.h b/fish_shell/fish_core.h index d391e9f..6e31133 100644 --- a/fish_shell/fish_core.h +++ b/fish_shell/fish_core.h @@ -13,7 +13,6 @@ /* WordArray functions */ WordArray * split(char *string, char *separator); -void freeWordArray(WordArray *array); /* Settings functions */ diff --git a/fish_shell/fish_types.h b/fish_shell/fish_types.h index a8d5b54..717d13d 100644 --- a/fish_shell/fish_types.h +++ b/fish_shell/fish_types.h @@ -12,6 +12,18 @@ typedef struct { int size; } WordArray; +typedef struct elem { + char * word; + struct elem * previous; + struct elem * next; +} WordListElement; + +typedef struct { + int size; + WordListElement * first; + WordListElement * last; +} WordList; + typedef struct { char *PS1; } Settings; diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index ef59f02..9ee8560 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -4,8 +4,109 @@ #include #include +#include +#include "fish_types.h" void crash(){ fprintf(stderr, "fish: Error allocating fucking pointer !"); exit(EXIT_FAILURE); } + +void freeWordArray(WordArray *array) { + int i; + if (array != NULL) { + for (i = 0; i < array->size; i++) { + free(array->words[i]); + } + free(array->words); + free(array); + } + +} + + +WordList *createWordList() { + WordList *list = (WordList*) malloc(sizeof(WordList)); + if (list == NULL) crash(); + else { + list->size = 0; + list->first = NULL; + list->last = NULL; + } + + return list; +} + +void addWordList(WordList *list, char *word) { + WordListElement *new = (WordListElement*) malloc(sizeof(WordListElement)); + if (new == NULL) crash(); + else { + new->next = NULL; + new->previous = list->last; + if (list->size == 0){ + list->first = new; + list->last = new; + } else { + list->last->next = new; + list->last = new; + } + new->word = strdup(word); + list->size++; + } +} + +void removeWordList(WordList *list) { + WordListElement *current = list->last; + if (current != NULL){ + list->last = current->previous; + if (current->previous != NULL) + current->previous->next = NULL; + if (current->word != NULL) + free(current->word); + free(current); + list->size--; + } +} + +void freeWordList(WordList *list) { + while (list->size != 0) + removeWordList(list); + free(list); +} + +WordArray *wordListToWordArray(WordList *list) { + WordArray *array = (WordArray*) malloc(sizeof(WordArray)); + WordListElement *current = list->first; + int i = 0; + + if (array == NULL) crash(); + else { + array->size = list->size; + array->words = (char **) malloc(sizeof(char *) * (list->size + 1)); + if (array->words == NULL) crash(); + + while (current != NULL){ + array->words[i] = current->word; + current->word = NULL; + current = current->next; + i++; + } + array->words[i] = NULL; + } + + freeWordList(list); + + return array; +} + +WordList *wordArrayToWordList(WordArray *array) { + WordList *list = createWordList(); + int i = 0; + + for (i=0; isize; i++) + addWordList(list, array->words[i]); + + freeWordArray(array); + + return list; +} diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index 27de991..84ddbea 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -5,6 +5,22 @@ #ifndef FISH_FISH_UTILS_H #define FISH_FISH_UTILS_H +#include "fish_types.h" + void crash(); +void freeWordArray(WordArray *array); + +WordList * createWordList(); + +void addWordList(WordList *list, char *word); + +void removeWordList(WordList *list); + +void freeWordList(WordList *list); + +WordArray * wordListToWordArray(WordList *list); + +WordList * wordArrayToWordList(WordArray * array); + #endif //FISH_FISH_UTILS_H