From 19ad9bdc49154115c53313ff6251542a96f12a8a Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Wed, 24 Feb 2021 19:20:15 +0100 Subject: [PATCH] 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 --- include/conf.h | 1 + src/confmisc.c | 30 ++++++++++++++++++++++++++++++ src/control/control_hw.c | 14 ++++---------- src/hwdep/hwdep_hw.c | 14 ++++---------- src/pcm/pcm_direct.c | 17 ++++------------- src/pcm/pcm_hw.c | 19 ++++--------------- src/pcm/pcm_softvol.c | 19 ++++--------------- src/rawmidi/rawmidi_hw.c | 14 ++++---------- src/timer/timer_hw.c | 14 ++++---------- 9 files changed, 59 insertions(+), 83 deletions(-) diff --git a/include/conf.h b/include/conf.h index 823ac639..cf88f1bd 100644 --- a/include/conf.h +++ b/include/conf.h @@ -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); diff --git a/src/confmisc.c b/src/confmisc.c index 3ce95c7a..3dfbf8b5 100644 --- a/src/confmisc.c +++ b/src/confmisc.c @@ -78,6 +78,7 @@ #include #include #include +#include #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. diff --git a/src/control/control_hw.c b/src/control/control_hw.c index b54d65f2..febc9da9 100644 --- a/src/control/control_hw.c +++ b/src/control/control_hw.c @@ -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; diff --git a/src/hwdep/hwdep_hw.c b/src/hwdep/hwdep_hw.c index d3037194..1d3cf8e1 100644 --- a/src/hwdep/hwdep_hw.c +++ b/src/hwdep/hwdep_hw.c @@ -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) { diff --git a/src/pcm/pcm_direct.c b/src/pcm/pcm_direct.c index 19c5a811..58363482 100644 --- a/src/pcm/pcm_direct.c +++ b/src/pcm/pcm_direct.c @@ -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) { diff --git a/src/pcm/pcm_hw.c b/src/pcm/pcm_hw.c index 2028790e..6382b7a0 100644 --- a/src/pcm/pcm_hw.c +++ b/src/pcm/pcm_hw.c @@ -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) { diff --git a/src/pcm/pcm_softvol.c b/src/pcm/pcm_softvol.c index 84f00b28..373865c5 100644 --- a/src/pcm/pcm_softvol.c +++ b/src/pcm/pcm_softvol.c @@ -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) { diff --git a/src/rawmidi/rawmidi_hw.c b/src/rawmidi/rawmidi_hw.c index eaa8a76d..99927d75 100644 --- a/src/rawmidi/rawmidi_hw.c +++ b/src/rawmidi/rawmidi_hw.c @@ -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) { diff --git a/src/timer/timer_hw.c b/src/timer/timer_hw.c index f08c0ac0..cfb77463 100644 --- a/src/timer/timer_hw.c +++ b/src/timer/timer_hw.c @@ -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) {