diff th_config.c @ 83:50006067bcd1

Add new function, th_cfg_find().
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 17 Oct 2013 08:54:22 +0300
parents a0e1b29be35d
children d739df7efba7
line wrap: on
line diff
--- a/th_config.c	Sat May 04 03:39:41 2013 +0300
+++ b/th_config.c	Thu Oct 17 08:54:22 2013 +0300
@@ -815,3 +815,53 @@
 
     return th_cfg_write_sect(ctx, cfg, 0);
 }
+
+
+/* Find a configuration item based on section, name, type.
+ * Name MUST be defined. Section can be NULL and type -1,
+ * first matching item will be returned.
+ */
+static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, BOOL *sect, const char *section, const char *name, const int type)
+{
+    while (item != NULL)
+    {
+        BOOL match = TRUE;
+        
+        if (item->type == ITEM_SECTION)
+        {
+            // Check section name if set
+            if (section != NULL && strcmp(section, item->name) == 0)
+                *sect = TRUE;
+
+            // Recurse to sub-section
+            th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, sect, section, name, type);
+            if (tmp != NULL)
+                return tmp;
+        }
+        else
+        // Has type check been set, and does it match?
+        if (type != -1 && item->type != type)
+            match = FALSE;
+        else
+        // Check name (not section name, tho)
+        if (strcmp(name, item->name) != 0)
+            match = FALSE;
+
+        // Do we have a match?
+        if (*sect && match)
+            return item;
+    }
+    
+    return NULL;
+}
+
+
+th_cfgitem_t *th_cfg_find(th_cfgitem_t *cfg, const char *section, const char *name, const int type)
+{
+    BOOL sect = FALSE;
+
+    if (section == NULL)
+        sect = TRUE;
+
+    return th_cfg_find_do(cfg, &sect, section, name, type);
+}