From 97ca9ae7548fcb4e2e31574c3cbf86a7640f3a31 Mon Sep 17 00:00:00 2001 From: "Geoffrey D. Bennett" Date: Sun, 31 Mar 2024 03:27:49 +1030 Subject: [PATCH] Add get_elem_by_substr() to alsa.[ch] --- src/alsa.c | 15 +++++++++++++++ src/alsa.h | 1 + 2 files changed, 16 insertions(+) diff --git a/src/alsa.c b/src/alsa.c index 3947fa7..97bf5b6 100644 --- a/src/alsa.c +++ b/src/alsa.c @@ -76,6 +76,21 @@ struct alsa_elem *get_elem_by_prefix(GArray *elems, const char *prefix) { return NULL; } +// return the first element with a name containing the given substring +struct alsa_elem *get_elem_by_substr(GArray *elems, const char *substr) { + for (int i = 0; i < elems->len; i++) { + struct alsa_elem *elem = &g_array_index(elems, struct alsa_elem, i); + + if (!elem->card) + continue; + + if (strstr(elem->name, substr)) + return elem; + } + + return NULL; +} + // find the maximum number in the matching elements // search by element name prefix and substring // e.g. get_max_elem_by_name(elems, "Line", "Pad Capture Switch") diff --git a/src/alsa.h b/src/alsa.h index 64b5cc1..4bd21b2 100644 --- a/src/alsa.h +++ b/src/alsa.h @@ -200,6 +200,7 @@ void fatal_alsa_error(const char *msg, int err); // locate elements or get information about them struct alsa_elem *get_elem_by_name(GArray *elems, const char *name); struct alsa_elem *get_elem_by_prefix(GArray *elems, const char *prefix); +struct alsa_elem *get_elem_by_substr(GArray *elems, const char *substr); int get_max_elem_by_name( GArray *elems, const char *prefix,