From 7d3856203d28281e3ffc6b365cc55b1d192a5599 Mon Sep 17 00:00:00 2001 From: standenboy Date: Thu, 25 Apr 2024 08:45:36 +0100 Subject: started cw --- comp/cw/code/parser/readfile.c | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 comp/cw/code/parser/readfile.c (limited to 'comp/cw/code/parser/readfile.c') diff --git a/comp/cw/code/parser/readfile.c b/comp/cw/code/parser/readfile.c new file mode 100644 index 0000000..391d5a5 --- /dev/null +++ b/comp/cw/code/parser/readfile.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +bool instring = false; + +char *readfile(char *filepath){ + FILE *fptr; + fptr = fopen(filepath, "r"); + + if (fptr == NULL) + return NULL; + int size = 10; + char *buf = malloc(size); + char c; + + int i = 0; + + buf = buf+i; + buf[0] = '{'; + buf = buf-i; + + i++; + + while ((c = getc(fptr)) != EOF){ + if (i > size + 1){ + size = size + 10; + buf = realloc(buf, size); + } + if (c == '"'){ + if (instring == false) + instring = true; + else + instring = false; + } + + if (c == '!' && instring == false){ + while ((c = getc(fptr)) != EOF && c != '\n'){} + } + buf = buf+i; + buf[0] = c; + buf = buf-i; + i++; + + } + + buf = buf+i; + buf[0] = '}'; + buf = buf-i; + + i++; + + buf = buf+i; + buf[0] = '\0'; + buf = buf-i; + + fclose(fptr); + + return buf; +} -- cgit v1.2.3