comparison tools/data2inc.c @ 2405:1f74cc842d71

Add some error checking to data2inc.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 13 Jan 2020 20:30:13 +0200
parents b7cd5dd0b82e
children b153bc46241d
comparison
equal deleted inserted replaced
2404:bcd33c77c605 2405:1f74cc842d71
24 char *desc; 24 char *desc;
25 char *fexts[SET_MAX_FEXTS]; 25 char *fexts[SET_MAX_FEXTS];
26 26
27 char *defDataType; 27 char *defDataType;
28 28
29 void (*writeHeader) (FILE *fh, const char *name); 29 int (*writeHeader) (FILE *fh, const char *name);
30 void (*writeDecl) (FILE *fh, const size_t len, const char *name); 30 int (*writeDecl) (FILE *fh, const size_t len, const char *name);
31 void (*writeData) (FILE *fh, const Uint8 *buf, const size_t len); 31 int (*writeData) (FILE *fh, const Uint8 *buf, const size_t len);
32 void (*writeFooter) (FILE *fh, const size_t len, const char *name); 32 int (*writeFooter) (FILE *fh, const size_t len, const char *name);
33 } DMOutputFormat; 33 } DMOutputFormat;
34 34
35 35
36 // 36 //
37 // Options 37 // Options
198 } 198 }
199 199
200 200
201 /* Assembler include data output functions 201 /* Assembler include data output functions
202 */ 202 */
203 void writeHeader_ASM(FILE *fh, const char *name) 203 int writeHeader_ASM(FILE *fh, const char *name)
204 { 204 {
205 int res = 0;
206
205 if (name) 207 if (name)
206 fprintf(fh, "; '%s'", name); 208 res = fprintf(fh, "; '%s'", name);
207 else 209 else
208 fprintf(fh, "; Generated"); 210 res = fprintf(fh, "; Generated");
209 211
210 fprintf(fh, " by %s v%s\n", 212 if (res >= 0)
213 res = fprintf(fh, " by %s v%s\n",
211 dmProgName, dmProgVersion); 214 dmProgName, dmProgVersion);
212 } 215
213 216 return res >= 0 ? DMERR_OK : dmGetErrno();
214 217 }
215 void writeDecl_ASM(FILE *fh, const size_t len, const char *name) 218
216 { 219
220 int writeDecl_ASM(FILE *fh, const size_t len, const char *name)
221 {
222 int res = 0;
223
217 if (optExtraData) 224 if (optExtraData)
218 fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len); 225 res = fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
219 226
220 fprintf(fh, "%s:\n", name); 227 if (res >= 0)
221 } 228 res = fprintf(fh, "%s:\n", name);
222 229
223 230 return res >= 0 ? DMERR_OK : dmGetErrno();
224 void writeData_ASM(FILE *fh, const Uint8 * buf, const size_t len) 231 }
225 { 232
226 fprintf(fh, "%s ", optDataType); 233
227 234 int writeData_ASM(FILE *fh, const Uint8 * buf, const size_t len)
228 for (size_t i = 0; i < len; i++) 235 {
236 int res = fprintf(fh, "%s ", optDataType);
237
238 for (size_t i = 0; res >= 0 && i < len; i++)
229 { 239 {
230 if (optFormatting) 240 if (optFormatting)
231 { 241 {
232 if (optHexMode) 242 if (optHexMode)
233 fprintf(fh, "$%.2x", buf[i]); 243 res = fprintf(fh, "$%.2x", buf[i]);
234 else 244 else
235 fprintf(fh, "%3d", buf[i]); 245 res = fprintf(fh, "%3d", buf[i]);
236 } 246 }
237 else 247 else
238 { 248 {
239 if (optHexMode) 249 if (optHexMode)
240 fprintf(fh, "$%x", buf[i]); 250 res = fprintf(fh, "$%x", buf[i]);
241 else 251 else
242 fprintf(fh, "%d", buf[i]); 252 res = fprintf(fh, "%d", buf[i]);
243 } 253 }
244 254
245 if (i + 1 < len) 255 if (res >= 0 && i + 1 < len)
246 fprintf(fh, optFormatting ? ", " : ","); 256 res = fprintf(fh, optFormatting ? ", " : ",");
247 } 257 }
248 } 258
249 259 return res >= 0 ? DMERR_OK : dmGetErrno();
250 260 }
251 void writeFooter_ASM(FILE *fh, const size_t len, const char *name) 261
252 { 262
263 int writeFooter_ASM(FILE *fh, const size_t len, const char *name)
264 {
265 int res;
253 (void) len; 266 (void) len;
267
254 if (optExtraData) 268 if (optExtraData)
255 fprintf(fh, "%s_end: \n", name); 269 res = fprintf(fh, "%s_end: \n", name);
256 else 270 else
257 fprintf(fh, "\n"); 271 res = fprintf(fh, "\n");
272
273 return res >= 0 ? DMERR_OK : dmGetErrno();
258 } 274 }
259 275
260 276
261 /* ANSI-C include data output functions 277 /* ANSI-C include data output functions
262 */ 278 */
263 void writeHeader_C(FILE *fh, const char *name) 279 int writeHeader_C(FILE *fh, const char *name)
264 { 280 {
281 int res;
282
265 if (name) 283 if (name)
266 fprintf(fh, "/* '%s' generated", name); 284 res = fprintf(fh, "/* '%s' generated", name);
267 else 285 else
268 fprintf(fh, "/* Generated"); 286 res = fprintf(fh, "/* Generated");
269 287
270 fprintf(fh, " by %s v%s\n" " */\n", 288 if (res >= 0)
289 res = fprintf(fh, " by %s v%s\n" " */\n",
271 dmProgName, dmProgVersion); 290 dmProgName, dmProgVersion);
272 } 291
273 292 return res >= 0 ? DMERR_OK : dmGetErrno();
274 293 }
275 void writeDecl_C(FILE *fh, const size_t len, const char *name) 294
276 { 295
277 fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n", 296 int writeDecl_C(FILE *fh, const size_t len, const char *name)
297 {
298 int res;
299
300 res = fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n",
278 optDataType, name, len); 301 optDataType, name, len);
279 302
280 printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n", 303 if (res >= 0)
304 res = printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n",
281 optDataType, name, len); 305 optDataType, name, len);
282 } 306
283 307 return res >= 0 ? DMERR_OK : dmGetErrno();
284 308 }
285 void writeData_C(FILE *fh, const Uint8 *buf, const size_t len) 309
286 { 310
287 for (size_t i = 0; i < len; i++) 311 int writeData_C(FILE *fh, const Uint8 *buf, const size_t len)
312 {
313 int res = 0;
314
315 for (size_t i = 0; res >= 0 && i < len; i++)
288 { 316 {
289 if (optFormatting) 317 if (optFormatting)
290 { 318 {
291 if (optHexMode) 319 if (optHexMode)
292 fprintf(fh, "0x%.2x", buf[i]); 320 res = fprintf(fh, "0x%.2x", buf[i]);
293 else 321 else
294 fprintf(fh, "%3d", buf[i]); 322 res = fprintf(fh, "%3d", buf[i]);
295 } 323 }
296 else 324 else
297 { 325 {
298 if (optHexMode) 326 if (optHexMode)
299 fprintf(fh, "0x%x", buf[i]); 327 res = fprintf(fh, "0x%x", buf[i]);
300 else 328 else
301 fprintf(fh, "%d", buf[i]); 329 res = fprintf(fh, "%d", buf[i]);
302 } 330 }
303 331
304 fprintf(fh, optFormatting ? ", " : ","); 332 if (res >= 0)
305 } 333 res = fprintf(fh, optFormatting ? ", " : ",");
306 } 334 }
307 335
308 336 return res >= 0 ? DMERR_OK : dmGetErrno();
309 void writeFooter_C(FILE *fh, const size_t len, const char *name) 337 }
310 { 338
339
340 int writeFooter_C(FILE *fh, const size_t len, const char *name)
341 {
342 int res;
311 (void) len; 343 (void) len;
312 (void) name; 344 (void) name;
313 345
314 fprintf(fh, "};\n"); 346 res = fprintf(fh, "};\n");
315 } 347 return res >= 0 ? DMERR_OK : dmGetErrno();
316 348 }
317 349
350
351 /*
352 * List of formats
353 */
318 static const DMOutputFormat dmFormatList[] = 354 static const DMOutputFormat dmFormatList[] =
319 { 355 {
320 { 356 {
321 "asm", 357 "asm",
322 "XA65 compatible assembler", 358 "XA65 compatible assembler",
453 489
454 // Get sourcefile size 490 // Get sourcefile size
455 totalSize = dmGetFileSize(inFile); 491 totalSize = dmGetFileSize(inFile);
456 492
457 // Output header 493 // Output header
458 if (!optQuiet) 494 if (!optQuiet &&
459 setFormat->writeHeader(outFile, optOutFilename); 495 (res = setFormat->writeHeader(outFile, optOutFilename)) != DMERR_OK)
496 {
497 dmErrorMsg("Error writing output header: %s\n",
498 dmErrorStr(res));
499 goto exit;
500 }
460 501
461 if (optAddLine) 502 if (optAddLine)
462 fprintf(outFile, "%s\n", optAddLine); 503 fprintf(outFile, "%s\n", optAddLine);
463 504
464 // Output declaration 505 // Output declaration
480 { 521 {
481 for (int i = 0; i < optIndentation; i++) 522 for (int i = 0; i < optIndentation; i++)
482 fputs(" ", outFile); 523 fputs(" ", outFile);
483 } 524 }
484 525
485 setFormat->writeData(outFile, dataBuf, res); 526 if ((res = setFormat->writeData(outFile, dataBuf, res)) != DMERR_OK)
527 {
528 dmErrorMsg("Error writing output data: %s\n",
529 dmErrorStr(res));
530 goto exit;
531 }
486 532
487 fprintf(outFile, "\n"); 533 fprintf(outFile, "\n");
488 } 534 }
489 } 535 }
490 536
491 // Output footer 537 // Output footer
492 setFormat->writeFooter(outFile, totalSize, optObjName); 538 if ((res = setFormat->writeFooter(outFile, totalSize, optObjName)) != DMERR_OK)
539 {
540 dmErrorMsg("Error writing output footer: %s\n",
541 dmErrorStr(res));
542 goto exit;
543 }
493 544
494 exit: 545 exit:
495 // Cleanup 546 // Cleanup
496 if (inFile != NULL) 547 if (inFile != NULL)
497 fclose(inFile); 548 fclose(inFile);