X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/0933f1a2a401a418a90dc948824e184b5dcc9c93..eca082a93f2619cfa10733947a81fa779cb49573:/src/pulse/proplist.c diff --git a/src/pulse/proplist.c b/src/pulse/proplist.c index db4c9344..a5ab7d26 100644 --- a/src/pulse/proplist.c +++ b/src/pulse/proplist.c @@ -28,7 +28,6 @@ #include #include -#include #include #include @@ -45,15 +44,15 @@ struct property { #define MAKE_HASHMAP(p) ((pa_hashmap*) (p)) #define MAKE_PROPLIST(p) ((pa_proplist*) (p)) -static pa_bool_t property_name_valid(const char *key) { +int pa_proplist_key_valid(const char *key) { if (!pa_ascii_valid(key)) - return FALSE; + return 0; if (strlen(key) <= 0) - return FALSE; + return 0; - return TRUE; + return 1; } static void property_free(struct property *prop) { @@ -65,32 +64,31 @@ static void property_free(struct property *prop) { } pa_proplist* pa_proplist_new(void) { - return MAKE_PROPLIST(pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func)); + return MAKE_PROPLIST(pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) property_free)); } void pa_proplist_free(pa_proplist* p) { pa_assert(p); - pa_proplist_clear(p); - pa_hashmap_free(MAKE_HASHMAP(p), NULL, NULL); + pa_hashmap_free(MAKE_HASHMAP(p)); } /** Will accept only valid UTF-8 */ int pa_proplist_sets(pa_proplist *p, const char *key, const char *value) { struct property *prop; - pa_bool_t add = FALSE; + bool add = false; pa_assert(p); pa_assert(key); pa_assert(value); - if (!property_name_valid(key) || !pa_utf8_valid(value)) + if (!pa_proplist_key_valid(key) || !pa_utf8_valid(value)) return -1; if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) { prop = pa_xnew(struct property, 1); prop->key = pa_xstrdup(key); - add = TRUE; + add = true; } else pa_xfree(prop->value); @@ -106,7 +104,7 @@ int pa_proplist_sets(pa_proplist *p, const char *key, const char *value) { /** Will accept only valid UTF-8 */ static int proplist_setn(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) { struct property *prop; - pa_bool_t add = FALSE; + bool add = false; char *k, *v; pa_assert(p); @@ -116,7 +114,7 @@ static int proplist_setn(pa_proplist *p, const char *key, size_t key_length, con k = pa_xstrndup(key, key_length); v = pa_xstrndup(value, value_length); - if (!property_name_valid(k) || !pa_utf8_valid(v)) { + if (!pa_proplist_key_valid(k) || !pa_utf8_valid(v)) { pa_xfree(k); pa_xfree(v); return -1; @@ -125,7 +123,7 @@ static int proplist_setn(pa_proplist *p, const char *key, size_t key_length, con if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), k))) { prop = pa_xnew(struct property, 1); prop->key = k; - add = TRUE; + add = true; } else { pa_xfree(prop->value); pa_xfree(k); @@ -140,9 +138,24 @@ static int proplist_setn(pa_proplist *p, const char *key, size_t key_length, con return 0; } +/** Will accept only valid UTF-8 */ +int pa_proplist_setp(pa_proplist *p, const char *pair) { + const char *t; + + pa_assert(p); + pa_assert(pair); + + if (!(t = strchr(pair, '='))) + return -1; + + return proplist_setn(p, + pair, t - pair, + t + 1, strchr(pair, 0) - t - 1); +} + static int proplist_sethex(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) { struct property *prop; - pa_bool_t add = FALSE; + bool add = false; char *k, *v; uint8_t *d; size_t dn; @@ -153,7 +166,7 @@ static int proplist_sethex(pa_proplist *p, const char *key, size_t key_length, c k = pa_xstrndup(key, key_length); - if (!property_name_valid(k)) { + if (!pa_proplist_key_valid(k)) { pa_xfree(k); return -1; } @@ -173,7 +186,7 @@ static int proplist_sethex(pa_proplist *p, const char *key, size_t key_length, c if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), k))) { prop = pa_xnew(struct property, 1); prop->key = k; - add = TRUE; + add = true; } else { pa_xfree(prop->value); pa_xfree(k); @@ -192,7 +205,7 @@ static int proplist_sethex(pa_proplist *p, const char *key, size_t key_length, c /** Will accept only valid UTF-8 */ int pa_proplist_setf(pa_proplist *p, const char *key, const char *format, ...) { struct property *prop; - pa_bool_t add = FALSE; + bool add = false; va_list ap; char *v; @@ -200,7 +213,7 @@ int pa_proplist_setf(pa_proplist *p, const char *key, const char *format, ...) { pa_assert(key); pa_assert(format); - if (!property_name_valid(key) || !pa_utf8_valid(format)) + if (!pa_proplist_key_valid(key) || !pa_utf8_valid(format)) return -1; va_start(ap, format); @@ -213,7 +226,7 @@ int pa_proplist_setf(pa_proplist *p, const char *key, const char *format, ...) { if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) { prop = pa_xnew(struct property, 1); prop->key = pa_xstrdup(key); - add = TRUE; + add = true; } else pa_xfree(prop->value); @@ -232,24 +245,25 @@ fail: int pa_proplist_set(pa_proplist *p, const char *key, const void *data, size_t nbytes) { struct property *prop; - pa_bool_t add = FALSE; + bool add = false; pa_assert(p); pa_assert(key); - pa_assert(data); + pa_assert(data || nbytes == 0); - if (!property_name_valid(key)) + if (!pa_proplist_key_valid(key)) return -1; if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) { prop = pa_xnew(struct property, 1); prop->key = pa_xstrdup(key); - add = TRUE; + add = true; } else pa_xfree(prop->value); prop->value = pa_xmalloc(nbytes+1); - memcpy(prop->value, data, nbytes); + if (nbytes > 0) + memcpy(prop->value, data, nbytes); ((char*) prop->value)[nbytes] = 0; prop->nbytes = nbytes; @@ -265,7 +279,7 @@ const char *pa_proplist_gets(pa_proplist *p, const char *key) { pa_assert(p); pa_assert(key); - if (!property_name_valid(key)) + if (!pa_proplist_key_valid(key)) return NULL; if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) @@ -294,7 +308,7 @@ int pa_proplist_get(pa_proplist *p, const char *key, const void **data, size_t * pa_assert(data); pa_assert(nbytes); - if (!property_name_valid(key)) + if (!pa_proplist_key_valid(key)) return -1; if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) @@ -306,7 +320,7 @@ int pa_proplist_get(pa_proplist *p, const char *key, const void **data, size_t * return 0; } -void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, pa_proplist *other) { +void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, const pa_proplist *other) { struct property *prop; void *state = NULL; @@ -317,6 +331,8 @@ void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, pa_proplist *othe if (mode == PA_UPDATE_SET) pa_proplist_clear(p); + /* MAKE_HASHMAP turns the const pointer into a non-const pointer, but + * that's ok, because we don't modify the hashmap contents. */ while ((prop = pa_hashmap_iterate(MAKE_HASHMAP(other), &state, NULL))) { if (mode == PA_UPDATE_MERGE && pa_proplist_contains(p, prop->key)) @@ -327,18 +343,15 @@ void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, pa_proplist *othe } int pa_proplist_unset(pa_proplist *p, const char *key) { - struct property *prop; - pa_assert(p); pa_assert(key); - if (!property_name_valid(key)) + if (!pa_proplist_key_valid(key)) return -1; - if (!(prop = pa_hashmap_remove(MAKE_HASHMAP(p), key))) + if (pa_hashmap_remove_and_free(MAKE_HASHMAP(p), key) < 0) return -2; - property_free(prop); return 0; } @@ -350,7 +363,7 @@ int pa_proplist_unset_many(pa_proplist *p, const char * const keys[]) { pa_assert(keys); for (k = keys; *k; k++) - if (!property_name_valid(*k)) + if (!pa_proplist_key_valid(*k)) return -1; for (k = keys; *k; k++) @@ -626,7 +639,7 @@ int pa_proplist_contains(pa_proplist *p, const char *key) { pa_assert(p); pa_assert(key); - if (!property_name_valid(key)) + if (!pa_proplist_key_valid(key)) return -1; if (!(pa_hashmap_get(MAKE_HASHMAP(p), key))) @@ -636,22 +649,20 @@ int pa_proplist_contains(pa_proplist *p, const char *key) { } void pa_proplist_clear(pa_proplist *p) { - struct property *prop; pa_assert(p); - while ((prop = pa_hashmap_steal_first(MAKE_HASHMAP(p)))) - property_free(prop); + pa_hashmap_remove_all(MAKE_HASHMAP(p)); } -pa_proplist* pa_proplist_copy(pa_proplist *template) { - pa_proplist *p; +pa_proplist* pa_proplist_copy(const pa_proplist *p) { + pa_proplist *copy; - pa_assert_se(p = pa_proplist_new()); + pa_assert_se(copy = pa_proplist_new()); - if (template) - pa_proplist_update(p, PA_UPDATE_REPLACE, template); + if (p) + pa_proplist_update(copy, PA_UPDATE_REPLACE, p); - return p; + return copy; } unsigned pa_proplist_size(pa_proplist *p) { @@ -665,3 +676,32 @@ int pa_proplist_isempty(pa_proplist *p) { return pa_hashmap_isempty(MAKE_HASHMAP(p)); } + +int pa_proplist_equal(pa_proplist *a, pa_proplist *b) { + const void *key = NULL; + struct property *a_prop = NULL; + struct property *b_prop = NULL; + void *state = NULL; + + pa_assert(a); + pa_assert(b); + + if (a == b) + return 1; + + if (pa_proplist_size(a) != pa_proplist_size(b)) + return 0; + + while ((a_prop = pa_hashmap_iterate(MAKE_HASHMAP(a), &state, &key))) { + if (!(b_prop = pa_hashmap_get(MAKE_HASHMAP(b), key))) + return 0; + + if (a_prop->nbytes != b_prop->nbytes) + return 0; + + if (memcmp(a_prop->value, b_prop->value, a_prop->nbytes) != 0) + return 0; + } + + return 1; +}