4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/hashmap.h>
35 #include <pulsecore/idxset.h>
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/source.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/macro.h>
49 static int add_key_value(pa_hashmap
*map
, char *key
, char *value
, const char* const valid_keys
[]) {
56 if (pa_hashmap_get(map
, key
)) {
64 for (v
= valid_keys
; *v
; v
++)
65 if (strcmp(*v
, key
) == 0)
75 e
= pa_xnew(struct entry
, 1);
78 pa_hashmap_put(map
, key
, e
);
83 pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
84 pa_hashmap
*map
= NULL
;
86 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
98 const char *p
, *key
, *value
;
99 size_t key_len
= 0, value_len
= 0;
103 for (p
= args
; *p
; p
++) {
108 else if (!isspace(*p
)) {
117 else if (isspace(*p
))
127 } else if (*p
== '"') {
128 state
= VALUE_DOUBLE_QUOTES
;
131 } else if (isspace(*p
)) {
132 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
136 state
= VALUE_SIMPLE
;
143 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
149 case VALUE_DOUBLE_QUOTES
:
151 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
159 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
168 if (state
== VALUE_START
) {
169 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
171 } else if (state
== VALUE_SIMPLE
) {
172 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
174 } else if (state
!= WHITESPACE
)
178 return (pa_modargs
*) map
;
183 pa_modargs_free((pa_modargs
*) map
);
188 static void free_func(void *p
, PA_GCC_UNUSED
void*userdata
) {
197 void pa_modargs_free(pa_modargs
*ma
) {
198 pa_hashmap
*map
= (pa_hashmap
*) ma
;
199 pa_hashmap_free(map
, free_func
, NULL
);
202 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
203 pa_hashmap
*map
= (pa_hashmap
*) ma
;
206 if (!(e
= pa_hashmap_get(map
, key
)))
212 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
219 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
222 if (pa_atou(v
, value
) < 0)
228 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
235 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
238 if (pa_atoi(v
, value
) < 0)
244 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, pa_bool_t
*value
) {
252 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
258 if ((r
= pa_parse_boolean(v
)) < 0)
265 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
274 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
277 channels
= ss
.channels
;
278 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
280 ss
.channels
= (uint8_t) channels
;
282 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
283 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
286 if (!pa_sample_spec_valid(&ss
))
294 int pa_modargs_get_channel_map(pa_modargs
*ma
, const char *name
, pa_channel_map
*rmap
) {
303 if ((cm
= pa_modargs_get_value(ma
, name
? name
: "channel_map", NULL
)))
304 if (!pa_channel_map_parse(&map
, cm
))
307 if (!pa_channel_map_valid(&map
))
314 int pa_modargs_get_sample_spec_and_channel_map(pa_modargs
*ma
, pa_sample_spec
*rss
, pa_channel_map
*rmap
, pa_channel_map_def_t def
) {
324 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0)
327 if (!pa_channel_map_init_auto(&map
, ss
.channels
, def
))
330 if (pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)
333 if (map
.channels
!= ss
.channels
)