changeset 138:3e221c16b087

Improvements in configuration file handing.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Nov 2010 21:46:16 +0200
parents fad8c31e41e6
children c39399725f7b
files th_config.c th_config.h
diffstat 2 files changed, 100 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/th_config.c	Tue Nov 02 21:45:28 2010 +0200
+++ b/th_config.c	Tue Nov 02 21:46:16 2010 +0200
@@ -1,7 +1,7 @@
 /*
  * Very simple configuration handling functions
  * Programmed and designed by Matti 'ccr' Hamalainen
- * (C) Copyright 2004-2008 Tecnic Software productions (TNSP)
+ * (C) Copyright 2004-2010 Tecnic Software productions (TNSP)
  *
  * Please read file 'COPYING' for information on license and distribution.
  */
@@ -38,7 +38,7 @@
 
 /* Allocate and add new item to configuration
  */
-static cfgitem_t *th_cfg_add(cfgitem_t **cfg, char *name, int type, void *data)
+static cfgitem_t *th_cfg_add(cfgitem_t **cfg, const char *name, const int type, void *data)
 {
     cfgitem_t *node;
 
@@ -181,6 +181,21 @@
 }
 
 
+int th_cfg_add_string_list(cfgitem_t ** cfg, char * name, qlist_t **data)
+{
+    cfgitem_t *node;
+    
+    if (data == NULL)
+        return -5;
+    
+    node = th_cfg_add(cfg, name, ITEM_STRING_LIST, (void *) data);
+    if (node == NULL)
+        return -1;
+
+    return 0;
+}
+
+
 /* Read a given file into configuration structure and variables
  */
 enum {
@@ -194,7 +209,8 @@
     PM_STRING,
     PM_INT,
     PM_BOOL,
-    PM_SECTION
+    PM_SECTION,
+    PM_ARRAY
 };
 
 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
@@ -345,6 +361,10 @@
                         nextMode = PM_STRING;
                         break;
 
+                    case ITEM_STRING_LIST:
+                        nextMode = PM_ARRAY;
+                        break;
+
                     case ITEM_INT:
                     case ITEM_UINT:
                         nextMode = PM_INT;
@@ -395,6 +415,27 @@
             }
             break;
 
+        case PM_ARRAY:
+            if (isStart) {
+                switch (item->type) {
+                    case ITEM_STRING_LIST:
+                        prevMode = parseMode;
+                        parseMode = PM_STRING;
+                        break;
+                }
+            } else if (c == ',') {
+                switch (item->type) {
+                    case ITEM_STRING_LIST:
+                        prevMode = parseMode;
+                        parseMode = PM_STRING;
+                        break;
+                }
+            } else {
+                prevMode = parseMode;
+                parseMode = PM_NORMAL;
+            }
+            break;
+
         case PM_SECTION:
             /* Section parsing mode */
             if (c != '{') {
@@ -427,13 +468,25 @@
                 /* End of string, set the value */
                 tmpStr[strPos] = 0;
                 
-                if (item->type == ITEM_HEX_TRIPLET) {
-                } else if (item->type == ITEM_STRING) {
-                    th_pstrcpy((char **) item->data, tmpStr);
+                switch (item->type) {
+                    case ITEM_HEX_TRIPLET:
+                        *(int *) item->data = th_get_hex_triplet(tmpStr);
+                        prevMode = parseMode;
+                        parseMode = PM_NORMAL;
+                        break;
+                    case ITEM_STRING:
+                        th_pstrcpy((char **) item->data, tmpStr);
+                        prevMode = parseMode;
+                        parseMode = PM_NORMAL;
+                        break;
+                    case ITEM_STRING_LIST:
+//                        th_cfg_add_to_array(item, th_strdup(tmpStr));
+                        prevMode = parseMode;
+                        parseMode = PM_NEXT;
+                        nextMode = PM_ARRAY;
+                        break;
                 }
                 
-                prevMode = parseMode;
-                parseMode = PM_NORMAL;
             } else {
                 /* Add character to string */
                 VADDCH(c)
@@ -588,31 +641,56 @@
             
             switch (item->type) {
             case ITEM_STRING:
-                if (*((char **) item->data) == NULL) {
+                if (*(item->val_str) == NULL) {
                     if (fprintf(f->file, "#%s = \"\"\n", item->name) < 0)
                         return -3;
                 } else {
                     if (fprintf(f->file, "%s = \"%s\"\n",
-                        item->name, *((char **) item->data)) < 0)
+                        item->name, *(item->val_str)) < 0)
+                        return -3;
+                }
+                break;
+
+            case ITEM_STRING_LIST:
+                if (*(item->list) == NULL) {
+                    if (fprintf(f->file, "#%s = \"\", \"\"\n", item->name) < 0)
+                        return -3;
+                } else {
+                    qlist_t *node = *(item->list);
+                    size_t n = th_llist_length(node);
+                    if (fprintf(f->file, "%s = ", item->name) < 0)
+                        return -3;
+                    
+                    while (node != NULL) {
+                        if (node->data != NULL)
+                            fprintf(f->file, "\"%s\"", (char *) node->data);
+
+                        if (--n > 0)
+                            fprintf(f->file, ", ");
+                        
+                        node = node->next;
+                    }
+
+                    if (fprintf(f->file, "\n") < 0)
                         return -3;
                 }
                 break;
 
             case ITEM_INT:
                 if (fprintf(f->file, "%s = %i\n",
-                    item->name, *((int *) item->data)) < 0)
+                    item->name, *(item->val_int)) < 0)
                     return -4;
                 break;
 
             case ITEM_UINT:
                 if (fprintf(f->file, "%s = %d\n",
-                    item->name, *((unsigned int *) item->data)) < 0)
+                    item->name, *(item->val_uint)) < 0)
                     return -5;
                 break;
 
             case ITEM_BOOL:
                 if (fprintf(f->file, "%s = %s\n",
-                    item->name, *((BOOL *) item->data) ? "yes" : "no") < 0)
+                    item->name, *(item->val_bool) ? "yes" : "no") < 0)
                     return -6;
                 break;
             
--- a/th_config.h	Tue Nov 02 21:45:28 2010 +0200
+++ b/th_config.h	Tue Nov 02 21:46:16 2010 +0200
@@ -26,7 +26,10 @@
     ITEM_UINT,
     ITEM_BOOL,
     ITEM_FLOAT,
-    ITEM_HEX_TRIPLET
+    ITEM_HEX_TRIPLET,
+
+    ITEM_STRING_LIST,
+    ITEM_HEX_TRIPLET_LIST
 };
 
 
@@ -34,11 +37,13 @@
     int  type;
     char *name;
     union {
-        void *data;
         int *val_int;
         unsigned int *val_uint;
-        char *val_str;
+        char **val_str;
         BOOL *val_bool;
+
+        void *data;
+        qlist_t **list;
         struct _cfgitem_t *section;
     };
     
@@ -61,6 +66,7 @@
 int     th_cfg_add_float(cfgitem_t **cfg, char *name, float *data, float itemDef);
 int     th_cfg_add_hexvalue(cfgitem_t **cfg, char *name, int *data, int itemDef);
 
+int     th_cfg_add_string_list(cfgitem_t **cfg, char *name, qlist_t **list);
 
 #ifdef __cplusplus
 }