diff options
Diffstat (limited to 'rbutil/icons/bin2c.c')
-rw-r--r-- | rbutil/icons/bin2c.c | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/rbutil/icons/bin2c.c b/rbutil/icons/bin2c.c new file mode 100644 index 0000000000..de4bed8348 --- /dev/null +++ b/rbutil/icons/bin2c.c | |||
@@ -0,0 +1,172 @@ | |||
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) | ||
11 | // | ||
12 | // examples: | ||
13 | // bin2c -c myimage.png myimage_png.cpp | ||
14 | // bin2c -z sometext.txt sometext_txt.cpp | ||
15 | |||
16 | #include <ctype.h> | ||
17 | #include <stdio.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | #ifndef PATH_MAX | ||
22 | #define PATH_MAX 1024 | ||
23 | #endif | ||
24 | |||
25 | int useconst = 0; | ||
26 | int zeroterminated = 0; | ||
27 | |||
28 | int myfgetc(FILE *f) | ||
29 | { | ||
30 | int c = fgetc(f); | ||
31 | if (c == EOF && zeroterminated) { | ||
32 | zeroterminated = 0; | ||
33 | return 0; | ||
34 | } | ||
35 | return c; | ||
36 | } | ||
37 | |||
38 | |||
39 | void process(const char *ifname, const char *ofname) | ||
40 | { | ||
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 = ofname; | ||
54 | } | ||
55 | |||
56 | strcpy(buf2, cp3); | ||
57 | cp2 = strrchr(buf2, '.'); | ||
58 | *cp2 = '.'; | ||
59 | cp2++; | ||
60 | *cp2 = 'h'; | ||
61 | cp2++; | ||
62 | *cp2 ='\0'; | ||
63 | |||
64 | |||
65 | ifile = fopen(ifname, "rb"); | ||
66 | if (ifile == NULL) { | ||
67 | fprintf(stderr, "cannot open %s for reading\n", ifname); | ||
68 | exit(1); | ||
69 | } | ||
70 | ofile = fopen(ofname, "wb"); | ||
71 | if (ofile == NULL) { | ||
72 | fprintf(stderr, "cannot open %s for writing\n", ofname); | ||
73 | exit(1); | ||
74 | } | ||
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; | ||
84 | } | ||
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; | ||
97 | } | ||
98 | fprintf(ofile, "0x%.2x, ", c); | ||
99 | col += 6; | ||
100 | |||
101 | } | ||
102 | fprintf(ofile, "\n};\n"); | ||
103 | |||
104 | /* modified */ | ||
105 | fprintf(ofile,"int %s_length = %i; \n",buf,counter); | ||
106 | |||
107 | |||
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); | ||
113 | } | ||
114 | |||
115 | fprintf(o2file, "#ifndef __%s__ \n", buf); | ||
116 | fprintf(o2file, "#define __%s__ \n", buf); | ||
117 | |||
118 | fprintf(o2file, "extern %sunsigned char %s[]; \n\n", useconst ? "const " : "", buf); | ||
119 | fprintf(o2file, "extern int %s_length; \n\n", buf); | ||
120 | |||
121 | fprintf(o2file, "#endif \n"); | ||
122 | |||
123 | fclose(ifile); | ||
124 | fclose(ofile); | ||
125 | fclose(o2file); | ||
126 | } | ||
127 | |||
128 | void usage(void) | ||
129 | { | ||
130 | fprintf(stderr, "usage: bin2c <input_files> \n"); | ||
131 | exit(1); | ||
132 | } | ||
133 | |||
134 | int main(int argc, char **argv) | ||
135 | { | ||
136 | if (argc < 2) { | ||
137 | usage(); | ||
138 | } | ||
139 | int i; | ||
140 | for(i = 1;i < argc ; i++) | ||
141 | { | ||
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); | ||
149 | } | ||
150 | |||
151 | |||
152 | /* | ||
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(); | ||
164 | } | ||
165 | } | ||
166 | if (argc != 3) { | ||
167 | usage(); | ||
168 | } | ||
169 | process(argv[1], argv[2]); | ||
170 | */ | ||
171 | return 0; | ||
172 | } | ||