From aa128ed4890806de42106e8824fd47fe3f37e608 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 20 Jan 2022 18:02:41 +0100 Subject: [PATCH] spa: make common function to find type from short name --- spa/include/spa/debug/types.h | 20 ++++++++++++++++++++ spa/include/spa/utils/json-pod.h | 18 ++---------------- src/modules/module-adapter/adapter.c | 16 +--------------- src/tools/pw-cli.c | 18 ++---------------- 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/spa/include/spa/debug/types.h b/spa/include/spa/debug/types.h index 3bd43c308..55fb379aa 100644 --- a/spa/include/spa/debug/types.h +++ b/spa/include/spa/debug/types.h @@ -96,6 +96,26 @@ static inline uint32_t spa_debug_type_find_type(const struct spa_type_info *info return SPA_ID_INVALID; } +static inline const struct spa_type_info *spa_debug_type_find_short(const struct spa_type_info *info, const char *name) +{ + while (info && info->name) { + if (strcmp(spa_debug_type_short_name(info->name), name) == 0) + return info; + if (strcmp(info->name, name) == 0) + return info; + if (info->type != 0 && info->type == (uint32_t)atoi(name)) + return info; + info++; + } + return NULL; +} + +static inline uint32_t spa_debug_type_find_type_short(const struct spa_type_info *info, const char *name) +{ + if ((info = spa_debug_type_find_short(info, name)) == NULL) + return SPA_ID_INVALID; + return info->type; +} /** * \} */ diff --git a/spa/include/spa/utils/json-pod.h b/spa/include/spa/utils/json-pod.h index 18acb1149..34c7e08f6 100644 --- a/spa/include/spa/utils/json-pod.h +++ b/spa/include/spa/utils/json-pod.h @@ -44,20 +44,6 @@ extern "C" { * \{ */ -static inline const struct spa_type_info *spa_debug_type_info_short(const struct spa_type_info *info, const char *name) -{ - while (info && info->name) { - if (spa_streq(info->name, name)) - return info; - if (spa_streq(spa_debug_type_short_name(info->name), name)) - return info; - if (info->type != 0 && info->type == (uint32_t)atoi(name)) - return info; - info++; - } - return NULL; -} - static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags, uint32_t id, const struct spa_type_info *info, struct spa_json *iter, const char *value, int len) { @@ -80,7 +66,7 @@ static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags const struct spa_type_info *pi; if ((l = spa_json_next(&it[0], &v)) <= 0) break; - if ((pi = spa_debug_type_info_short(ti->values, key)) != NULL) + if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL) type = pi->type; else if (!spa_atou32(key, &type, 0)) continue; @@ -149,7 +135,7 @@ static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags spa_json_parse_stringn(value, len, val, len+1); switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) { case SPA_TYPE_Id: - if ((ti = spa_debug_type_info_short(info->values, val)) != NULL) + if ((ti = spa_debug_type_find_short(info->values, val)) != NULL) type = ti->type; else if (!spa_atou32(val, &type, 0)) return -EINVAL; diff --git a/src/modules/module-adapter/adapter.c b/src/modules/module-adapter/adapter.c index 104777d5f..08f7aa3c8 100644 --- a/src/modules/module-adapter/adapter.c +++ b/src/modules/module-adapter/adapter.c @@ -173,20 +173,6 @@ static const struct pw_impl_node_events node_events = { .port_init = node_port_init, }; -static const struct spa_type_info *find_type_info(const struct spa_type_info *info, const char *name) -{ - while (info && info->name) { - if (spa_streq(info->name, name)) - return info; - if (spa_streq(spa_debug_type_short_name(info->name), name)) - return info; - if (info->type != 0 && info->type == (uint32_t)atoi(name)) - return info; - info++; - } - return NULL; -} - static int handle_node_param(struct pw_impl_node *node, const char *key, const char *value) { const struct spa_type_info *ti; @@ -195,7 +181,7 @@ static int handle_node_param(struct pw_impl_node *node, const char *key, const c struct spa_pod *pod; int res; - ti = find_type_info(spa_type_param, key); + ti = spa_debug_type_find_short(spa_type_param, key); if (ti == NULL) return -ENOENT; diff --git a/src/tools/pw-cli.c b/src/tools/pw-cli.c index efade2c9a..0172a6864 100644 --- a/src/tools/pw-cli.c +++ b/src/tools/pw-cli.c @@ -1477,20 +1477,6 @@ static bool do_export_node(struct data *data, const char *cmd, char *args, char return false; } -static const struct spa_type_info *find_type_info(const struct spa_type_info *info, const char *name) -{ - while (info && info->name) { - if (spa_streq(info->name, name)) - return info; - if (spa_streq(spa_debug_type_short_name(info->name), name)) - return info; - if (info->type != 0 && info->type == (uint32_t)atoi(name)) - return info; - info++; - } - return NULL; -} - static bool do_enum_params(struct data *data, const char *cmd, char *args, char **error) { struct remote_data *rd = data->current; @@ -1507,7 +1493,7 @@ static bool do_enum_params(struct data *data, const char *cmd, char *args, char } id = atoi(a[0]); - ti = find_type_info(spa_type_param, a[1]); + ti = spa_debug_type_find_short(spa_type_param, a[1]); if (ti == NULL) { *error = spa_aprintf("%s: unknown param type: %s", cmd, a[1]); return false; @@ -1574,7 +1560,7 @@ static bool do_set_param(struct data *data, const char *cmd, char *args, char ** return false; } - ti = find_type_info(spa_type_param, a[1]); + ti = spa_debug_type_find_short(spa_type_param, a[1]); if (ti == NULL) { *error = spa_aprintf("%s: unknown param type: %s", cmd, a[1]); return false;