properties: Expose _update_string() method

Refactor an internal method and expose as _update_string() to
update an existing properties structure with a string representation.
This commit is contained in:
Wim Taymans 2020-12-29 13:15:54 +01:00
parent 5f561334fb
commit 923e82df27
3 changed files with 56 additions and 53 deletions

View file

@ -145,41 +145,64 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict)
return &impl->this; return &impl->this;
} }
static struct pw_properties * SPA_EXPORT
properties_new_string(const char *str) int pw_properties_update_string(struct pw_properties *props, const char *str, size_t size)
{ {
struct properties *impl; struct properties *impl = SPA_CONTAINER_OF(props, struct properties, this);
const char *state = NULL, *s = NULL; struct spa_json it[2];
size_t len; int count = 0;
int res;
impl = properties_new(16); spa_json_init(&it[0], str, size);
if (impl == NULL) if (spa_json_enter_object(&it[0], &it[1]) > 0) {
return NULL; char key[1024], *val;
s = pw_split_walk(str, " \t\n\r", &len, &state); while (spa_json_get_string(&it[1], key, sizeof(key)-1)) {
while (s) { int len;
char *val, *eq; const char *value;
if ((val = strndup(s, len)) == NULL) if ((len = spa_json_next(&it[1], &value)) <= 0)
goto error_errno; break;
eq = strchr(val, '='); if (key[0] == '#')
if (eq && eq != val) { continue;
*eq = '\0'; if (spa_json_is_null(value, len))
add_func(&impl->this, val, strdup(eq+1)); val = NULL;
} else { 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); free(val);
} }
s = pw_split_walk(str, " \t\n\r", &len, &state); } else {
} const char *state = NULL, *s = NULL;
return &impl->this; size_t len;
error_errno: s = pw_split_walk(str, " \t\n\r", &len, &state);
res = -errno; while (s) {
pw_properties_free(&impl->this); char *val, *eq;
errno = -res;
return NULL; if (s[0] == '#')
continue;
if ((val = strndup(s, 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);
}
}
return count;
} }
/** Make a new properties object from the given str /** Make a new properties object from the given str
@ -197,39 +220,17 @@ struct pw_properties *
pw_properties_new_string(const char *object) pw_properties_new_string(const char *object)
{ {
struct properties *impl; struct properties *impl;
struct spa_json it[2];
char key[256], *val;
int res; int res;
spa_json_init(&it[0], object, strlen(object));
if (spa_json_enter_object(&it[0], &it[1]) < 0)
return properties_new_string(object);
impl = properties_new(16); impl = properties_new(16);
if (impl == NULL) if (impl == NULL)
return NULL; return NULL;
while (spa_json_get_string(&it[1], key, sizeof(key)-1)) { if ((res = pw_properties_update_string(&impl->this, object, strlen(object))) < 0)
int len; goto error;
const char *value;
if ((len = spa_json_next(&it[1], &value)) <= 0)
break;
if (spa_json_is_container(value, len))
len = spa_json_container_len(&it[1], value, len);
if ((val = strndup(value, len)) == NULL)
goto error_errno;
if (spa_json_is_string(value, len))
spa_json_parse_string(value, len, val);
add_func(&impl->this, strdup(key), val);
}
return &impl->this; return &impl->this;
error_errno: error:
res = errno;
pw_properties_free(&impl->this); pw_properties_free(&impl->this);
errno = -res; errno = -res;
return NULL; return NULL;

View file

@ -64,6 +64,8 @@ int pw_properties_update_keys(struct pw_properties *props,
int pw_properties_update(struct pw_properties *oldprops, int pw_properties_update(struct pw_properties *oldprops,
const struct spa_dict *dict); const struct spa_dict *dict);
int pw_properties_update_string(struct pw_properties *props,
const char *str, size_t size);
int pw_properties_add(struct pw_properties *oldprops, int pw_properties_add(struct pw_properties *oldprops,
const struct spa_dict *dict); const struct spa_dict *dict);

View file

@ -317,7 +317,7 @@ static void test_new_json(void)
props = pw_properties_new_string("{ \"foo\": \"bar\\n\\t\", \"bar\": 1.8, \"empty\": [ \"foo\", \"bar\" ], \"\": \"gg\""); props = pw_properties_new_string("{ \"foo\": \"bar\\n\\t\", \"bar\": 1.8, \"empty\": [ \"foo\", \"bar\" ], \"\": \"gg\"");
spa_assert(props != NULL); spa_assert(props != NULL);
spa_assert(props->flags == 0); spa_assert(props->flags == 0);
spa_assert(props->dict.n_items == 4); spa_assert(props->dict.n_items == 3);
spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar\n\t")); spa_assert(!strcmp(pw_properties_get(props, "foo"), "bar\n\t"));
spa_assert(!strcmp(pw_properties_get(props, "bar"), "1.8")); spa_assert(!strcmp(pw_properties_get(props, "bar"), "1.8"));