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
28 #include <pulse/xmalloc.h>
29 #include <pulse/utf8.h>
30 #include <pulse/i18n.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_utf8_valid(key
))
58 static void property_free(struct property
*prop
) {
62 pa_xfree(prop
->value
);
66 pa_proplist
* pa_proplist_new(void) {
69 return MAKE_PROPLIST(pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
));
72 void pa_proplist_free(pa_proplist
* p
) {
76 pa_hashmap_free(MAKE_HASHMAP(p
), NULL
, NULL
);
79 /** Will accept only valid UTF-8 */
80 int pa_proplist_sets(pa_proplist
*p
, const char *key
, const char *value
) {
81 struct property
*prop
;
82 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 int pa_proplist_setf(pa_proplist
*p
, const char *key
, const char *format
, ...) {
115 if (!property_name_valid(key
) || !pa_utf8_valid(format
))
118 va_start(ap
, format
);
119 t
= pa_vsprintf_malloc(format
, ap
);
122 r
= pa_proplist_sets(p
, key
, t
);
128 int pa_proplist_set(pa_proplist
*p
, const char *key
, const void *data
, size_t nbytes
) {
129 struct property
*prop
;
130 pa_bool_t add
= FALSE
;
135 if (!property_name_valid(key
))
138 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
))) {
139 prop
= pa_xnew(struct property
, 1);
140 prop
->key
= pa_xstrdup(key
);
143 pa_xfree(prop
->value
);
145 prop
->value
= pa_xmemdup(data
, nbytes
);
146 prop
->nbytes
= nbytes
;
149 pa_hashmap_put(MAKE_HASHMAP(p
), prop
->key
, prop
);
154 const char *pa_proplist_gets(pa_proplist
*p
, const char *key
) {
155 struct property
*prop
;
160 if (!property_name_valid(key
))
163 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
166 if (prop
->nbytes
<= 0)
169 if (((char*) prop
->value
)[prop
->nbytes
-1] != 0)
172 if (strlen((char*) prop
->value
) != prop
->nbytes
-1)
175 if (!pa_utf8_valid((char*) prop
->value
))
178 return (char*) prop
->value
;
181 int pa_proplist_get(pa_proplist
*p
, const char *key
, const void **data
, size_t *nbytes
) {
182 struct property
*prop
;
187 if (!property_name_valid(key
))
190 if (!(prop
= pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
194 *nbytes
= prop
->nbytes
;
199 void pa_proplist_update(pa_proplist
*p
, pa_update_mode_t mode
, pa_proplist
*other
) {
200 struct property
*prop
;
204 pa_assert(mode
== PA_UPDATE_SET
|| mode
== PA_UPDATE_MERGE
|| mode
== PA_UPDATE_REPLACE
);
207 if (mode
== PA_UPDATE_SET
)
208 pa_proplist_clear(p
);
210 while ((prop
= pa_hashmap_iterate(MAKE_HASHMAP(other
), &state
, NULL
))) {
212 if (mode
== PA_UPDATE_MERGE
&& pa_proplist_contains(p
, prop
->key
))
215 pa_assert_se(pa_proplist_set(p
, prop
->key
, prop
->value
, prop
->nbytes
) == 0);
219 int pa_proplist_unset(pa_proplist
*p
, const char *key
) {
220 struct property
*prop
;
225 if (!property_name_valid(key
))
228 if (!(prop
= pa_hashmap_remove(MAKE_HASHMAP(p
), key
)))
235 int pa_proplist_unset_many(pa_proplist
*p
, const char * const keys
[]) {
236 const char * const * k
;
242 for (k
= keys
; *k
; k
++)
243 if (!property_name_valid(*k
))
246 for (k
= keys
; *k
; k
++)
247 if (pa_proplist_unset(p
, *k
) >= 0)
253 const char *pa_proplist_iterate(pa_proplist
*p
, void **state
) {
254 struct property
*prop
;
256 if (!(prop
= pa_hashmap_iterate(MAKE_HASHMAP(p
), state
, NULL
)))
262 char *pa_proplist_to_string(pa_proplist
*p
) {
269 buf
= pa_strbuf_new();
271 while ((key
= pa_proplist_iterate(p
, &state
))) {
275 if ((v
= pa_proplist_gets(p
, key
)))
276 pa_strbuf_printf(buf
, "%s = \"%s\"\n", key
, v
);
282 pa_assert_se(pa_proplist_get(p
, key
, &value
, &nbytes
) == 0);
283 c
= pa_xnew(char, nbytes
*2+1);
284 pa_hexstr((const uint8_t*) value
, nbytes
, c
, nbytes
*2+1);
286 pa_strbuf_printf(buf
, "%s = hex:%s\n", key
, c
);
291 return pa_strbuf_tostring_free(buf
);
294 int pa_proplist_contains(pa_proplist
*p
, const char *key
) {
298 if (!property_name_valid(key
))
301 if (!(pa_hashmap_get(MAKE_HASHMAP(p
), key
)))
307 void pa_proplist_clear(pa_proplist
*p
) {
308 struct property
*prop
;
311 while ((prop
= pa_hashmap_steal_first(MAKE_HASHMAP(p
))))
315 pa_proplist
* pa_proplist_copy(pa_proplist
*template) {
318 pa_assert_se(p
= pa_proplist_new());
321 pa_proplist_update(p
, PA_UPDATE_REPLACE
, template);