json: add helper function to parse channel positions

Use the helper instead of duplicating the same code.

Also add some helpers to parse a json array of uint32_t

Move some functions to convert between type name and id.
This commit is contained in:
Wim Taymans 2024-09-18 09:54:34 +02:00
parent 911a601b95
commit e2991f6398
34 changed files with 256 additions and 791 deletions

View file

@ -185,6 +185,24 @@ static inline int spa_json_begin_array(struct spa_json * iter, const char *data,
return spa_json_begin_container(iter, data, size, '[', false);
}
#define spa_json_make_str_array_unpack(maxlen,type,conv) \
{ \
struct spa_json iter; \
char v[maxlen]; \
uint32_t count = 0; \
if (spa_json_begin_array_relax(&iter, arr, arr_len) <= 0) \
return -EINVAL; \
while (spa_json_get_string(&iter, v, sizeof(v)) > 0 && count < max) \
values[count++] = conv(v); \
return count; \
}
static inline int spa_json_str_array_uint32(const char *arr, size_t arr_len,
uint32_t *values, size_t max)
{
spa_json_make_str_array_unpack(32,uint32_t, atoi);
}
/**
* \}
*/

View file

@ -10,6 +10,7 @@ extern "C" {
#endif
#include <spa/utils/defs.h>
#include <spa/utils/string.h>
/** \defgroup spa_types Types
* Data type information enumerations
@ -136,6 +137,34 @@ static inline const char *spa_type_short_name(const char *name)
return name;
}
static inline uint32_t spa_type_from_short_name(const char *name,
const struct spa_type_info *info, uint32_t unknown)
{
int i;
for (i = 0; info[i].name; i++) {
if (spa_streq(name, spa_type_short_name(info[i].name)))
return info[i].type;
}
return unknown;
}
static inline const char * spa_type_to_name(uint32_t type,
const struct spa_type_info *info, const char *unknown)
{
int i;
for (i = 0; info[i].name; i++) {
if (info[i].type == type)
return info[i].name;
}
return unknown;
}
static inline const char * spa_type_to_short_name(uint32_t type,
const struct spa_type_info *info, const char *unknown)
{
const char *n = spa_type_to_name(type, info, unknown);
return n ? spa_type_short_name(n) : NULL;
}
/**
* \}
*/