each option's values are string arrays and can be converted to int if they
[libgliar] / src / cfg.c
index 124449e..50c4f9c 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -11,11 +11,11 @@ the Free Software Foundation, either version 3 of the License, or
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+along with this program.       If not, see <http://www.gnu.org/licenses/>.
 
 Author: Eleni Maria Stea <elene.mst@gmail.com>
 */
@@ -27,7 +27,7 @@ Author: Eleni Maria Stea <elene.mst@gmail.com>
 #include "cfg.h"
 
 static char *stripspace(char *s);
-
+static void concat_values(struct cfgopt *opt);
 
 struct cfgopt *gliar_load_cfg(const char *fname)
 {
@@ -59,46 +59,58 @@ struct cfgopt *gliar_load_cfg(const char *fname)
                        if(opt) {
                                opt->next = optlist;
                                optlist = opt;
+                               concat_values(opt);
                        }
 
                        if((opt = malloc(sizeof *opt))) {
                                if((opt->key = malloc(strlen(line) + 1))) {
                                        strcpy(opt->key, line);
                                        opt->str_val = 0;
-          opt->num_val = 0;
-          opt->type = unknown;
+                                       opt->num_val = 0;
+                                       opt->str_count = 0;
+                                       opt->type = GLIAR_STRING;
                                } else {
                                        free(opt);
                                        opt = 0;
                                }
                        }
                } else {
-                       char *tmp;
-                       int prev_len = opt->str_val ? strlen(opt->str_val) : 0;
+                       int new_sz = opt->str_count + 1;
+                       char **tmp;
+
+                       char *end;
+                       int num = strtol(line, &end, 10);
 
-                       if(opt && (tmp = realloc(opt->str_val, prev_len + strlen(line) + 2))) {
-        opt->type = str;
+                       if(!*end) {
+                               opt->num_val = line;
+                               opt->type = GLIAR_NUMBER;
+                       }
+
+                       if(opt && (tmp = realloc(opt->str_val, new_sz * sizeof(char*)))) {
                                opt->str_val = tmp;
-                               if(prev_len) {
-                                       strcat(opt->str_val, " ");
-                                       strcat(opt->str_val, line);
-                               } else {
-                                       strcpy(opt->str_val, line);
+                               if((opt->str_val[new_sz - 1] = malloc(strlen(line) + 1))) {
+                                       strcpy(opt->str_val[new_sz -1], line);
+                                       opt->str_count = new_sz;
                                }
                        }
+
+                       if(new_sz > 1) {
+                               opt->type = GLIAR_STRING;
+                       }
                }
        }
 
        if(opt) {
                opt->next = optlist;
                optlist = opt;
+               concat_values(opt);
        }
 
        fclose(fp);
        return optlist;
 }
 
-const char *gliar_find_opt(struct cfgopt *list, const char *name)
+const struct cfgopt *gliar_find_opt(struct cfgopt *list, const char *name)
 {
        if(!list || !name) {
                return 0;
@@ -106,7 +118,7 @@ const char *gliar_find_opt(struct cfgopt *list, const char *name)
 
        while(list) {
                if(strcmp(list->key, name) == 0) {
-                       return list->str_val;
+                       return list;
                }
                list = list->next;
        }
@@ -117,7 +129,15 @@ void gliar_print_opt(struct cfgopt *list)
 {
        printf("OPTIONS\n");
        while(list) {
-               printf("\"%s\" -> \"%s\"\n", list->key, list->str_val);
+               if(list->type == GLIAR_NUMBER) {
+                       printf("\"%s\" -> %d\n", list->key, list->num_val);
+               }
+               else {
+                       int i;
+                       for(i=0; i<list->str_count; i++) {
+                               printf("\"%s\" -> \"%s\"\n", list->key, list->str_val[i]);
+                       }
+               }
                list = list->next;
        }
 }
@@ -133,3 +153,24 @@ static char *stripspace(char *s)
        }
        return s;
 }
+
+static void concat_values(struct cfgopt *opt)
+{
+       int i;
+       int sz = opt->str_count - 1;
+
+       for(i=0; i<opt->str_count; i++) {
+               sz += strlen(opt->str_val[i]);
+       }
+
+       if(!(opt->conc_vals = malloc(sz + 1))) {
+               return;
+       }
+
+       *opt->conc_vals = 0;
+       for(i=0; i<opt->str_count - 1; i++) {
+               strcat(opt->conc_vals, opt->str_val[i]);
+               strcat(opt->conc_vals, " ");
+       }
+       strcat(opt->conc_vals, opt->str_val[i]);
+}