forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcify.c
41 lines (35 loc) · 1.18 KB
/
cify.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
// Reads a file and outputs a C file with a symbol containing all the data in the file.
#include <stdio.h>
#include <ctype.h>
int main(int argc, const char *argv[]) {
if (argc != 5) {
fprintf(stderr, "usage: %s symbol input output.c output.h", argv[0]);
return 1;
}
FILE *input = fopen(argv[2], "r");
FILE *output = fopen(argv[3], "w");
fprintf(output, "const char %s[] = \"", argv[1]);
int ch;
int size = 0;
while ((ch = getc(input)) != EOF) {
if (isprint(ch) && ch != '\\' && ch != '"') {
putc(ch, output);
} else switch (ch) {
case '"': fputs("\\\"", output); break;
case '\\': fputs("\\\\", output); break;
case '\n': fputs("\\n", output); break;
case '\r': fputs("\\r", output); break;
default: fprintf(output, "\\%03o", ch); break;
}
size++;
}
int padding = 4096 - size % 4096;
for (int i = 0; i < padding; i++) {
fputs("\0", output);
}
fprintf(output, "\";\n");
fclose(input);
fclose(output);
output = fopen(argv[4], "w");
fprintf(output, "extern const char %s[%d];\n", argv[1], size + padding);
}