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>
31 #include <pulse/i18n.h>
33 #include <pulsecore/hashmap.h>
34 #include <pulsecore/strbuf.h>
35 #include <pulsecore/core-util.h>
45 #define MAKE_HASHMAP(p) ((pa_hashmap*) (p))
46 #define MAKE_PROPLIST(p) ((pa_proplist*) (p))
48 static pa_bool_t
property_name_valid(const char *key
) {
50 if (!pa_ascii_valid(key
))
59 static void property_free(struct property
*prop
) {
63 pa_xfree(prop
->value
);
67 pa_proplist
* pa_proplist_new(void) {
68 return MAKE_PROPLIST(pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
));
71 void pa_proplist_free(pa_proplist
* p
) {
75 pa_hashmap_free(MAKE_HASHMAP(p
), NULL
, NULL
);
78 /** Will accept only valid UTF-8 */
79 int pa_proplist_sets(pa_proplist
*p
, const char *key
, const char *value
) {
80 struct property
*prop
;
81 pa_bool_t add
= FALSE
;
87 if (!property_name_valid(key
) || !pa_utf8_valid(value
))
90 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
91 prop
= pa_xnew(struct property
, 1);
92 prop
->key
= pa_xstrdup(key
);
95 pa_xfree(prop
->value
);
97 prop
->value
= pa_xstrdup(value
);
98 prop
->nbytes
= strlen(value
)+1;
101 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
106 /** Will accept only valid UTF-8 */
107 static int proplist_setn(pa_proplist
*p
, const char *key
, size_t key_length
, const char *value
, size_t value_length
) {
108 struct property
*prop
;
109 pa_bool_t add
= FALSE
;
116 k
= pa_xstrndup(key
, key_length
);
117 v
= pa_xstrndup(value
, value_length
);
119 if (!property_name_valid(k
) || !pa_utf8_valid(v
)) {
125 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), k
))) {
126 prop
= pa_xnew(struct property
, 1);
130 pa_xfree(prop
->value
);
135 prop
->nbytes
= strlen(v
)+1;
138 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
143 static int proplist_sethex(pa_proplist
*p
, const char *key
, size_t key_length
, const char *value
, size_t value_length
) {
144 struct property
*prop
;
145 pa_bool_t add
= FALSE
;
154 k
= pa_xstrndup(key
, key_length
);
156 if (!property_name_valid(k
)) {
161 v
= pa_xstrndup(value
, value_length
);
162 d
= pa_xmalloc(value_length
*2+1);
164 if ((dn
= pa_parsehex(v
, d
, value_length
*2)) == (size_t) -1) {
173 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), k
))) {
174 prop
= pa_xnew(struct property
, 1);
178 pa_xfree(prop
->value
);
187 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
192 /** Will accept only valid UTF-8 */
193 int pa_proplist_setf(pa_proplist
*p
, const char *key
, const char *format
, ...) {
194 struct property
*prop
;
195 pa_bool_t add
= FALSE
;
203 if (!property_name_valid(key
) || !pa_utf8_valid(format
))
206 va_start(ap
, format
);
207 v
= pa_vsprintf_malloc(format
, ap
);
210 if (!pa_utf8_valid(v
))
213 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
214 prop
= pa_xnew(struct property
, 1);
215 prop
->key
= pa_xstrdup(key
);
218 pa_xfree(prop
->value
);
221 prop
->nbytes
= strlen(v
)+1;
224 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
233 int pa_proplist_set(pa_proplist
*p
, const char *key
, const void *data
, size_t nbytes
) {
234 struct property
*prop
;
235 pa_bool_t add
= FALSE
;
241 if (!property_name_valid(key
))
244 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
245 prop
= pa_xnew(struct property
, 1);
246 prop
->key
= pa_xstrdup(key
);
249 pa_xfree(prop
->value
);
251 prop
->value
= pa_xmalloc(nbytes
+1);
252 memcpy(prop
->value
, data
, nbytes
);
253 ((char*) prop
->value
)[nbytes
] = 0;
254 prop
->nbytes
= nbytes
;
257 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
262 const char *pa_proplist_gets(pa_proplist
*p
, const char *key
) {
263 struct property
*prop
;
268 if (!property_name_valid(key
))
271 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
274 if (prop
->nbytes
<= 0)
277 if (((char*) prop
->value
)[prop
->nbytes
-1] != 0)
280 if (strlen((char*) prop
->value
) != prop
->nbytes
-1)
283 if (!pa_utf8_valid((char*) prop
->value
))
286 return (char*) prop
->value
;
289 int pa_proplist_get(pa_proplist
*p
, const char *key
, const void **data
, size_t *nbytes
) {
290 struct property
*prop
;
297 if (!property_name_valid(key
))
300 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
304 *nbytes
= prop
->nbytes
;
309 void pa_proplist_update(pa_proplist
*p
, pa_update_mode_t mode
, pa_proplist
*other
) {
310 struct property
*prop
;
314 pa_assert(mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
);
317 if (mode
== PA_UPDATE_SET
)
318 pa_proplist_clear(p
);
320 while ((prop
= pa_hashmap_iterate(MAKE_HASHMAP(other
), &state
, NULL
))) {
322 if (mode
== PA_UPDATE_MERGE
&& pa_proplist_contains(p
, prop
->key
))
325 pa_assert_se(pa_proplist_set(p
, prop
->key
, prop
->value
, prop
->nbytes
) == 0);
329 int pa_proplist_unset(pa_proplist
*p
, const char *key
) {
330 struct property
*prop
;
335 if (!property_name_valid(key
))
338 if (!(prop
= pa_hashmap_remove(MAKE_HASHMAP(p
), key
)))
345 int pa_proplist_unset_many(pa_proplist
*p
, const char * const keys
[]) {
346 const char * const * k
;
352 for (k
= keys
; *k
; k
++)
353 if (!property_name_valid(*k
))
356 for (k
= keys
; *k
; k
++)
357 if (pa_proplist_unset(p
, *k
) >= 0)
363 const char *pa_proplist_iterate(pa_proplist
*p
, void **state
) {
364 struct property
*prop
;
366 if (!(prop
= pa_hashmap_iterate(MAKE_HASHMAP(p
), state
, NULL
)))
372 char *pa_proplist_to_string_sep(pa_proplist
*p
, const char *sep
) {
380 buf
= pa_strbuf_new();
382 while ((key
= pa_proplist_iterate(p
, &state
))) {
385 if (!pa_strbuf_isempty(buf
))
386 pa_strbuf_puts(buf
, sep
);
388 if ((v
= pa_proplist_gets(p
, key
))) {
391 pa_strbuf_printf(buf
, "%s = \"", key
);
396 h
= strcspn(t
, "\"");
399 pa_strbuf_putsn(buf
, t
, h
);
406 pa_assert(*t
== '"');
407 pa_strbuf_puts(buf
, "\\\"");
412 pa_strbuf_puts(buf
, "\"");
418 pa_assert_se(pa_proplist_get(p
, key
, &value
, &nbytes
) == 0);
419 c
= pa_xmalloc(nbytes
*2+1);
420 pa_hexstr((const uint8_t*) value
, nbytes
, c
, nbytes
*2+1);
422 pa_strbuf_printf(buf
, "%s = hex:%s", key
, c
);
427 return pa_strbuf_tostring_free(buf
);
430 char *pa_proplist_to_string(pa_proplist
*p
) {
433 s
= pa_proplist_to_string_sep(p
, "\n");
434 t
= pa_sprintf_malloc("%s\n", s
);
440 pa_proplist
*pa_proplist_from_string(const char *s
) {
448 VALUE_DOUBLE_QUOTES_ESCAPE
,
455 const char *p
, *key
= NULL
, *value
= NULL
;
456 size_t key_len
= 0, value_len
= 0;
460 pl
= pa_proplist_new();
472 else if (!isspace(*p
)) {
484 else if (isspace(*p
))
495 else if (!isspace(*p
))
502 else if (strncmp(p
, "hex:", 4) == 0) {
507 } else if (*p
== '\'') {
511 } else if (*p
== '"') {
512 state
= VALUE_DOUBLE_QUOTES
;
515 } else if (!isspace(*p
)) {
516 state
= VALUE_SIMPLE
;
523 if (*p
== 0 || isspace(*p
)) {
524 if (proplist_setn(pl
, key
, key_len
, value
, value_len
) < 0)
535 case VALUE_DOUBLE_QUOTES
:
538 else if (*p
== '"') {
541 v
= pa_unescape(pa_xstrndup(value
, value_len
));
543 if (proplist_setn(pl
, key
, key_len
, v
, strlen(v
)) < 0) {
550 } else if (*p
== '\\') {
551 state
= VALUE_DOUBLE_QUOTES_ESCAPE
;
557 case VALUE_DOUBLE_QUOTES_ESCAPE
:
561 state
= VALUE_DOUBLE_QUOTES
;
569 else if (*p
== '\'') {
572 v
= pa_unescape(pa_xstrndup(value
, value_len
));
574 if (proplist_setn(pl
, key
, key_len
, v
, strlen(v
)) < 0) {
581 } else if (*p
== '\\') {
582 state
= VALUE_TICKS_ESCAPED
;
588 case VALUE_TICKS_ESCAPED
:
598 if ((*p
>= '0' && *p
<= '9') ||
599 (*p
>= 'A' && *p
<= 'F') ||
600 (*p
>= 'a' && *p
<= 'f')) {
602 } else if (*p
== 0 || isspace(*p
)) {
604 if (proplist_sethex(pl
, key
, key_len
, value
, value_len
) < 0)
618 return MAKE_PROPLIST(pl
);
621 pa_proplist_free(pl
);
625 int pa_proplist_contains(pa_proplist
*p
, const char *key
) {
629 if (!property_name_valid(key
))
632 if (!(pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
638 void pa_proplist_clear(pa_proplist
*p
) {
639 struct property
*prop
;
642 while ((prop
= pa_hashmap_steal_first(MAKE_HASHMAP(p
))))
646 pa_proplist
* pa_proplist_copy(pa_proplist
*template) {
649 pa_assert_se(p
= pa_proplist_new());
652 pa_proplist_update(p
, PA_UPDATE_REPLACE
, template);
657 unsigned pa_proplist_size(pa_proplist
*p
) {
660 return pa_hashmap_size(MAKE_HASHMAP(p
));
663 int pa_proplist_isempty(pa_proplist
*p
) {
666 return pa_hashmap_isempty(MAKE_HASHMAP(p
));