]> git.alsa-project.org Git - alsa-lib.git/commitdiff
conf: introduce snd_config_get_card() function
authorJaroslav Kysela <perex@perex.cz>
Wed, 24 Feb 2021 18:20:15 +0000 (19:20 +0100)
committerJaroslav Kysela <perex@perex.cz>
Fri, 26 Feb 2021 20:08:28 +0000 (21:08 +0100)
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>
include/conf.h
src/confmisc.c
src/control/control_hw.c
src/hwdep/hwdep_hw.c
src/pcm/pcm_direct.c
src/pcm/pcm_hw.c
src/pcm/pcm_softvol.c
src/rawmidi/rawmidi_hw.c
src/timer/timer_hw.c

index 823ac639c09e9684c6e04a276d18e14bf5893fa9..cf88f1bd8367ddff39100c84d0475966c28b15c1 100644 (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);
 
index 3ce95c7a942cb3898c5dfa192ce3d6de849856d7..3dfbf8b59cfe67bf5b5c5f407788a43989bd9f2b 100644 (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.
index b54d65f25f168a7f7d4c82b9af5fdfb7173764b4..febc9da94ee011dff75b4b0723d161258d75cdc6 100644 (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;
index d303719435c270665f4fc8b38abd7f6eafce220e..1d3cf8e1e6c97ca7ecb5bd638cd5eecfcf43a1ab 100644 (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) {
index 19c5a811262f14ade1fcc2a4f4826feda4466a8f..58363482b6d6df5fe33e2989b62bbb23c5fc5454 100644 (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) {
index 2028790eba78b3a7253214d158b0d0d992421e35..6382b7a0c17f133933c18303c6700e174c7972d5 100644 (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) {
index 84f00b28e23b8108fdc863915e8bbe6a0680d08a..373865c59763d555765241e69640f551e956e506 100644 (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) {
index eaa8a76de52f800bf00eb093da132a916241f26a..99927d75e5ccc84acd239a25ffe628cbbef37670 100644 (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) {
index f08c0ac08a087ccbca0151d42d6013c324b23082..cfb774632f491246883184b78ac8c273a3d7c2df 100644 (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) {