The install window doesn't need to be wider than the other ones.
[Rockbox.git] / rbutil / icons / bin2c.c
blob99400444ef3f3bbacf45362920401fbc39521ba7
1 // bin2c.c
2 //
3 // convert a binary file into a C source vector
4 //
5 // put into the public domain by Sandro Sigala
6 //
7 // syntax: bin2c [-c] [-z] <input_file> <output_file>
8 //
9 // -c add the "const" keyword to definition
10 // -z terminate the array with a zero (useful for embedded C strings)
12 // examples:
13 // bin2c -c myimage.png myimage_png.cpp
14 // bin2c -z sometext.txt sometext_txt.cpp
16 #include <ctype.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #ifndef PATH_MAX
22 #define PATH_MAX 1024
23 #endif
25 int useconst = 0;
26 int zeroterminated = 0;
28 int myfgetc(FILE *f)
30 int c = fgetc(f);
31 if (c == EOF && zeroterminated) {
32 zeroterminated = 0;
33 return 0;
35 return c;
39 void process(const char *ifname, const char *ofname)
41 FILE *ifile, *ofile;
42 /* modified */
43 int counter=0;
44 char buf2[PATH_MAX];
45 char* cp2;
46 char* cp3;
47 if ((cp3 = strrchr(ofname, '/')) != NULL)
48 ++cp3;
49 else {
50 if ((cp3 = strrchr(ofname, '\\')) != NULL)
51 ++cp3;
52 else
53 cp3 = (char*) ofname;
56 strcpy(buf2, cp3);
57 cp2 = strrchr(buf2, '.');
58 *cp2 = '.';
59 cp2++;
60 *cp2 = 'h';
61 cp2++;
62 *cp2 ='\0';
65 ifile = fopen(ifname, "rb");
66 if (ifile == NULL) {
67 fprintf(stderr, "cannot open %s for reading\n", ifname);
68 exit(1);
70 ofile = fopen(ofname, "wb");
71 if (ofile == NULL) {
72 fprintf(stderr, "cannot open %s for writing\n", ofname);
73 exit(1);
75 char buf[PATH_MAX], *p;
76 const char *cp;
77 if ((cp = strrchr(ifname, '/')) != NULL)
78 ++cp;
79 else {
80 if ((cp = strrchr(ifname, '\\')) != NULL)
81 ++cp;
82 else
83 cp = ifname;
85 strcpy(buf, cp);
86 for (p = buf; *p != '\0'; ++p)
87 if (!isalnum(*p))
88 *p = '_';
89 fprintf(ofile,"#include \"%s\" \n\n",buf2);
90 fprintf(ofile, "%sunsigned char %s[] = {\n", useconst ? "const " : "", buf);
91 int c, col = 1;
92 while ((c = myfgetc(ifile)) != EOF) {
93 counter++;
94 if (col >= 78 - 6) {
95 fputc('\n', ofile);
96 col = 1;
98 fprintf(ofile, "0x%.2x, ", c);
99 col += 6;
102 fprintf(ofile, "\n};\n");
104 /* modified */
105 fprintf(ofile,"int %s_length = %i; \n",buf,counter);
108 FILE *o2file;
109 o2file = fopen(buf2, "wb");
110 if (o2file == NULL) {
111 fprintf(stderr, "cannot open %s for writing\n", buf2);
112 exit(1);
115 fprintf(o2file, "#ifndef __%s__ \n", buf);
116 fprintf(o2file, "#define __%s__ \n", buf);
118 fprintf(o2file, "extern %sunsigned char %s[]; \n\n", useconst ? "const " : "", buf);
119 fprintf(o2file, "extern int %s_length; \n\n", buf);
121 fprintf(o2file, "#endif \n");
123 fclose(ifile);
124 fclose(ofile);
125 fclose(o2file);
128 void usage(void)
130 fprintf(stderr, "usage: bin2c <input_files> \n");
131 exit(1);
134 int main(int argc, char **argv)
136 if (argc < 2) {
137 usage();
139 int i;
140 for(i = 1;i < argc ; i++)
142 char buf[PATH_MAX];
143 char* cp;
144 strcpy(buf, argv[i]);
145 cp = strrchr(buf, '.');
146 cp++;
147 strcpy(cp,"cpp");
148 process(argv[i], buf);
153 while (argc > 3) {
154 if (!strcmp(argv[1], "-c")) {
155 useconst = 1;
156 --argc;
157 ++argv;
158 } else if (!strcmp(argv[1], "-z")) {
159 zeroterminated = 1;
160 --argc;
161 ++argv;
162 } else {
163 usage();
166 if (argc != 3) {
167 usage();
169 process(argv[1], argv[2]);
171 return 0;