forked from notaz/picodrive
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathtextfilter.c
178 lines (154 loc) · 3.42 KB
/
textfilter.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <string.h>
#include <ctype.h>
static int check_defines(const char **defs, int defcount, char *tdef)
{
int i, len;
while (isspace(*tdef)) tdef++;
len = strlen(tdef);
for (i = 0; i < len; i++)
if (tdef[i] == ' ' || tdef[i] == '\r' || tdef[i] == '\n') break;
tdef[i] = 0;
for (i = 0; i < defcount; i++)
{
if (strcmp(defs[i], tdef) == 0)
return 1;
}
return 0;
}
static void do_counters(char *str)
{
static int counter_id = -1, counter;
char buff[1024];
char *s = str;
while ((s = strstr(s, "@@")))
{
if (s[2] < '0' || s[2] > '9') { s++; continue; }
if (counter_id != s[2] - '0') {
counter_id = s[2] - '0';
counter = 1;
}
snprintf(buff, sizeof(buff), "%i%s", counter++, s + 3);
strcpy(s, buff);
}
}
static int my_fputs(char *s, FILE *stream)
{
char *p;
for (p = s + strlen(s) - 1; p >= s; p--)
if (!isspace(*p))
break;
p++;
/* use DOS endings for better viewer compatibility */
memcpy(p, "\r\n", 3);
return fputs(s, stream);
}
int main(int argc, char *argv[])
{
char path[256], path_file[256];
char buff[1024];
FILE *fi, *fo;
int skip_mode = 0, ifdef_level = 0, skip_level = 0, line = 0;
char *p;
if (argc < 3)
{
printf("usage:\n%s <file_in> <file_out> [defines...]\n", argv[0]);
return 1;
}
fi = fopen(argv[1], "r");
if (fi == NULL)
{
printf("failed to open: %s\n", argv[1]);
return 2;
}
fo = fopen(argv[2], "wb");
if (fo == NULL)
{
printf("failed to open: %s\n", argv[2]);
return 3;
}
snprintf(path, sizeof(path), "%s", argv[1]);
for (p = path + strlen(path) - 1; p > path; p--) {
if (*p == '/' || *p == '\\') {
p[1] = 0;
break;
}
}
for (++line; !feof(fi); line++)
{
char *fgs;
fgs = fgets(buff, sizeof(buff), fi);
if (fgs == NULL) break;
if (buff[0] == '#')
{
/* control char */
if (strncmp(buff, "#ifdef ", 7) == 0)
{
ifdef_level++;
if (!skip_mode && !check_defines((void *) &argv[3], argc-3, buff + 7))
skip_mode = 1, skip_level = ifdef_level;
}
else if (strncmp(buff, "#ifndef ", 8) == 0)
{
ifdef_level++;
if (!skip_mode && check_defines((void *) &argv[3], argc-3, buff + 8))
skip_mode = 1, skip_level = ifdef_level;
}
else if (strncmp(buff, "#else", 5) == 0)
{
if (!skip_mode || skip_level == ifdef_level)
skip_mode ^= 1, skip_level = ifdef_level;
}
else if (strncmp(buff, "#endif", 6) == 0)
{
if (skip_level == ifdef_level)
skip_mode = 0;
ifdef_level--;
if (ifdef_level == 0) skip_mode = 0;
if (ifdef_level < 0)
{
printf("%i: warning: #endif without #ifdef, ignoring\n", line);
ifdef_level = 0;
}
}
else if (strncmp(buff, "#include ", 9) == 0)
{
char *pe, *p = buff + 9;
FILE *ftmp;
if (skip_mode)
continue;
while (*p && (*p == ' ' || *p == '\"'))
p++;
for (pe = p + strlen(p) - 1; pe > p; pe--) {
if (isspace(*pe) || *pe == '\"') *pe = 0;
else break;
}
snprintf(path_file, sizeof(path_file), "%s%s", path, p);
ftmp = fopen(path_file, "r");
if (ftmp == NULL) {
printf("%i: error: failed to include \"%s\"\n", line, p);
return 1;
}
while (!feof(ftmp))
{
fgs = fgets(buff, sizeof(buff), ftmp);
if (fgs == NULL)
break;
my_fputs(buff, fo);
}
fclose(ftmp);
continue;
}
/* skip line */
continue;
}
if (!skip_mode)
{
do_counters(buff);
my_fputs(buff, fo);
}
}
fclose(fi);
fclose(fo);
return 0;
}