diff sidlib.c @ 88:c5ff71d64e53

More work on sidlib SLDB code.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Feb 2016 01:13:48 +0200
parents e1ff9cd27a84
children 681dfc3b006e
line wrap: on
line diff
--- a/sidlib.c	Fri Feb 12 00:13:27 2016 +0200
+++ b/sidlib.c	Fri Feb 12 01:13:48 2016 +0200
@@ -241,37 +241,12 @@
 }
 
 
-static int si_sldb_get_hex_value(const char *str, size_t *pos)
-{
-    int result = 0;
-
-    do
-    {
-        char ch = str[*pos];
-        result <<= 4;
-        if (ch >= 'A' && ch <= 'F')
-            result |= ch - 'A';
-        else
-        if (ch >= 'a' && ch <= 'f')
-            result |= ch - 'a';
-        else
-        if (ch >= '0' && ch <= '9')
-            result |= ch - '0';
-        else
-            break;
-    }
-    while (1);
-
-    return result;
-}
-
-
 static int si_sldb_gettime(const char *str, size_t *pos)
 {
     int result;
 
     // Check if it starts with a digit
-    if (isdigit(str[*pos]))
+    if (th_isdigit(str[*pos]))
     {
         // Get minutes-field
         result = si_sldb_get_value(str, pos) * 60;
@@ -290,7 +265,7 @@
         result = -1;
 
     // Ignore and skip the possible attributes
-    while (str[*pos] && !isspace(str[*pos]))
+    while (str[*pos] && !th_isspace(str[*pos]))
         (*pos)++;
 
     return result;
@@ -383,13 +358,27 @@
 }
 
 
+SIDLibSLDB * si_sldb_new(void)
+{
+    return (SIDLibSLDB *) th_malloc0(sizeof(SIDLibSLDB));
+}
+
+
 // Read SLDB database to memory
 //
 int si_sldb_read(th_ioctx *ctx, SIDLibSLDB *dbh)
 {
-    char line[PSID_BUFFER2_SIZE];
+    char *line = NULL;
 
-    while (thfgets(line, PSID_BUFFER2_SIZE, ctx) != NULL)
+    if ((line = th_malloc(PSID_BUFFER_SIZE)) == NULL)
+    {
+        th_io_error(ctx, THERR_MALLOC,
+            "Error allocating temporary data buffer of %d bytes.\n",
+            PSID_BUFFER_SIZE);
+        return ctx->errno;
+    }
+
+    while (thfgets(line, PSID_BUFFER_SIZE, ctx) != NULL)
     {
         SIDLibSLDBNode *tmnode;
         size_t pos = 0;
@@ -434,6 +423,7 @@
         }
     }
 
+    th_free(line);
     return THERR_OK;
 }
 
@@ -483,9 +473,9 @@
         size_t i;
 
         // Allocate memory for index-table
-        dbh->pindex = (SIDLibSLDBNode * *) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->n);
+        dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->n);
         if (dbh->pindex == NULL)
-            return -1;
+            return THERR_MALLOC;
 
         // Get node-pointers to table
         for (i = 0, node = dbh->nodes; node && i < dbh->n; node = node->next)
@@ -495,7 +485,7 @@
         qsort(dbh->pindex, dbh->n, sizeof(SIDLibSLDBNode *), si_sldb_compare_nodes);
     }
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -503,19 +493,22 @@
 //
 void si_sldb_free(SIDLibSLDB *dbh)
 {
-    SIDLibSLDBNode *node = dbh->nodes;
-    while (node != NULL)
+    if (dbh != NULL)
     {
-        SIDLibSLDBNode *next = node->next;
-        si_sldb_node_free(node);
-        node = next;
+        SIDLibSLDBNode *node = dbh->nodes;
+        while (node != NULL)
+        {
+            SIDLibSLDBNode *next = node->next;
+            si_sldb_node_free(node);
+            node = next;
+        }
+
+        dbh->nodes = NULL;
+        dbh->n = 0;
+
+        th_free_r(&dbh->pindex);
+        th_free(dbh);
     }
-
-    dbh->nodes = NULL;
-    dbh->n = 0;
-
-    th_free_r(&dbh->pindex);
-    th_free(dbh);
 }
 
 
@@ -523,7 +516,7 @@
 {
     SIDLibSLDBNode keyItem, *key, **item;
 
-    memcpy(&keyItem.hash, &hash, sizeof(th_md5hash_t));
+    memcpy(&keyItem.hash, hash, sizeof(th_md5hash_t));
     key = &keyItem;
     item = bsearch(&key, dbh->pindex, dbh->n, sizeof(dbh->pindex[0]), si_sldb_compare_nodes);