properties: add type-specific pw_properties_get helpers

Two method calls to get properties,
- pw_properties_fetch_$type()
- pw_properties_get_$type()

The former allows for easy error checking and conditional setting of
properties. The latter for easy fetching of a property with a default
value. This simplifies the code and makes property parsing behave more
consistently across the code base.

Before:
   str = pw_properties_get(props, "foo");
   if (str)
	myval = pw_properties_parse_int(props, str);
	if (myval == 0) /* parsing error or a valid zero */
	    do_stuff(myval);

Now:
   if (pw_properties_fetch_int32(props, "foo", &myval) == 0)
	do_stuff(myval);
This commit is contained in:
Peter Hutterer 2021-09-02 16:35:49 +10:00 committed by Wim Taymans
parent 696a4b22e9
commit 0028b294d0
2 changed files with 167 additions and 0 deletions

View file

@ -495,6 +495,117 @@ const char *pw_properties_get(const struct pw_properties *properties, const char
return pw_array_get_unchecked(&impl->items, index, struct spa_dict_item)->value;
}
/** Fetch a property as uint32_t.
*
* \param properties a \ref pw_properties
* \param key a key
* \param value set to the value of the property on success, otherwise left
* unmodified
* \return 0 on success or a negative errno otherwise
* \retval -ENOENT The property does not exist
* \retval -EINVAL The property is not in the expected format
*/
SPA_EXPORT
int pw_properties_fetch_uint32(const struct pw_properties *properties, const char *key,
uint32_t *value)
{
const char *str = pw_properties_get(properties, key);
if (!str)
return -ENOENT;
return spa_atou32(str, value, 0) ? 0 : -EINVAL;
}
/** Fetch a property as int32_t
*
* \param properties a \ref pw_properties
* \param key a key
* \param value set to the value of the property on success, otherwise left
* unmodified
* \return 0 on success or a negative errno otherwise
* \retval -ENOENT The property does not exist
* \retval -EINVAL The property is not in the expected format
*/
SPA_EXPORT
int pw_properties_fetch_int32(const struct pw_properties *properties, const char *key,
int32_t *value)
{
const char *str = pw_properties_get(properties, key);
if (!str)
return -ENOENT;
return spa_atoi32(str, value, 0) ? 0 : -EINVAL;
}
/** Fetch a property as uint64_t.
*
* \param properties a \ref pw_properties
* \param key a key
* \param value set to the value of the property on success, otherwise left
* unmodified
* \return 0 on success or a negative errno otherwise
* \retval -ENOENT The property does not exist
* \retval -EINVAL The property is not in the expected format
*/
SPA_EXPORT
int pw_properties_fetch_uint64(const struct pw_properties *properties, const char *key,
uint64_t *value)
{
const char *str = pw_properties_get(properties, key);
if (!str)
return -ENOENT;
return spa_atou64(str, value, 0) ? 0 : -EINVAL;
}
/** Fetch a property as int64_t
*
* \param properties a \ref pw_properties
* \param key a key
* \param value set to the value of the property on success, otherwise left
* unmodified
* \return 0 on success or a negative errno otherwise
* \retval -ENOENT The property does not exist
* \retval -EINVAL The property is not in the expected format
*/
SPA_EXPORT
int pw_properties_fetch_int64(const struct pw_properties *properties, const char *key,
int64_t *value)
{
const char *str = pw_properties_get(properties, key);
if (!str)
return -ENOENT;
return spa_atoi64(str, value, 0) ? 0 : -EINVAL;
}
/** Fetch a property as boolean value
*
* \param properties a \ref pw_properties
* \param key a key
* \param value set to the value of the property on success, otherwise left
* unmodified
* \return 0 on success or a negative errno otherwise
* \retval -ENOENT The property does not exist
* \retval -EINVAL The property is not in the expected format
*/
SPA_EXPORT
int pw_properties_fetch_bool(const struct pw_properties *properties, const char *key,
bool *value)
{
const char *str = pw_properties_get(properties, key);
if (!str)
return -ENOENT;
*value = spa_atob(str);
return 0;
}
/** Iterate property values
*
* \param properties a \ref pw_properties

View file

@ -97,6 +97,62 @@ pw_properties_setva(struct pw_properties *properties,
const char *
pw_properties_get(const struct pw_properties *properties, const char *key);
int
pw_properties_fetch_uint32(const struct pw_properties *properties, const char *key, uint32_t *value);
int
pw_properties_fetch_int32(const struct pw_properties *properties, const char *key, int32_t *value);
int
pw_properties_fetch_uint64(const struct pw_properties *properties, const char *key, uint64_t *value);
int
pw_properties_fetch_int64(const struct pw_properties *properties, const char *key, int64_t *value);
int
pw_properties_fetch_bool(const struct pw_properties *properties, const char *key, bool *value);
static inline uint32_t
pw_properties_get_uint32(const struct pw_properties *properties, const char *key, uint32_t deflt)
{
uint32_t val = deflt;
pw_properties_fetch_uint32(properties, key, &val);
return val;
}
static inline int32_t
pw_properties_get_int32(const struct pw_properties *properties, const char *key, int32_t deflt)
{
int32_t val = deflt;
pw_properties_fetch_int32(properties, key, &val);
return val;
}
static inline uint64_t
pw_properties_get_uint64(const struct pw_properties *properties, const char *key, uint64_t deflt)
{
uint64_t val = deflt;
pw_properties_fetch_uint64(properties, key, &val);
return val;
}
static inline int64_t
pw_properties_get_int64(const struct pw_properties *properties, const char *key, int64_t deflt)
{
int64_t val = deflt;
pw_properties_fetch_int64(properties, key, &val);
return val;
}
static inline bool
pw_properties_get_bool(const struct pw_properties *properties, const char *key, bool deflt)
{
bool val = deflt;
pw_properties_fetch_bool(properties, key, &val);
return val;
}
const char *
pw_properties_iterate(const struct pw_properties *properties, void **state);