mirror of
https://gitlab.com/klmp200/fish.git
synced 2024-11-25 02:24:21 +00:00
Lecture depuis la console et C en mode nazi
This commit is contained in:
parent
4c3ad26c2a
commit
aed934b3a7
@ -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})
|
@ -6,38 +6,69 @@
|
||||
#include <string.h>
|
||||
#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){
|
||||
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;i<size;i++){
|
||||
free(array + i);
|
||||
if (array != NULL) {
|
||||
for (i = 0; i < array->size; i++) {
|
||||
free(array->words[i]);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user