1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-22 08:43:20 +00:00

Lecture depuis la console et C en mode nazi

This commit is contained in:
Antoine Bartuccio 2017-05-13 22:40:41 +02:00
parent 4c3ad26c2a
commit aed934b3a7
4 changed files with 122 additions and 29 deletions

View File

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.7)
project(fish) project(fish)
set(CMAKE_C_STANDARD 99) 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) set(SOURCE_FILES main.c fish_lib/fish_lib.c fish_lib/fish_lib.h)
add_executable(fish ${SOURCE_FILES}) add_executable(fish ${SOURCE_FILES})

View File

@ -6,38 +6,69 @@
#include <string.h> #include <string.h>
#include "fish_lib.h" #include "fish_lib.h"
void fishLoop(){ #define FISH_BUFFER_SIZE 1024
printf("banana"); #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 nb = 0;
int i = 0; int i = 0;
int k = 0;
while (string[i] != '\0'){ while (string[i] != '\0'){
if (string[i] == separator){ while (separators[k] != '\0'){
nb++; if (string[i] == separators[k]){
nb++;
}
k++;
} }
i++; i++;
k = 0;
} }
return nb; return nb;
} }
Word * split(char *string, char *separator){ WordArray * split(char *string, char *separator){
int array_size = countSeparator(string, separator[0]) + 1; int array_size = countSeparators(string, separator) + 1;
Word *tokens = malloc(sizeof(Word) * array_size); WordArray *tokens = (WordArray*) malloc(sizeof(WordArray));
char *to_delete = strdup(string); char *to_delete = strdup(string);
char *token = NULL; char *token = NULL;
int i = 0; 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 !"); fprintf(stderr, "fish: Error allocating fucking pointer !");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while((token = strsep(&to_delete, separator)) != NULL){ while((token = strsep(&to_delete, separator)) != NULL){
printf("%s\n", token); tokens->words[i] = strdup(token);
tokens[i].word = strdup(token); i++;
tokens[i].size = strlen(token);
} }
free(to_delete); free(to_delete);
@ -45,16 +76,66 @@ Word * split(char *string, char *separator){
return tokens; return tokens;
} }
void freeWordArray(Word *array, int size) { void freeWordArray(WordArray *array) {
int i; int i;
for (i=0;i<size;i++){ if (array != NULL) {
free(array + i); for (i = 0; i < array->size; i++) {
free(array->words[i]);
}
free(array);
} }
free(array);
} }
char *fishReadLine() { 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; 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;
}

View File

@ -6,18 +6,32 @@
#define FISH_FISH_LIB_H #define FISH_FISH_LIB_H
typedef struct { typedef struct {
char * word; char ** words;
size_t size; int size;
} Word; } 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(); char * fishReadLine();
Word * split(char *string, char *separator); int countSeparators(char *string, char *separators);
int countSeparator(char *string, char separator); char * fishExpand(char* line);
void freeWordArray(Word *array, int size);
#endif //FISH_FISH_LIB_H #endif //FISH_FISH_LIB_H

5
main.c
View File

@ -1,12 +1,9 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "fish_lib/fish_lib.h" #include "fish_lib/fish_lib.h"
int main() { int main() {
/* todo load config file */ /* todo load config file */
fishLoop(getSettings());
split("I love eating bananas", " ");
fishLoop();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }