diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2019-11-10 18:49:28 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2019-11-10 18:49:28 -0800 |
commit | 44ec797349713f28314bd329bee940cd55fad411 (patch) | |
tree | 2128c6ba385d4969b30b54259791811df315daf4 /src/settings.c | |
parent | 4b82076cb381f91c1225eb39efa924e935043d8b (diff) |
settings: s/setting_desc_new/setting_desc_clone/
Slight refactor to make call sites less annoying.
Now takes a (setting_desc_t *) instead of the members as discrete
parameters, and returns an errno on error so callers can simply
propagate error codes out rather than having to get access to errno
defines, check for NULL and return -ENOMEM etc.
It also makes the call sites self documenting by employing designated
initializers in compound literals for the supplied setting_desc_t.
This is in prep for runtime-configurable module settings.
Diffstat (limited to 'src/settings.c')
-rw-r--r-- | src/settings.c | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/src/settings.c b/src/settings.c index d8be774..c24c280 100644 --- a/src/settings.c +++ b/src/settings.c @@ -214,50 +214,53 @@ int settings_apply_desc_generators(const settings_t *settings, const setting_des /* convenience helper for creating a new setting description */ -/* copies of everything supplied are made in newly allocated memory */ -setting_desc_t * setting_desc_new(const char *name, const char *key, const char *regex, const char *preferred, const char *values[], const char *annotations[]) +/* copies of everything supplied are made in newly allocated memory, stored @ res_desc */ +/* returns < 0 on error */ +int setting_desc_clone(const setting_desc_t *desc, setting_desc_t **res_desc) { - setting_desc_t *desc; + setting_desc_t *d; - assert(name); - assert(preferred); /* XXX: require a preferred default? */ - assert(!annotations || values); + assert(desc); + assert(desc->name); + assert(desc->preferred); /* XXX: require a preferred default? */ + assert(!desc->annotations || desc->values); - desc = calloc(1, sizeof(setting_desc_t)); - if (!desc) - return NULL; + d = calloc(1, sizeof(setting_desc_t)); + if (!d) + return -ENOMEM; - desc->name = strdup(name); - if (key) /* This is inappropriately subtle, but when key is NULL, the value will be the key, and there will be no value side at all. */ - desc->key = strdup(key); - if (regex) - desc->regex = strdup(regex); + d->name = strdup(desc->name); + if (desc->key) /* This is inappropriately subtle, but when key is NULL, the value will be the key, and there will be no value side at all. */ + d->key = strdup(desc->key); + if (desc->regex) + d->regex = strdup(desc->regex); - desc->preferred = strdup(preferred); + d->preferred = strdup(desc->preferred); - if (values) { + if (desc->values) { unsigned i; - for (i = 0; values[i]; i++); + for (i = 0; desc->values[i]; i++); - desc->values = calloc(i + 1, sizeof(*values)); + d->values = calloc(i + 1, sizeof(*desc->values)); - if (annotations) - desc->annotations = calloc(i + 1, sizeof(*annotations)); + if (desc->annotations) + d->annotations = calloc(i + 1, sizeof(*desc->annotations)); - for (i = 0; values[i]; i++) { - desc->values[i] = strdup(values[i]); + for (i = 0; desc->values[i]; i++) { + d->values[i] = strdup(desc->values[i]); - if (annotations) { - assert(annotations[i]); - desc->annotations[i] = strdup(annotations[i]); + if (desc->annotations) { + assert(desc->annotations[i]); + d->annotations[i] = strdup(desc->annotations[i]); } } } /* TODO: handle allocation errors above... */ + *res_desc = d; - return desc; + return 0; } |