# HG changeset patch # User Matti Hamalainen # Date 1587989799 -10800 # Node ID f5848606d5ad05106e97132a0735219c5af8be9b # Parent fd02e78f6fdc3d0104fc4005683f1a78e90d8e65 Improve error handling. diff -r fd02e78f6fdc -r f5848606d5ad tools/xm2jss.c --- a/tools/xm2jss.c Mon Apr 27 03:15:19 2020 +0300 +++ b/tools/xm2jss.c Mon Apr 27 15:16:39 2020 +0300 @@ -1286,8 +1286,8 @@ { DMResource *inFile = NULL; FILE *outFile = NULL; - JSSModule *sm, *dm; - int result; + JSSModule *sm = NULL, *dm = NULL; + int res = DMERR_OK; dmInitProg("xm2jss", "XM to JSSMOD converter", "0.8", NULL, NULL); dmVerbosity = 0; @@ -1300,16 +1300,17 @@ // Check arguments if (optInFilename == NULL || optOutFilename == NULL) { - dmErrorMsg("Input or output file not specified. Try --help.\n"); - return 1; + res = dmError(DMERR_INVALID_ARGS, + "Input or output file not specified. Try --help.\n"); + goto out; } // Read the source file - if ((result = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) + if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) { dmErrorMsg("Error opening input file '%s', %d: %s\n", - optInFilename, result, dmErrorStr(result)); - return 1; + optInFilename, res, dmErrorStr(res)); + goto out; } // Initialize miniJSS @@ -1317,17 +1318,17 @@ // Read file dmMsg(1, "Reading XM-format file ...\n"); - result = jssLoadXM(inFile, &sm, FALSE); + res = jssLoadXM(inFile, &sm, FALSE); dmf_close(inFile); - if (result != 0) + if (res != 0) { - dmErrorMsg("Error while loading XM file (%d), ", result); + dmErrorMsg("Error while loading XM file (%d), ", res); if (optIgnoreErrors) fprintf(stderr, "ignoring. This may cause problems.\n"); else { fprintf(stderr, "giving up. Use --ignore if you want to try to convert anyway.\n"); - return 2; + goto out; } } @@ -1338,10 +1339,8 @@ // Remove samples if (optStripSamples) { - int i; - dmMsg(1, "Stripping samples...\n"); - for (i = 0; i < sm->ninstruments; i++) + for (int i = 0; i < sm->ninstruments; i++) { dmFree(sm->instruments[i]->data); sm->instruments[i]->data = NULL; @@ -1351,10 +1350,8 @@ // Remove instruments if (optStripInstr) { - int i; - dmMsg(1, "Stripping instruments...\n"); - for (i = 0; i < sm->ninstruments; i++) + for (int i = 0; i < sm->ninstruments; i++) { dmFree(sm->instruments[i]); sm->instruments[i] = NULL; @@ -1381,7 +1378,8 @@ { dmMsg(1, "Optimizing module data...\n"); dm = jssOptimizeModule(sm); - } else + } + else dm = sm; // Write output file @@ -1396,22 +1394,25 @@ dmMsg(1, "Writing JSSMOD-format file [patMode=0x%04x, samp8=0x%02x, samp16=0x%02x]\n", optPatternMode, optSampMode8, optSampMode16); - result = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16); - - dmFree(sm); + res = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16); fclose(outFile); - if (result != 0) + if (res != 0) { dmErrorMsg( - "Error while saving JSSMOD file, %d: %s\n" + "Error while saving JSSMOD file: %s\n" "WARNING: The resulting file may be broken!\n", - result, dmErrorStr(result)); + dmErrorStr(res)); } else { dmMsg(1, "Conversion complete.\n"); } - return 0; + +out: + jssFreeModule(sm); + dmFree(dm); + + return res; }