spa: add spa_json_begin_array/object and relaxed versions

Add spa_json_begin_array/object to replace
spa_json_init+spa_json_begin_array/object

This function is better because it does not waste a useless spa_json
structure as an iterator. The relaxed versions also error out when the
container is mismatched because parsing a mismatched container is not
going to give any results anyway.
This commit is contained in:
Wim Taymans 2024-09-13 13:09:54 +02:00
parent feccb882b6
commit cd81b5f39a
51 changed files with 401 additions and 452 deletions

View file

@ -452,17 +452,16 @@ static int add_pro_profile(pa_card *impl, uint32_t index)
static bool contains_string(const char *arr, const char *str)
{
struct spa_json it[2];
struct spa_json it[1];
char v[256];
if (arr == NULL || str == NULL)
return false;
spa_json_init(&it[0], arr, strlen(arr));
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], arr, strlen(arr));
if (spa_json_begin_array_relax(&it[0], arr, strlen(arr)) <= 0)
return false;
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0) {
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0) {
if (spa_streq(v, str))
return true;
}

View file

@ -968,16 +968,15 @@ int spa_alsa_init(struct state *state, const struct spa_dict *info)
} else if (spa_streq(k, "clock.quantum-limit")) {
spa_atou32(s, &state->quantum_limit, 0);
} else if (spa_streq(k, SPA_KEY_API_ALSA_BIND_CTLS)) {
struct spa_json it[2];
struct spa_json it[1];
char v[256];
unsigned int i = 0;
/* Read a list of ALSA control names to bind as params */
spa_json_init(&it[0], s, strlen(s));
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], s, strlen(s));
if (spa_json_begin_array_relax(&it[0], s, strlen(s)) <= 0)
continue;
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
i < SPA_N_ELEMENTS(state->bound_ctls)) {
snprintf(state->bound_ctls[i].name,
sizeof(state->bound_ctls[i].name), "%s", v);

View file

@ -325,15 +325,14 @@ static inline uint32_t spa_alsa_channel_from_name(const char *name)
static inline void spa_alsa_parse_position(struct channel_map *map, const char *val, size_t len)
{
struct spa_json it[2];
struct spa_json it[1];
char v[256];
spa_json_init(&it[0], val, len);
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], val, len);
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
return;
map->channels = 0;
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
map->channels < SPA_AUDIO_MAX_CHANNELS) {
map->pos[map->channels++] = spa_alsa_channel_from_name(v);
}
@ -341,16 +340,15 @@ static inline void spa_alsa_parse_position(struct channel_map *map, const char *
static inline uint32_t spa_alsa_parse_rates(uint32_t *rates, uint32_t max, const char *val, size_t len)
{
struct spa_json it[2];
struct spa_json it[1];
char v[256];
uint32_t count;
spa_json_init(&it[0], val, len);
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], val, len);
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
return 0;
count = 0;
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 && count < max)
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 && count < max)
rates[count++] = atoi(v);
return count;
}
@ -367,15 +365,14 @@ static inline uint32_t spa_alsa_iec958_codec_from_name(const char *name)
static inline void spa_alsa_parse_iec958_codecs(uint64_t *codecs, const char *val, size_t len)
{
struct spa_json it[2];
struct spa_json it[1];
char v[256];
spa_json_init(&it[0], val, len);
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], val, len);
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
return;
*codecs = 0;
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0)
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0)
*codecs |= 1ULL << spa_alsa_iec958_codec_from_name(v);
}