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 (strcmp(*v
, key
) == 0)
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 pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
102 VALUE_SIMPLE_ESCAPED
,
104 VALUE_DOUBLE_QUOTES_ESCAPED
,
109 const char *p
, *key
= NULL
, *value
= NULL
;
110 size_t key_len
= 0, value_len
= 0;
111 pa_modargs
*ma
= pa_xnew(pa_modargs
, 1);
113 ma
->raw
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
114 ma
->unescaped
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
121 for (p
= args
; *p
; p
++) {
127 else if (!isspace(*p
)) {
137 else if (isspace(*p
))
148 } else if (*p
== '"') {
149 state
= VALUE_DOUBLE_QUOTES
;
152 } else if (isspace(*p
)) {
153 if (add_key_value(ma
,
154 pa_xstrndup(key
, key_len
),
159 } else if (*p
== '\\') {
160 state
= VALUE_SIMPLE_ESCAPED
;
164 state
= VALUE_SIMPLE
;
172 if (add_key_value(ma
,
173 pa_xstrndup(key
, key_len
),
174 pa_xstrndup(value
, value_len
),
178 } else if (*p
== '\\') {
179 state
= VALUE_SIMPLE_ESCAPED
;
185 case VALUE_SIMPLE_ESCAPED
:
186 state
= VALUE_SIMPLE
;
190 case VALUE_DOUBLE_QUOTES
:
192 if (add_key_value(ma
,
193 pa_xstrndup(key
, key_len
),
194 pa_xstrndup(value
, value_len
),
198 } else if (*p
== '\\') {
199 state
= VALUE_DOUBLE_QUOTES_ESCAPED
;
205 case VALUE_DOUBLE_QUOTES_ESCAPED
:
206 state
= VALUE_DOUBLE_QUOTES
;
212 if (add_key_value(ma
,
213 pa_xstrndup(key
, key_len
),
214 pa_xstrndup(value
, value_len
),
218 } else if (*p
== '\\') {
219 state
= VALUE_TICKS_ESCAPED
;
225 case VALUE_TICKS_ESCAPED
:
232 if (state
== VALUE_START
) {
233 if (add_key_value(ma
, pa_xstrndup(key
, key_len
), pa_xstrdup(""), valid_keys
) < 0)
235 } else if (state
== VALUE_SIMPLE
) {
236 if (add_key_value(ma
, pa_xstrndup(key
, key_len
), pa_xstrdup(value
), valid_keys
) < 0)
238 } else if (state
!= WHITESPACE
)
250 static void free_func(void *p
, void*userdata
) {
259 void pa_modargs_free(pa_modargs
*ma
) {
262 pa_hashmap_free(ma
->raw
, free_func
, NULL
);
263 pa_hashmap_free(ma
->unescaped
, free_func
, NULL
);
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
, pa_bool_t
*value
) {
326 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
332 if ((r
= pa_parse_boolean(v
)) < 0)
339 int pa_modargs_get_sample_spec(pa_modargs
*ma
, pa_sample_spec
*rss
) {
347 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0 ||
349 ss
.rate
> PA_RATE_MAX
)
352 channels
= ss
.channels
;
353 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0 ||
355 channels
>= PA_CHANNELS_MAX
)
357 ss
.channels
= (uint8_t) channels
;
359 if ((format
= pa_modargs_get_value(ma
, "format", NULL
)))
360 if ((ss
.format
= pa_parse_sample_format(format
)) < 0)
363 if (!pa_sample_spec_valid(&ss
))
371 int pa_modargs_get_channel_map(pa_modargs
*ma
, const char *name
, pa_channel_map
*rmap
) {
379 if ((cm
= pa_modargs_get_value(ma
, name
? name
: "channel_map", NULL
)))
380 if (!pa_channel_map_parse(&map
, cm
))
383 if (!pa_channel_map_valid(&map
))
390 int pa_modargs_get_sample_spec_and_channel_map(
393 pa_channel_map
*rmap
,
394 pa_channel_map_def_t def
) {
404 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0)
409 if (ss
.channels
!= map
.channels
)
410 pa_channel_map_init_extend(&map
, ss
.channels
, def
);
412 if (pa_modargs_get_channel_map(ma
, NULL
, &map
) < 0)
415 if (map
.channels
!= ss
.channels
)
424 int pa_modargs_get_proplist(pa_modargs
*ma
, const char *name
, pa_proplist
*p
, pa_update_mode_t m
) {
432 if (!(v
= modargs_get_value_raw(ma
, name
, NULL
)))
435 if (!(n
= pa_proplist_from_string(v
)))
438 pa_proplist_update(p
, m
, n
);
444 const char *pa_modargs_iterate(pa_modargs
*ma
, void **state
) {
449 if (!(e
= pa_hashmap_iterate(ma
->unescaped
, state
, NULL
)))