From 1a2d0ffdbfb69a2858fb45facfb18468106edd9a Mon Sep 17 00:00:00 2001 From: dx Date: Wed, 20 May 2020 16:24:57 +0100 Subject: +function to get the lines of some given file Uses the dynamic array to alleviate the burdens of computing specific memory sizes. Terminates lines for parsing. --- Converter/includes/compiler.h | 3 +++ Converter/src/compiler.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Converter/includes/compiler.h b/Converter/includes/compiler.h index 6e1eca8..8d84b71 100644 --- a/Converter/includes/compiler.h +++ b/Converter/includes/compiler.h @@ -15,4 +15,7 @@ int find_string(string src, string query, size_t sz_src, size_t sz_query); /* Check if a given character is a text token*/ bool is_token(char c); +/* Given a file, get the lines */ +char **get_lines(char *filename); + #endif // __COMPILER_H_ diff --git a/Converter/src/compiler.c b/Converter/src/compiler.c index e2dd74b..ddbe6ff 100644 --- a/Converter/src/compiler.c +++ b/Converter/src/compiler.c @@ -1,4 +1,5 @@ #include "../includes/compiler.h" +#include "../includes/array.h" #include #include @@ -189,3 +190,32 @@ string compile_line(string line, size_t size_of_line, string line_or_source) return compiled_line; } + +char **get_lines(char *filename) +{ + FILE *fp; + char **lines, *line; + array_t *arr; + int i; + + array_init(arr); + + fp = fopen(filename, "r"); + + while(!feof(fp)) + { + // Get line + line = malloc(sizeof(*line) * 1024); + fgets(line, 1024, fp); + + // Terminate line + for (i = 0; line[i] != '\n'; ++i); + line[i] = '\0'; + array_append(arr, line); + } + + lines = (char**)arr->data; + free(arr); + + return lines; +} -- cgit v1.2.3-13-gbd6f