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"
47 static int add_key_value(struct 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 struct pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
72 struct 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 (struct pa_modargs
*) map
;
162 pa_modargs_free((struct pa_modargs
*) map
);
168 static void free_func(void *p
, void*userdata
) {
176 void pa_modargs_free(struct pa_modargs
*ma
) {
177 struct pa_hashmap
*map
= (struct pa_hashmap
*) ma
;
178 pa_hashmap_free(map
, free_func
, NULL
);
181 const char *pa_modargs_get_value(struct pa_modargs
*ma
, const char *key
, const char *def
) {
182 struct pa_hashmap
*map
= (struct pa_hashmap
*) ma
;
185 if (!(e
= pa_hashmap_get(map
, key
)))
191 int pa_modargs_get_value_u32(struct pa_modargs
*ma
, const char *key
, uint32_t *value
) {
195 assert(ma
&& key
&& value
);
197 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
203 l
= strtoul(v
, &e
, 0);
207 *value
= (uint32_t) l
;
211 int pa_modargs_get_value_s32(struct pa_modargs
*ma
, const char *key
, int32_t *value
) {
215 assert(ma
&& key
&& value
);
217 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
223 l
= strtol(v
, &e
, 0);
227 *value
= (int32_t) l
;
231 int pa_modargs_get_value_boolean(struct pa_modargs
*ma
, const char *key
, int *value
) {
234 assert(ma
&& key
&& 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(struct pa_modargs
*ma
, struct pa_sample_spec
*rss
) {
252 struct pa_sample_spec ss
;
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
))