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);
}

View file

@ -440,15 +440,14 @@ void channel_map_parse(const char *str, struct channel_map *map)
void channel_map_parse_position(const char *str, struct channel_map *map)
{
struct spa_json it[2];
struct spa_json it[1];
char v[256];
spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], str, strlen(str));
if (spa_json_begin_array_relax(&it[0], str, strlen(str)) <= 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->map[map->channels++] = channel_name2id(v);
}

View file

@ -177,7 +177,7 @@ static int do_extension_stream_restore_read(struct module *module, struct client
reply = reply_new(client, tag);
spa_dict_for_each(item, &client->routes->dict) {
struct spa_json it[3];
struct spa_json it[2];
const char *value;
char name[1024], key[128];
char device_name[1024] = "\0";
@ -191,45 +191,44 @@ static int do_extension_stream_restore_read(struct module *module, struct client
pw_log_debug("%s -> %s: %s", item->key, name, item->value);
spa_json_init(&it[0], item->value, strlen(item->value));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
if (spa_json_begin_object(&it[0], item->value, strlen(item->value)) <= 0)
continue;
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
if (spa_streq(key, "volume")) {
if (spa_json_get_float(&it[1], &volume) <= 0)
if (spa_json_get_float(&it[0], &volume) <= 0)
continue;
}
else if (spa_streq(key, "mute")) {
if (spa_json_get_bool(&it[1], &mute) <= 0)
if (spa_json_get_bool(&it[0], &mute) <= 0)
continue;
}
else if (spa_streq(key, "volumes")) {
vol = VOLUME_INIT;
if (spa_json_enter_array(&it[1], &it[2]) <= 0)
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
continue;
for (vol.channels = 0; vol.channels < CHANNELS_MAX; vol.channels++) {
if (spa_json_get_float(&it[2], &vol.values[vol.channels]) <= 0)
if (spa_json_get_float(&it[1], &vol.values[vol.channels]) <= 0)
break;
}
}
else if (spa_streq(key, "channels")) {
if (spa_json_enter_array(&it[1], &it[2]) <= 0)
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
continue;
for (map.channels = 0; map.channels < CHANNELS_MAX; map.channels++) {
char chname[16];
if (spa_json_get_string(&it[2], chname, sizeof(chname)) <= 0)
if (spa_json_get_string(&it[1], chname, sizeof(chname)) <= 0)
break;
map.map[map.channels] = channel_name2id(chname);
}
}
else if (spa_streq(key, "target-node")) {
if (spa_json_get_string(&it[1], device_name, sizeof(device_name)) <= 0)
if (spa_json_get_string(&it[0], device_name, sizeof(device_name)) <= 0)
continue;
}
else if (spa_json_next(&it[1], &value) <= 0)
else if (spa_json_next(&it[0], &value) <= 0)
break;
}
message_put(reply,

View file

@ -930,21 +930,20 @@ static void manager_object_data_timeout(void *data, struct pw_manager_object *o,
static int json_object_find(const char *obj, const char *key, char *value, size_t len)
{
struct spa_json it[2];
struct spa_json it[1];
const char *v;
char k[128];
spa_json_init(&it[0], obj, strlen(obj));
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
if (spa_json_begin_object(&it[0], obj, strlen(obj)) <= 0)
return -EINVAL;
while (spa_json_get_string(&it[1], k, sizeof(k)) > 0) {
while (spa_json_get_string(&it[0], k, sizeof(k)) > 0) {
if (spa_streq(k, key)) {
if (spa_json_get_string(&it[1], value, len) <= 0)
if (spa_json_get_string(&it[0], value, len) <= 0)
continue;
return 0;
} else {
if (spa_json_next(&it[1], &v) <= 0)
if (spa_json_next(&it[0], &v) <= 0)
break;
}
}

View file

@ -977,29 +977,28 @@ int servers_create_and_start(struct impl *impl, const char *addresses, struct pw
{
int len, res, count = 0, err = 0; /* store the first error to return when no servers could be created */
const char *v;
struct spa_json it[3];
struct spa_json it[2];
/* update `err` if it hasn't been set to an errno */
#define UPDATE_ERR(e) do { if (err == 0) err = (e); } while (false)
/* collect addresses into an array of `struct sockaddr_storage` */
spa_json_init(&it[0], addresses, strlen(addresses));
/* [ <server-spec> ... ] */
if (spa_json_enter_array(&it[0], &it[1]) < 0)
if (spa_json_begin_array(&it[0], addresses, strlen(addresses)) < 0)
return -EINVAL;
/* a server-spec is either an address or an object */
while ((len = spa_json_next(&it[1], &v)) > 0) {
while ((len = spa_json_next(&it[0], &v)) > 0) {
char addr_str[FORMATTED_SOCKET_ADDR_STRLEN] = { 0 };
char key[128], client_access[64] = { 0 };
struct sockaddr_storage addrs[2];
int i, max_clients = MAX_CLIENTS, listen_backlog = LISTEN_BACKLOG, n_addr;
if (spa_json_is_object(v, len)) {
spa_json_enter(&it[1], &it[2]);
while (spa_json_get_string(&it[2], key, sizeof(key)) > 0) {
if ((len = spa_json_next(&it[2], &v)) <= 0)
spa_json_enter(&it[0], &it[1]);
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
if ((len = spa_json_next(&it[1], &v)) <= 0)
break;
if (spa_streq(key, "address")) {