summaryrefslogtreecommitdiff
path: root/comp/cw/code/parser/readfile.c
blob: 391d5a5a126c1ff3c1c8bec721ab9382ebe1f6df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}