From 2fd35488c7a27b90e1be353916e52d1cb25d8db4 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 31 Dec 2020 10:12:09 +0100 Subject: [PATCH] properties: just use the parser in all cases We can just use the json parser to parse the old key=val syntax as well now. --- src/pipewire/properties.c | 61 ++++++++++++------------------------- src/tests/test-properties.c | 2 +- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/pipewire/properties.c b/src/pipewire/properties.c index 09571d45b..9c969252e 100644 --- a/src/pipewire/properties.c +++ b/src/pipewire/properties.c @@ -150,57 +150,36 @@ int pw_properties_update_string(struct pw_properties *props, const char *str, si { struct properties *impl = SPA_CONTAINER_OF(props, struct properties, this); struct spa_json it[2]; + char key[1024], *val; int count = 0; spa_json_init(&it[0], str, size); - if (spa_json_enter_object(&it[0], &it[1]) > 0) { - char key[1024], *val; + if (spa_json_enter_object(&it[0], &it[1]) <= 0) + spa_json_init(&it[1], str, size); - while (spa_json_get_string(&it[1], key, sizeof(key)-1)) { - int len; - const char *value; + while (spa_json_get_string(&it[1], key, sizeof(key)-1)) { + int len; + const char *value; - if ((len = spa_json_next(&it[1], &value)) <= 0) - break; + if ((len = spa_json_next(&it[1], &value)) <= 0) + break; - if (key[0] == '#') - continue; - if (spa_json_is_null(value, len)) - val = NULL; - else { - if (spa_json_is_container(value, len)) - len = spa_json_container_len(&it[1], value, len); + if (key[0] == '#') + continue; + if (spa_json_is_null(value, len)) + val = NULL; + else { + if (spa_json_is_container(value, len)) + len = spa_json_container_len(&it[1], value, len); - if ((val = strndup(value, len)) == NULL) - return -errno; - - if (spa_json_is_string(value, len)) - spa_json_parse_string(value, len, val); - } - count += pw_properties_set(&impl->this, key, val); - free(val); - } - } else { - const char *state = NULL, *s = NULL; - size_t len; - - s = pw_split_walk(str, " \t\n\r", &len, &state); - while (s) { - char *val, *eq; - - if (s[0] == '#') - continue; - if ((val = strndup(s, len)) == NULL) + if ((val = strndup(value, len)) == NULL) return -errno; - eq = strchr(val, '='); - if (eq && eq != val) { - *eq = '\0'; - count += pw_properties_set(&impl->this, val, eq+1); - } - free(val); - s = pw_split_walk(str, " \t\n\r", &len, &state); + if (spa_json_is_string(value, len)) + spa_json_parse_string(value, len, val); } + count += pw_properties_set(&impl->this, key, val); + free(val); } return count; } diff --git a/src/tests/test-properties.c b/src/tests/test-properties.c index 5d631e40f..6fc52c248 100644 --- a/src/tests/test-properties.c +++ b/src/tests/test-properties.c @@ -211,7 +211,7 @@ static void test_new_string(void) { struct pw_properties *props; - props = pw_properties_new_string("foo=bar bar=baz ignore him=too empty= =gg"); + props = pw_properties_new_string("foo=bar bar=baz \"#ignore\"=ignore him=too empty=\"\" =gg"); spa_assert(props != NULL); spa_assert(props->flags == 0); spa_assert(props->dict.n_items == 4);