From 4c3ad26c2a8596b7e3da79813e4404be86bdaeda Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 11 May 2017 17:16:06 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20de=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- fish_lib/fish_lib.c | 60 +++++++++++++++++++++++++++++++++++++++++++++ fish_lib/fish_lib.h | 23 +++++++++++++++++ main.c | 10 ++++++-- 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 fish_lib/fish_lib.c create mode 100644 fish_lib/fish_lib.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 02331de..cf94989 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(fish) set(CMAKE_C_STANDARD 99) -set(SOURCE_FILES main.c) +set(SOURCE_FILES main.c fish_lib/fish_lib.c fish_lib/fish_lib.h) add_executable(fish ${SOURCE_FILES}) \ No newline at end of file diff --git a/fish_lib/fish_lib.c b/fish_lib/fish_lib.c new file mode 100644 index 0000000..73f89d2 --- /dev/null +++ b/fish_lib/fish_lib.c @@ -0,0 +1,60 @@ +// +// Created by Antoine Bartuccio on 11/05/2017. +// +#include +#include +#include +#include "fish_lib.h" + +void fishLoop(){ + printf("banana"); +} + +int countSeparator(char *string, char separator) { + int nb = 0; + int i = 0; + while (string[i] != '\0'){ + if (string[i] == separator){ + nb++; + } + i++; + } + return nb; +} + +Word * split(char *string, char *separator){ + int array_size = countSeparator(string, separator[0]) + 1; + Word *tokens = malloc(sizeof(Word) * array_size); + char *to_delete = strdup(string); + char *token = NULL; + int i = 0; + + if (tokens == NULL || to_delete == NULL){ + fprintf(stderr, "fish: Error allocating fucking pointer !"); + exit(EXIT_FAILURE); + } + + while((token = strsep(&to_delete, separator)) != NULL){ + printf("%s\n", token); + tokens[i].word = strdup(token); + tokens[i].size = strlen(token); + } + + free(to_delete); + + return tokens; +} + +void freeWordArray(Word *array, int size) { + int i; + for (i=0;i +#include +#include "fish_lib/fish_lib.h" int main() { - printf("Hello, World!\n"); - return 0; + /* todo load config file */ + + split("I love eating bananas", " "); + fishLoop(); + + return EXIT_SUCCESS; } \ No newline at end of file