Debian Codegraph @

common/skin2cc.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include <errno.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #ifdef WINDOWS
23 #define SKINS_CC "skins.cpp"
24 #define SKIN2CC_CONF "skin2cpp.conf"
25 #define SKIN2CC "skin2cpp"
26 #else
27 #define SKINS_CC "skins.cc"
28 #define SKIN2CC_CONF "skin2cc.conf"
29 #define SKIN2CC "skin2cc"
30 #endif
31
32 FILE *out;
33
34 void write_bytes(FILE *file) {
35 int pos;
36 int first = 1;
37 int c;
38 while ((c = fgetc(file)) != EOF) {
39 int width = c < 10 ? 1 : c < 100 ? 2 : 3;
40 if (first) {
41 first = 0;
42 fprintf(out, " ");
43 pos = 4;
44 } else if (pos + width > 74) {
45 fprintf(out, ",\n ");
46 pos = 4;
47 } else {
48 fprintf(out, ", ");
49 pos += 2;
50 }
51 fprintf(out, "%d", c);
52 pos += width;
53 }
54 }
55
56 int main(int argc, char *argv[]) {
57 FILE *conf;
58 FILE *inp;
59 char line[256];
60 char skinname[100][256];
61 char skinfile[100][256];
62 int nskins = 0, i;
63
64 out = fopen(SKINS_CC, "w");
65 if (out == NULL) {
66 fprintf(stderr, "%s: can't open output file \"%s\"\n", SKIN2CC, SKINS_CC);
67 return 1;
68 }
69
70 fprintf(out,
71 "/* %s\n"
72 " * Contains the built-in skins for Free42 (Unix & Windows).\n"
73 " * This file is generated by the %s program,\n"
74 " * under control of the %s file, which is\n"
75 " * a list of skin descriptions and their user-visible names;\n"
76 " * each entry consists of the skin name on one line, followed\n"
77 " * by the user-visible name on the next line.\n"
78 " * The skins consist of two files, <name>.layout (the layout \n"
79 " * description), and <name>.gif (the skin bitmap); these files \n"
80 " * are looked for in ../.\n"
81 " * NOTE: this is a generated file; do not edit!\n"
82 " */\n\n", SKINS_CC, SKIN2CC, SKIN2CC_CONF);
83
84 conf = fopen(SKIN2CC_CONF, "r");
85 if (conf == NULL) {
86 int err = errno;
87 fprintf(stderr, "Can't open \"%s\": %s (%d).\n",
88 SKIN2CC_CONF, strerror(err), err);
89 fclose(out);
90 remove(SKINS_CC);
91 return 1;
92 }
93
94 while (1) {
95 int len;
96
97 if (fgets(line, 256, conf) == NULL)
98 break;
99 len = strlen(line);
100 if (len > 0 && line[len - 1] == '\n')
101 line[--len] = 0;
102 strcpy(skinfile[nskins], line);
103
104 if (fgets(line, 256, conf) == NULL)
105 break;
106 len = strlen(line);
107 if (len > 0 && line[len - 1] == '\n')
108 line[--len] = 0;
109 strcpy(skinname[nskins], line);
110
111 nskins++;
112 }
113 fclose(conf);
114
115
116 fprintf(out, "/****************************************/\n");
117 fprintf(out, "/* Number of skins defined in this file */\n");
118 fprintf(out, "/****************************************/\n\n");
119
120 fprintf(out, "extern const int skin_count = %d;\n\n\n", nskins);
121
122
123 fprintf(out, "/**************/\n");
124 fprintf(out, "/* Skin names */\n");
125 fprintf(out, "/**************/\n\n");
126
127 #ifdef WINDOWS
128 fprintf(out, "extern const wchar_t * const skin_name[] = {\n");
129 for (i = 0; i < nskins; i++)
130 fprintf(out, " L\"%s\"%s\n", skinname[i], i < nskins - 1 ? "," : "");
131 fprintf(out, "};\n\n\n");
132 #else
133 fprintf(out, "extern const char * const skin_name[] = {\n");
134 for (i = 0; i < nskins; i++)
135 fprintf(out, " \"%s\"%s\n", skinname[i], i < nskins - 1 ? "," : "");
136 fprintf(out, "};\n\n\n");
137 #endif
138
139
140 fprintf(out, "/*************************************/\n");
141 fprintf(out, "/* Sizes of skin layout descriptions */\n");
142 fprintf(out, "/*************************************/\n\n");
143
144 fprintf(out, "extern const long skin_layout_size[] = {\n");
145 for (i = 0; i < nskins; i++) {
146 char fname[1024];
147 strcpy(fname, "../");
148 strcat(fname, skinfile[i]);
149 strcat(fname, ".layout");
150 inp = fopen(fname, "rb");
151 if (inp == NULL) {
152 int err = errno;
153 fprintf(stderr, "Can't open \"%s\": %s (%d)\n",
154 fname, strerror(err), err);
155 fclose(out);
156 remove(SKINS_CC);
157 return 1;
158 }
159 fseek(inp, 0, SEEK_END);
160 fprintf(out, " %ld%s\n", ftell(inp), i < nskins - 1 ? "," : "");
161 fclose(inp);
162 }
163 fprintf(out, "};\n\n\n");
164
165
166 fprintf(out, "/****************************/\n");
167 fprintf(out, "/* Skin layout descriptions */\n");
168 fprintf(out, "/****************************/\n\n");
169
170 for (i = 0; i < nskins; i++) {
171 char fname[1024];
172 strcpy(fname, "../");
173 strcat(fname, skinfile[i]);
174 strcat(fname, ".layout");
175 inp = fopen(fname, "rb");
176 if (inp == NULL) {
177 int err = errno;
178 fprintf(stderr, "Can't open \"%s\": %s (%d)\n",
179 fname, strerror(err), err);
180 fclose(out);
181 remove(SKINS_CC);
182 return 1;
183 }
184 fprintf(out, "static const unsigned char skin%d_layout_data[] = {\n", i);
185 write_bytes(inp);
186 fprintf(out, "\n};\n\n");
187 fclose(inp);
188 }
189 fprintf(out, "extern const unsigned char * const skin_layout_data[] = {\n");
190 for (i = 0; i < nskins; i++)
191 fprintf(out, " skin%d_layout_data%s\n", i, i < nskins - 1 ? "," : "");
192 fprintf(out, "};\n\n\n");
193
194
195 fprintf(out, "/*************************/\n");
196 fprintf(out, "/* Sizes of skin bitmaps */\n");
197 fprintf(out, "/*************************/\n\n");
198
199 fprintf(out, "extern const long skin_bitmap_size[] = {\n");
200 for (i = 0; i < nskins; i++) {
201 char fname[1024];
202 strcpy(fname, "../");
203 strcat(fname, skinfile[i]);
204 strcat(fname, ".gif");
205 inp = fopen(fname, "rb");
206 if (inp == NULL) {
207 int err = errno;
208 fprintf(stderr, "Can't open \"%s\": %s (%d)\n",
209 fname, strerror(err), err);
210 fclose(out);
211 remove(SKINS_CC);
212 return 1;
213 }
214 fseek(inp, 0, SEEK_END);
215 fprintf(out, " %ld%s\n", ftell(inp), i < nskins - 1 ? "," : "");
216 fclose(inp);
217 }
218 fprintf(out, "};\n\n\n");
219
220
221 fprintf(out, "/****************/\n");
222 fprintf(out, "/* Skin bitmaps */\n");
223 fprintf(out, "/****************/\n\n");
224
225 for (i = 0; i < nskins; i++) {
226 char fname[1024];
227 strcpy(fname, "../");
228 strcat(fname, skinfile[i]);
229 strcat(fname, ".gif");
230 inp = fopen(fname, "rb");
231 if (inp == NULL) {
232 int err = errno;
233 fprintf(stderr, "Can't open \"%s\": %s (%d)\n",
234 fname, strerror(err), err);
235 fclose(out);
236 remove(SKINS_CC);
237 return 1;
238 }
239 fprintf(out, "static const unsigned char skin%d_bitmap_data[] = {\n", i);
240 write_bytes(inp);
241 fprintf(out, "\n};\n\n");
242 fclose(inp);
243 }
244 fprintf(out, "extern const unsigned char * const skin_bitmap_data[] = {\n");
245 for (i = 0; i < nskins; i++)
246 fprintf(out, " skin%d_bitmap_data%s\n", i, i < nskins - 1 ? "," : "");
247 fprintf(out, "};\n\n\n");
248
249
250 fprintf(out, "/***********/\n");
251 fprintf(out, "/* The End */\n");
252 fprintf(out, "/***********/\n");
253 fclose(out);
254 return 0;
255 }