]> code.delx.au - pulseaudio/blob - polyp/modargs.c
d58b391b187ffcf3e653e35d191b08d7f12e4531
[pulseaudio] / polyp / modargs.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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.
10
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.
15
16 You should have received a copy of the GNU 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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <ctype.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "hashmap.h"
32 #include "modargs.h"
33 #include "idxset.h"
34 #include "sample-util.h"
35 #include "namereg.h"
36 #include "sink.h"
37 #include "source.h"
38 #include "xmalloc.h"
39 #include "util.h"
40
41 struct pa_modargs;
42
43 struct entry {
44 char *key, *value;
45 };
46
47 static int add_key_value(struct pa_hashmap *map, char *key, char *value, const char* const* valid_keys) {
48 struct entry *e;
49 assert(map && key && value);
50
51 if (valid_keys) {
52 const char*const* v;
53 for (v = valid_keys; *v; v++)
54 if (strcmp(*v, key) == 0)
55 break;
56
57 if (!*v) {
58 pa_xfree(key);
59 pa_xfree(value);
60 return -1;
61 }
62 }
63
64 e = pa_xmalloc(sizeof(struct entry));
65 e->key = key;
66 e->value = value;
67 pa_hashmap_put(map, key, e);
68 return 0;
69 }
70
71 struct pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
72 struct pa_hashmap *map = NULL;
73
74 map = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
75 assert(map);
76
77 if (args) {
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;
81
82 key = value = NULL;
83 state = WHITESPACE;
84 for (p = args; *p; p++) {
85 switch (state) {
86 case WHITESPACE:
87 if (*p == '=')
88 goto fail;
89 else if (!isspace(*p)) {
90 key = p;
91 state = KEY;
92 key_len = 1;
93 }
94 break;
95 case KEY:
96 if (*p == '=')
97 state = VALUE_START;
98 else
99 key_len++;
100 break;
101 case VALUE_START:
102 if (*p == '\'') {
103 state = VALUE_TICKS;
104 value = p+1;
105 value_len = 0;
106 } else if (*p == '"') {
107 state = VALUE_DOUBLE_QUOTES;
108 value = p+1;
109 value_len = 0;
110 } else if (isspace(*p)) {
111 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
112 goto fail;
113 state = WHITESPACE;
114 } else {
115 state = VALUE_SIMPLE;
116 value = p;
117 value_len = 1;
118 }
119 break;
120 case VALUE_SIMPLE:
121 if (isspace(*p)) {
122 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
123 goto fail;
124 state = WHITESPACE;
125 } else
126 value_len++;
127 break;
128 case VALUE_DOUBLE_QUOTES:
129 if (*p == '"') {
130 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
131 goto fail;
132 state = WHITESPACE;
133 } else
134 value_len++;
135 break;
136 case VALUE_TICKS:
137 if (*p == '\'') {
138 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
139 goto fail;
140 state = WHITESPACE;
141 } else
142 value_len++;
143 break;
144 }
145 }
146
147 if (state == VALUE_START) {
148 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
149 goto fail;
150 } else if (state == VALUE_SIMPLE) {
151 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys) < 0)
152 goto fail;
153 } else if (state != WHITESPACE)
154 goto fail;
155 }
156
157 return (struct pa_modargs*) map;
158
159 fail:
160
161 if (map)
162 pa_modargs_free((struct pa_modargs*) map);
163
164 return NULL;
165 }
166
167
168 static void free_func(void *p, void*userdata) {
169 struct entry *e = p;
170 assert(e);
171 pa_xfree(e->key);
172 pa_xfree(e->value);
173 pa_xfree(e);
174 }
175
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);
179 }
180
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;
183 struct entry*e;
184
185 if (!(e = pa_hashmap_get(map, key)))
186 return def;
187
188 return e->value;
189 }
190
191 int pa_modargs_get_value_u32(struct pa_modargs *ma, const char *key, uint32_t *value) {
192 const char *v;
193 char *e;
194 unsigned long l;
195 assert(ma && key && value);
196
197 if (!(v = pa_modargs_get_value(ma, key, NULL)))
198 return 0;
199
200 if (!*v)
201 return -1;
202
203 l = strtoul(v, &e, 0);
204 if (*e)
205 return -1;
206
207 *value = (uint32_t) l;
208 return 0;
209 }
210
211 int pa_modargs_get_value_s32(struct pa_modargs *ma, const char *key, int32_t *value) {
212 const char *v;
213 char *e;
214 signed long l;
215 assert(ma && key && value);
216
217 if (!(v = pa_modargs_get_value(ma, key, NULL)))
218 return 0;
219
220 if (!*v)
221 return -1;
222
223 l = strtol(v, &e, 0);
224 if (*e)
225 return -1;
226
227 *value = (int32_t) l;
228 return 0;
229 }
230
231 int pa_modargs_get_value_boolean(struct pa_modargs *ma, const char *key, int *value) {
232 const char *v;
233 int r;
234 assert(ma && key && 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(struct pa_modargs *ma, struct pa_sample_spec *rss) {
250 const char *format;
251 uint32_t channels;
252 struct pa_sample_spec ss;
253 assert(ma && rss);
254
255 /* DEBUG_TRAP;*/
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 }