comparison tools/data2inc.c @ 2574:3feca4682680

Move functions around to get rid of the unknown-size dmFormatList array forward declaration (without the need to give it a size), so that we compile cleanly with -pedantic.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 Mar 2022 14:48:56 +0200
parents d75431bf1a7d
children 9807ae37ad69
comparison
equal deleted inserted replaced
2573:21d296803fac 2574:3feca4682680
70 { 26, 'i', "indentation" , "Set indentation (negative value = tab)", OPT_ARGREQ }, 70 { 26, 'i', "indentation" , "Set indentation (negative value = tab)", OPT_ARGREQ },
71 { 28, 'e', "extra-data" , "Add object end labels and size in asm output", OPT_NONE }, 71 { 28, 'e', "extra-data" , "Add object end labels and size in asm output", OPT_NONE },
72 }; 72 };
73 73
74 static const int optListN = sizeof(optList) / sizeof(optList[0]); 74 static const int optListN = sizeof(optList) / sizeof(optList[0]);
75 static const DMOutputFormat dmFormatList[]; 75
76 static const int ndmFormatList; 76
77 static void dmPrintIndentation(FILE *fh)
78 {
79 int level;
80 const char *str;
81
82 if (optIndentation < 0)
83 {
84 level = - optIndentation;
85 str = "\t";
86 }
87 else
88 {
89 level = optIndentation;
90 str = " ";
91 }
92
93 for (int i = 0; i < level; i++)
94 fputs(str, fh);
95 }
96
97
98 static int dmHandleError(const int res)
99 {
100 return res >= 0 ? DMERR_OK : dmGetErrno();
101 }
102
103
104 off_t dmGetFileSize(FILE *fh)
105 {
106 off_t len, pos = ftello(fh);
107 fseeko(fh, 0, SEEK_END);
108 len = ftello(fh);
109 fseeko(fh, pos, SEEK_SET);
110 return len;
111 }
112
113
114 /* Assembler include data output functions
115 */
116 int writeHeader_ASM(FILE *fh, void *ctx, const char *name)
117 {
118 int res = 0;
119 (void) ctx;
120
121 if (name)
122 res = fprintf(fh, "; '%s'", name);
123 else
124 res = fprintf(fh, "; Generated");
125
126 if (res >= 0)
127 res = fprintf(fh, " by %s v%s\n",
128 dmProgName, dmProgVersion);
129
130 return dmHandleError(res);
131 }
132
133
134 int writeDecl_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
135 {
136 int res = 0;
137 (void) ctx;
138
139 if (optExtraData)
140 res = fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
141
142 if (res >= 0)
143 res = fprintf(fh, "%s:\n", name);
144
145 return dmHandleError(res);
146 }
147
148
149 int writeData_ASM(FILE *fh, void *ctx, const Uint8 * buf, const size_t len)
150 {
151 int res;
152 (void) ctx;
153
154 dmPrintIndentation(fh);
155 res = fprintf(fh, "%s ", optDataType);
156
157 for (size_t i = 0; res >= 0 && i < len; i++)
158 {
159 if (optFormatting)
160 {
161 if (optHexMode)
162 res = fprintf(fh, "$%.2x", buf[i]);
163 else
164 res = fprintf(fh, "%3d", buf[i]);
165 }
166 else
167 {
168 if (optHexMode)
169 res = fprintf(fh, "$%x", buf[i]);
170 else
171 res = fprintf(fh, "%d", buf[i]);
172 }
173
174 if (res >= 0 && i + 1 < len)
175 res = fprintf(fh, optFormatting ? ", " : ",");
176 }
177
178 if (res >= 0)
179 res = fprintf(fh, "\n");
180
181 return dmHandleError(res);
182 }
183
184
185 int writeFooter_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
186 {
187 int res;
188 (void) len;
189 (void) ctx;
190
191 if (optExtraData)
192 res = fprintf(fh, "%s_end: \n", name);
193 else
194 res = fprintf(fh, "\n");
195
196 return dmHandleError(res);
197 }
198
199
200 /* ANSI-C include data output functions
201 */
202 int writeHeader_C(FILE *fh, void *ctx, const char *name)
203 {
204 int res;
205 (void) ctx;
206
207 if (name)
208 res = fprintf(fh, "/* '%s' generated", name);
209 else
210 res = fprintf(fh, "/* Generated");
211
212 if (res >= 0)
213 res = fprintf(fh, " by %s v%s\n" " */\n",
214 dmProgName, dmProgVersion);
215
216 return dmHandleError(res);
217 }
218
219
220 int writeDecl_C(FILE *fh, void *ctx, const size_t len, const char *name)
221 {
222 int res;
223 (void) ctx;
224
225 res = fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n",
226 optDataType, name, len);
227
228 if (res >= 0)
229 res = printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n",
230 optDataType, name, len);
231
232 return dmHandleError(res);
233 }
234
235
236 int writeData_C(FILE *fh, void *ctx, const Uint8 *buf, const size_t len)
237 {
238 int res = 0;
239 (void) ctx;
240
241 dmPrintIndentation(fh);
242
243 for (size_t i = 0; res >= 0 && i < len; i++)
244 {
245 if (optFormatting)
246 {
247 if (optHexMode)
248 res = fprintf(fh, "0x%.2x", buf[i]);
249 else
250 res = fprintf(fh, "%3d", buf[i]);
251 }
252 else
253 {
254 if (optHexMode)
255 res = fprintf(fh, "0x%x", buf[i]);
256 else
257 res = fprintf(fh, "%d", buf[i]);
258 }
259
260 if (res >= 0)
261 res = fprintf(fh, optFormatting ? ", " : ",");
262 }
263
264 if (res >= 0)
265 res = fprintf(fh, "\n");
266
267 return dmHandleError(res);
268 }
269
270
271 int writeFooter_C(FILE *fh, void *ctx, const size_t len, const char *name)
272 {
273 int res;
274 (void) len;
275 (void) name;
276 (void) ctx;
277
278 res = fprintf(fh, "};\n");
279 return dmHandleError(res);
280 }
281
282
283 /*
284 * List of formats
285 */
286 static const DMOutputFormat dmFormatList[] =
287 {
288 {
289 "asm",
290 "XA65 compatible assembler",
291 { "s", "asm", NULL },
292 ".byte",
293
294 NULL,
295 NULL,
296
297 writeHeader_ASM,
298 writeDecl_ASM,
299 writeData_ASM,
300 writeFooter_ASM,
301 },
302
303 {
304 "c",
305 "ANSI C array",
306 { "c", "h", "cc", "cpp", "hpp", "c++", NULL },
307 "unsigned char",
308
309 NULL,
310 NULL,
311
312 writeHeader_C,
313 writeDecl_C,
314 writeData_C,
315 writeFooter_C,
316 },
317 };
318
319 static const int ndmFormatList = sizeof(dmFormatList) / sizeof(dmFormatList[0]);
320
321
77 322
78 323
79 void argShowHelp() 324 void argShowHelp()
80 { 325 {
81 dmPrintBanner(stdout, dmProgName, 326 dmPrintBanner(stdout, dmProgName,
205 450
206 return TRUE; 451 return TRUE;
207 } 452 }
208 453
209 454
210 static void dmPrintIndentation(FILE *fh)
211 {
212 int level;
213 const char *str;
214
215 if (optIndentation < 0)
216 {
217 level = - optIndentation;
218 str = "\t";
219 }
220 else
221 {
222 level = optIndentation;
223 str = " ";
224 }
225
226 for (int i = 0; i < level; i++)
227 fputs(str, fh);
228 }
229
230
231 static int dmHandleError(const int res)
232 {
233 return res >= 0 ? DMERR_OK : dmGetErrno();
234 }
235
236
237 /* Assembler include data output functions
238 */
239 int writeHeader_ASM(FILE *fh, void *ctx, const char *name)
240 {
241 int res = 0;
242 (void) ctx;
243
244 if (name)
245 res = fprintf(fh, "; '%s'", name);
246 else
247 res = fprintf(fh, "; Generated");
248
249 if (res >= 0)
250 res = fprintf(fh, " by %s v%s\n",
251 dmProgName, dmProgVersion);
252
253 return dmHandleError(res);
254 }
255
256
257 int writeDecl_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
258 {
259 int res = 0;
260 (void) ctx;
261
262 if (optExtraData)
263 res = fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
264
265 if (res >= 0)
266 res = fprintf(fh, "%s:\n", name);
267
268 return dmHandleError(res);
269 }
270
271
272 int writeData_ASM(FILE *fh, void *ctx, const Uint8 * buf, const size_t len)
273 {
274 int res;
275 (void) ctx;
276
277 dmPrintIndentation(fh);
278 res = fprintf(fh, "%s ", optDataType);
279
280 for (size_t i = 0; res >= 0 && i < len; i++)
281 {
282 if (optFormatting)
283 {
284 if (optHexMode)
285 res = fprintf(fh, "$%.2x", buf[i]);
286 else
287 res = fprintf(fh, "%3d", buf[i]);
288 }
289 else
290 {
291 if (optHexMode)
292 res = fprintf(fh, "$%x", buf[i]);
293 else
294 res = fprintf(fh, "%d", buf[i]);
295 }
296
297 if (res >= 0 && i + 1 < len)
298 res = fprintf(fh, optFormatting ? ", " : ",");
299 }
300
301 if (res >= 0)
302 res = fprintf(fh, "\n");
303
304 return dmHandleError(res);
305 }
306
307
308 int writeFooter_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
309 {
310 int res;
311 (void) len;
312 (void) ctx;
313
314 if (optExtraData)
315 res = fprintf(fh, "%s_end: \n", name);
316 else
317 res = fprintf(fh, "\n");
318
319 return dmHandleError(res);
320 }
321
322
323 /* ANSI-C include data output functions
324 */
325 int writeHeader_C(FILE *fh, void *ctx, const char *name)
326 {
327 int res;
328 (void) ctx;
329
330 if (name)
331 res = fprintf(fh, "/* '%s' generated", name);
332 else
333 res = fprintf(fh, "/* Generated");
334
335 if (res >= 0)
336 res = fprintf(fh, " by %s v%s\n" " */\n",
337 dmProgName, dmProgVersion);
338
339 return dmHandleError(res);
340 }
341
342
343 int writeDecl_C(FILE *fh, void *ctx, const size_t len, const char *name)
344 {
345 int res;
346 (void) ctx;
347
348 res = fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n",
349 optDataType, name, len);
350
351 if (res >= 0)
352 res = printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n",
353 optDataType, name, len);
354
355 return dmHandleError(res);
356 }
357
358
359 int writeData_C(FILE *fh, void *ctx, const Uint8 *buf, const size_t len)
360 {
361 int res = 0;
362 (void) ctx;
363
364 dmPrintIndentation(fh);
365
366 for (size_t i = 0; res >= 0 && i < len; i++)
367 {
368 if (optFormatting)
369 {
370 if (optHexMode)
371 res = fprintf(fh, "0x%.2x", buf[i]);
372 else
373 res = fprintf(fh, "%3d", buf[i]);
374 }
375 else
376 {
377 if (optHexMode)
378 res = fprintf(fh, "0x%x", buf[i]);
379 else
380 res = fprintf(fh, "%d", buf[i]);
381 }
382
383 if (res >= 0)
384 res = fprintf(fh, optFormatting ? ", " : ",");
385 }
386
387 if (res >= 0)
388 res = fprintf(fh, "\n");
389
390 return dmHandleError(res);
391 }
392
393
394 int writeFooter_C(FILE *fh, void *ctx, const size_t len, const char *name)
395 {
396 int res;
397 (void) len;
398 (void) name;
399 (void) ctx;
400
401 res = fprintf(fh, "};\n");
402 return dmHandleError(res);
403 }
404
405
406 /*
407 * List of formats
408 */
409 static const DMOutputFormat dmFormatList[] =
410 {
411 {
412 "asm",
413 "XA65 compatible assembler",
414 { "s", "asm", NULL },
415 ".byte",
416
417 NULL,
418 NULL,
419
420 writeHeader_ASM,
421 writeDecl_ASM,
422 writeData_ASM,
423 writeFooter_ASM,
424 },
425
426 {
427 "c",
428 "ANSI C array",
429 { "c", "h", "cc", "cpp", "hpp", "c++", NULL },
430 "unsigned char",
431
432 NULL,
433 NULL,
434
435 writeHeader_C,
436 writeDecl_C,
437 writeData_C,
438 writeFooter_C,
439 },
440 };
441
442 static const int ndmFormatList = sizeof(dmFormatList) / sizeof(dmFormatList[0]);
443
444
445 off_t dmGetFileSize(FILE *fh)
446 {
447 off_t len, pos = ftello(fh);
448 fseeko(fh, 0, SEEK_END);
449 len = ftello(fh);
450 fseeko(fh, pos, SEEK_SET);
451 return len;
452 }
453
454
455 const DMOutputFormat *dmGuessFormatFromName(const char *filename) 455 const DMOutputFormat *dmGuessFormatFromName(const char *filename)
456 { 456 {
457 const char *fext; 457 const char *fext;
458 458
459 if ((fext = strrchr(filename, '.')) == NULL) 459 if ((fext = strrchr(filename, '.')) == NULL)