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 static pa_bool_t
property_name_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
) {
74 pa_hashmap_free(MAKE_HASHMAP(p
), NULL
, NULL
);
77 /** Will accept only valid UTF-8 */
78 int pa_proplist_sets(pa_proplist
*p
, const char *key
, const char *value
) {
79 struct property
*prop
;
80 pa_bool_t add
= FALSE
;
86 if (!property_name_valid(key
) || !pa_utf8_valid(value
))
89 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
90 prop
= pa_xnew(struct property
, 1);
91 prop
->key
= pa_xstrdup(key
);
94 pa_xfree(prop
->value
);
96 prop
->value
= pa_xstrdup(value
);
97 prop
->nbytes
= strlen(value
)+1;
100 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
105 /** Will accept only valid UTF-8 */
106 static int proplist_setn(pa_proplist
*p
, const char *key
, size_t key_length
, const char *value
, size_t value_length
) {
107 struct property
*prop
;
108 pa_bool_t add
= FALSE
;
115 k
= pa_xstrndup(key
, key_length
);
116 v
= pa_xstrndup(value
, value_length
);
118 if (!property_name_valid(k
) || !pa_utf8_valid(v
)) {
124 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), k
))) {
125 prop
= pa_xnew(struct property
, 1);
129 pa_xfree(prop
->value
);
134 prop
->nbytes
= strlen(v
)+1;
137 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
142 /** Will accept only valid UTF-8 */
143 int pa_proplist_setp(pa_proplist
*p
, const char *pair
) {
149 if (!(t
= strchr(pair
, '=')))
152 return proplist_setn(p
,
154 t
+ 1, strchr(pair
, 0) - t
- 1);
157 static int proplist_sethex(pa_proplist
*p
, const char *key
, size_t key_length
, const char *value
, size_t value_length
) {
158 struct property
*prop
;
159 pa_bool_t add
= FALSE
;
168 k
= pa_xstrndup(key
, key_length
);
170 if (!property_name_valid(k
)) {
175 v
= pa_xstrndup(value
, value_length
);
176 d
= pa_xmalloc(value_length
*2+1);
178 if ((dn
= pa_parsehex(v
, d
, value_length
*2)) == (size_t) -1) {
187 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), k
))) {
188 prop
= pa_xnew(struct property
, 1);
192 pa_xfree(prop
->value
);
201 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
206 /** Will accept only valid UTF-8 */
207 int pa_proplist_setf(pa_proplist
*p
, const char *key
, const char *format
, ...) {
208 struct property
*prop
;
209 pa_bool_t add
= FALSE
;
217 if (!property_name_valid(key
) || !pa_utf8_valid(format
))
220 va_start(ap
, format
);
221 v
= pa_vsprintf_malloc(format
, ap
);
224 if (!pa_utf8_valid(v
))
227 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
228 prop
= pa_xnew(struct property
, 1);
229 prop
->key
= pa_xstrdup(key
);
232 pa_xfree(prop
->value
);
235 prop
->nbytes
= strlen(v
)+1;
238 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
247 int pa_proplist_set(pa_proplist
*p
, const char *key
, const void *data
, size_t nbytes
) {
248 struct property
*prop
;
249 pa_bool_t add
= FALSE
;
253 pa_assert(data
|| nbytes
== 0);
255 if (!property_name_valid(key
))
258 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
259 prop
= pa_xnew(struct property
, 1);
260 prop
->key
= pa_xstrdup(key
);
263 pa_xfree(prop
->value
);
265 prop
->value
= pa_xmalloc(nbytes
+1);
267 memcpy(prop
->value
, data
, nbytes
);
268 ((char*) prop
->value
)[nbytes
] = 0;
269 prop
->nbytes
= nbytes
;
272 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
277 const char *pa_proplist_gets(pa_proplist
*p
, const char *key
) {
278 struct property
*prop
;
283 if (!property_name_valid(key
))
286 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
289 if (prop
->nbytes
<= 0)
292 if (((char*) prop
->value
)[prop
->nbytes
-1] != 0)
295 if (strlen((char*) prop
->value
) != prop
->nbytes
-1)
298 if (!pa_utf8_valid((char*) prop
->value
))
301 return (char*) prop
->value
;
304 int pa_proplist_get(pa_proplist
*p
, const char *key
, const void **data
, size_t *nbytes
) {
305 struct property
*prop
;
312 if (!property_name_valid(key
))
315 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
319 *nbytes
= prop
->nbytes
;
324 void pa_proplist_update(pa_proplist
*p
, pa_update_mode_t mode
, const pa_proplist
*other
) {
325 struct property
*prop
;
329 pa_assert(mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
);
332 if (mode
== PA_UPDATE_SET
)
333 pa_proplist_clear(p
);
335 /* MAKE_HASHMAP turns the const pointer into a non-const pointer, but
336 * that's ok, because we don't modify the hashmap contents. */
337 while ((prop
= pa_hashmap_iterate(MAKE_HASHMAP(other
), &state
, NULL
))) {
339 if (mode
== PA_UPDATE_MERGE
&& pa_proplist_contains(p
, prop
->key
))
342 pa_assert_se(pa_proplist_set(p
, prop
->key
, prop
->value
, prop
->nbytes
) == 0);
346 int pa_proplist_unset(pa_proplist
*p
, const char *key
) {
347 struct property
*prop
;
352 if (!property_name_valid(key
))
355 if (!(prop
= pa_hashmap_remove(MAKE_HASHMAP(p
), key
)))
362 int pa_proplist_unset_many(pa_proplist
*p
, const char * const keys
[]) {
363 const char * const * k
;
369 for (k
= keys
; *k
; k
++)
370 if (!property_name_valid(*k
))
373 for (k
= keys
; *k
; k
++)
374 if (pa_proplist_unset(p
, *k
) >= 0)
380 const char *pa_proplist_iterate(pa_proplist
*p
, void **state
) {
381 struct property
*prop
;
383 if (!(prop
= pa_hashmap_iterate(MAKE_HASHMAP(p
), state
, NULL
)))
389 char *pa_proplist_to_string_sep(pa_proplist
*p
, const char *sep
) {
397 buf
= pa_strbuf_new();
399 while ((key
= pa_proplist_iterate(p
, &state
))) {
402 if (!pa_strbuf_isempty(buf
))
403 pa_strbuf_puts(buf
, sep
);
405 if ((v
= pa_proplist_gets(p
, key
))) {
408 pa_strbuf_printf(buf
, "%s = \"", key
);
413 h
= strcspn(t
, "\"");
416 pa_strbuf_putsn(buf
, t
, h
);
423 pa_assert(*t
== '"');
424 pa_strbuf_puts(buf
, "\\\"");
429 pa_strbuf_puts(buf
, "\"");
435 pa_assert_se(pa_proplist_get(p
, key
, &value
, &nbytes
) == 0);
436 c
= pa_xmalloc(nbytes
*2+1);
437 pa_hexstr((const uint8_t*) value
, nbytes
, c
, nbytes
*2+1);
439 pa_strbuf_printf(buf
, "%s = hex:%s", key
, c
);
444 return pa_strbuf_tostring_free(buf
);
447 char *pa_proplist_to_string(pa_proplist
*p
) {
450 s
= pa_proplist_to_string_sep(p
, "\n");
451 t
= pa_sprintf_malloc("%s\n", s
);
457 pa_proplist
*pa_proplist_from_string(const char *s
) {
465 VALUE_DOUBLE_QUOTES_ESCAPE
,
472 const char *p
, *key
= NULL
, *value
= NULL
;
473 size_t key_len
= 0, value_len
= 0;
477 pl
= pa_proplist_new();
489 else if (!isspace(*p
)) {
501 else if (isspace(*p
))
512 else if (!isspace(*p
))
519 else if (strncmp(p
, "hex:", 4) == 0) {
524 } else if (*p
== '\'') {
528 } else if (*p
== '"') {
529 state
= VALUE_DOUBLE_QUOTES
;
532 } else if (!isspace(*p
)) {
533 state
= VALUE_SIMPLE
;
540 if (*p
== 0 || isspace(*p
)) {
541 if (proplist_setn(pl
, key
, key_len
, value
, value_len
) < 0)
552 case VALUE_DOUBLE_QUOTES
:
555 else if (*p
== '"') {
558 v
= pa_unescape(pa_xstrndup(value
, value_len
));
560 if (proplist_setn(pl
, key
, key_len
, v
, strlen(v
)) < 0) {
567 } else if (*p
== '\\') {
568 state
= VALUE_DOUBLE_QUOTES_ESCAPE
;
574 case VALUE_DOUBLE_QUOTES_ESCAPE
:
578 state
= VALUE_DOUBLE_QUOTES
;
586 else if (*p
== '\'') {
589 v
= pa_unescape(pa_xstrndup(value
, value_len
));
591 if (proplist_setn(pl
, key
, key_len
, v
, strlen(v
)) < 0) {
598 } else if (*p
== '\\') {
599 state
= VALUE_TICKS_ESCAPED
;
605 case VALUE_TICKS_ESCAPED
:
615 if ((*p
>= '0' && *p
<= '9') ||
616 (*p
>= 'A' && *p
<= 'F') ||
617 (*p
>= 'a' && *p
<= 'f')) {
619 } else if (*p
== 0 || isspace(*p
)) {
621 if (proplist_sethex(pl
, key
, key_len
, value
, value_len
) < 0)
635 return MAKE_PROPLIST(pl
);
638 pa_proplist_free(pl
);
642 int pa_proplist_contains(pa_proplist
*p
, const char *key
) {
646 if (!property_name_valid(key
))
649 if (!(pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
655 void pa_proplist_clear(pa_proplist
*p
) {
656 struct property
*prop
;
659 while ((prop
= pa_hashmap_steal_first(MAKE_HASHMAP(p
))))
663 pa_proplist
* pa_proplist_copy(const pa_proplist
*p
) {
666 pa_assert_se(copy
= pa_proplist_new());
669 pa_proplist_update(copy
, PA_UPDATE_REPLACE
, p
);
674 unsigned pa_proplist_size(pa_proplist
*p
) {
677 return pa_hashmap_size(MAKE_HASHMAP(p
));
680 int pa_proplist_isempty(pa_proplist
*p
) {
683 return pa_hashmap_isempty(MAKE_HASHMAP(p
));
686 int pa_proplist_equal(pa_proplist
*a
, pa_proplist
*b
) {
687 const void *key
= NULL
;
688 struct property
*a_prop
= NULL
;
689 struct property
*b_prop
= NULL
;
698 if (pa_proplist_size(a
) != pa_proplist_size(b
))
701 while ((a_prop
= pa_hashmap_iterate(MAKE_HASHMAP(a
), &state
, &key
))) {
702 if (!(b_prop
= pa_hashmap_get(MAKE_HASHMAP(b
), key
)))
705 if (a_prop
->nbytes
!= b_prop
->nbytes
)
708 if (memcmp(a_prop
->value
, b_prop
->value
, a_prop
->nbytes
) != 0)