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
34 #include "sample-util.h"
45 static int add_key_value(pa_hashmap
*map
, char *key
, char *value
, const char* const valid_keys
[]) {
47 assert(map
&& key
&& value
);
51 for (v
= valid_keys
; *v
; v
++)
52 if (strcmp(*v
, key
) == 0)
62 e
= pa_xmalloc(sizeof(struct entry
));
65 pa_hashmap_put(map
, key
, e
);
69 pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
70 pa_hashmap
*map
= NULL
;
72 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
76 enum { WHITESPACE
, KEY
, VALUE_START
, VALUE_SIMPLE
, VALUE_DOUBLE_QUOTES
, VALUE_TICKS
} state
;
77 const char *p
, *key
, *value
;
78 size_t key_len
= 0, value_len
= 0;
82 for (p
= args
; *p
; p
++) {
87 else if (!isspace(*p
)) {
104 } else if (*p
== '"') {
105 state
= VALUE_DOUBLE_QUOTES
;
108 } else if (isspace(*p
)) {
109 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
113 state
= VALUE_SIMPLE
;
120 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
126 case VALUE_DOUBLE_QUOTES
:
128 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
136 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrndup(value
, value_len
), valid_keys
) < 0)
145 if (state
== VALUE_START
) {
146 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
148 } else if (state
== VALUE_SIMPLE
) {
149 if (add_key_value(map
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
151 } else if (state
!= WHITESPACE
)
155 return (pa_modargs
*) map
;
160 pa_modargs_free((pa_modargs
*) map
);
166 static void free_func(void *p
, PA_GCC_UNUSED
void*userdata
) {
174 void pa_modargs_free(pa_modargs
*ma
) {
175 pa_hashmap
*map
= (pa_hashmap
*) ma
;
176 pa_hashmap_free(map
, free_func
, NULL
);
179 const char *pa_modargs_get_value(pa_modargs
*ma
, const char *key
, const char *def
) {
180 pa_hashmap
*map
= (pa_hashmap
*) ma
;
183 if (!(e
= pa_hashmap_get(map
, key
)))
189 int pa_modargs_get_value_u32(pa_modargs
*ma
, const char *key
, uint32_t *value
) {
191 assert(ma
&& key
&& value
);
193 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
196 if (pa_atou(v
, value
) < 0)
202 int pa_modargs_get_value_s32(pa_modargs
*ma
, const char *key
, int32_t *value
) {
204 assert(ma
&& key
&& value
);
206 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
209 if (pa_atoi(v
, value
) < 0)
215 int pa_modargs_get_value_boolean(pa_modargs
*ma
, const char *key
, int *value
) {
218 assert(ma
&& key
&& value
);
220 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
226 if ((r
= pa_parse_boolean(v
)) < 0)
233 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
242 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
245 channels
= ss
.channels
;
246 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
248 ss
.channels
= (uint8_t) channels
;
250 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
251 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
254 if (!pa_sample_spec_valid(&ss
))