From feccb882b66e12d505a2cad02483edcf1398b560 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 13 Sep 2024 09:22:27 +0200 Subject: [PATCH] spa: add spa_json_begin That combines spa_json_init() and spa_json_next, a common thing to do. --- pipewire-alsa/alsa-plugins/pcm_pipewire.c | 3 +-- spa/include/spa/utils/json-pod.h | 3 +-- spa/include/spa/utils/json.h | 6 ++++++ spa/tools/spa-json-dump.c | 3 +-- src/modules/module-protocol-pulse/format.c | 12 ++++-------- src/tools/pw-config.c | 3 +-- src/tools/pw-dump.c | 3 +-- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/pipewire-alsa/alsa-plugins/pcm_pipewire.c b/pipewire-alsa/alsa-plugins/pcm_pipewire.c index 84bff9621..4c153670e 100644 --- a/pipewire-alsa/alsa-plugins/pcm_pipewire.c +++ b/pipewire-alsa/alsa-plugins/pcm_pipewire.c @@ -1122,8 +1122,7 @@ static int parse_value(const char *str, struct param_info *info) const char *val; int len; - spa_json_init(&it[0], str, strlen(str)); - if ((len = spa_json_next(&it[0], &val)) <= 0) + if ((len = spa_json_begin(&it[0], str, strlen(str), &val)) <= 0) return -EINVAL; if (spa_json_is_array(val, len)) { diff --git a/spa/include/spa/utils/json-pod.h b/spa/include/spa/utils/json-pod.h index bbd0ec7a9..7318e47fc 100644 --- a/spa/include/spa/utils/json-pod.h +++ b/spa/include/spa/utils/json-pod.h @@ -139,8 +139,7 @@ static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags, struct spa_json iter; const char *val; - spa_json_init(&iter, value, len); - if ((len = spa_json_next(&iter, &val)) <= 0) + if ((len = spa_json_begin(&iter, value, len, &val)) <= 0) return -EINVAL; return spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len); diff --git a/spa/include/spa/utils/json.h b/spa/include/spa/utils/json.h index d54bef345..86b42806a 100644 --- a/spa/include/spa/utils/json.h +++ b/spa/include/spa/utils/json.h @@ -356,6 +356,12 @@ static inline bool spa_json_get_error(struct spa_json *iter, const char *start, return true; } +static inline int spa_json_begin(struct spa_json * iter, const char *data, size_t size, const char **val) +{ + spa_json_init(iter, data, size); + return spa_json_next(iter, val); +} + static inline int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type) { const char *value; diff --git a/spa/tools/spa-json-dump.c b/spa/tools/spa-json-dump.c index 0e2e8cecc..62712bb72 100644 --- a/spa/tools/spa-json-dump.c +++ b/spa/tools/spa-json-dump.c @@ -123,8 +123,7 @@ static int process_json(const char *filename, void *buf, size_t size) struct spa_json it; const char *value; - spa_json_init(&it, buf, size); - if ((len = spa_json_next(&it, &value)) <= 0) { + if ((len = spa_json_begin(&it, buf, size, &value)) <= 0) { fprintf(stderr, "not a valid file '%s': %s\n", filename, spa_strerror(len)); return -EINVAL; } diff --git a/src/modules/module-protocol-pulse/format.c b/src/modules/module-protocol-pulse/format.c index 5626fe273..4ed12dc46 100644 --- a/src/modules/module-protocol-pulse/format.c +++ b/src/modules/module-protocol-pulse/format.c @@ -806,8 +806,7 @@ static uint32_t format_info_get_format(const struct format_info *info) if ((str = pw_properties_get(info->props, "format.sample_format")) == NULL) return SPA_AUDIO_FORMAT_UNKNOWN; - spa_json_init(&it[0], str, strlen(str)); - if ((len = spa_json_next(&it[0], &val)) <= 0) + if ((len = spa_json_begin(&it[0], str, strlen(str), &val)) <= 0) return SPA_AUDIO_FORMAT_UNKNOWN; if (spa_json_is_string(val, len)) @@ -825,8 +824,7 @@ static int format_info_get_rate(const struct format_info *info) if ((str = pw_properties_get(info->props, "format.rate")) == NULL) return -ENOENT; - spa_json_init(&it[0], str, strlen(str)); - if ((len = spa_json_next(&it[0], &val)) <= 0) + if ((len = spa_json_begin(&it[0], str, strlen(str), &val)) <= 0) return -EINVAL; if (spa_json_is_int(val, len)) { if (spa_json_parse_int(val, len, &v) <= 0) @@ -862,8 +860,7 @@ int format_info_to_spec(const struct format_info *info, struct sample_spec *ss, if ((str = pw_properties_get(info->props, "format.channels")) == NULL) return -ENOENT; - spa_json_init(&it[0], str, strlen(str)); - if ((len = spa_json_next(&it[0], &val)) <= 0) + if ((len = spa_json_begin(&it[0], str, strlen(str), &val)) <= 0) return -EINVAL; if (spa_json_is_float(val, len)) { if (spa_json_parse_float(val, len, &f) <= 0) @@ -877,8 +874,7 @@ int format_info_to_spec(const struct format_info *info, struct sample_spec *ss, return -ENOTSUP; if ((str = pw_properties_get(info->props, "format.channel_map")) != NULL) { - spa_json_init(&it[0], str, strlen(str)); - if ((len = spa_json_next(&it[0], &val)) <= 0) + if ((len = spa_json_begin(&it[0], str, strlen(str), &val)) <= 0) return -EINVAL; if (!spa_json_is_string(val, len)) return -EINVAL; diff --git a/src/tools/pw-config.c b/src/tools/pw-config.c index de38095fb..bfc64e427 100644 --- a/src/tools/pw-config.c +++ b/src/tools/pw-config.c @@ -66,8 +66,7 @@ static int do_merge_section(void *data, const char *location, const char *sectio int l; const char *value; - spa_json_init(&it[0], str, len); - if ((l = spa_json_next(&it[0], &value)) <= 0) + if ((l = spa_json_begin(&it[0], str, len, &value)) <= 0) return 0; if (spa_json_is_array(value, l)) { diff --git a/src/tools/pw-dump.c b/src/tools/pw-dump.c index c52b330cb..fe5a10419 100644 --- a/src/tools/pw-dump.c +++ b/src/tools/pw-dump.c @@ -1144,8 +1144,7 @@ static void json_dump(struct data *d, const char *key, const char *value) struct spa_json it[1]; int len; const char *val; - spa_json_init(&it[0], value, strlen(value)); - if ((len = spa_json_next(&it[0], &val)) >= 0) + if ((len = spa_json_begin(&it[0], value, strlen(value), &val)) >= 0) json_dump_val(d, key, &it[0], val, len); }