-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.c
42 lines (35 loc) · 926 Bytes
/
preprocessor.c
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
#include "Preprocessor.h"
void preprocess(FILE* source) {
FILE* data = fopen(DATA_FILE, "w+");
FILE* text = fopen(TEXT_FILE, "w+");
FILE* output = NULL;
if(source && data && text) {
char line[257] = {'\0'};
while( fgets(line,sizeof line,source) != NULL) {
char* strippedLine = stripLine(line);
if (strncmp(".data", strippedLine, 5) == 0) output = data;
else if (strncmp(".text", strippedLine, 5) == 0) output = text;
else if (output) fprintf(output, "%s", stripLine(line));
memset(line, '\0', sizeof(line));
}
}
fclose(data);
fclose(text);
}
char* stripLine(char* line) {
rmComment(line);
return rmLeadingSpace(line);
}
void rmComment(char* line ) {
char* ret = strchr(line,'#');
if(ret != NULL) {
*ret = '\n';
*(ret+1) = '\0';
}
}
char* rmLeadingSpace(char* line) {
while(isspace(*line) || *line == '\n') {
line++;
}
return line;
}