mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-08 13:30:08 -05:00
properties: add pw_properties_check_string for checking parse errors
Add pw_properties_check_string which checks a props JSON string for parse errors.
This commit is contained in:
parent
7f5e0f0425
commit
7ee8df39e8
2 changed files with 53 additions and 26 deletions
|
|
@ -8,6 +8,7 @@
|
||||||
#include <spa/utils/ansi.h>
|
#include <spa/utils/ansi.h>
|
||||||
#include <spa/utils/json.h>
|
#include <spa/utils/json.h>
|
||||||
#include <spa/utils/string.h>
|
#include <spa/utils/string.h>
|
||||||
|
#include <spa/utils/cleanup.h>
|
||||||
|
|
||||||
#include "pipewire/array.h"
|
#include "pipewire/array.h"
|
||||||
#include "pipewire/log.h"
|
#include "pipewire/log.h"
|
||||||
|
|
@ -128,6 +129,42 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict)
|
||||||
return &impl->this;
|
return &impl->this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool update_string(struct pw_properties *props, const char *str, size_t size,
|
||||||
|
int *count, int *err_line, int *err_col)
|
||||||
|
{
|
||||||
|
struct spa_json it[2];
|
||||||
|
char key[1024];
|
||||||
|
|
||||||
|
spa_json_init(&it[0], str, size);
|
||||||
|
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
|
||||||
|
spa_json_init(&it[1], str, size);
|
||||||
|
|
||||||
|
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
|
||||||
|
int len;
|
||||||
|
const char *value;
|
||||||
|
spa_autofree char *val = NULL;
|
||||||
|
|
||||||
|
if ((len = spa_json_next(&it[1], &value)) <= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (spa_json_is_null(value, len))
|
||||||
|
val = NULL;
|
||||||
|
else {
|
||||||
|
if (spa_json_is_container(value, len))
|
||||||
|
len = spa_json_container_len(&it[1], value, len);
|
||||||
|
if (len <= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (props && (val = malloc(len+1)) != NULL)
|
||||||
|
spa_json_parse_stringn(value, len, val, len+1);
|
||||||
|
}
|
||||||
|
if (props)
|
||||||
|
*count += pw_properties_set(props, key, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return !spa_json_get_error(&it[1], str, err_line, err_col);
|
||||||
|
}
|
||||||
|
|
||||||
/** Update the properties from the given string, overwriting any
|
/** Update the properties from the given string, overwriting any
|
||||||
* existing keys with the new values from \a str.
|
* existing keys with the new values from \a str.
|
||||||
*
|
*
|
||||||
|
|
@ -139,37 +176,25 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict)
|
||||||
SPA_EXPORT
|
SPA_EXPORT
|
||||||
int pw_properties_update_string(struct pw_properties *props, const char *str, size_t size)
|
int pw_properties_update_string(struct pw_properties *props, const char *str, size_t size)
|
||||||
{
|
{
|
||||||
struct properties *impl = SPA_CONTAINER_OF(props, struct properties, this);
|
|
||||||
struct spa_json it[2];
|
|
||||||
char key[1024], *val;
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
spa_json_init(&it[0], str, size);
|
update_string(props, str, size, &count, NULL, NULL);
|
||||||
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
|
|
||||||
spa_json_init(&it[1], str, size);
|
|
||||||
|
|
||||||
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
|
|
||||||
int len;
|
|
||||||
const char *value;
|
|
||||||
|
|
||||||
if ((len = spa_json_next(&it[1], &value)) <= 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (spa_json_is_null(value, len))
|
|
||||||
val = NULL;
|
|
||||||
else {
|
|
||||||
if (spa_json_is_container(value, len))
|
|
||||||
len = spa_json_container_len(&it[1], value, len);
|
|
||||||
|
|
||||||
if ((val = malloc(len+1)) != NULL)
|
|
||||||
spa_json_parse_stringn(value, len, val, len+1);
|
|
||||||
}
|
|
||||||
count += pw_properties_set(&impl->this, key, val);
|
|
||||||
free(val);
|
|
||||||
}
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Check \a str is a well-formed properties JSON string.
|
||||||
|
*
|
||||||
|
* \param line Return value for parse error line position
|
||||||
|
* \param col Return value for parse error column position
|
||||||
|
* \return true if string is valid
|
||||||
|
* \since 1.1.0
|
||||||
|
*/
|
||||||
|
SPA_EXPORT
|
||||||
|
bool pw_properties_check_string(const char *str, size_t size, int *line, int *col)
|
||||||
|
{
|
||||||
|
return update_string(NULL, str, size, NULL, line, col);
|
||||||
|
}
|
||||||
|
|
||||||
/** Make a new properties object from the given str
|
/** Make a new properties object from the given str
|
||||||
*
|
*
|
||||||
* \a object should be a whitespace separated list of key=value
|
* \a object should be a whitespace separated list of key=value
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,8 @@ int pw_properties_update(struct pw_properties *props,
|
||||||
int pw_properties_update_string(struct pw_properties *props,
|
int pw_properties_update_string(struct pw_properties *props,
|
||||||
const char *str, size_t size);
|
const char *str, size_t size);
|
||||||
|
|
||||||
|
bool pw_properties_check_string(const char *str, size_t size, int *line, int *col);
|
||||||
|
|
||||||
int pw_properties_add(struct pw_properties *oldprops,
|
int pw_properties_add(struct pw_properties *oldprops,
|
||||||
const struct spa_dict *dict);
|
const struct spa_dict *dict);
|
||||||
int pw_properties_add_keys(struct pw_properties *oldprops,
|
int pw_properties_add_keys(struct pw_properties *oldprops,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue