summaryrefslogtreecommitdiff
path: root/comp/cw/code/parser/readfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'comp/cw/code/parser/readfile.c')
-rw-r--r--comp/cw/code/parser/readfile.c61
1 files changed, 61 insertions, 0 deletions
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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+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;
+}