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
[]) {
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
) {
78 pa_hashmap
*map
= NULL
;
80 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
83 enum { WHITESPACE
, KEY
, VALUE_START
, VALUE_SIMPLE
, VALUE_DOUBLE_QUOTES
, VALUE_TICKS
} state
;
84 const char *p
, *key
, *value
;
85 size_t key_len
= 0, value_len
= 0;
89 for (p
= args
; *p
; p
++) {
94 else if (!isspace(*p
)) {
111 } else if (*p
== '"') {
112 state
= VALUE_DOUBLE_QUOTES
;
115 } else if (isspace(*p
)) {
116 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
120 state
= VALUE_SIMPLE
;
127 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
133 case VALUE_DOUBLE_QUOTES
:
135 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
143 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
152 if (state
== VALUE_START
) {
153 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
155 } else if (state
== VALUE_SIMPLE
) {
156 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
158 } else if (state
!= WHITESPACE
)
162 return (pa_modargs
*) map
;
167 pa_modargs_free((pa_modargs
*) map
);
172 static void free_func(void *p
, PA_GCC_UNUSED
void*userdata
) {
181 void pa_modargs_free(pa_modargs
*ma
) {
182 pa_hashmap
*map
= (pa_hashmap
*) ma
;
183 pa_hashmap_free(map
, free_func
, NULL
);
186 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
187 pa_hashmap
*map
= (pa_hashmap
*) ma
;
190 if (!(e
= pa_hashmap_get(map
, key
)))
196 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
203 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
206 if (pa_atou(v
, value
) < 0)
212 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
219 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
222 if (pa_atoi(v
, value
) < 0)
228 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, pa_bool_t
*value
) {
236 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
242 if ((r
= pa_parse_boolean(v
)) < 0)
249 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
258 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
261 channels
= ss
.channels
;
262 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
264 ss
.channels
= (uint8_t) channels
;
266 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
267 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
270 if (!pa_sample_spec_valid(&ss
))
278 int pa_modargs_get_channel_map(pa_modargs
*ma
, const char *name
, pa_channel_map
*rmap
) {
287 if ((cm
= pa_modargs_get_value(ma
, name
? name
: "channel_map", NULL
)))
288 if (!pa_channel_map_parse(&map
, cm
))
291 if (!pa_channel_map_valid(&map
))
298 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
) {
308 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0)
311 if (!pa_channel_map_init_auto(&map
, ss
.channels
, def
))
314 if (pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)
317 if (map
.channels
!= ss
.channels
)