changeset 2473:f5848606d5ad

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Apr 2020 15:16:39 +0300
parents fd02e78f6fdc
children e2e94f9afe1b
files tools/xm2jss.c
diffstat 1 files changed, 26 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- 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;
 }