X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=conf.c;h=ff55b13b6d9c464f4a5cdcd8f1dcc3b3896c7f41;hb=c92c369d2828143182f9223646073dfb6bc1e421;hp=9d8dff9d541f2417dc0154f4f0d51ca4050380bb;hpb=7e8ce091da421353576edbd2a4a4e620fe0a62bd;p=wmpus diff --git a/conf.c b/conf.c index 9d8dff9..ff55b13 100644 --- a/conf.c +++ b/conf.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, Andy Spencer + * Copyright (c) 2011-2012, Andy Spencer * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -13,6 +13,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF */ +#define _GNU_SOURCE #include #include #include @@ -23,10 +24,11 @@ #include "conf.h" /* Types */ -typedef enum { NUMBER, STRING } type_t; - typedef struct { - type_t type; + enum { + NUMBER, + STRING, + } type; char *key; union { int num; @@ -41,31 +43,36 @@ static char **conf_argv; static char conf_path[256]; /* Helpers */ -static int conf_cmp(const void *_a, const void *_b) +static int entry_cmp(const void *_a, const void *_b) { const entry_t *a = _a; const entry_t *b = _b; return strcmp(a->key, b->key); } -static entry_t *conf_get(const char *key) +static entry_t *entry_get(const char *key) { entry_t try = { .key = (char*)key }; - entry_t **found = tfind(&try, &conf, conf_cmp); + entry_t **found = tfind(&try, &conf, entry_cmp); return found ? *found : NULL; } -static void conf_set(const char *key, int num, const char *str) +static void entry_free(entry_t *entry) +{ + free(entry->key); + if (entry->type == STRING) + free(entry->str); + free(entry); +} + +static void entry_set(const char *key, int num, const char *str) { entry_t *entry; /* Free old item */ - if ((entry = conf_get(key))) { - tdelete(entry, &conf, conf_cmp); - free(entry->key); - if (entry->type == STRING) - free(entry->str); - free(entry); + if ((entry = entry_get(key))) { + tdelete(entry, &conf, entry_cmp); + entry_free(entry); } /* Insert new item */ @@ -74,13 +81,13 @@ static void conf_set(const char *key, int num, const char *str) if (str) { entry->type = STRING; entry->str = strdup(str); - printf("set_str: %s = %s\n", key, str); + //printf("set_str: %s = %s\n", key, str); } else { entry->type = NUMBER; entry->num = num; - printf("set_num: %s = %d\n", key, num); + //printf("set_num: %s = %d\n", key, num); } - tsearch(entry, &conf, conf_cmp); + tsearch(entry, &conf, entry_cmp); } static char *strtrim(char *str) @@ -103,7 +110,7 @@ static void load_file(const char *path) char key[256]={}, val[256]={}, fullkey[256]={}; FILE *fd = fopen(path, "rt"); if (!fd) return; - printf("load_file: %s\n", path); + //printf("load_file: %s\n", path); while (fgets(line, sizeof(line), fd)) { /* Find special characters */ char *lbrace = strchr( line , '['); @@ -127,7 +134,7 @@ static void load_file(const char *path) section, strtrim(key)); if (!strchr(fullkey, ' ')) { conf_set_str(fullkey, val); - printf(" [%s] = [%s]\n", fullkey, val); + //printf(" [%s] = [%s]\n", fullkey, val); } } else if (section[0] && equal) { @@ -147,7 +154,7 @@ static void load_file(const char *path) conf_set_int(fullkey, 0); else conf_set_str(fullkey, trim); - printf(" [%s] = [%s]\n", fullkey, trim); + //printf(" [%s] = [%s]\n", fullkey, trim); } } } @@ -224,26 +231,26 @@ static void load_args(int argc, char **argv) /* Configuration file functions */ int conf_get_int(const char *key, int def) { - entry_t *entry = conf_get(key); + entry_t *entry = entry_get(key); return entry && entry->type == NUMBER ? entry->num : def; } void conf_set_int(const char *key, int value) { - conf_set(key, value, NULL); + entry_set(key, value, NULL); } const char *conf_get_str(const char *key, const char *def) { - entry_t *entry = conf_get(key); + entry_t *entry = entry_get(key); return entry && entry->type == STRING ? entry->str : def; } void conf_set_str(const char *key, const char *value) { - conf_set(key, 0, value); + entry_set(key, 0, value); } void conf_reload(void) @@ -264,4 +271,5 @@ void conf_init(int argc, char **argv) void conf_free(void) { + tdestroy(conf, (void(*)(void*))entry_free); }