From aed934b3a7c02e38af74c74b4afdc1dd3766933a Mon Sep 17 00:00:00 2001 From: klmp200 Date: Sat, 13 May 2017 22:40:41 +0200 Subject: [PATCH] Lecture depuis la console et C en mode nazi --- CMakeLists.txt | 1 + fish_lib/fish_lib.c | 113 +++++++++++++++++++++++++++++++++++++------- fish_lib/fish_lib.h | 30 ++++++++---- main.c | 7 +-- 4 files changed, 122 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cf94989..108974e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.7) project(fish) set(CMAKE_C_STANDARD 99) +set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra") 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 index 73f89d2..a81d076 100644 --- a/fish_lib/fish_lib.c +++ b/fish_lib/fish_lib.c @@ -6,38 +6,69 @@ #include #include "fish_lib.h" -void fishLoop(){ - printf("banana"); +#define FISH_BUFFER_SIZE 1024 +#define FISH_TOKENS " \t\r\n\a" + +void fishLoop(Settings * settings){ + char * line = NULL; + WordArray * splited = NULL; + int exited = 0; + int i; + + while (!exited) { + printf("%s", settings->PS1); + line = fishReadLine(); + line = fishExpand(line); + + splited = split(line, FISH_TOKENS); + for (i = 0; i < splited->size; i++) { + printf("%s\n", splited->words[i]); + if(!strcmp(splited->words[i], "exit")) + exited = 1; + } + + freeWordArray(splited); + free(line); + } } -int countSeparator(char *string, char separator) { +int countSeparators(char *string, char *separators) { int nb = 0; int i = 0; + int k = 0; while (string[i] != '\0'){ - if (string[i] == separator){ - nb++; + while (separators[k] != '\0'){ + if (string[i] == separators[k]){ + nb++; + } + k++; } i++; + k = 0; } return nb; } -Word * split(char *string, char *separator){ - int array_size = countSeparator(string, separator[0]) + 1; - Word *tokens = malloc(sizeof(Word) * array_size); +WordArray * split(char *string, char *separator){ + int array_size = countSeparators(string, separator) + 1; + WordArray *tokens = (WordArray*) malloc(sizeof(WordArray)); char *to_delete = strdup(string); char *token = NULL; int i = 0; - if (tokens == NULL || to_delete == NULL){ + if (tokens != NULL){ + tokens->words = (char **) malloc(sizeof(char*) * array_size); + tokens->size = array_size; + } + + if (tokens == NULL || to_delete == NULL || tokens->words == 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); + tokens->words[i] = strdup(token); + i++; } free(to_delete); @@ -45,16 +76,66 @@ Word * split(char *string, char *separator){ return tokens; } -void freeWordArray(Word *array, int size) { +void freeWordArray(WordArray *array) { int i; - for (i=0;isize; i++) { + free(array->words[i]); + } + free(array); } - free(array); } char *fishReadLine() { + size_t bufferSize = FISH_BUFFER_SIZE; + int position = 0; + char *line = malloc(sizeof(char*) * bufferSize); + int c; + + if (line == NULL){ + fprintf(stderr, "fish: Error allocating fucking buffer shit !"); + exit(EXIT_FAILURE); + } + + while (1){ + c = getchar(); + + if (c == EOF || c == '\n'){ + line[position] = '\0'; + return line; + } else { + line[position] = (char) c; + } + + position++; + + if ((size_t) position > bufferSize){ + bufferSize+=bufferSize; + line = realloc(line, bufferSize); + if (line == NULL){ + fprintf(stderr, "fish: Error allocating fucking buffer shit !"); + exit(EXIT_FAILURE); + } + } + } + + return NULL; } +char *fishExpand(char *line) { + return line; +} + +Settings *getSettings() { + Settings *s = (Settings*) malloc(sizeof(Settings)); + if (s == NULL){ + fprintf(stderr, "fish: Error allocating fucking settings"); + exit(EXIT_FAILURE); + } + s->PS1 = strdup("~>"); + + return s; +} + diff --git a/fish_lib/fish_lib.h b/fish_lib/fish_lib.h index 77e922a..f940a7d 100644 --- a/fish_lib/fish_lib.h +++ b/fish_lib/fish_lib.h @@ -6,18 +6,32 @@ #define FISH_FISH_LIB_H typedef struct { - char * word; - size_t size; -} Word; + char ** words; + int size; +} WordArray; -void fishLoop(); +typedef struct { + char *PS1; +} Settings; + +/* WordArray functions */ + +WordArray * split(char *string, char *separator); +void freeWordArray(WordArray *array); + +/* Settings functions */ + +Settings * getSettings(); + + +/* General purpose functions */ + +void fishLoop(Settings * settings); char * fishReadLine(); -Word * split(char *string, char *separator); +int countSeparators(char *string, char *separators); -int countSeparator(char *string, char separator); - -void freeWordArray(Word *array, int size); +char * fishExpand(char* line); #endif //FISH_FISH_LIB_H diff --git a/main.c b/main.c index 6c36156..4cfd21c 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,9 @@ -#include #include #include "fish_lib/fish_lib.h" int main() { /* todo load config file */ - - split("I love eating bananas", " "); - fishLoop(); + fishLoop(getSettings()); return EXIT_SUCCESS; -} \ No newline at end of file +}