9 #include "sample-util.h"
20 static int add_key_value(struct pa_hashmap *map, char *key, char *value, const char* const* valid_keys) {
22 assert(map && key && value);
26 for (v = valid_keys; *v; v++)
27 if (strcmp(*v, key) == 0)
37 e = malloc(sizeof(struct entry));
41 pa_hashmap_put(map, key, e);
45 struct pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
46 struct pa_hashmap *map = NULL;
48 map = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
52 enum { WHITESPACE, KEY, VALUE_START, VALUE_SIMPLE, VALUE_DOUBLE_QUOTES, VALUE_TICKS } state;
53 const char *p, *key, *value;
54 size_t key_len, value_len;
58 for (p = args; *p; p++) {
63 else if (!isspace(*p)) {
80 } else if (*p == '"') {
81 state = VALUE_DOUBLE_QUOTES;
84 } else if (isspace(*p)) {
85 if (add_key_value(map, strndup(key, key_len), strdup(""), valid_keys) < 0)
96 if (add_key_value(map, strndup(key, key_len), strndup(value, value_len), valid_keys) < 0)
102 case VALUE_DOUBLE_QUOTES:
104 if (add_key_value(map, strndup(key, key_len), strndup(value, value_len), valid_keys) < 0)
112 if (add_key_value(map, strndup(key, key_len), strndup(value, value_len), valid_keys) < 0)
121 if (state == VALUE_START) {
122 if (add_key_value(map, strndup(key, key_len), strdup(""), valid_keys) < 0)
124 } else if (state == VALUE_SIMPLE) {
125 if (add_key_value(map, strndup(key, key_len), strdup(value), valid_keys) < 0)
127 } else if (state != WHITESPACE)
131 return (struct pa_modargs*) map;
136 pa_modargs_free((struct pa_modargs*) map);
142 static void free_func(void *p, void*userdata) {
150 void pa_modargs_free(struct pa_modargs*ma) {
151 struct pa_hashmap *map = (struct pa_hashmap*) ma;
152 pa_hashmap_free(map, free_func, NULL);
155 const char *pa_modargs_get_value(struct pa_modargs *ma, const char *key, const char *def) {
156 struct pa_hashmap *map = (struct pa_hashmap*) ma;
159 if (!(e = pa_hashmap_get(map, key)))
165 int pa_modargs_get_value_u32(struct pa_modargs *ma, const char *key, uint32_t *value) {
169 assert(ma && key && value);
171 if (!(v = pa_modargs_get_value(ma, key, NULL)))
177 l = strtoul(v, &e, 0);
181 *value = (uint32_t) l;
185 int pa_modargs_get_sample_spec(struct pa_modargs *ma, struct pa_sample_spec *rss) {
188 struct pa_sample_spec ss;
192 if ((pa_modargs_get_value_u32(ma, "rate", &ss.rate)) < 0)
195 channels = ss.channels;
196 if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0)
198 ss.channels = (uint8_t) channels;
200 if ((format = pa_modargs_get_value(ma, "format", NULL))) {
201 if (strcmp(format, "s16le") == 0)
202 ss.format = PA_SAMPLE_S16LE;
203 else if (strcmp(format, "s16be") == 0)
204 ss.format = PA_SAMPLE_S16BE;
205 else if (strcmp(format, "s16ne") == 0 || strcmp(format, "s16") == 0 || strcmp(format, "16") == 0)
206 ss.format = PA_SAMPLE_S16NE;
207 else if (strcmp(format, "u8") == 0 || strcmp(format, "8") == 0)
208 ss.format = PA_SAMPLE_U8;
209 else if (strcmp(format, "float32") == 0)
210 ss.format = PA_SAMPLE_FLOAT32;
211 else if (strcmp(format, "ulaw") == 0)
212 ss.format = PA_SAMPLE_ULAW;
213 else if (strcmp(format, "alaw") == 0)
214 ss.format = PA_SAMPLE_ALAW;
219 if (!pa_sample_spec_valid(&ss))
227 int pa_modargs_get_source_index(struct pa_modargs *ma, struct pa_core *c, uint32_t *index) {
231 if (!(t = pa_modargs_get_value(ma, "source", NULL)))
232 *index = PA_IDXSET_INVALID;
234 struct pa_source *source;
235 if (!(source = pa_namereg_get(c, t, PA_NAMEREG_SOURCE)))
238 *index = source->index;
244 int pa_modargs_get_sink_index(struct pa_modargs *ma, struct pa_core *c, uint32_t *index) {
248 if (!(t = pa_modargs_get_value(ma, "sink", NULL)))
249 *index = PA_IDXSET_INVALID;
251 struct pa_sink *sink;
252 if (!(sink = pa_namereg_get(c, t, PA_NAMEREG_SINK)))
255 *index = sink->index;