diff --git a/src/pipewire/properties.c b/src/pipewire/properties.c index fbe8a51f0..269b77fb6 100644 --- a/src/pipewire/properties.c +++ b/src/pipewire/properties.c @@ -64,6 +64,19 @@ static int find_index(const struct pw_properties *this, const char *key) return -1; } +static struct properties *properties_new(int prealloc) +{ + struct properties *impl; + + impl = calloc(1, sizeof(struct properties)); + if (impl == NULL) + return NULL; + + pw_array_init(&impl->items, prealloc); + + return impl; +} + /** Make a new properties object * * \param key a first key @@ -78,12 +91,10 @@ struct pw_properties *pw_properties_new(const char *key, ...) va_list varargs; const char *value; - impl = calloc(1, sizeof(struct properties)); + impl = properties_new(16); if (impl == NULL) return NULL; - pw_array_init(&impl->items, 16); - va_start(varargs, key); while (key != NULL) { value = va_arg(varargs, char *); @@ -107,12 +118,10 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict) uint32_t i; struct properties *impl; - impl = calloc(1, sizeof(struct properties)); + impl = properties_new(16); if (impl == NULL) return NULL; - pw_array_init(&impl->items, 16); - for (i = 0; i < dict->n_items; i++) { if (dict->items[i].key != NULL) add_func(&impl->this, strdup(dict->items[i].key), @@ -122,6 +131,43 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict) return &impl->this; } +/** Make a new properties object from the given str + * + * \a str should be a whitespace separated list of key=value + * strings. + * + * \param args a property description + * \return a new properties object + * + * \memberof pw_properties + */ +struct pw_properties * +pw_properties_new_string(const char *str) +{ + + struct properties *impl; + const char *state = NULL, *s = NULL; + size_t len; + + impl = properties_new(16); + if (impl == NULL) + return NULL; + + s = pw_split_walk(str, " \t\n\r", &len, &state); + while (s) { + char *val, *eq; + + val = strndup(s, len); + eq = strchr(val, '='); + if (eq) { + *eq = '\0'; + add_func(&impl->this, val, strdup(eq+1)); + } + s = pw_split_walk(str, " \t\n\r", &len, &state); + } + return &impl->this; +} + /** Copy a properties object * * \param properties properties to copy diff --git a/src/pipewire/properties.h b/src/pipewire/properties.h index 2176603a0..40b9a502f 100644 --- a/src/pipewire/properties.h +++ b/src/pipewire/properties.h @@ -45,6 +45,9 @@ pw_properties_new(const char *key, ...); struct pw_properties * pw_properties_new_dict(const struct spa_dict *dict); +struct pw_properties * +pw_properties_new_string(const char *args); + struct pw_properties * pw_properties_copy(const struct pw_properties *properties);