diff --git a/conf/config_file.c b/conf/config_file.c index dc945c2e6a..0293bf9c44 100644 --- a/conf/config_file.c +++ b/conf/config_file.c @@ -21,7 +21,10 @@ #include #include #include + +#ifndef STANDALONE #include "general.h" +#endif struct entry_list { @@ -132,21 +135,32 @@ static void print_config(config_file_t *conf) struct entry_list *tmp = conf->entries; while (tmp != NULL) { +#ifdef STANDALONE + fprintf(stderr, "Config => Key: \"%s\", Value: \"%s\"\n", tmp->key, tmp->value); +#else SSNES_LOG("Config => Key: \"%s\", Value: \"%s\"\n", tmp->key, tmp->value); +#endif tmp = tmp->next; } } config_file_t *config_file_new(const char *path) { - FILE *file = fopen(path, "r"); - if (!file) - return NULL; struct config_file *conf = calloc(1, sizeof(*conf)); if (conf == NULL) return NULL; + if (path == NULL) + return conf; + + FILE *file = fopen(path, "r"); + if (!file) + { + free(conf); + return NULL; + } + struct entry_list *tail = conf->entries; while (!feof(file)) @@ -174,8 +188,7 @@ config_file_t *config_file_new(const char *path) } fclose(file); - if (g_extern.verbose) - print_config(conf); + print_config(conf); return conf; } @@ -290,4 +303,72 @@ bool config_get_bool(config_file_t *conf, const char *key, bool *in) return false; } +void config_set_string(config_file_t *conf, const char *key, const char *val) +{ + struct entry_list *list = conf->entries; + struct entry_list *last = list; + while (list != NULL) + { + if (strcmp(key, list->key) == 0) + { + free(list->value); + list->value = strdup(val); + return; + } + last = list; + list = list->next; + } + struct entry_list *elem = calloc(1, sizeof(*elem)); + elem->key = strdup(key); + elem->value = strdup(val); + + if (last) + last->next = elem; + else + conf->entries = elem; +} + +void config_set_double(config_file_t *conf, const char *key, double val) +{ + char buf[128]; + snprintf(buf, sizeof(buf), "%lf", val); + config_set_string(conf, key, buf); +} + +void config_set_int(config_file_t *conf, const char *key, int val) +{ + char buf[128]; + snprintf(buf, sizeof(buf), "%d", val); + config_set_string(conf, key, buf); +} + +void config_set_char(config_file_t *conf, const char *key, char val) +{ + char buf[2]; + snprintf(buf, sizeof(buf), "%c", val); + config_set_string(conf, key, buf); +} + +void config_set_bool(config_file_t *conf, const char *key, bool val) +{ + config_set_string(conf, key, val ? "true" : "false"); +} + +bool config_file_write(config_file_t *conf, const char *path) +{ + FILE *file = fopen(path, "w"); + if (!file) + return false; + + struct entry_list *list = conf->entries; + + while (list != NULL) + { + fprintf(file, "%s = \"%s\"\n", list->key, list->value); + list = list->next; + } + + fclose(file); + return true; +} diff --git a/conf/config_file.h b/conf/config_file.h index 1992127564..a893fdac18 100644 --- a/conf/config_file.h +++ b/conf/config_file.h @@ -30,7 +30,7 @@ typedef struct config_file config_file_t; // - Format is: key = value. There can be as many spaces as you like in-between. // - Value can be wrapped inside "" for multiword strings. (foo = "hai u") -// Loads a config file. Returns NULL if file doesn't exist. +// Loads a config file. Returns NULL if file doesn't exist. NULL path will create an empty config file. config_file_t *config_file_new(const char *path); // Frees config file. void config_file_free(config_file_t *conf); @@ -48,6 +48,14 @@ bool config_get_string(config_file_t *conf, const char *entry, char **in); // Extracts a boolean from config. Valid boolean true are "true" and "1". Valid false are "false" and "0". Other values will be treated as an error. bool config_get_bool(config_file_t *conf, const char *entry, bool *in); +// Setters. +void config_set_double(config_file_t *conf, const char *entry, double value); +void config_set_int(config_file_t *conf, const char *entry, int val); +void config_set_char(config_file_t *conf, const char *entry, char val); +void config_set_string(config_file_t *conf, const char *entry, const char *val); +void config_set_bool(config_file_t *conf, const char *entry, bool val); +bool config_file_write(config_file_t *conf, const char *path); + #endif