Completed the get/set config functions.
authorArt Cancro <ajc@uncensored.citadel.org>
Sat, 25 Apr 2015 05:10:12 +0000 (01:10 -0400)
committerArt Cancro <ajc@uncensored.citadel.org>
Sat, 25 Apr 2015 05:10:12 +0000 (01:10 -0400)
citadel/config.c
citadel/config.h

index 4a6f49e4a0834fb081c80a91806a565a04765740..93a9f10669382c09f4d7712c16852eb07086fb4c 100644 (file)
@@ -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;
+}
 
 
 
index 3b7f4c531b197f147a143e732b24b2f1c355bfb3..8da53f8970adb1297217ea632e7bff2076232d4b 100644 (file)
 #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);