]> code.delx.au - pulseaudio/blob - src/pulsecore/modargs.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / modargs.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/hashmap.h>
35 #include <pulsecore/idxset.h>
36 #include <pulsecore/sample-util.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/source.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/macro.h>
42
43 #include "modargs.h"
44
45 struct entry {
46 char *key, *value;
47 };
48
49 static int add_key_value(pa_hashmap *map, char *key, char *value, const char* const valid_keys[]) {
50 struct entry *e;
51
52 pa_assert(map);
53 pa_assert(key);
54 pa_assert(value);
55
56 if (valid_keys) {
57 const char*const* v;
58 for (v = valid_keys; *v; v++)
59 if (strcmp(*v, key) == 0)
60 break;
61
62 if (!*v) {
63 pa_xfree(key);
64 pa_xfree(value);
65 return -1;
66 }
67 }
68
69 e = pa_xnew(struct entry, 1);
70 e->key = key;
71 e->value = value;
72 pa_hashmap_put(map, key, e);
73
74 return 0;
75 }
76
77 pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
78 pa_hashmap *map = NULL;
79
80 map = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
81
82 if (args) {
83 enum { WHITESPACE, KEY, VALUE_START, VALUE_SIMPLE, VALUE_DOUBLE_QUOTES, VALUE_TICKS } state;
84 const char *p, *key, *value;
85 size_t key_len = 0, value_len = 0;
86
87 key = value = NULL;
88 state = WHITESPACE;
89 for (p = args; *p; p++) {
90 switch (state) {
91 case WHITESPACE:
92 if (*p == '=')
93 goto fail;
94 else if (!isspace(*p)) {
95 key = p;
96 state = KEY;
97 key_len = 1;
98 }
99 break;
100 case KEY:
101 if (*p == '=')
102 state = VALUE_START;
103 else
104 key_len++;
105 break;
106 case VALUE_START:
107 if (*p == '\'') {
108 state = VALUE_TICKS;
109 value = p+1;
110 value_len = 0;
111 } else if (*p == '"') {
112 state = VALUE_DOUBLE_QUOTES;
113 value = p+1;
114 value_len = 0;
115 } else if (isspace(*p)) {
116 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
117 goto fail;
118 state = WHITESPACE;
119 } else {
120 state = VALUE_SIMPLE;
121 value = p;
122 value_len = 1;
123 }
124 break;
125 case VALUE_SIMPLE:
126 if (isspace(*p)) {
127 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
128 goto fail;
129 state = WHITESPACE;
130 } else
131 value_len++;
132 break;
133 case VALUE_DOUBLE_QUOTES:
134 if (*p == '"') {
135 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
136 goto fail;
137 state = WHITESPACE;
138 } else
139 value_len++;
140 break;
141 case VALUE_TICKS:
142 if (*p == '\'') {
143 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
144 goto fail;
145 state = WHITESPACE;
146 } else
147 value_len++;
148 break;
149 }
150 }
151
152 if (state == VALUE_START) {
153 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
154 goto fail;
155 } else if (state == VALUE_SIMPLE) {
156 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys) < 0)
157 goto fail;
158 } else if (state != WHITESPACE)
159 goto fail;
160 }
161
162 return (pa_modargs*) map;
163
164 fail:
165
166 if (map)
167 pa_modargs_free((pa_modargs*) map);
168
169 return NULL;
170 }
171
172 static void free_func(void *p, PA_GCC_UNUSED void*userdata) {
173 struct entry *e = p;
174 pa_assert(e);
175
176 pa_xfree(e->key);
177 pa_xfree(e->value);
178 pa_xfree(e);
179 }
180
181 void pa_modargs_free(pa_modargs*ma) {
182 pa_hashmap *map = (pa_hashmap*) ma;
183 pa_hashmap_free(map, free_func, NULL);
184 }
185
186 const char *pa_modargs_get_value(pa_modargs *ma, const char *key, const char *def) {
187 pa_hashmap *map = (pa_hashmap*) ma;
188 struct entry*e;
189
190 if (!(e = pa_hashmap_get(map, key)))
191 return def;
192
193 return e->value;
194 }
195
196 int pa_modargs_get_value_u32(pa_modargs *ma, const char *key, uint32_t *value) {
197 const char *v;
198
199 pa_assert(ma);
200 pa_assert(key);
201 pa_assert(value);
202
203 if (!(v = pa_modargs_get_value(ma, key, NULL)))
204 return 0;
205
206 if (pa_atou(v, value) < 0)
207 return -1;
208
209 return 0;
210 }
211
212 int pa_modargs_get_value_s32(pa_modargs *ma, const char *key, int32_t *value) {
213 const char *v;
214
215 pa_assert(ma);
216 pa_assert(key);
217 pa_assert(value);
218
219 if (!(v = pa_modargs_get_value(ma, key, NULL)))
220 return 0;
221
222 if (pa_atoi(v, value) < 0)
223 return -1;
224
225 return 0;
226 }
227
228 int pa_modargs_get_value_boolean(pa_modargs *ma, const char *key, int *value) {
229 const char *v;
230 int r;
231
232 pa_assert(ma);
233 pa_assert(key);
234 pa_assert(value);
235
236 if (!(v = pa_modargs_get_value(ma, key, NULL)))
237 return 0;
238
239 if (!*v)
240 return -1;
241
242 if ((r = pa_parse_boolean(v)) < 0)
243 return -1;
244
245 *value = r;
246 return 0;
247 }
248
249 int pa_modargs_get_sample_spec(pa_modargs *ma, pa_sample_spec *rss) {
250 const char *format;
251 uint32_t channels;
252 pa_sample_spec ss;
253
254 pa_assert(ma);
255 pa_assert(rss);
256
257 ss = *rss;
258 if ((pa_modargs_get_value_u32(ma, "rate", &ss.rate)) < 0)
259 return -1;
260
261 channels = ss.channels;
262 if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0)
263 return -1;
264 ss.channels = (uint8_t) channels;
265
266 if ((format = pa_modargs_get_value(ma, "format", NULL)))
267 if ((ss.format = pa_parse_sample_format(format)) < 0)
268 return -1;
269
270 if (!pa_sample_spec_valid(&ss))
271 return -1;
272
273 *rss = ss;
274
275 return 0;
276 }
277
278 int pa_modargs_get_channel_map(pa_modargs *ma, const char *name, pa_channel_map *rmap) {
279 pa_channel_map map;
280 const char *cm;
281
282 pa_assert(ma);
283 pa_assert(rmap);
284
285 map = *rmap;
286
287 if ((cm = pa_modargs_get_value(ma, name ? name : "channel_map", NULL)))
288 if (!pa_channel_map_parse(&map, cm))
289 return -1;
290
291 if (!pa_channel_map_valid(&map))
292 return -1;
293
294 *rmap = map;
295 return 0;
296 }
297
298 int pa_modargs_get_sample_spec_and_channel_map(pa_modargs *ma, pa_sample_spec *rss, pa_channel_map *rmap, pa_channel_map_def_t def) {
299 pa_sample_spec ss;
300 pa_channel_map map;
301
302 pa_assert(ma);
303 pa_assert(rss);
304 pa_assert(rmap);
305
306 ss = *rss;
307
308 if (pa_modargs_get_sample_spec(ma, &ss) < 0)
309 return -1;
310
311 if (!pa_channel_map_init_auto(&map, ss.channels, def))
312 map.channels = 0;
313
314 if (pa_modargs_get_channel_map(ma, NULL, &map) < 0)
315 return -1;
316
317 if (map.channels != ss.channels)
318 return -1;
319
320 *rmap = map;
321 *rss = ss;
322
323 return 0;
324 }