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 /** Will accept only valid UTF-8 */
144 int pa_proplist_setp(pa_proplist
*p
, const char *pair
) {
150 if (!(t
= strchr(pair
, '=')))
153 return proplist_setn(p
,
155 t
+ 1, strchr(pair
, 0) - t
- 1);
158 static int proplist_sethex(pa_proplist
*p
, const char *key
, size_t key_length
, const char *value
, size_t value_length
) {
159 struct property
*prop
;
160 pa_bool_t add
= FALSE
;
169 k
= pa_xstrndup(key
, key_length
);
171 if (!property_name_valid(k
)) {
176 v
= pa_xstrndup(value
, value_length
);
177 d
= pa_xmalloc(value_length
*2+1);
179 if ((dn
= pa_parsehex(v
, d
, value_length
*2)) == (size_t) -1) {
188 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), k
))) {
189 prop
= pa_xnew(struct property
, 1);
193 pa_xfree(prop
->value
);
202 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
207 /** Will accept only valid UTF-8 */
208 int pa_proplist_setf(pa_proplist
*p
, const char *key
, const char *format
, ...) {
209 struct property
*prop
;
210 pa_bool_t add
= FALSE
;
218 if (!property_name_valid(key
) || !pa_utf8_valid(format
))
221 va_start(ap
, format
);
222 v
= pa_vsprintf_malloc(format
, ap
);
225 if (!pa_utf8_valid(v
))
228 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
229 prop
= pa_xnew(struct property
, 1);
230 prop
->key
= pa_xstrdup(key
);
233 pa_xfree(prop
->value
);
236 prop
->nbytes
= strlen(v
)+1;
239 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
248 int pa_proplist_set(pa_proplist
*p
, const char *key
, const void *data
, size_t nbytes
) {
249 struct property
*prop
;
250 pa_bool_t add
= FALSE
;
254 pa_assert(data
|| nbytes
== 0);
256 if (!property_name_valid(key
))
259 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
260 prop
= pa_xnew(struct property
, 1);
261 prop
->key
= pa_xstrdup(key
);
264 pa_xfree(prop
->value
);
266 prop
->value
= pa_xmalloc(nbytes
+1);
268 memcpy(prop
->value
, data
, nbytes
);
269 ((char*) prop
->value
)[nbytes
] = 0;
270 prop
->nbytes
= nbytes
;
273 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
278 const char *pa_proplist_gets(pa_proplist
*p
, const char *key
) {
279 struct property
*prop
;
284 if (!property_name_valid(key
))
287 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
290 if (prop
->nbytes
<= 0)
293 if (((char*) prop
->value
)[prop
->nbytes
-1] != 0)
296 if (strlen((char*) prop
->value
) != prop
->nbytes
-1)
299 if (!pa_utf8_valid((char*) prop
->value
))
302 return (char*) prop
->value
;
305 int pa_proplist_get(pa_proplist
*p
, const char *key
, const void **data
, size_t *nbytes
) {
306 struct property
*prop
;
313 if (!property_name_valid(key
))
316 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
320 *nbytes
= prop
->nbytes
;
325 void pa_proplist_update(pa_proplist
*p
, pa_update_mode_t mode
, pa_proplist
*other
) {
326 struct property
*prop
;
330 pa_assert(mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
);
333 if (mode
== PA_UPDATE_SET
)
334 pa_proplist_clear(p
);
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 (!property_name_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 (!property_name_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 (!property_name_valid(key
))
648 if (!(pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
654 void pa_proplist_clear(pa_proplist
*p
) {
655 struct property
*prop
;
658 while ((prop
= pa_hashmap_steal_first(MAKE_HASHMAP(p
))))
662 pa_proplist
* pa_proplist_copy(pa_proplist
*template) {
665 pa_assert_se(p
= pa_proplist_new());
668 pa_proplist_update(p
, PA_UPDATE_REPLACE
, template);
673 unsigned pa_proplist_size(pa_proplist
*p
) {
676 return pa_hashmap_size(MAKE_HASHMAP(p
));
679 int pa_proplist_isempty(pa_proplist
*p
) {
682 return pa_hashmap_isempty(MAKE_HASHMAP(p
));