]> git.sesse.net Git - nageru/blob - shared/bin2h.cpp
Fix compilation with non-system CEF.
[nageru] / shared / bin2h.cpp
1 #include <stdio.h>
2 #include <string>
3
4 using namespace std;
5
6 int main(int argc, char **argv)
7 {
8         if (argc != 4) {
9                 fprintf(stderr, "Usage: bin2h INFILE BASENAME OUTFILE\n");
10                 return 1;
11         }
12
13         string basename = argv[2];
14         for (char &ch : basename) {
15                 if (!isalpha(ch) && !isdigit(ch)) {
16                         ch = '_';
17                 }
18         }
19
20         FILE *infp = fopen(argv[1], "rb");
21         if (infp == nullptr) {
22                 perror(argv[1]);
23                 exit(1);
24         }
25
26         FILE *outfp = fopen(argv[3], "w");
27         if (outfp == nullptr) {
28                 perror(argv[3]);
29                 exit(1);
30         }
31
32         fprintf(outfp, "// Generated by bin2h.cpp from %s. Do not edit by hand.\n", argv[1]);
33         fprintf(outfp, "#include <stddef.h>\n");
34         fprintf(outfp, "unsigned char _binary_%s[] = {", basename.c_str());
35
36         size_t num_bytes = 0;
37         while (!feof(infp)) {
38                 if (num_bytes++ % 16 == 0) {
39                         fprintf(outfp, "\n\t");
40                 }
41                 int ch = getc(infp);
42                 if (ch == -1) {
43                         break;
44                 }
45                 fprintf(outfp, "0x%02x, ", ch);
46         }
47         fprintf(outfp, "\n};\n");
48         fprintf(outfp, "unsigned char *_binary_%s_data = _binary_%s;\n", basename.c_str(), basename.c_str());
49         fprintf(outfp, "size_t _binary_%s_size = sizeof(_binary_%s);\n", basename.c_str(), basename.c_str());
50         return 0;
51 }