From a04f42637efa2a1ba380f9d35c1b8b5d573430a5 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Mon, 7 Jun 2021 19:22:42 -0400 Subject: Added the `set_config_opt()` function. This function takes the offset of a given member of a config option in a `config` struct, and sets the member to the value supplied by `val`, with the type of value being determined by `type`. --- config.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'config.c') diff --git a/config.c b/config.c index 3a6b54b..859b23c 100644 --- a/config.c +++ b/config.c @@ -87,6 +87,16 @@ config_val parse_option_value(const config_opt *opt, char *value) { return val; } +void set_config_opt(config *conf, config_type type, size_t offset, config_val val) { + switch (type) { + case TYPE_INT : *(int *)(conf+offset) = val.i; break; + case TYPE_STRING: *(char **)(conf+offset) = val.str; break; + case TYPE_FLOAT : *(float *)(conf+offset) = val.f; break; + case TYPE_BOOL : *(int *)(conf+offset) = val.b; break; + default : break; + } +} + config *parse_config(const char *filename) { /* Size of the file, in bytes. */ long filesize = 0; @@ -120,6 +130,7 @@ config *parse_config(const char *filename) { /* Is the config option valid? */ if (opt->name != NULL) { config_val val = parse_option_value(opt, value); + set_config_opt(cfg, opt->type, opt->offset, val); } } } -- cgit v1.2.3-13-gbd6f