2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio 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.1 of the License,
9 or (at your option) any later version.
11 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/hashmap.h>
33 #include <pulsecore/idxset.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
41 pa_hashmap *unescaped;
48 static int add_key_value(pa_modargs *ma, char *key, char *value, const char* const valid_keys[]) {
54 pa_assert(ma->unescaped);
58 if (pa_hashmap_get(ma->unescaped, key)) {
66 for (v = valid_keys; *v; v++)
67 if (pa_streq(*v, key))
77 raw = pa_xstrdup(value);
79 e = pa_xnew(struct entry, 1);
81 e->value = pa_unescape(value);
82 pa_hashmap_put(ma->unescaped, key, e);
84 if (pa_streq(raw, value))
87 e = pa_xnew(struct entry, 1);
88 e->key = pa_xstrdup(key);
90 pa_hashmap_put(ma->raw, key, e);
96 static void free_func(void *p) {
105 pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
111 VALUE_SIMPLE_ESCAPED,
113 VALUE_DOUBLE_QUOTES_ESCAPED,
118 const char *p, *key = NULL, *value = NULL;
119 size_t key_len = 0, value_len = 0;
120 pa_modargs *ma = pa_xnew(pa_modargs, 1);
122 ma->raw = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, free_func);
123 ma->unescaped = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, free_func);
130 for (p = args; *p; p++) {
136 else if (!isspace(*p)) {
146 else if (isspace(*p))
157 } else if (*p == '"') {
158 state = VALUE_DOUBLE_QUOTES;
161 } else if (isspace(*p)) {
162 if (add_key_value(ma,
163 pa_xstrndup(key, key_len),
168 } else if (*p == '\\') {
169 state = VALUE_SIMPLE_ESCAPED;
173 state = VALUE_SIMPLE;
181 if (add_key_value(ma,
182 pa_xstrndup(key, key_len),
183 pa_xstrndup(value, value_len),
187 } else if (*p == '\\') {
188 state = VALUE_SIMPLE_ESCAPED;
194 case VALUE_SIMPLE_ESCAPED:
195 state = VALUE_SIMPLE;
199 case VALUE_DOUBLE_QUOTES:
201 if (add_key_value(ma,
202 pa_xstrndup(key, key_len),
203 pa_xstrndup(value, value_len),
207 } else if (*p == '\\') {
208 state = VALUE_DOUBLE_QUOTES_ESCAPED;
214 case VALUE_DOUBLE_QUOTES_ESCAPED:
215 state = VALUE_DOUBLE_QUOTES;
221 if (add_key_value(ma,
222 pa_xstrndup(key, key_len),
223 pa_xstrndup(value, value_len),
227 } else if (*p == '\\') {
228 state = VALUE_TICKS_ESCAPED;
234 case VALUE_TICKS_ESCAPED:
241 if (state == VALUE_START) {
242 if (add_key_value(ma, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
244 } else if (state == VALUE_SIMPLE) {
245 if (add_key_value(ma, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys) < 0)
247 } else if (state != WHITESPACE)
259 void pa_modargs_free(pa_modargs*ma) {
262 pa_hashmap_free(ma->raw);
263 pa_hashmap_free(ma->unescaped);
267 const char *pa_modargs_get_value(pa_modargs *ma, const char *key, const char *def) {
273 if (!(e = pa_hashmap_get(ma->unescaped, key)))
279 static const char *modargs_get_value_raw(pa_modargs *ma, const char *key, const char *def) {
285 if (!(e = pa_hashmap_get(ma->raw, key)))
286 if (!(e = pa_hashmap_get(ma->unescaped, key)))
292 int pa_modargs_get_value_u32(pa_modargs *ma, const char *key, uint32_t *value) {
297 if (!(v = pa_modargs_get_value(ma, key, NULL)))
300 if (pa_atou(v, value) < 0)
306 int pa_modargs_get_value_s32(pa_modargs *ma, const char *key, int32_t *value) {
311 if (!(v = pa_modargs_get_value(ma, key, NULL)))
314 if (pa_atoi(v, value) < 0)
320 int pa_modargs_get_value_boolean(pa_modargs *ma, const char *key, bool *value) {
326 if (!(v = pa_modargs_get_value(ma, key, NULL)))
332 if ((r = pa_parse_boolean(v)) < 0)
339 int pa_modargs_get_value_double(pa_modargs *ma, const char *key, double *value) {
344 if (!(v = pa_modargs_get_value(ma, key, NULL)))
347 if (pa_atod(v, value) < 0)
353 int pa_modargs_get_value_volume(pa_modargs *ma, const char *key, pa_volume_t *value) {
358 if (!(v = pa_modargs_get_value(ma, key, NULL)))
361 if (pa_parse_volume(v, value) < 0)
367 int pa_modargs_get_sample_spec(pa_modargs *ma, pa_sample_spec *rss) {
375 if ((pa_modargs_get_value_u32(ma, "rate", &ss.rate)) < 0 ||
377 ss.rate > PA_RATE_MAX)
380 channels = ss.channels;
381 if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0 ||
383 channels >= PA_CHANNELS_MAX)
385 ss.channels = (uint8_t) channels;
387 if ((format = pa_modargs_get_value(ma, "format", NULL)))
388 if ((ss.format = pa_parse_sample_format(format)) < 0)
391 if (!pa_sample_spec_valid(&ss))
399 int pa_modargs_get_alternate_sample_rate(pa_modargs *ma, uint32_t *alternate_rate) {
401 pa_assert(alternate_rate);
403 if ((pa_modargs_get_value_u32(ma, "alternate_rate", alternate_rate)) < 0 ||
404 *alternate_rate <= 0 ||
405 *alternate_rate > PA_RATE_MAX ||
406 !((*alternate_rate % 4000 == 0) || (*alternate_rate % 11025 == 0)))
412 int pa_modargs_get_channel_map(pa_modargs *ma, const char *name, pa_channel_map *rmap) {
420 if ((cm = pa_modargs_get_value(ma, name ? name : "channel_map", NULL)))
421 if (!pa_channel_map_parse(&map, cm))
424 if (!pa_channel_map_valid(&map))
431 int pa_modargs_get_sample_spec_and_channel_map(
434 pa_channel_map *rmap,
435 pa_channel_map_def_t def) {
445 if (pa_modargs_get_sample_spec(ma, &ss) < 0)
450 if (ss.channels != map.channels)
451 pa_channel_map_init_extend(&map, ss.channels, def);
453 if (pa_modargs_get_channel_map(ma, NULL, &map) < 0)
456 if (map.channels != ss.channels) {
457 if (!pa_modargs_get_value(ma, "channels", NULL))
458 ss.channels = map.channels;
469 int pa_modargs_get_proplist(pa_modargs *ma, const char *name, pa_proplist *p, pa_update_mode_t m) {
477 if (!(v = modargs_get_value_raw(ma, name, NULL)))
480 if (!(n = pa_proplist_from_string(v)))
483 pa_proplist_update(p, m, n);
489 const char *pa_modargs_iterate(pa_modargs *ma, void **state) {
494 if (!(e = pa_hashmap_iterate(ma->unescaped, state, NULL)))