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