changeset 2474:e2e94f9afe1b

Various cleanups, use saner variable names.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Apr 2020 18:08:39 +0300
parents f5848606d5ad
children b9734859db5c
files tools/xm2jss.c
diffstat 1 files changed, 117 insertions(+), 119 deletions(-) [+]
line wrap: on
line diff
--- a/tools/xm2jss.c	Mon Apr 27 15:16:39 2020 +0300
+++ b/tools/xm2jss.c	Mon Apr 27 18:08:39 2020 +0300
@@ -917,7 +917,7 @@
 
 /* Optimize a given module
  */
-JSSModule *jssOptimizeModule(JSSModule *m)
+JSSModule *jssOptimizeModule(JSSModule *src)
 {
     BOOL usedPatterns[jsetMaxPatterns + 1],
          usedInstruments[jsetMaxInstruments + 1],
@@ -927,28 +927,28 @@
          mapPatterns[jsetMaxPatterns + 1],
          dupPatterns[jsetMaxPatterns + 1];
 
-    JSSModule *r = NULL;
+    JSSModule *dst = NULL;
     int n8, n16, nunused, ndupes;
 
     // Allocate a new module
-    if ((r = jssAllocateModule()) == NULL)
+    if ((dst = jssAllocateModule()) == NULL)
         return NULL;
 
     // Copy things
-    r->moduleType       = m->moduleType;
-    r->moduleName       = dm_strdup(m->moduleName);
-    r->trackerName      = dm_strdup(m->trackerName);
-    r->defSpeed         = m->defSpeed;
-    r->defTempo         = m->defTempo;
-    r->defFlags         = m->defFlags;
-    r->defRestartPos    = m->defRestartPos;
-    r->intVersion       = m->intVersion;
-    r->nchannels        = m->nchannels;
-    r->norders          = m->norders;
+    dst->moduleType       = src->moduleType;
+    dst->moduleName       = src->moduleName;
+    dst->trackerName      = src->trackerName;
+    dst->defSpeed         = src->defSpeed;
+    dst->defTempo         = src->defTempo;
+    dst->defFlags         = src->defFlags;
+    dst->defRestartPos    = src->defRestartPos;
+    dst->intVersion       = src->intVersion;
+    dst->nchannels        = src->nchannels;
+    dst->norders          = src->norders;
 
     for (int i = 0; i < jsetNChannels; i++)
     {
-        r->defPanning[i] = m->defPanning[i];
+        dst->defPanning[i] = src->defPanning[i];
     }
 
     // Initialize values
@@ -972,21 +972,21 @@
     // by going through all patterns specified in the order list
     //
     dmMsg(1, "Scanning patterns for used instruments and channels...\n");
-    for (int norder = 0; norder < m->norders; norder++)
+    for (int norder = 0; norder < src->norders; norder++)
     {
-        int npat = m->orderList[norder];
-        if (npat >= 0 && npat < m->npatterns)
+        int npat = src->orderList[norder];
+        if (npat >= 0 && npat < src->npatterns)
         {
-            JSSPattern *pattern = m->patterns[npat];
+            JSSPattern *pattern = src->patterns[npat];
             if (pattern != NULL)
             {
                 // Scan for used instruments etc
-                BOOL empty = jssScanPattern(m, pattern, npat, usedExtInstruments, NULL);
+                BOOL empty = jssScanPattern(src, pattern, npat, usedExtInstruments, NULL);
 
                 // Empty patterns with known number of rows are "removed"
                 if (empty && pattern->nrows == jsetDefaultRows)
                 {
-                    m->orderList[norder] = jsetNotSet;
+                    src->orderList[norder] = jsetNotSet;
                     usedPatterns[npat] = FALSE;
                 }
                 else
@@ -998,7 +998,7 @@
                     npat, norder);
 
                 // Fix it.
-                m->orderList[norder] = jsetNotSet;
+                src->orderList[norder] = jsetNotSet;
             }
         }
         else
@@ -1008,7 +1008,7 @@
                 norder, npat);
 
             // Fix it.
-            m->orderList[norder] = jsetNotSet;
+            src->orderList[norder] = jsetNotSet;
         }
     }
 
@@ -1016,23 +1016,23 @@
     // Find used sample instruments
     //
     dmMsg(1, "Checking ext.instruments for used sample instruments...\n");
-    for (int i = 0; i < jsetMaxInstruments; i++)
-    if (usedExtInstruments[i] && m->extInstruments[i] != NULL)
+    for (int neinst = 0; neinst < jsetMaxInstruments; neinst++)
+    if (usedExtInstruments[neinst] && src->extInstruments[neinst] != NULL)
     {
-        JSSExtInstrument *e = m->extInstruments[i];
+        JSSExtInstrument *eip = src->extInstruments[neinst];
 
         for (int note = 0; note < jsetNNotes; note++)
-        if (e->sNumForNotes[note] != jsetNotSet)
+        if (eip->sNumForNotes[note] != jsetNotSet)
         {
-            int q = e->sNumForNotes[note];
-            if (q >= 0 && q < m->ninstruments)
+            int q = eip->sNumForNotes[note];
+            if (q >= 0 && q < src->ninstruments)
             {
                 usedInstruments[q] = TRUE;
             }
             else
             {
                 dmErrorMsg("Ext.instrument #%d sNumForNotes[%d] value out range (%d < %d).\n",
-                    i + 1, note, m->ninstruments, q + 1);
+                    neinst + 1, note, src->ninstruments, q + 1);
             }
         }
     }
@@ -1046,13 +1046,13 @@
     if (usedPatterns[pat1])
     {
         // Sanity check patterns
-        if (pat1 >= m->npatterns)
+        if (pat1 >= src->npatterns)
         {
-            dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", pat1, m->npatterns);
+            dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", pat1, src->npatterns);
             continue;
         }
 
-        if (m->patterns[pat1] == NULL)
+        if (src->patterns[pat1] == NULL)
         {
             dmErrorMsg("Pattern 0x%x used but is NULL.\n", pat1);
             continue;
@@ -1066,57 +1066,57 @@
         }
 
         // Check for duplicate patterns of "pat1" and mark them as such
-        for (int pat2 = 0; pat2 < m->npatterns; pat2++)
-        if (pat1 != pat2 && m->patterns[pat2] != NULL &&
+        for (int pat2 = 0; pat2 < src->npatterns; pat2++)
+        if (pat1 != pat2 && src->patterns[pat2] != NULL &&
             dupPatterns[pat2] == jsetNotSet &&
-            jssComparePattern(m->patterns[pat1], m->patterns[pat2]))
+            jssComparePattern(src->patterns[pat1], src->patterns[pat2]))
         {
             dmPrint(1, " * %d and %d are dupes.\n", pat1, pat2);
             dupPatterns[pat2] = pat1;
             ndupes++;
         }
 
-        mapPatterns[pat1] = r->npatterns;
-        r->patterns[r->npatterns] = m->patterns[pat1];
-        (r->npatterns)++;
+        mapPatterns[pat1] = dst->npatterns;
+        dst->patterns[dst->npatterns] = src->patterns[pat1];
+        (dst->npatterns)++;
     }
     else
-    if (m->patterns[pat1] != NULL)
+    if (src->patterns[pat1] != NULL)
         nunused++;
 
     dmMsg(1, "%d used patterns (%d unused, %d duplicates).\n",
-        r->npatterns, nunused, ndupes);
+        dst->npatterns, nunused, ndupes);
 
     //
     // Re-map instruments
     //
     dmMsg(1, "Creating sample instrument remaps...\n");
     nunused = n8 = n16 = 0;
-    for (int i = 0; i < jsetMaxInstruments; i++)
-    if (usedInstruments[i])
+    for (int ninst = 0; ninst < jsetMaxInstruments; ninst++)
+    if (usedInstruments[ninst])
     {
         JSSInstrument *ip;
         if (optStripInstr)
             continue;
 
-        if (i >= m->ninstruments)
+        if (ninst >= src->ninstruments)
         {
             dmErrorMsg("Instrument 0x%x >= 0x%x, but used!\n",
-                i + 1, m->ninstruments);
+                ninst + 1, src->ninstruments);
             continue;
         }
 
-        if ((ip = m->instruments[i]) == NULL)
+        if ((ip = src->instruments[ninst]) == NULL)
         {
-            dmErrorMsg("Instrument 0x%x used but is NULL.\n", i + 1);
+            dmErrorMsg("Instrument 0x%x used but is NULL.\n", ninst + 1);
             continue;
         }
 
-        dmPrint(2, "%02x -> %02x : ", i + 1, r->ninstruments + 1);
+        dmPrint(2, "%02x -> %02x : ", ninst + 1, dst->ninstruments + 1);
 
-        mapInstruments[i] = r->ninstruments;
-        r->instruments[r->ninstruments] = ip;
-        (r->ninstruments)++;
+        mapInstruments[ninst] = dst->ninstruments;
+        dst->instruments[dst->ninstruments] = ip;
+        (dst->ninstruments)++;
 
         if (ip->flags & jsf16bit)
             n16++;
@@ -1124,44 +1124,44 @@
             n8++;
     }
     else
-    if (m->instruments[i] != NULL)
+    if (src->instruments[ninst] != NULL)
         nunused++;
 
     dmPrint(2, "\n");
     dmMsg(1, "Total of %d [16-bit] + %d [8-bit] samples = %d instruments (%d unused).\n",
-        n16, n8, r->ninstruments, nunused);
+        n16, n8, dst->ninstruments, nunused);
 
     //
     // Re-map ext.instruments
     //
     dmMsg(1, "Creating ext.instrument remaps...\n");
     nunused = 0;
-    for (int i = 0; i < jsetMaxInstruments; i++)
-    if (usedExtInstruments[i])
+    for (int neinst = 0; neinst < jsetMaxInstruments; neinst++)
+    if (usedExtInstruments[neinst])
     {
         JSSExtInstrument *eip;
 
         if (optStripExtInstr)
             continue;
 
-        if (i >= m->nextInstruments)
+        if (neinst >= src->nextInstruments)
         {
             dmErrorMsg("Ext.instrument 0x%x >= 0x%x, but used!\n",
-                i + 1, m->nextInstruments);
+                neinst + 1, src->nextInstruments);
             continue;
         }
 
-        if ((eip = m->extInstruments[i]) == NULL)
+        if ((eip = src->extInstruments[neinst]) == NULL)
         {
-            dmErrorMsg("Extended instrument 0x%x used but is NULL.\n", i + 1);
+            dmErrorMsg("Extended instrument 0x%x used but is NULL.\n", neinst + 1);
             continue;
         }
 
-        dmPrint(2, "%02x -> %02x : ", i + 1, r->nextInstruments + 1);
+        dmPrint(2, "%02x -> %02x : ", neinst + 1, dst->nextInstruments + 1);
 
-        mapExtInstruments[i] = r->nextInstruments;
-        r->extInstruments[r->nextInstruments] = eip;
-        (r->nextInstruments)++;
+        mapExtInstruments[neinst] = dst->nextInstruments;
+        dst->extInstruments[dst->nextInstruments] = eip;
+        (dst->nextInstruments)++;
 
         // Re-map sNumForNotes table for this ext.instrument
         for (int note = 0; note < jsetNNotes; note++)
@@ -1178,7 +1178,7 @@
                 {
                     map = jsetNotSet;
                     dmErrorMsg("Einst=%d, note=%d, sNumForNote=%d (%d max)\n",
-                        i + 1, note, q + 1, r->ninstruments);
+                        neinst + 1, note, q + 1, dst->ninstruments);
                 }
 
                 dmPrint(3, "%02x.%02x ", q + 1, map + 1);
@@ -1187,50 +1187,50 @@
         }
     }
     else
-    if (m->extInstruments[i] != NULL)
+    if (src->extInstruments[neinst] != NULL)
         nunused++;
 
     dmPrint(2, "\n");
     dmMsg(1, "%d extended instruments (%d unused).\n",
-        r->nextInstruments, nunused);
+        dst->nextInstruments, nunused);
 
     //
     // Remap pattern data with remapped instrument data
     //
-    for (int i = 0; i < r->npatterns; i++)
+    for (int npat = 0; npat < dst->npatterns; npat++)
     {
-        JSSPattern *p = r->patterns[i];
-        JSSNote *n = p->data;
+        JSSPattern *pat = dst->patterns[npat];
+        JSSNote *note = pat->data;
 
-        for (int row = 0; row < p->nrows; row++)
-        for (int channel = 0; channel < p->nchannels; channel++, n++)
+        for (int row = 0; row < pat->nrows; row++)
+        for (int channel = 0; channel < pat->nchannels; channel++, note++)
         {
             // If not stripping extended instruments, check for
             // the validity of the used instrument and remap
             if (!optStripExtInstr)
             {
-                if (n->instrument >= 0 && n->instrument < jsetMaxInstruments)
-                    n->instrument = mapExtInstruments[n->instrument];
+                if (note->instrument >= 0 && note->instrument < jsetMaxInstruments)
+                    note->instrument = mapExtInstruments[note->instrument];
 
-                if (n->instrument != jsetNotSet &&
-                    r->extInstruments[n->instrument] == NULL)
+                if (note->instrument != jsetNotSet &&
+                    dst->extInstruments[note->instrument] == NULL)
                 {
                     dmErrorMsg("Non-existing instrument used #%d, INTERNAL ERROR.\n",
-                        n->instrument + 1);
+                        note->instrument + 1);
                 }
             }
 
             // Convert certain effects
             char effect;
-            JMPGETEFFECT(effect, n->effect);
+            JMPGETEFFECT(effect, note->effect);
             switch (effect)
             {
                 case 'C': // Cxx = Set volume
-                    if (n->volume == jsetNotSet)
+                    if (note->volume == jsetNotSet)
                     {
-                        n->volume = n->param;
-                        n->effect = jsetNotSet;
-                        n->param = jsetNotSet;
+                        note->volume = note->param;
+                        note->effect = jsetNotSet;
+                        note->param = jsetNotSet;
                     }
                     break;
             }
@@ -1242,15 +1242,15 @@
     //
     dmMsg(1, "Remapping orders list.\n");
     nunused = 0;
-    for (int i = 0; i < m->norders; i++)
+    for (int nord = 0; nord < src->norders; nord++)
     {
-        int map = mapPatterns[m->orderList[i]];
-        if (map != m->orderList[i])
+        int map = mapPatterns[src->orderList[nord]];
+        if (map != src->orderList[nord])
         {
-            dmPrint(2, "%02x -> %02x : ", m->orderList[i], map);
+            dmPrint(2, "%02x -> %02x : ", src->orderList[nord], map);
             nunused++;
         }
-        r->orderList[i] = map;
+        dst->orderList[nord] = map;
     }
     if (nunused)
         dmPrint(2, "\n");
@@ -1258,27 +1258,27 @@
     //
     // Do final pass on patterns to remove unused channels
     //
-    for (int i = 0; i < r->npatterns; i++)
+    for (int npat = 0; npat < dst->npatterns; npat++)
     {
-        JSSPattern *p = r->patterns[i];
+        JSSPattern *pat = dst->patterns[npat];
 
-        jssScanPattern(r, p, i, NULL, p->used);
+        jssScanPattern(dst, pat, npat, NULL, pat->used);
 
-        p->nmap = 0;
-        for (int i = 0; i < r->nchannels; i++)
+        pat->nmap = 0;
+        for (int nchn = 0; nchn < dst->nchannels; nchn++)
         {
-            if (p->used[i])
-                p->map[p->nmap++] = i;
+            if (pat->used[nchn])
+                pat->map[pat->nmap++] = nchn;
         }
 
-        if (p->nmap != p->nchannels)
+        if (pat->nmap != pat->nchannels)
         {
             dmMsg(2, "Pattern %d: %d/%d used channels (%d unused).\n",
-                i, p->nchannels - p->nmap, p->nchannels, p->nmap);
+                npat, pat->nchannels - pat->nmap, pat->nchannels, pat->nmap);
         }
     }
 
-    return r;
+    return dst;
 }
 
 
@@ -1286,7 +1286,7 @@
 {
     DMResource *inFile = NULL;
     FILE *outFile = NULL;
-    JSSModule *sm = NULL, *dm = NULL;
+    JSSModule *src = NULL, *dst = NULL;
     int res = DMERR_OK;
 
     dmInitProg("xm2jss", "XM to JSSMOD converter", "0.8", NULL, NULL);
@@ -1318,7 +1318,7 @@
 
     // Read file
     dmMsg(1, "Reading XM-format file ...\n");
-    res = jssLoadXM(inFile, &sm, FALSE);
+    res = jssLoadXM(inFile, &src, FALSE);
     dmf_close(inFile);
     if (res != 0)
     {
@@ -1340,10 +1340,10 @@
     if (optStripSamples)
     {
         dmMsg(1, "Stripping samples...\n");
-        for (int i = 0; i < sm->ninstruments; i++)
+        for (int i = 0; i < src->ninstruments; i++)
         {
-            dmFree(sm->instruments[i]->data);
-            sm->instruments[i]->data = NULL;
+            dmFree(src->instruments[i]->data);
+            src->instruments[i]->data = NULL;
         }
     }
 
@@ -1351,50 +1351,48 @@
     if (optStripInstr)
     {
         dmMsg(1, "Stripping instruments...\n");
-        for (int i = 0; i < sm->ninstruments; i++)
+        for (int i = 0; i < src->ninstruments; i++)
         {
-            dmFree(sm->instruments[i]);
-            sm->instruments[i] = NULL;
+            dmFree(src->instruments[i]);
+            src->instruments[i] = NULL;
         }
-        sm->ninstruments = 0;
+        src->ninstruments = 0;
     }
 
     // Remove ext.instruments
     if (optStripExtInstr)
     {
-        int i;
-
         dmMsg(1, "Stripping ext.instruments...\n");
-        for (i = 0; i < sm->nextInstruments; i++)
+        for (int i = 0; i < src->nextInstruments; i++)
         {
-            dmFree(sm->extInstruments[i]);
-            sm->extInstruments[i] = NULL;
+            dmFree(src->extInstruments[i]);
+            src->extInstruments[i] = NULL;
         }
-        sm->nextInstruments = 0;
+        src->nextInstruments = 0;
     }
 
     // Run the optimization procedure
     if (optOptimize)
     {
         dmMsg(1, "Optimizing module data...\n");
-        dm = jssOptimizeModule(sm);
+        dst = jssOptimizeModule(src);
     }
     else
-        dm = sm;
+        dst = src;
 
     // Write output file
     if ((outFile = fopen(optOutFilename, "wb")) == NULL)
     {
-        int err = dmGetErrno();
-        dmErrorMsg("Error creating output file '%s', %d: %s\n",
-            optOutFilename, err, dmErrorStr(err));
-        return 1;
+        res = dmGetErrno();
+        dmErrorMsg("Error creating output file '%s': %s\n",
+            optOutFilename, dmErrorStr(res));
+        goto out;
     }
 
     dmMsg(1, "Writing JSSMOD-format file [patMode=0x%04x, samp8=0x%02x, samp16=0x%02x]\n",
         optPatternMode, optSampMode8, optSampMode16);
 
-    res = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16);
+    res = jssSaveJSSMOD(outFile, dst, optPatternMode, optSampMode8, optSampMode16);
 
     fclose(outFile);
 
@@ -1411,8 +1409,8 @@
     }
 
 out:
-    jssFreeModule(sm);
-    dmFree(dm);
+    jssFreeModule(src);
+    dmFree(dst);
     
     return res;
 }