comparison tools/data2inc.c @ 2565:d56a0e86067a

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Feb 2022 11:49:58 +0200
parents c6ee41fd98dd
children d75431bf1a7d
comparison
equal deleted inserted replaced
2564:2cf4e995b50c 2565:d56a0e86067a
479 FILE *inFile = NULL, *outFile = NULL; 479 FILE *inFile = NULL, *outFile = NULL;
480 Uint8 *dataBuf = NULL; 480 Uint8 *dataBuf = NULL;
481 void *ctx = NULL; 481 void *ctx = NULL;
482 size_t dataSize; 482 size_t dataSize;
483 off_t totalSize; 483 off_t totalSize;
484 int res; 484 int res = DMERR_OK;
485 485
486 // Initialize 486 // Initialize
487 dmInitProg("data2inc", "Data to include file converter", "0.7", NULL, NULL); 487 dmInitProg("data2inc", "Data to include file converter", "0.7", NULL, NULL);
488 dmVerbosity = 0; 488 dmVerbosity = 0;
489 489
490 // Parse arguments 490 // Parse arguments
491 if (!dmArgsProcess(argc, argv, optList, optListN, 491 if (!dmArgsProcess(argc, argv, optList, optListN,
492 argHandleOpt, argHandleFile, OPTH_BAILOUT)) 492 argHandleOpt, argHandleFile, OPTH_BAILOUT))
493 exit(1); 493 goto out;
494 494
495 // Determine output type, if not specified 495 // Determine output type, if not specified
496 if (setFormat == NULL) 496 if (setFormat == NULL)
497 { 497 {
498 if (optOutFilename == NULL) 498 if (optOutFilename == NULL)
499 { 499 {
500 dmErrorMsg("Output format not specified and no output filename given (try --help)\n"); 500 argShowHelp();
501 goto exit; 501 res = dmError(DMERR_INVALID_ARGS,
502 "Output format not specified and no output filename given.\n");
503 goto out;
502 } 504 }
503 505
504 if ((setFormat = dmGuessFormatFromName(optOutFilename)) == NULL) 506 if ((setFormat = dmGuessFormatFromName(optOutFilename)) == NULL)
505 { 507 {
506 dmErrorMsg("Could not guess output format from filename '%s'.\n", 508 dmErrorMsg("Could not guess output format from filename '%s'.\n",
507 optOutFilename); 509 optOutFilename);
508 goto exit; 510 goto out;
509 } 511 }
510 512
511 dmMsg(0, "Guessed output format: %s (%s)\n", 513 dmMsg(0, "Guessed output format: %s (%s)\n",
512 setFormat->desc, setFormat->name); 514 setFormat->desc, setFormat->name);
513 } 515 }
523 if ((inFile = fopen(optInFilename, "rb")) == NULL) 525 if ((inFile = fopen(optInFilename, "rb")) == NULL)
524 { 526 {
525 res = dmGetErrno(); 527 res = dmGetErrno();
526 dmErrorMsg("Error opening input file '%s'. (%s)\n", 528 dmErrorMsg("Error opening input file '%s'. (%s)\n",
527 optInFilename, dmErrorStr(res)); 529 optInFilename, dmErrorStr(res));
528 goto exit; 530 goto out;
529 } 531 }
530 532
531 if (optOutFilename == NULL) 533 if (optOutFilename == NULL)
532 outFile = stdout; 534 outFile = stdout;
533 else 535 else
534 if ((outFile = fopen(optOutFilename, "wa")) == NULL) 536 if ((outFile = fopen(optOutFilename, "wa")) == NULL)
535 { 537 {
536 res = dmGetErrno(); 538 res = dmGetErrno();
537 dmErrorMsg("Error creating output file '%s'. (%s)\n", 539 dmErrorMsg("Error creating output file '%s'. (%s)\n",
538 optOutFilename, dmErrorStr(res)); 540 optOutFilename, dmErrorStr(res));
539 goto exit; 541 goto out;
540 } 542 }
541 543
542 // Allocate linebuffer 544 // Allocate linebuffer
543 dataSize = optLineLen * sizeof(Uint8); 545 dataSize = optLineLen * sizeof(Uint8);
544 if ((dataBuf = dmMalloc(dataSize)) == NULL) 546 if ((dataBuf = dmMalloc(dataSize)) == NULL)
545 { 547 {
546 dmErrorMsg("Could not allocate %" DM_PRIu_SIZE_T " byte buffer.\n", 548 dmErrorMsg("Could not allocate %" DM_PRIu_SIZE_T " byte buffer.\n",
547 dataSize); 549 dataSize);
548 goto exit; 550 goto out;
549 } 551 }
550 552
551 // Get sourcefile size 553 // Get sourcefile size
552 totalSize = dmGetFileSize(inFile); 554 totalSize = dmGetFileSize(inFile);
553 555
556 (res = setFormat->initContext(&ctx)) != DMERR_OK) 558 (res = setFormat->initContext(&ctx)) != DMERR_OK)
557 { 559 {
558 dmErrorMsg("Error initializing format %s (%s) context: %s\n", 560 dmErrorMsg("Error initializing format %s (%s) context: %s\n",
559 setFormat->name, setFormat->desc, 561 setFormat->name, setFormat->desc,
560 dmErrorStr(res)); 562 dmErrorStr(res));
561 goto exit; 563 goto out;
562 } 564 }
563 565
564 // Output header 566 // Output header
565 if (!optQuiet && 567 if (!optQuiet &&
566 (res = setFormat->writeHeader(outFile, ctx, optOutFilename)) != DMERR_OK) 568 (res = setFormat->writeHeader(outFile, ctx, optOutFilename)) != DMERR_OK)
567 { 569 {
568 dmErrorMsg("Error writing output header: %s\n", 570 dmErrorMsg("Error writing output header: %s\n",
569 dmErrorStr(res)); 571 dmErrorStr(res));
570 goto exit; 572 goto out;
571 } 573 }
572 574
573 if (optAddLine) 575 if (optAddLine)
574 fprintf(outFile, "%s\n", optAddLine); 576 fprintf(outFile, "%s\n", optAddLine);
575 577
577 if (setFormat->writeDecl != NULL && 579 if (setFormat->writeDecl != NULL &&
578 (res = setFormat->writeDecl(outFile, ctx, totalSize, optObjName)) != DMERR_OK) 580 (res = setFormat->writeDecl(outFile, ctx, totalSize, optObjName)) != DMERR_OK)
579 { 581 {
580 dmErrorMsg("Error writing output declaration: %s\n", 582 dmErrorMsg("Error writing output declaration: %s\n",
581 dmErrorStr(res)); 583 dmErrorStr(res));
582 goto exit; 584 goto out;
583 } 585 }
584 586
585 // Output data 587 // Output data
586 while (!feof(inFile)) 588 while (!feof(inFile))
587 { 589 {
589 if (res > 0 && 591 if (res > 0 &&
590 (res = setFormat->writeData(outFile, ctx, dataBuf, res)) != DMERR_OK) 592 (res = setFormat->writeData(outFile, ctx, dataBuf, res)) != DMERR_OK)
591 { 593 {
592 dmErrorMsg("Error writing output data: %s\n", 594 dmErrorMsg("Error writing output data: %s\n",
593 dmErrorStr(res)); 595 dmErrorStr(res));
594 goto exit; 596 goto out;
595 } 597 }
596 } 598 }
597 599
598 // Output footer 600 // Output footer
599 if (setFormat->writeFooter != NULL && 601 if (setFormat->writeFooter != NULL &&
600 (res = setFormat->writeFooter(outFile, ctx, totalSize, optObjName)) != DMERR_OK) 602 (res = setFormat->writeFooter(outFile, ctx, totalSize, optObjName)) != DMERR_OK)
601 { 603 {
602 dmErrorMsg("Error writing output footer: %s\n", 604 dmErrorMsg("Error writing output footer: %s\n",
603 dmErrorStr(res)); 605 dmErrorStr(res));
604 goto exit; 606 goto out;
605 } 607 }
606 608
607 exit: 609 out:
608 // Cleanup 610 // Cleanup
609 if (inFile != NULL) 611 if (inFile != NULL)
610 fclose(inFile); 612 fclose(inFile);
611 613
612 if (outFile != NULL) 614 if (outFile != NULL)
621 dmErrorMsg("Error closing format %s (%s) context: %s\n", 623 dmErrorMsg("Error closing format %s (%s) context: %s\n",
622 setFormat->name, setFormat->desc, 624 setFormat->name, setFormat->desc,
623 dmErrorStr(res)); 625 dmErrorStr(res));
624 } 626 }
625 627
626 return 0; 628 return res;
627 } 629 }