]> code.delx.au - pulseaudio/blob - src/polypcore/modargs.c
0706294673c8d3b58bdb010718567259eb9715ec
[pulseaudio] / src / polypcore / 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 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.
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 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
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 entry {
42 char *key, *value;
43 };
44
45 static int add_key_value(pa_hashmap *map, char *key, char *value, const char* const valid_keys[]) {
46 struct entry *e;
47 assert(map && key && value);
48
49 if (valid_keys) {
50 const char*const* v;
51 for (v = valid_keys; *v; v++)
52 if (strcmp(*v, key) == 0)
53 break;
54
55 if (!*v) {
56 pa_xfree(key);
57 pa_xfree(value);
58 return -1;
59 }
60 }
61
62 e = pa_xmalloc(sizeof(struct entry));
63 e->key = key;
64 e->value = value;
65 pa_hashmap_put(map, key, e);
66 return 0;
67 }
68
69 pa_modargs *pa_modargs_new(const char *args, const char* const* valid_keys) {
70 pa_hashmap *map = NULL;
71
72 map = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
73 assert(map);
74
75 if (args) {
76 enum { WHITESPACE, KEY, VALUE_START, VALUE_SIMPLE, VALUE_DOUBLE_QUOTES, VALUE_TICKS } state;
77 const char *p, *key, *value;
78 size_t key_len = 0, value_len = 0;
79
80 key = value = NULL;
81 state = WHITESPACE;
82 for (p = args; *p; p++) {
83 switch (state) {
84 case WHITESPACE:
85 if (*p == '=')
86 goto fail;
87 else if (!isspace(*p)) {
88 key = p;
89 state = KEY;
90 key_len = 1;
91 }
92 break;
93 case KEY:
94 if (*p == '=')
95 state = VALUE_START;
96 else
97 key_len++;
98 break;
99 case VALUE_START:
100 if (*p == '\'') {
101 state = VALUE_TICKS;
102 value = p+1;
103 value_len = 0;
104 } else if (*p == '"') {
105 state = VALUE_DOUBLE_QUOTES;
106 value = p+1;
107 value_len = 0;
108 } else if (isspace(*p)) {
109 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
110 goto fail;
111 state = WHITESPACE;
112 } else {
113 state = VALUE_SIMPLE;
114 value = p;
115 value_len = 1;
116 }
117 break;
118 case VALUE_SIMPLE:
119 if (isspace(*p)) {
120 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
121 goto fail;
122 state = WHITESPACE;
123 } else
124 value_len++;
125 break;
126 case VALUE_DOUBLE_QUOTES:
127 if (*p == '"') {
128 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
129 goto fail;
130 state = WHITESPACE;
131 } else
132 value_len++;
133 break;
134 case VALUE_TICKS:
135 if (*p == '\'') {
136 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0)
137 goto fail;
138 state = WHITESPACE;
139 } else
140 value_len++;
141 break;
142 }
143 }
144
145 if (state == VALUE_START) {
146 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0)
147 goto fail;
148 } else if (state == VALUE_SIMPLE) {
149 if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys) < 0)
150 goto fail;
151 } else if (state != WHITESPACE)
152 goto fail;
153 }
154
155 return (pa_modargs*) map;
156
157 fail:
158
159 if (map)
160 pa_modargs_free((pa_modargs*) map);
161
162 return NULL;
163 }
164
165
166 static void free_func(void *p, PA_GCC_UNUSED void*userdata) {
167 struct entry *e = p;
168 assert(e);
169 pa_xfree(e->key);
170 pa_xfree(e->value);
171 pa_xfree(e);
172 }
173
174 void pa_modargs_free(pa_modargs*ma) {
175 pa_hashmap *map = (pa_hashmap*) ma;
176 pa_hashmap_free(map, free_func, NULL);
177 }
178
179 const char *pa_modargs_get_value(pa_modargs *ma, const char *key, const char *def) {
180 pa_hashmap *map = (pa_hashmap*) ma;
181 struct entry*e;
182
183 if (!(e = pa_hashmap_get(map, key)))
184 return def;
185
186 return e->value;
187 }
188
189 int pa_modargs_get_value_u32(pa_modargs *ma, const char *key, uint32_t *value) {
190 const char *v;
191 assert(ma && key && value);
192
193 if (!(v = pa_modargs_get_value(ma, key, NULL)))
194 return 0;
195
196 if (pa_atou(v, value) < 0)
197 return -1;
198
199 return 0;
200 }
201
202 int pa_modargs_get_value_s32(pa_modargs *ma, const char *key, int32_t *value) {
203 const char *v;
204 assert(ma && key && value);
205
206 if (!(v = pa_modargs_get_value(ma, key, NULL)))
207 return 0;
208
209 if (pa_atoi(v, value) < 0)
210 return -1;
211
212 return 0;
213 }
214
215 int pa_modargs_get_value_boolean(pa_modargs *ma, const char *key, int *value) {
216 const char *v;
217 int r;
218 assert(ma && key && value);
219
220 if (!(v = pa_modargs_get_value(ma, key, NULL)))
221 return 0;
222
223 if (!*v)
224 return -1;
225
226 if ((r = pa_parse_boolean(v)) < 0)
227 return -1;
228
229 *value = r;
230 return 0;
231 }
232
233 int pa_modargs_get_sample_spec(pa_modargs *ma, pa_sample_spec *rss) {
234 const char *format;
235 uint32_t channels;
236 pa_sample_spec ss;
237 assert(ma && rss);
238
239 /* DEBUG_TRAP;*/
240
241 ss = *rss;
242 if ((pa_modargs_get_value_u32(ma, "rate", &ss.rate)) < 0)
243 return -1;
244
245 channels = ss.channels;
246 if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0)
247 return -1;
248 ss.channels = (uint8_t) channels;
249
250 if ((format = pa_modargs_get_value(ma, "format", NULL)))
251 if ((ss.format = pa_parse_sample_format(format)) < 0)
252 return -1;
253
254 if (!pa_sample_spec_valid(&ss))
255 return -1;
256
257 *rss = ss;
258
259 return 0;
260 }