]> git.alsa-project.org Git - alsa-lib.git/commitdiff
conf: add snd_config_is_array() function
authorJaroslav Kysela <perex@perex.cz>
Fri, 8 May 2020 10:00:42 +0000 (12:00 +0200)
committerJaroslav Kysela <perex@perex.cz>
Fri, 8 May 2020 10:00:42 +0000 (12:00 +0200)
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/conf.h
src/conf.c

index 456b272e1bf8c423d9910442e6454c9b869b8505..823ac639c09e9684c6e04a276d18e14bf5893fa9 100644 (file)
@@ -139,6 +139,7 @@ int snd_config_imake_safe_string(snd_config_t **config, const char *key, const c
 int snd_config_imake_pointer(snd_config_t **config, const char *key, const void *ptr);
 
 snd_config_type_t snd_config_get_type(const snd_config_t *config);
+int snd_config_is_array(const snd_config_t *config);
 
 int snd_config_set_id(snd_config_t *config, const char *id);
 int snd_config_set_integer(snd_config_t *config, long value);
index 46bc1fb85a32a83ed79e1c4f9f9ab938b759ef6c..b59f232c433e53e15cc9aca6e657081a7231879b 100644 (file)
@@ -1784,6 +1784,45 @@ snd_config_type_t snd_config_get_type(const snd_config_t *config)
        return config->type;
 }
 
+static int check_array_item(const char *id, int index)
+{
+       const char *p;
+       long val;
+
+       for (p = id; *p; p++) {
+               if (*p < '0' || *p > '9')
+                       return 0;
+       }
+
+       if (safe_strtol(id, &val))
+               return 0;
+       return val == index;
+}
+
+/**
+ * \brief Returns if the compound is an array.
+ * \param config Handle to the configuration node.
+ * \return A positive value when true, zero when false, otherwise a negative error code.
+ */
+int snd_config_is_array(const snd_config_t *config)
+{
+       int idx;
+       snd_config_iterator_t i, next;
+       snd_config_t *node;
+
+       assert(config);
+       if (config->type != SND_CONFIG_TYPE_COMPOUND)
+               return -EINVAL;
+       idx = 0;
+       snd_config_for_each(i, next, config) {
+               node = snd_config_iterator_entry(i);
+               if (!check_array_item(node->id, idx))
+                       return 0;
+               idx++;
+       }
+       return 1;
+}
+
 /**
  * \brief Returns the id of a configuration node.
  * \param[in] config Handle to the configuration node.