spa: add spa_json_object_next

This gets the next key and value from an object. This function is better
because it will skip key/value pairs that don't fit in the array to hold
the key.

The previous code patter would stop parsing the object as soon as a key
larger than the available space was found.
This commit is contained in:
Wim Taymans 2024-09-13 16:26:36 +02:00
parent cd81b5f39a
commit ce390d5b22
24 changed files with 171 additions and 269 deletions

View file

@ -1825,20 +1825,21 @@ static int do_auto_port_config(struct impl *this, const char *str)
#define POSITION_PRESERVE 0
#define POSITION_AUX 1
#define POSITION_UNKNOWN 2
int res, position = POSITION_PRESERVE;
int l, res, position = POSITION_PRESERVE;
struct spa_pod *param;
bool have_format = false, monitor = false, control = false;
struct spa_audio_info format = { 0, };
enum spa_param_port_config_mode mode = SPA_PARAM_PORT_CONFIG_MODE_none;
struct spa_json it[1];
char key[1024], val[256];
const char *v;
if (spa_json_begin_object(&it[0], str, strlen(str)) <= 0)
return -EINVAL;
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
if (spa_json_get_string(&it[0], val, sizeof(val)) <= 0)
break;
while ((l = spa_json_object_next(&it[0], key, sizeof(key), &v)) > 0) {
if (spa_json_parse_stringn(v, l, val, sizeof(val)) <= 0)
continue;
if (spa_streq(key, "mode")) {
mode = spa_debug_type_find_type_short(spa_type_param_port_config_mode, val);

View file

@ -87,26 +87,24 @@ static int do_match(const char *rules, struct spa_dict *dict, uint32_t *no_featu
while (spa_json_enter_object(&rules_arr, &it[0]) > 0) {
char key[256];
int match = true;
int match = true, len;
uint32_t no_features_cur = 0;
const char *value;
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
while ((len = spa_json_object_next(&it[0], key, sizeof(key), &value)) > 0) {
char val[4096];
const char *str, *value;
int len;
const char *str;
bool success = false;
if (spa_streq(key, "no-features")) {
if (spa_json_enter_array(&it[0], &it[1]) > 0) {
if (spa_json_is_array(value, len) > 0) {
spa_json_enter(&it[0], &it[1]);
while (spa_json_get_string(&it[1], val, sizeof(val)) > 0)
no_features_cur |= parse_feature(val);
}
continue;
}
if ((len = spa_json_next(&it[0], &value)) <= 0)
break;
if (spa_json_is_null(value, len)) {
value = NULL;
} else {
@ -161,17 +159,13 @@ static void load_quirks(struct spa_bt_quirks *this, const char *str, size_t len)
struct spa_json rules;
char key[1024];
struct spa_error_location loc;
int sz;
const char *value;
if (spa_json_enter_object(&data, &rules) <= 0)
spa_json_init(&rules, str, len);
while (spa_json_get_string(&rules, key, sizeof(key)) > 0) {
int sz;
const char *value;
if ((sz = spa_json_next(&rules, &value)) <= 0)
break;
while ((sz = spa_json_object_next(&rules, key, sizeof(key), &value)) > 0) {
if (!spa_json_is_container(value, sz))
continue;