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

@ -68,25 +68,24 @@ static int parse_cmd(void *user_data, const char *location,
const char *section, const char *str, size_t len)
{
struct impl *impl = user_data;
struct spa_json it[3];
struct spa_json it[2];
char key[512];
int res = 0;
spa_autofree char *s = strndup(str, len);
spa_json_init(&it[0], s, len);
if (spa_json_enter_array(&it[0], &it[1]) < 0) {
if (spa_json_begin_array(&it[0], s, len) < 0) {
pw_log_error("config file error: pulse.cmd is not an array");
return -EINVAL;
}
while (spa_json_enter_object(&it[1], &it[2]) > 0) {
while (spa_json_enter_object(&it[0], &it[1]) > 0) {
char *cmd = NULL, *args = NULL, *flags = NULL;
while (spa_json_get_string(&it[2], key, sizeof(key)) > 0) {
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
const char *val;
int len;
if ((len = spa_json_next(&it[2], &val)) <= 0)
if ((len = spa_json_next(&it[1], &val)) <= 0)
break;
if (spa_streq(key, "cmd")) {
@ -97,7 +96,7 @@ static int parse_cmd(void *user_data, const char *location,
spa_json_parse_stringn(val, len, args, len+1);
} else if (spa_streq(key, "flags")) {
if (spa_json_is_container(val, len))
len = spa_json_container_len(&it[2], val, len);
len = spa_json_container_len(&it[1], val, len);
flags = (char*)val;
spa_json_parse_stringn(val, len, flags, len+1);
}