From a2b5825a4b4a2bc2dfd37abf0e6214cc62a48959 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 18 Feb 2019 01:10:45 +0100 Subject: [PATCH] config: simplify command line configuration for arrays --- lib/config_helper.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/config_helper.c b/lib/config_helper.c index 23b924598..6c3b7471a 100644 --- a/lib/config_helper.c +++ b/lib/config_helper.c @@ -165,9 +165,26 @@ int json_to_config(json_t *json, config_setting_t *parent) } #endif /* LIBCONFIG_FOUND */ +void json_object_extend_key_value_token(json_t *obj, const char *key, const char *value) +{ + char *str = strdup(value); + char *delim = ","; + + char *lasts; + char *token = strtok_r(str, delim, &lasts); + + while (token) { + json_object_extend_key_value(obj, key, token); + + token = strtok_r(NULL, delim, &lasts); + } + + free(str); +} + void json_object_extend_key_value(json_t *obj, const char *key, const char *value) { - char *end, *cpy, *key1, *key2; + char *end, *cpy, *key1, *key2, *lasts; double real; long integer; @@ -178,8 +195,8 @@ void json_object_extend_key_value(json_t *obj, const char *key, const char *valu subobj = obj; cpy = strdup(key); - key1 = strtok(cpy, "."); - key2 = strtok(NULL, "."); + key1 = strtok_r(cpy, ".", &lasts); + key2 = strtok_r(NULL, ".", &lasts); while (key1 && key2) { existing = json_object_get(subobj, key1); @@ -193,7 +210,7 @@ void json_object_extend_key_value(json_t *obj, const char *key, const char *valu } key1 = key2; - key2 = strtok(NULL, "."); + key2 = strtok_r(NULL, ".", &lasts); } /* Try to parse as integer */ @@ -254,7 +271,7 @@ json_t * json_load_cli(int argc, const char *argv[]) const char *key = NULL; const char *value = NULL; const char *sep; - char *cpy; + char *cpy, *lasts; json_t *json = json_object(); @@ -274,10 +291,10 @@ json_t * json_load_cli(int argc, const char *argv[]) if (sep) { cpy = strdup(key); - key = strtok(cpy, "="); - value = strtok(NULL, ""); + key = strtok_r(cpy, "=", &lasts); + value = strtok_r(NULL, "", &lasts); - json_object_extend_key_value(json, key, value); + json_object_extend_key_value_token(json, key, value); free(cpy); key = NULL; @@ -291,7 +308,7 @@ json_t * json_load_cli(int argc, const char *argv[]) value = opt; - json_object_extend_key_value(json, key, value); + json_object_extend_key_value_token(json, key, value); key = NULL; } } @@ -324,17 +341,17 @@ int json_object_extend(json_t *obj, json_t *merge) int json_object_extend_str(json_t *obj, const char *str) { - char *key, *value, *cpy; + char *key, *value, *cpy, *lasts; cpy = strdup(str); - key = strtok(cpy, "="); - value = strtok(NULL, ""); + key = strtok_r(cpy, "=", &lasts); + value = strtok_r(NULL, "", &lasts); if (!key || !value) return -1; - json_object_extend_key_value(obj, key, value); + json_object_extend_key_value_token(obj, key, value); free(cpy);