topology: add tplg_get_unsigned() function

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2019-12-14 14:05:49 +01:00
parent 1047a5f3c0
commit 14e43a1187
4 changed files with 55 additions and 25 deletions

View file

@ -55,6 +55,53 @@ int tplg_get_integer(snd_config_t *n, int *val, int base)
}
}
/*
* Get unsigned integer value
*/
int tplg_get_unsigned(snd_config_t *n, unsigned *val, int base)
{
const char *str;
long lval;
long long llval;
unsigned long uval;
int err;
switch (snd_config_get_type(n)) {
case SND_CONFIG_TYPE_INTEGER:
err = snd_config_get_integer(n, &lval);
if (err < 0)
return err;
if (lval < 0 || lval > UINT_MAX)
return -ERANGE;
*val = lval;
return err;
case SND_CONFIG_TYPE_INTEGER64:
err = snd_config_get_integer64(n, &llval);
if (err < 0)
return err;
if (llval < 0 || llval > UINT_MAX)
return -ERANGE;
*val = llval;
return err;
case SND_CONFIG_TYPE_STRING:
err = snd_config_get_string(n, &str);
if (err < 0)
return err;
errno = 0;
uval = strtoul(str, NULL, base);
if (errno == ERANGE && uval == ULONG_MAX)
return -ERANGE;
if (errno && uval == 0)
return -EINVAL;
if (uval > UINT_MAX)
return -ERANGE;
*val = uval;
return 0;
default:
return -EINVAL;
}
}
/*
* Parse compound
*/