2 This file is part of PulseAudio.
4 Copyright 2004-2006 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 published
8 by the Free Software Foundation; either version 2.1 of the License,
9 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 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
43 static int add_key_value(pa_hashmap
*map
, char *key
, char *value
, const char* const valid_keys
[]) {
50 if (pa_hashmap_get(map
, key
)) {
58 for (v
= valid_keys
; *v
; v
++)
59 if (strcmp(*v
, key
) == 0)
69 e
= pa_xnew(struct entry
, 1);
72 pa_hashmap_put(map
, key
, e
);
77 pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
85 VALUE_DOUBLE_QUOTES_ESCAPED
,
90 const char *p
, *key
= NULL
, *value
= NULL
;
91 size_t key_len
= 0, value_len
= 0;
94 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
97 return (pa_modargs
*) map
;
101 for (p
= args
; *p
; p
++) {
107 else if (!isspace(*p
)) {
117 else if (isspace(*p
))
128 } else if (*p
== '"') {
129 state
= VALUE_DOUBLE_QUOTES
;
132 } else if (isspace(*p
)) {
133 if (add_key_value(map
,
134 pa_xstrndup(key
, key_len
),
139 } else if (*p
== '\\') {
140 state
= VALUE_SIMPLE_ESCAPED
;
144 state
= VALUE_SIMPLE
;
152 if (add_key_value(map
,
153 pa_xstrndup(key
, key_len
),
154 pa_unescape(pa_xstrndup(value
, value_len
)),
158 } else if (*p
== '\\') {
159 state
= VALUE_SIMPLE_ESCAPED
;
165 case VALUE_SIMPLE_ESCAPED
:
166 state
= VALUE_SIMPLE
;
170 case VALUE_DOUBLE_QUOTES
:
172 if (add_key_value(map
,
173 pa_xstrndup(key
, key_len
),
174 pa_unescape(pa_xstrndup(value
, value_len
)),
178 } else if (*p
== '\\') {
179 state
= VALUE_DOUBLE_QUOTES_ESCAPED
;
185 case VALUE_DOUBLE_QUOTES_ESCAPED
:
186 state
= VALUE_DOUBLE_QUOTES
;
192 if (add_key_value(map
,
193 pa_xstrndup(key
, key_len
),
194 pa_unescape(pa_xstrndup(value
, value_len
)),
198 } else if (*p
== '\\') {
199 state
= VALUE_TICKS_ESCAPED
;
205 case VALUE_TICKS_ESCAPED
:
212 if (state
== VALUE_START
) {
213 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
215 } else if (state
== VALUE_SIMPLE
) {
216 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
218 } else if (state
!= WHITESPACE
)
221 return (pa_modargs
*) map
;
225 pa_modargs_free((pa_modargs
*) map
);
230 static void free_func(void *p
, void*userdata
) {
239 void pa_modargs_free(pa_modargs
*ma
) {
240 pa_hashmap
*map
= (pa_hashmap
*) ma
;
241 pa_hashmap_free(map
, free_func
, NULL
);
244 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
245 pa_hashmap
*map
= (pa_hashmap
*) ma
;
248 if (!(e
= pa_hashmap_get(map
, key
)))
254 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
261 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
264 if (pa_atou(v
, value
) < 0)
270 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
277 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
280 if (pa_atoi(v
, value
) < 0)
286 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, pa_bool_t
*value
) {
294 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
300 if ((r
= pa_parse_boolean(v
)) < 0)
307 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
316 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0 ||
318 ss
.rate
> PA_RATE_MAX
)
321 channels
= ss
.channels
;
322 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0 ||
324 channels
>= PA_CHANNELS_MAX
)
326 ss
.channels
= (uint8_t) channels
;
328 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
329 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
332 if (!pa_sample_spec_valid(&ss
))
340 int pa_modargs_get_channel_map(pa_modargs
*ma
, const char *name
, pa_channel_map
*rmap
) {
349 if ((cm
= pa_modargs_get_value(ma
, name
? name
: "channel_map", NULL
)))
350 if (!pa_channel_map_parse(&map
, cm
))
353 if (!pa_channel_map_valid(&map
))
360 int pa_modargs_get_sample_spec_and_channel_map(
363 pa_channel_map
*rmap
,
364 pa_channel_map_def_t def
) {
375 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0)
380 if (ss
.channels
!= map
.channels
)
381 pa_channel_map_init_extend(&map
, ss
.channels
, def
);
383 if (pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)
386 if (map
.channels
!= ss
.channels
)
395 int pa_modargs_get_proplist(pa_modargs
*ma
, const char *name
, pa_proplist
*p
, pa_update_mode_t m
) {
403 if (!(v
= pa_modargs_get_value(ma
, name
, NULL
)))
406 if (!(n
= pa_proplist_from_string(v
)))
409 pa_proplist_update(p
, m
, n
);
415 const char *pa_modargs_iterate(pa_modargs
*ma
, void **state
) {
416 pa_hashmap
*map
= (pa_hashmap
*) ma
;
419 if (!(e
= pa_hashmap_iterate(map
, state
, NULL
)))