conf: introduce snd_config_get_card() function

It's helper for the "card" entries parsing. It reduces
the code in most of open_hw functions.

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2021-02-24 19:20:15 +01:00
parent b9a4997e92
commit 19ad9bdc49
9 changed files with 59 additions and 83 deletions

View File

@ -187,6 +187,7 @@ snd_config_t *snd_config_iterator_entry(const snd_config_iterator_t iterator);
int snd_config_get_bool_ascii(const char *ascii);
int snd_config_get_bool(const snd_config_t *conf);
int snd_config_get_card(const snd_config_t *conf);
int snd_config_get_ctl_iface_ascii(const char *ascii);
int snd_config_get_ctl_iface(const snd_config_t *conf);

View File

@ -78,6 +78,7 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include "local.h"
/**
@ -142,6 +143,35 @@ int snd_config_get_bool(const snd_config_t *conf)
return err;
}
/**
* \brief Gets the card number from a configuration node.
* \param conf Handle to the configuration node to be parsed.
* \return The card number if successful, otherwise a negative error code.
*/
int snd_config_get_card(const snd_config_t *conf)
{
const char *str, *id;
long v;
int err;
if ((err = snd_config_get_integer(conf, &v)) < 0) {
if ((err = snd_config_get_string(conf, &str)) < 0) {
snd_config_get_id(conf, &id);
SNDERR("Invalid field %s", id);
return -EINVAL;
}
err = snd_card_get_index(str);
if (err < 0) {
SNDERR("Cannot get card index for %s", str);
return err;
}
v = err;
}
if (v < 0 || v > INT_MAX)
return -EINVAL;
return v;
}
/**
* \brief Gets the control interface index from the given ASCII string.
* \param ascii The string to be parsed.

View File

@ -441,7 +441,6 @@ int _snd_ctl_hw_open(snd_ctl_t **handlep, char *name, snd_config_t *root ATTRIBU
{
snd_config_iterator_t i, next;
long card = -1;
const char *str;
int err;
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
@ -451,15 +450,10 @@ int _snd_ctl_hw_open(snd_ctl_t **handlep, char *name, snd_config_t *root ATTRIBU
if (_snd_conf_generic_id(id))
continue;
if (strcmp(id, "card") == 0) {
err = snd_config_get_integer(n, &card);
if (err < 0) {
err = snd_config_get_string(n, &str);
if (err < 0)
return -EINVAL;
card = snd_card_get_index(str);
if (card < 0)
return card;
}
err = snd_config_get_card(n);
if (err < 0)
return err;
card = err;
continue;
}
return -EINVAL;

View File

@ -151,7 +151,6 @@ int _snd_hwdep_hw_open(snd_hwdep_t **hwdep, char *name,
{
snd_config_iterator_t i, next;
long card = -1, device = 0;
const char *str;
int err;
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
@ -161,15 +160,10 @@ int _snd_hwdep_hw_open(snd_hwdep_t **hwdep, char *name,
if (_snd_conf_generic_id(id))
continue;
if (strcmp(id, "card") == 0) {
err = snd_config_get_integer(n, &card);
if (err < 0) {
err = snd_config_get_string(n, &str);
if (err < 0)
return -EINVAL;
card = snd_card_get_index(str);
if (card < 0)
return card;
}
err = snd_config_get_card(n);
if (err < 0)
return err;
card = err;
continue;
}
if (strcmp(id, "device") == 0) {

View File

@ -1834,19 +1834,10 @@ static int _snd_pcm_direct_get_slave_ipc_offset(snd_config_t *root,
continue;
}
if (strcmp(id, "card") == 0) {
err = snd_config_get_integer(n, &card);
if (err < 0) {
err = snd_config_get_string(n, &str);
if (err < 0) {
SNDERR("Invalid type for %s", id);
return -EINVAL;
}
card = snd_card_get_index(str);
if (card < 0) {
SNDERR("Invalid value for %s", id);
return card;
}
}
err = snd_config_get_card(n);
if (err < 0)
return err;
card = err;
continue;
}
if (strcmp(id, "device") == 0) {

View File

@ -1816,21 +1816,10 @@ int _snd_pcm_hw_open(snd_pcm_t **pcmp, const char *name,
if (snd_pcm_conf_generic_id(id))
continue;
if (strcmp(id, "card") == 0) {
err = snd_config_get_integer(n, &card);
if (err < 0) {
err = snd_config_get_string(n, &str);
if (err < 0) {
SNDERR("Invalid type for %s", id);
err = -EINVAL;
goto fail;
}
card = snd_card_get_index(str);
if (card < 0) {
SNDERR("Invalid value for %s", id);
err = card;
goto fail;
}
}
err = snd_config_get_card(n);
if (err < 0)
goto fail;
card = err;
continue;
}
if (strcmp(id, "device") == 0) {

View File

@ -1000,21 +1000,10 @@ int _snd_pcm_parse_control_id(snd_config_t *conf, snd_ctl_elem_id_t *ctl_id,
if (strcmp(id, "comment") == 0)
continue;
if (strcmp(id, "card") == 0) {
const char *str;
long v;
if ((err = snd_config_get_integer(n, &v)) < 0) {
if ((err = snd_config_get_string(n, &str)) < 0) {
SNDERR("Invalid field %s", id);
goto _err;
}
*cardp = snd_card_get_index(str);
if (*cardp < 0) {
SNDERR("Cannot get index for %s", str);
err = *cardp;
goto _err;
}
} else
*cardp = v;
err = snd_config_get_card(n);
if (err < 0)
goto _err;
*cardp = err;
continue;
}
if (strcmp(id, "iface") == 0 || strcmp(id, "interface") == 0) {

View File

@ -321,7 +321,6 @@ int _snd_rawmidi_hw_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
{
snd_config_iterator_t i, next;
long card = -1, device = 0, subdevice = -1;
const char *str;
int err;
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
@ -331,15 +330,10 @@ int _snd_rawmidi_hw_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
if (snd_rawmidi_conf_generic_id(id))
continue;
if (strcmp(id, "card") == 0) {
err = snd_config_get_integer(n, &card);
if (err < 0) {
err = snd_config_get_string(n, &str);
if (err < 0)
return -EINVAL;
card = snd_card_get_index(str);
if (card < 0)
return card;
}
err = snd_config_get_card(n);
if (err < 0)
return err;
card = err;
continue;
}
if (strcmp(id, "device") == 0) {

View File

@ -288,7 +288,6 @@ int _snd_timer_hw_open(snd_timer_t **timer, char *name,
snd_config_iterator_t i, next;
long dev_class = SND_TIMER_CLASS_GLOBAL, dev_sclass = SND_TIMER_SCLASS_NONE;
long card = 0, device = 0, subdevice = 0;
const char *str;
int err;
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
@ -310,15 +309,10 @@ int _snd_timer_hw_open(snd_timer_t **timer, char *name,
continue;
}
if (strcmp(id, "card") == 0) {
err = snd_config_get_integer(n, &card);
if (err < 0) {
err = snd_config_get_string(n, &str);
if (err < 0)
return -EINVAL;
card = snd_card_get_index(str);
if (card < 0)
return card;
}
err = snd_config_get_card(n);
if (err < 0)
return err;
card = err;
continue;
}
if (strcmp(id, "device") == 0) {