changeset 514:db3fc3d4969e

Return proper THERR_* error codes from th_config functions, at least for some parts.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 26 Dec 2019 09:45:41 +0200
parents e412a39e2b7a
children d512555bdd0f
files th_config.c
diffstat 1 files changed, 66 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/th_config.c	Thu Dec 26 09:10:07 2019 +0200
+++ b/th_config.c	Thu Dec 26 09:45:41 2019 +0200
@@ -70,11 +70,11 @@
 
     node = th_cfg_add(cfg, name, ITEM_INT, (void *) itemData);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
     *itemData = defValue;
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -85,11 +85,11 @@
 
     node = th_cfg_add(cfg, name, ITEM_HEX_TRIPLET, (void *) itemData);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
     *itemData = defValue;
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -102,11 +102,11 @@
 
     node = th_cfg_add(cfg, name, ITEM_UINT, (void *) itemData);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
     *itemData = defValue;
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -119,11 +119,11 @@
 
     node = th_cfg_add(cfg, name, ITEM_STRING, (void *) itemData);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
     *itemData = th_strdup(defValue);
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -136,11 +136,11 @@
 
     node = th_cfg_add(cfg, name, ITEM_BOOL, (void *) itemData);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
     *itemData = defValue;
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -152,9 +152,9 @@
 
     node = th_cfg_add(cfg, comment, ITEM_COMMENT, NULL);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -166,9 +166,9 @@
 
     node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) sect);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -177,13 +177,13 @@
     th_cfgitem_t *node;
 
     if (data == NULL)
-        return -5;
+        return THERR_NULLPTR;
 
     node = th_cfg_add(cfg, name, ITEM_STRING_LIST, (void *) data);
     if (node == NULL)
-        return -1;
+        return THERR_MALLOC;
 
-    return 0;
+    return THERR_OK;
 }
 
 
@@ -271,14 +271,14 @@
 }
 
 
-static BOOL th_cfg_set_item(th_cfgparserctx_t *ctx, th_cfgitem_t *item, const char *str)
+static void th_cfg_set_item(th_cfgparserctx_t *ctx, th_cfgitem_t *item, const char *str)
 {
-    BOOL ret = TRUE;
+    BOOL res = TRUE;
 
     switch (item->type)
     {
         case ITEM_HEX_TRIPLET:
-            ret  = th_get_hex_triplet(str, item->v.val_uint);
+            res  = th_get_hex_triplet(str, item->v.val_uint);
             break;
 
         case ITEM_STRING:
@@ -287,16 +287,18 @@
 
         case ITEM_STRING_LIST:
             {
-                char *tmps;
-                if ((tmps = th_strdup(str)) == NULL)
-                    ret = FALSE;
-                else
+                char *tmp;
+                if ((tmp = th_strdup(str)) != NULL)
                 {
-                    th_llist_append(item->v.list, tmps);
+                    th_llist_append(item->v.list, tmp);
                     th_cfg_set_next_parsemode(ctx, PM_LIST);
+                    // Early exit as we set the parsemode here
+                    return;
                 }
+                else
+                    res = FALSE;
             }
-            return ret;
+            break;
 
         case ITEM_INT:
             *(item->v.val_int) = atoi(str);
@@ -311,16 +313,15 @@
             break;
 
         case ITEM_BOOL:
-            ret = th_get_boolean(str, item->v.val_bool);
+            res = th_get_boolean(str, item->v.val_bool);
             break;
 
         default:
-            ret = FALSE;
+            res = FALSE;
             break;
     }
 
-    th_cfg_set_parsemode(ctx, ret ? PM_IDLE : PM_ERROR);
-    return ret;
+    th_cfg_set_parsemode(ctx, res ? PM_IDLE : PM_ERROR);
 }
 
 
@@ -330,14 +331,14 @@
     th_cfgitem_t *item = NULL;
     char *tmpStr = NULL;
     size_t strPos;
-    BOOL isEscaped, isFound, isStart, isError, validError, fpSet;
+    BOOL isEscaped, isStart, isError, fpSet;
 
     // Initialize values
     memset(&ctx, 0, sizeof(ctx));
     ctx.ch = -1;
+    ctx.nextMode = ctx.prevMode = ctx.parseMode = PM_IDLE;
+    isEscaped = fpSet = isStart = isError = FALSE;
     strPos = 0;
-    ctx.nextMode = ctx.prevMode = ctx.parseMode = PM_IDLE;
-    isEscaped = fpSet = isFound = isStart = isError = validError = FALSE;
 
     if ((tmpStr = th_malloc(SET_MAX_BUF + 1)) == NULL)
         goto out;
@@ -463,19 +464,18 @@
             if (ctx.ch == '=')
             {
                 // Find key from configuration
+                BOOL found;
                 tmpStr[strPos] = 0;
-                isFound = FALSE;
-                item = sect;
-                while (item != NULL && !isFound)
+
+                for (item = sect, found = FALSE;
+                    item != NULL && !found;
+                    item = (th_cfgitem_t *) item->node.next)
                 {
-                    if (item->name != NULL && strcmp(item->name, tmpStr) == 0)
-                        isFound = TRUE;
-                    else
-                        item = (th_cfgitem_t *) item->node.next;
+                    found = item->name != NULL && strcmp(item->name, tmpStr) == 0;
                 }
 
                 // Check if key was found
-                if (isFound)
+                if (found)
                 {
                     // Okay, set next mode
                     th_cfg_set_next_parsemode(&ctx, th_cfg_get_parsemode(item->type));
@@ -555,16 +555,12 @@
             else
             {
                 int res = th_cfg_read_sect(fh, item->v.section, nesting + 1);
-                ctx.ch = -1;
-                if (res > 0)
-                    validError = TRUE;
+                if (res == THERR_OK)
+                    th_cfg_set_parsemode(&ctx, PM_IDLE);
                 else
-                if (res < 0)
                     ctx.parseMode = PM_ERROR;
-                else
-                {
-                    th_cfg_set_parsemode(&ctx, PM_IDLE);
-                }
+
+                ctx.ch = -1;
             }
             break;
 
@@ -700,10 +696,10 @@
             if (th_cfg_is_end(ctx.ch))
             {
                 tmpStr[strPos] = 0;
-                isError = !th_cfg_set_item(&ctx, item, tmpStr);
+                th_cfg_set_item(&ctx, item, tmpStr);
             }
 
-            if (isError)
+            if (isError || ctx.parseMode == PM_ERROR)
             {
                 th_io_error(fh, THERR_INVALID_DATA,
                     "Invalid boolean value for '%s'.\n",
@@ -718,22 +714,18 @@
 out:
     th_free(tmpStr);
 
-    // Check for validation errors
-    if (validError)
-        return -91;
-
     // Return result
     if (ctx.parseMode == PM_ERROR)
-        return -2;
+        return fh->status;
     else
-        return 0;
+        return THERR_OK;
 }
 
 
 int th_cfg_read(th_ioctx *fh, th_cfgitem_t *cfg)
 {
     if (fh == NULL || cfg == NULL)
-        return -1;
+        return THERR_NULLPTR;
 
     return th_cfg_read_sect(fh, cfg, 0);
 }
@@ -849,7 +841,7 @@
     while (item != NULL)
     {
         if (item->name == NULL)
-            return -1;
+            return THERR_NULLPTR;
 
         if (item->type == ITEM_COMMENT)
         {
@@ -872,13 +864,13 @@
                     if (lineStart)
                     {
                         if (!th_print_indent(fh, nesting, "# "))
-                            return -3;
+                            return THERR_FWRITE;
 
                         lineStart = FALSE;
                     }
 
                     if (thfputc(*ptr, fh) == EOF)
-                        return -9;
+                        return THERR_FWRITE;
             }
 
             if (!lineFeed)
@@ -890,13 +882,13 @@
             int res;
 
             if (!th_print_indent(fh, nesting, "%s = {\n", item->name))
-                return -3;
+                return THERR_FWRITE;
 
             if ((res = th_cfg_write_sect(fh, item->v.section, nesting + 1)) != 0)
                 return res;
 
             if (!th_print_indent(fh, nesting, "}\n\n"))
-                return -3;
+                return THERR_FWRITE;
         }
         else
         if (item->type == ITEM_STRING_LIST)
@@ -904,7 +896,7 @@
             if (!th_cfg_is_item_valid(item))
             {
                 if (!th_print_indent(fh, nesting, "#%s = \"\", \"\"", item->name))
-                    return -3;
+                    return THERR_FWRITE;
             }
             else
             {
@@ -912,7 +904,7 @@
                 size_t n = th_llist_length(node);
 
                 if (!th_print_indent(fh, nesting, "%s = \n", item->name))
-                    return -3;
+                    return THERR_FWRITE;
 
                 for (; node != NULL; node = node->next)
                 if (node->data != NULL)
@@ -920,38 +912,34 @@
                     if (!th_print_indent(fh, nesting, "\"%s\"%s\n",
                         (char *) node->data,
                         --n > 0 ? "," : ""))
-                        return -3;
+                        return THERR_FWRITE;
                 }
 
                 if (!th_print_indent(fh, nesting, "\n"))
-                    return -3;
+                    return THERR_FWRITE;
             }
         }
         else
         {
             if (!th_print_indent(fh, nesting, "%s%s = ",
-                th_cfg_is_item_valid(item) ? "" : "#",
-                item->name))
-                return -3;
-
-            if (th_cfg_write_item(fh, item) < 0)
-                return -4;
-
-            if (thfprintf(fh, "\n") < 0)
-                return -3;
+                    th_cfg_is_item_valid(item) ? "" : "#",
+                    item->name) ||
+                th_cfg_write_item(fh, item) < 0 ||
+                thfprintf(fh, "\n") < 0)
+                return THERR_FWRITE;
         }
 
         item = (th_cfgitem_t *) item->node.next;
     }
 
-    return 0;
+    return THERR_OK;
 }
 
 
 int th_cfg_write(th_ioctx *fh, const th_cfgitem_t *cfg)
 {
     if (fh == NULL || cfg == NULL)
-        return -1;
+        return THERR_NULLPTR;
 
     thfprintf(fh, "# Configuration written by %s %s\n\n",
             th_prog_desc, th_prog_version);