Tkt j'ai tout testé ;)

This commit is contained in:
Antoine Bartuccio 2017-05-15 15:02:58 +02:00
parent 4228071f6d
commit b8735666e0
5 changed files with 129 additions and 13 deletions

View File

@ -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;

View File

@ -13,7 +13,6 @@
/* WordArray functions */
WordArray * split(char *string, char *separator);
void freeWordArray(WordArray *array);
/* Settings functions */

View File

@ -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;

View File

@ -4,8 +4,109 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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; i<array->size; i++)
addWordList(list, array->words[i]);
freeWordArray(array);
return list;
}

View File

@ -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