2 This file is part of PulseAudio.
4 Copyright 2007 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
30 #include <pulse/utf8.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/strbuf.h>
34 #include <pulsecore/core-util.h>
44 #define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
45 #define MAKE_PROPLIST(p) ((pa_proplist*) (p))
47 int pa_proplist_key_valid(const char *key) {
49 if (!pa_ascii_valid(key))
58 static void property_free(struct property *prop) {
62 pa_xfree(prop->value);
66 pa_proplist* pa_proplist_new(void) {
67 return MAKE_PROPLIST(pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func));
70 void pa_proplist_free(pa_proplist* p) {
73 pa_hashmap_free(MAKE_HASHMAP(p), (pa_free_cb_t) property_free);
76 /** Will accept only valid UTF-8 */
77 int pa_proplist_sets(pa_proplist *p, const char *key, const char *value) {
78 struct property *prop;
85 if (!pa_proplist_key_valid(key) || !pa_utf8_valid(value))
88 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
89 prop = pa_xnew(struct property, 1);
90 prop->key = pa_xstrdup(key);
93 pa_xfree(prop->value);
95 prop->value = pa_xstrdup(value);
96 prop->nbytes = strlen(value)+1;
99 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
104 /** Will accept only valid UTF-8 */
105 static int proplist_setn(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) {
106 struct property *prop;
114 k = pa_xstrndup(key, key_length);
115 v = pa_xstrndup(value, value_length);
117 if (!pa_proplist_key_valid(k) || !pa_utf8_valid(v)) {
123 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), k))) {
124 prop = pa_xnew(struct property, 1);
128 pa_xfree(prop->value);
133 prop->nbytes = strlen(v)+1;
136 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
141 /** Will accept only valid UTF-8 */
142 int pa_proplist_setp(pa_proplist *p, const char *pair) {
148 if (!(t = strchr(pair, '=')))
151 return proplist_setn(p,
153 t + 1, strchr(pair, 0) - t - 1);
156 static int proplist_sethex(pa_proplist *p, const char *key, size_t key_length, const char *value, size_t value_length) {
157 struct property *prop;
167 k = pa_xstrndup(key, key_length);
169 if (!pa_proplist_key_valid(k)) {
174 v = pa_xstrndup(value, value_length);
175 d = pa_xmalloc(value_length*2+1);
177 if ((dn = pa_parsehex(v, d, value_length*2)) == (size_t) -1) {
186 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), k))) {
187 prop = pa_xnew(struct property, 1);
191 pa_xfree(prop->value);
200 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
205 /** Will accept only valid UTF-8 */
206 int pa_proplist_setf(pa_proplist *p, const char *key, const char *format, ...) {
207 struct property *prop;
216 if (!pa_proplist_key_valid(key) || !pa_utf8_valid(format))
219 va_start(ap, format);
220 v = pa_vsprintf_malloc(format, ap);
223 if (!pa_utf8_valid(v))
226 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
227 prop = pa_xnew(struct property, 1);
228 prop->key = pa_xstrdup(key);
231 pa_xfree(prop->value);
234 prop->nbytes = strlen(v)+1;
237 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
246 int pa_proplist_set(pa_proplist *p, const char *key, const void *data, size_t nbytes) {
247 struct property *prop;
252 pa_assert(data || nbytes == 0);
254 if (!pa_proplist_key_valid(key))
257 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key))) {
258 prop = pa_xnew(struct property, 1);
259 prop->key = pa_xstrdup(key);
262 pa_xfree(prop->value);
264 prop->value = pa_xmalloc(nbytes+1);
266 memcpy(prop->value, data, nbytes);
267 ((char*) prop->value)[nbytes] = 0;
268 prop->nbytes = nbytes;
271 pa_hashmap_put(MAKE_HASHMAP(p), prop->key, prop);
276 const char *pa_proplist_gets(pa_proplist *p, const char *key) {
277 struct property *prop;
282 if (!pa_proplist_key_valid(key))
285 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key)))
288 if (prop->nbytes <= 0)
291 if (((char*) prop->value)[prop->nbytes-1] != 0)
294 if (strlen((char*) prop->value) != prop->nbytes-1)
297 if (!pa_utf8_valid((char*) prop->value))
300 return (char*) prop->value;
303 int pa_proplist_get(pa_proplist *p, const char *key, const void **data, size_t *nbytes) {
304 struct property *prop;
311 if (!pa_proplist_key_valid(key))
314 if (!(prop = pa_hashmap_get(MAKE_HASHMAP(p), key)))
318 *nbytes = prop->nbytes;
323 void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, const pa_proplist *other) {
324 struct property *prop;
328 pa_assert(mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE);
331 if (mode == PA_UPDATE_SET)
332 pa_proplist_clear(p);
334 /* MAKE_HASHMAP turns the const pointer into a non-const pointer, but
335 * that's ok, because we don't modify the hashmap contents. */
336 while ((prop = pa_hashmap_iterate(MAKE_HASHMAP(other), &state, NULL))) {
338 if (mode == PA_UPDATE_MERGE && pa_proplist_contains(p, prop->key))
341 pa_assert_se(pa_proplist_set(p, prop->key, prop->value, prop->nbytes) == 0);
345 int pa_proplist_unset(pa_proplist *p, const char *key) {
346 struct property *prop;
351 if (!pa_proplist_key_valid(key))
354 if (!(prop = pa_hashmap_remove(MAKE_HASHMAP(p), key)))
361 int pa_proplist_unset_many(pa_proplist *p, const char * const keys[]) {
362 const char * const * k;
368 for (k = keys; *k; k++)
369 if (!pa_proplist_key_valid(*k))
372 for (k = keys; *k; k++)
373 if (pa_proplist_unset(p, *k) >= 0)
379 const char *pa_proplist_iterate(pa_proplist *p, void **state) {
380 struct property *prop;
382 if (!(prop = pa_hashmap_iterate(MAKE_HASHMAP(p), state, NULL)))
388 char *pa_proplist_to_string_sep(pa_proplist *p, const char *sep) {
396 buf = pa_strbuf_new();
398 while ((key = pa_proplist_iterate(p, &state))) {
401 if (!pa_strbuf_isempty(buf))
402 pa_strbuf_puts(buf, sep);
404 if ((v = pa_proplist_gets(p, key))) {
407 pa_strbuf_printf(buf, "%s = \"", key);
412 h = strcspn(t, "\"");
415 pa_strbuf_putsn(buf, t, h);
422 pa_assert(*t == '"');
423 pa_strbuf_puts(buf, "\\\"");
428 pa_strbuf_puts(buf, "\"");
434 pa_assert_se(pa_proplist_get(p, key, &value, &nbytes) == 0);
435 c = pa_xmalloc(nbytes*2+1);
436 pa_hexstr((const uint8_t*) value, nbytes, c, nbytes*2+1);
438 pa_strbuf_printf(buf, "%s = hex:%s", key, c);
443 return pa_strbuf_tostring_free(buf);
446 char *pa_proplist_to_string(pa_proplist *p) {
449 s = pa_proplist_to_string_sep(p, "\n");
450 t = pa_sprintf_malloc("%s\n", s);
456 pa_proplist *pa_proplist_from_string(const char *s) {
464 VALUE_DOUBLE_QUOTES_ESCAPE,
471 const char *p, *key = NULL, *value = NULL;
472 size_t key_len = 0, value_len = 0;
476 pl = pa_proplist_new();
488 else if (!isspace(*p)) {
500 else if (isspace(*p))
511 else if (!isspace(*p))
518 else if (strncmp(p, "hex:", 4) == 0) {
523 } else if (*p == '\'') {
527 } else if (*p == '"') {
528 state = VALUE_DOUBLE_QUOTES;
531 } else if (!isspace(*p)) {
532 state = VALUE_SIMPLE;
539 if (*p == 0 || isspace(*p)) {
540 if (proplist_setn(pl, key, key_len, value, value_len) < 0)
551 case VALUE_DOUBLE_QUOTES:
554 else if (*p == '"') {
557 v = pa_unescape(pa_xstrndup(value, value_len));
559 if (proplist_setn(pl, key, key_len, v, strlen(v)) < 0) {
566 } else if (*p == '\\') {
567 state = VALUE_DOUBLE_QUOTES_ESCAPE;
573 case VALUE_DOUBLE_QUOTES_ESCAPE:
577 state = VALUE_DOUBLE_QUOTES;
585 else if (*p == '\'') {
588 v = pa_unescape(pa_xstrndup(value, value_len));
590 if (proplist_setn(pl, key, key_len, v, strlen(v)) < 0) {
597 } else if (*p == '\\') {
598 state = VALUE_TICKS_ESCAPED;
604 case VALUE_TICKS_ESCAPED:
614 if ((*p >= '0' && *p <= '9') ||
615 (*p >= 'A' && *p <= 'F') ||
616 (*p >= 'a' && *p <= 'f')) {
618 } else if (*p == 0 || isspace(*p)) {
620 if (proplist_sethex(pl, key, key_len, value, value_len) < 0)
634 return MAKE_PROPLIST(pl);
637 pa_proplist_free(pl);
641 int pa_proplist_contains(pa_proplist *p, const char *key) {
645 if (!pa_proplist_key_valid(key))
648 if (!(pa_hashmap_get(MAKE_HASHMAP(p), key)))
654 void pa_proplist_clear(pa_proplist *p) {
657 pa_hashmap_remove_all(MAKE_HASHMAP(p), (pa_free_cb_t) property_free);
660 pa_proplist* pa_proplist_copy(const pa_proplist *p) {
663 pa_assert_se(copy = pa_proplist_new());
666 pa_proplist_update(copy, PA_UPDATE_REPLACE, p);
671 unsigned pa_proplist_size(pa_proplist *p) {
674 return pa_hashmap_size(MAKE_HASHMAP(p));
677 int pa_proplist_isempty(pa_proplist *p) {
680 return pa_hashmap_isempty(MAKE_HASHMAP(p));
683 int pa_proplist_equal(pa_proplist *a, pa_proplist *b) {
684 const void *key = NULL;
685 struct property *a_prop = NULL;
686 struct property *b_prop = NULL;
695 if (pa_proplist_size(a) != pa_proplist_size(b))
698 while ((a_prop = pa_hashmap_iterate(MAKE_HASHMAP(a), &state, &key))) {
699 if (!(b_prop = pa_hashmap_get(MAKE_HASHMAP(b), key)))
702 if (a_prop->nbytes != b_prop->nbytes)
705 if (memcmp(a_prop->value, b_prop->value, a_prop->nbytes) != 0)