]> git.alsa-project.org Git - alsa-lib.git/commitdiff
mixer: simple - add snd_mixer_selem_id_parse() from amixer
authorJaroslav Kysela <perex@perex.cz>
Mon, 11 Nov 2019 13:22:11 +0000 (14:22 +0100)
committerJaroslav Kysela <perex@perex.cz>
Thu, 14 Nov 2019 14:00:40 +0000 (15:00 +0100)
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/mixer.h
src/mixer/simple.c

index 9352894af550d8896ad28d0b474d6cb3983fdec2..51b6f04ac99c98b1322f4ae86b3a3a069d36e507 100644 (file)
@@ -304,6 +304,7 @@ const char *snd_mixer_selem_id_get_name(const snd_mixer_selem_id_t *obj);
 unsigned int snd_mixer_selem_id_get_index(const snd_mixer_selem_id_t *obj);
 void snd_mixer_selem_id_set_name(snd_mixer_selem_id_t *obj, const char *val);
 void snd_mixer_selem_id_set_index(snd_mixer_selem_id_t *obj, unsigned int val);
+int snd_mixer_selem_id_parse(snd_mixer_selem_id_t *dst, const char *str);
 
 /** \} */
 
index 34d58ede011f9dae2bee554e699d68321457c7ed..2861d97ce600567493703fcc8ad37f207e8ee0da 100644 (file)
@@ -1052,3 +1052,56 @@ void snd_mixer_selem_id_set_index(snd_mixer_selem_id_t *obj, unsigned int val)
        assert(obj);
        obj->index = val;
 }
+
+/**
+ * \brief Parse ASCII simple mixer element identifier
+ * \param dst Parsed simple mixer element identifier
+ * \param str Mixer simple element ASCII representation
+ */
+int snd_mixer_selem_id_parse(snd_mixer_selem_id_t *dst, const char *str)
+{
+       int c, size;
+       char buf[128];
+       char *ptr = buf;
+
+       memset(dst, 0, sizeof(*dst));
+       while (*str == ' ' || *str == '\t')
+               str++;
+       if (!(*str))
+               return -EINVAL;
+       size = 1;       /* for '\0' */
+       if (*str != '"' && *str != '\'') {
+               while (*str && *str != ',') {
+                       if (size < (int)sizeof(buf)) {
+                               *ptr++ = *str;
+                               size++;
+                       }
+                       str++;
+               }
+       } else {
+               c = *str++;
+               while (*str && *str != c) {
+                       if (size < (int)sizeof(buf)) {
+                               *ptr++ = *str;
+                               size++;
+                       }
+                       str++;
+               }
+               if (*str == c)
+                       str++;
+       }
+       if (*str == '\0') {
+               *ptr = 0;
+               goto _set;
+       }
+       if (*str != ',')
+               return -EINVAL;
+       *ptr = 0;       /* terminate the string */
+       str++;
+       if (str[0] < '0' || str[1] > '9')
+               return -EINVAL;
+       dst->index = atoi(str);
+       _set:
+       snd_strlcpy(dst->name, buf, sizeof(dst->name));
+       return 0;
+}