-
Notifications
You must be signed in to change notification settings - Fork 20
/
cltoheader.c
83 lines (71 loc) · 1.49 KB
/
cltoheader.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
// Copyright Naoki Shibata 2018. Distributed under the MIT License.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
void escesc(char *out, char *in, int outlen) {
int ip = 0, op = 0, state = 0;
while(op < outlen-1) {
switch(state) {
case 0:
if (in[ip] == '\"') {
ip++;
out[op++] = '\\';
if (op >= outlen-1) break;
out[op++] = '\"';
if (op >= outlen-1) break;
state = 1;
} else {
out[op++] = in[ip++];
}
break;
case 1:
if (in[ip] == '\"') {
ip++;
out[op++] = '\\';
if (op >= outlen-1) break;
out[op++] = '\"';
if (op >= outlen-1) break;
state = 0;
} else if (in[ip] == '\\') {
ip++;
out[op++] = '\\';
if (op >= outlen-1) break;
out[op++] = '\\';
if (op >= outlen-1) break;
} else {
out[op++] = in[ip++];
}
break;
}
}
out[op] = '\0';
}
int main(int argc, char **argv) {
if (argc < 3) {
fprintf(stderr, "Usage : %s <input cl file> <varname>\n", argv[0]);
exit(-1);
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "Counldn't open %s\n", argv[1]);
exit(-1);
}
printf("static const char *%s =\n", argv[2]);
char line[1024], line2[1024];
for(;;) {
if (fgets(line, 1000, fp) == NULL) break;;
for(int i=0;i<1024;i++) {
if (line[i] == '\0') break;
if (line[i] == '\n') {
line[i] = '\0';
break;
}
}
escesc(line2, line, 1000);
printf("\"%s\\n\"\n", line2);
}
printf(";\n");
fclose(fp);
exit(0);
}