-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpreprocess.c
141 lines (126 loc) · 3.46 KB
/
cpreprocess.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "cpreprocess.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
char *strReplace(char *orig, char *rep, char *with) {
char *result;
char *ins;
char *tmp;
int lenRep;
int lenWith;
int lenFront;
int count;
if (!orig || !rep)
return NULL;
lenRep = strlen(rep);
if (lenRep == 0)
return NULL;
if (!with)
with = "";
lenWith = strlen(with);
ins = orig;
for (count = 0; tmp = strstr(ins, rep); ++count) {
ins = tmp + lenRep;
}
tmp = result = malloc(strlen(orig) + (lenWith - lenRep) * count + 1);
if (!result)
return NULL;
while (count--) {
ins = strstr(orig, rep);
lenFront = ins - orig;
tmp = strncpy(tmp, orig, lenFront) + lenFront;
tmp = strcpy(tmp, with) + lenWith;
orig += lenFront + lenRep;
}
strcpy(tmp, orig);
return result;
}
int countChars(char *s, char c) {
return *s == '\0' ? 0 : countChars(s + 1, c) + (*s == c);
}
char *preprocess(char *input) {
input = strReplace(input, "?""?=", "#");
input = strReplace(input, "?""?/", "\\");
input = strReplace(input, "?""?'", "^");
input = strReplace(input, "?""?(", "[");
input = strReplace(input, "?""?)", "]");
input = strReplace(input, "?""?!", "|");
input = strReplace(input, "?""?<", "{");
input = strReplace(input, "?""?>", "}");
input = strReplace(input, "?""?-", "~");
input = strReplace(input, "\\\n", "");
int numOfLines = countChars(input, '\n') + 2;
char **lines = calloc(numOfLines, sizeof(char *));
size_t inputLen = strlen(input);
char *line = strtok(input, "\n");
int linesIndex = 0;
while (line != NULL) {
lines[linesIndex++] = line;
line = strtok(NULL, "\n");
}
linesIndex = 0;
while (true) {
if (!lines[linesIndex])
break;
char *line = lines[linesIndex++];
size_t lineLen = strlen(line);
for (int i = 0; i < lineLen; i++) {
if (line[i] == '/' && line[i + 1] && line[i + 1] == '/') {
line[i] = ' ';
for (int j = i + 1; j < lineLen; j++)
line[j] = '\0';
}
}
}
linesIndex = 0;
while (true) {
if (!lines[linesIndex])
break;
char *line = lines[linesIndex++];
size_t lineLen = strlen(line);
for (int i = 0; i < lineLen; i++) {
if (line[i] == '/' && line[i + 1] && line[i + 1] == '*') {
line[i] = ' ';
for (int j = i + 1; j < lineLen; j++) {
if (line[j] == '*' && line[j + 1] && line[j + 1] == '/') {
if (line[j + 2])
strncpy(line, line + j + 2, lineLen - j - 2);
break;
} else {
line[j] = '\0';
if (j == lineLen - 1) {
line = lines[linesIndex++];
lineLen = strlen(line);
j = -1;
}
}
}
}
}
}
for (int i = 0; i < numOfLines; i++) {
if (lines[i]) {
for (int j = 0; j < strlen(lines[i]); j++) {
if (!isspace(lines[i][j]) && lines[i][j] != '#') break;
if (lines[i][j] == '#' && strncmp(lines[i] + j, "#include", strlen("#include")) == 0) {
// TODO
};
}
}
}
char *result = calloc(inputLen + 1, sizeof(char));
linesIndex = 0;
while (true) {
if (!lines[linesIndex])
break;
if (!linesIndex)
snprintf(result, inputLen + 1, "%s\n", lines[linesIndex++]);
else if (lines[linesIndex][0] != '\0')
snprintf(result + strlen(result), inputLen, "%s\n", lines[linesIndex++]);
else
linesIndex++;
}
return result;
}