From: Jaroslav Kysela Date: Mon, 11 Nov 2019 13:22:11 +0000 (+0100) Subject: mixer: simple - add snd_mixer_selem_id_parse() from amixer X-Git-Tag: v1.2.1~11 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=4ce38a5ff466d18039b2606938f866ea3a6c9f3c;p=alsa-lib.git mixer: simple - add snd_mixer_selem_id_parse() from amixer Signed-off-by: Jaroslav Kysela --- diff --git a/include/mixer.h b/include/mixer.h index 9352894a..51b6f04a 100644 --- a/include/mixer.h +++ b/include/mixer.h @@ -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); /** \} */ diff --git a/src/mixer/simple.c b/src/mixer/simple.c index 34d58ede..2861d97c 100644 --- a/src/mixer/simple.c +++ b/src/mixer/simple.c @@ -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; +}