forked from kainjow/Glypha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin-convert.c
85 lines (75 loc) · 1.67 KB
/
bin-convert.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
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
const char *in_path;
FILE *in_file = NULL;
FILE *out_file = NULL;
const char *nmspace = "GL";
const char *filename;
char variable[1024];
size_t i;
int num_bytes = 0;
int column = 0;
const int bytes_per_column = 12;
if (argc != 3) {
fprintf(stderr, "Invalid args\n");
return 1;
}
in_path = argv[1];
#ifdef _WIN32
(void)fopen_s(&in_file, in_path, "rb");
#else
in_file = fopen(in_path, "rb");
#endif
if (!in_file) {
fprintf(stderr, "Can't open input file %s\n", in_path);
return 1;
}
#ifdef _WIN32
(void)fopen_s(&out_file, argv[2], "wb");
#else
out_file = fopen(argv[2], "wb");
#endif
if (!out_file) {
fprintf(stderr, "Can't open output file %s\n", argv[2]);
fclose(in_file);
return 1;
}
filename = strrchr(in_path, '/');
if (filename) {
memcpy(variable, filename + 1, strlen(filename + 1) + 1);
} else {
memcpy(variable, in_path, strlen(in_path) + 1);
}
for (i = 0; i < strlen(variable); ++i) {
if (variable[i] == '.') {
variable[i] = '_';
}
}
fprintf(out_file, "#include \"GLResources.h\"\n");
fprintf(out_file, "unsigned char %s::%s[] = {\n", nmspace, variable);
for (;;) {
int ch = fgetc(in_file);
if (ch == EOF) {
if (feof(in_file)) {
fprintf(out_file, "\n");
}
break;
}
if (column == 0) {
fprintf(out_file, " ");
}
fprintf(out_file, "0x%02x, ", ch);
++column;
if (column == bytes_per_column) {
column = 0;
fprintf(out_file, "\n");
}
++num_bytes;
}
fprintf(out_file, "};\n");
fprintf(out_file, "unsigned int %s::%s_len = %d;\n", nmspace, variable, num_bytes);
fclose(out_file);
fclose(in_file);
return 0;
}