changeset 264:5afa6052f796

Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for fields, in order to allow multiple values for the same field, for example same (sub)tune might have multiple ARTIST fields designating different artists.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 05 Jan 2020 23:51:04 +0200
parents 0b66189c73d7
children 05bbe428a0ab
files sidlib.c sidlib.h
diffstat 2 files changed, 40 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/sidlib.c	Sun Jan 05 23:48:05 2020 +0200
+++ b/sidlib.c	Sun Jan 05 23:51:04 2020 +0200
@@ -594,7 +594,9 @@
 }
 
 
-static int sidlib_stildb_entry_realloc(SIDLibSTILNode *node, const int nsubtune, const BOOL alloc)
+static int sidlib_stildb_entry_realloc(SIDLibSTILNode *node,
+    const int nsubtune, const BOOL alloc,
+    const int nfield, const char *fdata)
 {
     if (node == NULL)
         return THERR_NULLPTR;
@@ -605,7 +607,7 @@
         size_t clearIndex, clearLength;
 
         node->subtunes = (SIDLibSTILSubTune **) th_realloc(
-            node->subtunes, (nsubtune + 1) * sizeof(SIDLibSTILSubTune **));
+            node->subtunes, (nsubtune + 1) * sizeof(SIDLibSTILSubTune *));
 
         if (node->subtunes == NULL)
             return THERR_MALLOC;
@@ -622,7 +624,7 @@
             clearLength = nsubtune - clearIndex + 1;
         }
 
-        memset(&(node->subtunes[clearIndex]), 0, clearLength * sizeof(SIDLibSTILSubTune **));
+        memset(&(node->subtunes[clearIndex]), 0, clearLength * sizeof(SIDLibSTILSubTune *));
 
         node->nsubtunes = nsubtune;
     }
@@ -633,6 +635,19 @@
         node->subtunes[nsubtune] = (SIDLibSTILSubTune *) th_malloc0(sizeof(SIDLibSTILSubTune));
         if (node->subtunes[nsubtune] == NULL)
             return THERR_MALLOC;
+
+    }
+
+    // Allocate field space, if needed
+    if (alloc && nfield >= 0 && nfield < STF_LAST)
+    {
+        SIDLibSTILField *field = &node->subtunes[nsubtune]->fields[nfield];
+
+        if ((field->data = th_realloc(field->data, (field->ndata + 1) * sizeof(char *))) == NULL)
+            return THERR_MALLOC;
+
+        field->data[field->ndata] = th_strdup(fdata);
+        field->ndata++;
     }
 
     return THERR_OK;
@@ -643,15 +658,21 @@
 {
     if (node != NULL)
     {
-        for (int i = 0; i <= node->nsubtunes; i++)
+        for (int nsubtune = 0; nsubtune <= node->nsubtunes; nsubtune++)
         {
-            SIDLibSTILSubTune *subtune = node->subtunes[i];
+            SIDLibSTILSubTune *subtune = node->subtunes[nsubtune];
             if (subtune != NULL)
             {
-                for (int field = 0; field < STF_LAST; field++)
-                    th_free(subtune->fields[field]);
+                for (int nfield = 0; nfield < STF_LAST; nfield++)
+                {
+                    SIDLibSTILField *field = &subtune->fields[nfield];
+                    for (int n = 0; n < field->ndata; n++)
+                        th_free(field->data[n]);
 
-                th_free(subtune
+                    th_free(field->data);
+                }
+
+                th_free(subtune);
             }
         }
 
@@ -675,7 +696,7 @@
     if ((node->filename = th_strdup(filename)) == NULL)
         return THERR_MALLOC;
 
-    if ((res = sidlib_stildb_entry_realloc(node, 1, FALSE)) != THERR_OK)
+    if ((res = sidlib_stildb_entry_realloc(node, 1, FALSE, -1, NULL)) != THERR_OK)
     {
         sidlib_stildb_node_free(node);
         return res;
@@ -1025,21 +1046,15 @@
 
                 if (field >= 0 && subtune >= 0)
                 {
-                    char *data;
-
                     // Supported field, add it
-                    if ((ret = sidlib_stildb_entry_realloc(entry, subtune, TRUE)) != THERR_OK)
-                        goto out;
-
-                    if ((data = th_strdup(tmpStr)) == NULL)
+                    if ((ret = sidlib_stildb_entry_realloc(
+                        entry, subtune, TRUE, field, tmpStr)) != THERR_OK)
                     {
                         ret = th_io_error(fh, THERR_MALLOC,
                             "Could not allocate memory for field '%s' on line #%d.",
                             fieldName, fh->line);
                         goto out;
                     }
-
-                    entry->subtunes[subtune]->fields[field] = data;
                 }
 
                 sidlib_stildb_set_parsemode(&ctx, PM_IDLE);
--- a/sidlib.h	Sun Jan 05 23:48:05 2020 +0200
+++ b/sidlib.h	Sun Jan 05 23:51:04 2020 +0200
@@ -83,7 +83,14 @@
 
 typedef struct
 {
-    char *fields[STF_LAST];
+    int ndata;
+    char **data;
+} SIDLibSTILField;
+
+
+typedef struct
+{
+    SIDLibSTILField fields[STF_LAST];
 } SIDLibSTILSubTune;