properties: add method to parse key=value list

This commit is contained in:
Wim Taymans 2018-01-27 11:19:23 +01:00
parent 1e411bfe09
commit b5f9983c4e
2 changed files with 55 additions and 6 deletions

View file

@ -64,6 +64,19 @@ static int find_index(const struct pw_properties *this, const char *key)
return -1; return -1;
} }
static struct properties *properties_new(int prealloc)
{
struct properties *impl;
impl = calloc(1, sizeof(struct properties));
if (impl == NULL)
return NULL;
pw_array_init(&impl->items, prealloc);
return impl;
}
/** Make a new properties object /** Make a new properties object
* *
* \param key a first key * \param key a first key
@ -78,12 +91,10 @@ struct pw_properties *pw_properties_new(const char *key, ...)
va_list varargs; va_list varargs;
const char *value; const char *value;
impl = calloc(1, sizeof(struct properties)); impl = properties_new(16);
if (impl == NULL) if (impl == NULL)
return NULL; return NULL;
pw_array_init(&impl->items, 16);
va_start(varargs, key); va_start(varargs, key);
while (key != NULL) { while (key != NULL) {
value = va_arg(varargs, char *); value = va_arg(varargs, char *);
@ -107,12 +118,10 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict)
uint32_t i; uint32_t i;
struct properties *impl; struct properties *impl;
impl = calloc(1, sizeof(struct properties)); impl = properties_new(16);
if (impl == NULL) if (impl == NULL)
return NULL; return NULL;
pw_array_init(&impl->items, 16);
for (i = 0; i < dict->n_items; i++) { for (i = 0; i < dict->n_items; i++) {
if (dict->items[i].key != NULL) if (dict->items[i].key != NULL)
add_func(&impl->this, strdup(dict->items[i].key), add_func(&impl->this, strdup(dict->items[i].key),
@ -122,6 +131,43 @@ struct pw_properties *pw_properties_new_dict(const struct spa_dict *dict)
return &impl->this; return &impl->this;
} }
/** Make a new properties object from the given str
*
* \a str should be a whitespace separated list of key=value
* strings.
*
* \param args a property description
* \return a new properties object
*
* \memberof pw_properties
*/
struct pw_properties *
pw_properties_new_string(const char *str)
{
struct properties *impl;
const char *state = NULL, *s = NULL;
size_t len;
impl = properties_new(16);
if (impl == NULL)
return NULL;
s = pw_split_walk(str, " \t\n\r", &len, &state);
while (s) {
char *val, *eq;
val = strndup(s, len);
eq = strchr(val, '=');
if (eq) {
*eq = '\0';
add_func(&impl->this, val, strdup(eq+1));
}
s = pw_split_walk(str, " \t\n\r", &len, &state);
}
return &impl->this;
}
/** Copy a properties object /** Copy a properties object
* *
* \param properties properties to copy * \param properties properties to copy

View file

@ -45,6 +45,9 @@ pw_properties_new(const char *key, ...);
struct pw_properties * struct pw_properties *
pw_properties_new_dict(const struct spa_dict *dict); pw_properties_new_dict(const struct spa_dict *dict);
struct pw_properties *
pw_properties_new_string(const char *args);
struct pw_properties * struct pw_properties *
pw_properties_copy(const struct pw_properties *properties); pw_properties_copy(const struct pw_properties *properties);