diff --git a/src/pipewire/properties.c b/src/pipewire/properties.c index 9f8908bd4..cbb388c65 100644 --- a/src/pipewire/properties.c +++ b/src/pipewire/properties.c @@ -146,8 +146,7 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict) return &impl->this; } -SPA_EXPORT -int pw_properties_update_string(struct pw_properties *props, const char *str, size_t size) +static int update_from_string(struct pw_properties *props, const char *str, size_t size, bool overwrite) { struct properties *impl = SPA_CONTAINER_OF(props, struct properties, this); struct spa_json it[2]; @@ -176,12 +175,42 @@ int pw_properties_update_string(struct pw_properties *props, const char *str, si if ((val = malloc(len+1)) != NULL) spa_json_parse_string(value, len, val); } - count += pw_properties_set(&impl->this, key, val); + if (overwrite || pw_properties_get(&impl->this, key) == NULL) + count += pw_properties_set(&impl->this, key, val); free(val); } return count; } +/** + * Update the properties from the given string, adding any new + * keys from \a str but leaving existing keys in \a props unmodified. + * + * \a str should be a whitespace separated list of key=value + * strings or a json object, see pw_properties_new_string(). + * + * \return The number of properties added + */ +SPA_EXPORT +int pw_properties_add_string(struct pw_properties *props, const char *str, size_t size) +{ + return update_from_string(props, str, size, false); +} + +/** Update the properties from the given string, overwriting any + * existing keys with the new values from \a str. + * + * \a str should be a whitespace separated list of key=value + * strings or a json object, see pw_properties_new_string(). + * + * \return The number of properties added or updated + */ +SPA_EXPORT +int pw_properties_update_string(struct pw_properties *props, const char *str, size_t size) +{ + return update_from_string(props, str, size, true); +} + /** Make a new properties object from the given str * * \a object should be a whitespace separated list of key=value diff --git a/src/pipewire/properties.h b/src/pipewire/properties.h index d700eceac..52c877f43 100644 --- a/src/pipewire/properties.h +++ b/src/pipewire/properties.h @@ -70,8 +70,24 @@ int pw_properties_update_ignore(struct pw_properties *props, int pw_properties_update(struct pw_properties *oldprops, const struct spa_dict *dict); +/** + * Update the properties from the given string updating \a props to + * - all key/value pairs from \a str + * - all key/value pairs from \a props that were not given in \a str + * + * \return The number of properties added or updated + */ int pw_properties_update_string(struct pw_properties *props, const char *str, size_t size); +/** + * Update the properties from the given string updating \a props to + * - all key/value pairs from \a props + * - all key/value pairs from \a str that were not given in \a str + * + * \return The number of properties added + */ +int pw_properties_add_string(struct pw_properties *props, + const char *str, size_t size); int pw_properties_add(struct pw_properties *oldprops, const struct spa_dict *dict);