From 2989b649a374a2ac678aa74835717130e7fe1389 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Sat, 25 Apr 2015 01:10:12 -0400 Subject: [PATCH] Completed the get/set config functions. --- citadel/config.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ citadel/config.h | 7 +++++ 2 files changed, 81 insertions(+) diff --git a/citadel/config.c b/citadel/config.c index 4a6f49e4a..93a9f1066 100644 --- a/citadel/config.c +++ b/citadel/config.c @@ -127,6 +127,7 @@ void brand_new_installation_set_defaults(void) { void initialize_config_system(void) { FILE *cfp; int rv; + ctdlconfig = NewHash(1, NULL); if (chdir(ctdl_bbsbase_dir) != 0) { fprintf(stderr, @@ -252,6 +253,79 @@ void shutdown_config_system(void) +/* + * Set a system config value. Simple key/value here. + */ +void CtdlSetConfigStr(char *key, char *value) +{ + int key_len = strlen(key); + int value_len = strlen(value); + + /* Save it in memory */ + Put(ctdlconfig, key, key_len, strdup(value), NULL); + + /* Also write it to the config database */ + + int dbv_size = key_len + value_len + 2; + char *dbv = malloc(dbv_size); + strcpy(dbv, key); + strcpy(&dbv[key_len + 1], value); + cdb_store(CDB_CONFIG, key, key_len, dbv, dbv_size); + free(dbv); +} + + +/* + * Set a numeric system config value (long integer) + */ +void CtdlSetConfigLong(char *key, long value) +{ + char longstr[256]; + sprintf(longstr, "%ld", value); + CtdlSetConfigStr(key, longstr); +} + + +/* + * Set a numeric system config value (integer) + */ +void CtdlSetConfigInt(char *key, int value) +{ + char intstr[256]; + sprintf(intstr, "%d", value); + CtdlSetConfigStr(key, intstr); +} + + +/* + * Fetch a system config value. Caller does *not* own the returned value and may not alter it. + */ +char *CtdlGetConfigStr(char *key) +{ + char *value = NULL; + struct cdbdata *cdb; + int key_len = strlen(key); + + /* First look in memory */ + if (GetHash(ctdlconfig, key, key_len, (void *)&value)) + { + return value; + } + + /* Then look in the database. */ + + cdb = cdb_fetch(CDB_CONFIG, key, key_len); + + if (cdb == NULL) { /* nope, not there either. */ + return(NULL); + } + + /* Got it. Save it in memory for the next fetch. */ + value = strdup(cdb->ptr + key_len + 1); /* The key was stored there too; skip past it */ + cdb_free(cdb); + Put(ctdlconfig, key, key_len, value, NULL); + return value; +} diff --git a/citadel/config.h b/citadel/config.h index 3b7f4c531..8da53f897 100644 --- a/citadel/config.h +++ b/citadel/config.h @@ -13,9 +13,16 @@ #include "serv_extensions.h" #include "citadel_dirs.h" +#define CtdlGetConfigInt(x) atoi(CtdlGetConfigStr(x)) +#define CtdlGetConfigLong(x) atol(CtdlGetConfigStr(x)) + void initialize_config_system(void); void shutdown_config_system(void); void put_config(void); +void CtdlSetConfigStr(char *, char *); +char *CtdlGetConfigStr(char *); +void CtdlSetConfigInt(char *key, int value); +void CtdlSetConfigLong(char *key, long value); char *CtdlGetSysConfig(char *sysconfname); void CtdlPutSysConfig(char *sysconfname, char *sysconfdata); -- 2.30.2