1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-24 18:14:18 +00:00

Début de parsing

This commit is contained in:
Antoine Bartuccio 2017-05-11 17:16:06 +02:00
parent 1a305be28b
commit 4c3ad26c2a
4 changed files with 92 additions and 3 deletions

View File

@ -3,5 +3,5 @@ project(fish)
set(CMAKE_C_STANDARD 99) 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}) add_executable(fish ${SOURCE_FILES})

60
fish_lib/fish_lib.c Normal file
View File

@ -0,0 +1,60 @@
//
// Created by Antoine Bartuccio on 11/05/2017.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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<size;i++){
free(array + i);
}
free(array);
}
char *fishReadLine() {
return NULL;
}

23
fish_lib/fish_lib.h Normal file
View File

@ -0,0 +1,23 @@
//
// Created by Antoine Bartuccio on 11/05/2017.
//
#ifndef FISH_FISH_LIB_H
#define FISH_FISH_LIB_H
typedef struct {
char * word;
size_t size;
} Word;
void fishLoop();
char * fishReadLine();
Word * split(char *string, char *separator);
int countSeparator(char *string, char separator);
void freeWordArray(Word *array, int size);
#endif //FISH_FISH_LIB_H

10
main.c
View File

@ -1,6 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "fish_lib/fish_lib.h"
int main() { int main() {
printf("Hello, World!\n"); /* todo load config file */
return 0;
split("I love eating bananas", " ");
fishLoop();
return EXIT_SUCCESS;
} }