4 This file is part of polypaudio.
6 polypaudio 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 of the License,
9 or (at your option) any later version.
11 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <polyp/xmalloc.h>
33 #include <polypcore/hashmap.h>
34 #include <polypcore/idxset.h>
35 #include <polypcore/sample-util.h>
36 #include <polypcore/namereg.h>
37 #include <polypcore/sink.h>
38 #include <polypcore/source.h>
39 #include <polypcore/core-util.h>
47 static int add_key_value(pa_hashmap
*map
, char *key
, char *value
, const char* const valid_keys
[]) {
49 assert(map
&& key
&& value
);
53 for (v
= valid_keys
; *v
; v
++)
54 if (strcmp(*v
, key
) == 0)
64 e
= pa_xmalloc(sizeof(struct entry
));
67 pa_hashmap_put(map
, key
, e
);
71 pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
72 pa_hashmap
*map
= NULL
;
74 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
78 enum { WHITESPACE
, KEY
, VALUE_START
, VALUE_SIMPLE
, VALUE_DOUBLE_QUOTES
, VALUE_TICKS
} state
;
79 const char *p
, *key
, *value
;
80 size_t key_len
= 0, value_len
= 0;
84 for (p
= args
; *p
; p
++) {
89 else if (!isspace(*p
)) {
106 } else if (*p
== '"') {
107 state
= VALUE_DOUBLE_QUOTES
;
110 } else if (isspace(*p
)) {
111 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
115 state
= VALUE_SIMPLE
;
122 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
128 case VALUE_DOUBLE_QUOTES
:
130 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
138 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
147 if (state
== VALUE_START
) {
148 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
150 } else if (state
== VALUE_SIMPLE
) {
151 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
153 } else if (state
!= WHITESPACE
)
157 return (pa_modargs
*) map
;
162 pa_modargs_free((pa_modargs
*) map
);
168 static void free_func(void *p
, PA_GCC_UNUSED
void*userdata
) {
176 void pa_modargs_free(pa_modargs
*ma
) {
177 pa_hashmap
*map
= (pa_hashmap
*) ma
;
178 pa_hashmap_free(map
, free_func
, NULL
);
181 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
182 pa_hashmap
*map
= (pa_hashmap
*) ma
;
185 if (!(e
= pa_hashmap_get(map
, key
)))
191 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
193 assert(ma
&& key
&& value
);
195 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
198 if (pa_atou(v
, value
) < 0)
204 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
206 assert(ma
&& key
&& value
);
208 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
211 if (pa_atoi(v
, value
) < 0)
217 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, int *value
) {
220 assert(ma
&& key
&& value
);
222 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
228 if ((r
= pa_parse_boolean(v
)) < 0)
235 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
244 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
247 channels
= ss
.channels
;
248 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
250 ss
.channels
= (uint8_t) channels
;
252 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
253 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
256 if (!pa_sample_spec_valid(&ss
))
264 int pa_modargs_get_channel_map(pa_modargs
*ma
, pa_channel_map
*rmap
) {
273 if ((cm
= pa_modargs_get_value(ma
, "channel_map", NULL
)))
274 if (!pa_channel_map_parse(&map
, cm
))
277 if (!pa_channel_map_valid(&map
))
284 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
) {
294 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0)
297 if (!pa_channel_map_init_auto(&map
, ss
.channels
, def
))
300 if (pa_modargs_get_channel_map(ma
, &map
) < 0)
303 if (map
.channels
!= ss
.channels
)