X-Git-Url: http://pileus.org/git/?p=wmpus;a=blobdiff_plain;f=conf.c;h=889bda6e4327839927c022626a816a186b61274b;hp=6a13c4489a83dd561e7b65a980c97f65e8e0621a;hb=refs%2Fheads%2Fmaster;hpb=ed3dbd05944250855712283a1c10419bf15f7c12 diff --git a/conf.c b/conf.c index 6a13c44..889bda6 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 @@ -23,10 +23,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; @@ -63,7 +64,7 @@ static void conf_set(const char *key, int num, const char *str) if ((entry = conf_get(key))) { tdelete(entry, &conf, conf_cmp); free(entry->key); - if (entry->type == string) + if (entry->type == STRING) free(entry->str); free(entry); } @@ -72,13 +73,13 @@ static void conf_set(const char *key, int num, const char *str) entry = new0(entry_t); entry->key = strdup(key); if (str) { - entry->type = string; + 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->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); } @@ -103,7 +104,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 +128,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 +148,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); } } } @@ -225,7 +226,7 @@ static void load_args(int argc, char **argv) int conf_get_int(const char *key, int def) { entry_t *entry = conf_get(key); - return entry && entry->type == number + return entry && entry->type == NUMBER ? entry->num : def; } @@ -237,7 +238,7 @@ void conf_set_int(const char *key, int value) const char *conf_get_str(const char *key, const char *def) { entry_t *entry = conf_get(key); - return entry && entry->type == string + return entry && entry->type == STRING ? entry->str : def; } @@ -256,8 +257,9 @@ void conf_init(int argc, char **argv) { conf_argc = argc; conf_argv = argv; - snprintf(conf_path, sizeof(conf_path), - "%s/%s", getenv("HOME"), ".wmpus"); + snprintf(conf_path, sizeof(conf_path), "%s/%s", + getenv("HOME") ?: getenv("HOMEPATH") ?: ".", + ".wmpus"); conf_reload(); }