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 <polypcore/hashmap.h>
32 #include <polypcore/idxset.h>
33 #include <polypcore/sample-util.h>
34 #include <polypcore/namereg.h>
35 #include <polypcore/sink.h>
36 #include <polypcore/source.h>
37 #include <polypcore/xmalloc.h>
38 #include <polypcore/util.h>
46 static int add_key_value(pa_hashmap
*map
, char *key
, char *value
, const char* const valid_keys
[]) {
48 assert(map
&& key
&& value
);
52 for (v
= valid_keys
; *v
; v
++)
53 if (strcmp(*v
, key
) == 0)
63 e
= pa_xmalloc(sizeof(struct entry
));
66 pa_hashmap_put(map
, key
, e
);
70 pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
71 pa_hashmap
*map
= NULL
;
73 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
77 enum { WHITESPACE
, KEY
, VALUE_START
, VALUE_SIMPLE
, VALUE_DOUBLE_QUOTES
, VALUE_TICKS
} state
;
78 const char *p
, *key
, *value
;
79 size_t key_len
= 0, value_len
= 0;
83 for (p
= args
; *p
; p
++) {
88 else if (!isspace(*p
)) {
105 } else if (*p
== '"') {
106 state
= VALUE_DOUBLE_QUOTES
;
109 } else if (isspace(*p
)) {
110 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
114 state
= VALUE_SIMPLE
;
121 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
127 case VALUE_DOUBLE_QUOTES
:
129 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
137 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
146 if (state
== VALUE_START
) {
147 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
149 } else if (state
== VALUE_SIMPLE
) {
150 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
152 } else if (state
!= WHITESPACE
)
156 return (pa_modargs
*) map
;
161 pa_modargs_free((pa_modargs
*) map
);
167 static void free_func(void *p
, PA_GCC_UNUSED
void*userdata
) {
175 void pa_modargs_free(pa_modargs
*ma
) {
176 pa_hashmap
*map
= (pa_hashmap
*) ma
;
177 pa_hashmap_free(map
, free_func
, NULL
);
180 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
181 pa_hashmap
*map
= (pa_hashmap
*) ma
;
184 if (!(e
= pa_hashmap_get(map
, key
)))
190 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
192 assert(ma
&& key
&& value
);
194 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
197 if (pa_atou(v
, value
) < 0)
203 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
205 assert(ma
&& key
&& value
);
207 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
210 if (pa_atoi(v
, value
) < 0)
216 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, int *value
) {
219 assert(ma
&& key
&& value
);
221 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
227 if ((r
= pa_parse_boolean(v
)) < 0)
234 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
243 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
246 channels
= ss
.channels
;
247 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
249 ss
.channels
= (uint8_t) channels
;
251 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
252 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
255 if (!pa_sample_spec_valid(&ss
))