forked from notaz/picodrive
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathmake_carthw_c.c
64 lines (50 loc) · 1.01 KB
/
make_carthw_c.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
FILE *fi, *fo;
char buf[256];
if (argc != 3) {
printf("usage:\n%s <carthw.cfg> <carthw.c>\n", argv[0]);
return 1;
}
fi = fopen(argv[1], "r");
fo = fopen(argv[2], "w");
if (fi == NULL || fo == NULL) {
printf("fopen failed\n");
return 1;
}
fprintf(fo, "/* generated by %s, do not modify */\n", argv[0]);
fprintf(fo, "static const char builtin_carthw_cfg[] =\n");
while ((fgets(buf, sizeof(buf), fi)))
{
char bufd[256];
char *d = bufd, *p = buf;
int quote = 0;
while (*p && isspace(*p))
p++;
if (*p == 0 || *p == '#')
continue;
/* section names not needed */
if (*p == '[')
strcpy(p, "[]");
for (; *p != 0; p++) {
if (!quote && *p == '#')
break;
if (!quote && isspace(*p))
continue;
if (*p == '"') {
quote = !quote;
*d++ = '\\';
}
*d++ = *p;
}
*d = 0;
fprintf(fo, " \"%s\\n\"\n", bufd);
}
fprintf(fo, ";\n");
fclose(fi);
fclose(fo);
return 0;
}