diff tools/dumpmod.c @ 2530:aacf3bd1cceb

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 16 May 2020 06:38:52 +0300
parents bc05bcfc4598
children d56a0e86067a
line wrap: on
line diff
--- a/tools/dumpmod.c	Sat May 16 06:11:53 2020 +0300
+++ b/tools/dumpmod.c	Sat May 16 06:38:52 2020 +0300
@@ -424,9 +424,9 @@
 
 int main(int argc, char *argv[])
 {
-    int result = -1, i;
     DMResource *file = NULL;
     JSSModule *mod = NULL;
+    int res = 0;
 
     dmInitProg("dumpmod", "miniJSS Module Viewer", "0.4", NULL, NULL);
     dmVerbosity = 0;
@@ -434,7 +434,10 @@
     // Parse arguments
     if (!dmArgsProcess(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+    {
+        res = 1;
+        goto exit;
+    }
 
     // Initialize miniJSS
     jssInit();
@@ -442,50 +445,60 @@
     // Open the file
     dmMsg(1, "Reading module file '%s'\n", optFilename);
     if (optFilename == NULL)
-        result = dmf_open_stdio_stream(stdin, &file);
+        res = dmf_open_stdio_stream(stdin, &file);
     else
-        result = dmf_open_stdio(optFilename, "rb", &file);
+        res = dmf_open_stdio(optFilename, "rb", &file);
 
-    if (result != DMERR_OK)
+    if (res != DMERR_OK)
     {
-        dmErrorMsg("Error opening input file '%s', #%d: %s\n",
-            optFilename, result, dmErrorStr(result));
-        return 1;
+        res = dmError(DMERR_FOPEN,
+            "Error opening input file '%s': %s\n",
+            optFilename, dmErrorStr(res));
+        goto exit;
     }
 
     // Read module file
     dmMsg(1, "Reading file: %s\n", optFilename);
 #ifdef JSS_SUP_XM
-    result = jssLoadXM(file, &mod, TRUE);
+    if (mod == NULL)
+    {
+        dmMsg(2, "* Trying XM...\n");
+        dmfreset(file);
+        if ((res = jssLoadXM(file, &mod, TRUE)) == DMERR_OK)
+        {
+            dmfreset(file);
+            res = jssLoadXM(file, &mod, FALSE);
+        }
+    }
 #endif
 #ifdef JSS_SUP_JSSMOD
-    dmfreset(file);
-    if (result != DMERR_OK)
+    if (mod == NULL)
     {
         dmMsg(1, "* Trying JSSMOD ...\n");
-        result = jssLoadJSSMOD(file, &mod, TRUE);
         dmfreset(file);
-        if (result == DMERR_OK)
-            result = jssLoadJSSMOD(file, &mod, FALSE);
-    }
-    else
-    {
-        dmMsg(2, "* Trying XM...\n");
-        result = jssLoadXM(file, &mod, FALSE);
+        if ((res = jssLoadJSSMOD(file, &mod, TRUE)) == DMERR_OK)
+        {
+            dmfreset(file);
+            res = jssLoadJSSMOD(file, &mod, FALSE);
+        }
     }
 #endif
     dmf_close(file);
 
     // Check for errors, we still might have some data tho
-    if (result != DMERR_OK)
+    if (res != DMERR_OK)
     {
-        dmErrorMsg("Error loading module file, %d: %s\n",
-            result, dmErrorStr(result));
+        dmErrorMsg("Error loading module file: %s\n",
+            dmErrorStr(res));
     }
 
     // Check if we have anything
     if (mod == NULL)
-        return 3;
+    {
+        res = dmError(DMERR_INIT_FAIL,
+            "Could not load module file.\n");
+        goto exit;
+    }
 
     // Print out information
     if (optViewGeneralInfo)
@@ -493,7 +506,7 @@
 
     if (optViewPatterns)
     {
-        for (i = 0; i < mod->npatterns; i++)
+        for (int i = 0; i < mod->npatterns; i++)
         if (mod->patterns[i] != NULL)
         {
             printf("\nPattern #%02x:\n", i);
@@ -507,7 +520,7 @@
         "ExtInstruments:\n"
         "---------------\n"
         );
-        for (i = 0; i < mod->nextInstruments; i++)
+        for (int i = 0; i < mod->nextInstruments; i++)
         if (mod->extInstruments[i] != NULL)
         {
             printf("#%02x: ", i + 1);
@@ -521,7 +534,7 @@
         "Instruments:\n"
         "------------\n"
         );
-        for (i = 0; i < mod->ninstruments; i++)
+        for (int i = 0; i < mod->ninstruments; i++)
         if (mod->instruments[i] != NULL)
         {
             printf("#%02x: ", i + 1);
@@ -529,9 +542,10 @@
         }
     }
 
+exit:
     // Free module data
     jssFreeModule(mod);
     jssClose();
 
-    return 0;
+    return res;
 }