]> code.delx.au - pulseaudio/blobdiff - src/pulse/proplist.c
Use pa_hashmap_remove_and_free() where appropriate
[pulseaudio] / src / pulse / proplist.c
index faa98b79e4517aa419fc1d28742981405cb14902..a5ab7d264fa0cb6eab877550b42867e863607718 100644 (file)
@@ -28,7 +28,6 @@
 
 #include <pulse/xmalloc.h>
 #include <pulse/utf8.h>
-#include <pulse/i18n.h>
 
 #include <pulsecore/hashmap.h>
 #include <pulsecore/strbuf.h>
@@ -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);
@@ -157,7 +155,7 @@ int pa_proplist_setp(pa_proplist *p, const char *pair) {
 
 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;
@@ -168,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;
     }
@@ -188,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);
@@ -207,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;
 
@@ -215,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);
@@ -228,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);
 
@@ -247,19 +245,19 @@ 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 || 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);
 
@@ -281,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)))
@@ -310,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)))
@@ -322,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;
 
@@ -333,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))
@@ -343,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;
 }
 
@@ -366,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++)
@@ -642,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)))
@@ -652,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) {