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

@ -42,10 +42,8 @@ static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags
spa_pod_builder_push_object(b, &f[0], info->parent, id);
spa_json_enter(iter, &it[0]);
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
while ((l = spa_json_object_next(&it[0], key, sizeof(key), &v)) > 0) {
const struct spa_type_info *pi;
if ((l = spa_json_next(&it[0], &v)) <= 0)
break;
if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL)
type = pi->type;
else if (!spa_atou32(key, &type, 0))

View file

@ -39,7 +39,7 @@ struct spa_json {
uint32_t depth;
};
#define SPA_JSON_INIT(data,size) ((struct spa_json) { (data), (data)+(size), 0, 0, 0 })
#define SPA_JSON_INIT(data,size) ((struct spa_json) { (data), (data)+(size), NULL, 0, 0 })
static inline void spa_json_init(struct spa_json * iter, const char *data, size_t size)
{
@ -54,6 +54,8 @@ static inline void spa_json_enter(struct spa_json * iter, struct spa_json * sub)
#define SPA_JSON_SAVE(iter) ((struct spa_json) { (iter)->cur, (iter)->end, NULL, (iter)->state, 0 })
#define SPA_JSON_START(iter,p) ((struct spa_json) { (p), (iter)->end, NULL, 0, 0 })
/** Get the next token. \a value points to the token and the return value
* is the length. Returns -1 on parse error, 0 on end of input. */
static inline int spa_json_next(struct spa_json * iter, const char **value)
@ -594,7 +596,7 @@ static inline int spa_json_parse_stringn(const char *val, int len, char *result,
{
const char *p;
if (maxlen <= len)
return -1;
return -ENOSPC;
if (!spa_json_is_string(val, len)) {
if (result != val)
memmove(result, val, len);
@ -712,6 +714,19 @@ static inline int spa_json_encode_string(char *str, int size, const char *val)
return len-1;
}
static inline int spa_json_object_next(struct spa_json *iter, char *key, int maxkeylen, const char **value)
{
int res1, res2;
while (true) {
res1 = spa_json_get_string(iter, key, maxkeylen);
if (res1 <= 0 && res1 != -ENOSPC)
return res1;
res2 = spa_json_next(iter, value);
if (res2 <= 0 || res1 != -ENOSPC)
return res2;
}
}
/**
* \}
*/

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;

View file

@ -82,14 +82,12 @@ static int dump(FILE *file, int indent, struct spa_json *it, const char *value,
spa_json_enter(it, &sub);
else
sub = *it;
while (spa_json_get_string(&sub, key, sizeof(key)) > 0) {
while ((len = spa_json_object_next(&sub, key, sizeof(key), &value)) > 0) {
fprintf(file, "%s\n%*s",
count++ > 0 ? "," : "",
indent+2, "");
encode_string(file, key, strlen(key));
fprintf(file, ": ");
if ((len = spa_json_next(&sub, &value)) <= 0)
break;
res = dump(file, indent+2, &sub, value, len);
if (res < 0) {
if (toplevel)