]> bicyclesonthemoon.info Git - ott/enhance/blob - f2h.c
reveal online
[ott/enhance] / f2h.c
1 /*
2 f2h.c
3 make file into h
4 30.11.2022
5
6 Copyright (C) 2022  Balthasar SzczepaƄski
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26 #include <errno.h>
27
28 char DEFAULT_NAME[] = "data";
29 FILE *infile, *outfile;
30 uint_fast8_t in_open = 0;
31 uint_fast8_t out_open = 0;
32
33 int cleanup(int r)
34 {
35         if (out_open)
36                 fclose(outfile);
37         if (in_open)
38                 fclose(infile);
39         return r;
40 }
41
42 int main (int argc, char **argv)
43 {
44         char *name;
45         uint_fast8_t i;
46         uint8_t x;
47         uint_fast64_t size=0;
48         
49         if (argc >= 2)
50                 name = argv[1];
51         else
52                 name = DEFAULT_NAME;
53         if (argc >= 3)
54         {
55                 infile = fopen(argv[2], "rb");
56                 if (infile==NULL)
57                         return cleanup(EIO);
58                 in_open = 1;
59         }
60         else
61                 infile = stdin;
62         if (argc >= 4)
63         {
64                 outfile = fopen(argv[3], "wt");
65                 if (outfile==NULL)
66                         return cleanup(EIO);
67                 out_open = 1;
68         }
69         else
70                 outfile = stdout;
71         
72         fprintf(
73                 outfile,
74                 "/* %s is autogenerated%s%s. */\n",
75                 out_open ? argv[3] : "File",
76                 in_open ? " from " : "",
77                 in_open ? argv[2] : ""
78         );
79         fputs ("#include <stdint.h>\n", outfile);
80         fprintf(
81                 outfile,
82                 "const uint8_t %s[] = {",
83                 name
84         );
85         for (i=0; fread(&x, 1, 1, infile)!=0;i=(i+1)&7)
86         {
87                 fprintf(
88                 outfile,
89                 "%s0x%02" PRIX8 ",",
90                 (i==0) ? "\n\t" : " ",
91                 x
92                 );
93                 ++size;
94         }
95         fputs ("\n};\n", outfile);
96         fprintf(
97                 outfile,
98                 "#define %s_size %" PRIu64 "\n",
99                 name,
100                 size
101         );
102         
103         return cleanup(0);
104 }