]> git.alsa-project.org Git - alsa-lib.git/commitdiff
conf: introduce snd_config_load_string()
authorJaroslav Kysela <perex@perex.cz>
Wed, 1 Dec 2021 09:14:12 +0000 (10:14 +0100)
committerJaroslav Kysela <perex@perex.cz>
Wed, 1 Dec 2021 09:18:58 +0000 (10:18 +0100)
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/conf.h
src/conf.c
src/ucm/ucm_subs.c
test/lsb/config.c

index c649be3bcb16e5eba0e6b85edda6f7023f984dfa..56ba6c233c30f09acb2f46e57929a43edbb33d21 100644 (file)
@@ -89,6 +89,7 @@ const char *snd_config_topdir(void);
 int snd_config_top(snd_config_t **config);
 
 int snd_config_load(snd_config_t *config, snd_input_t *in);
+int snd_config_load_string(snd_config_t **config, const char *s, size_t size);
 int snd_config_load_override(snd_config_t *config, snd_input_t *in);
 int snd_config_save(snd_config_t *config, snd_output_t *out);
 int snd_config_update(void);
index 09d74b5331b38f731ed7b47c7ed39b3c1ccdc8e2..0f6d2ba82bab2cbc0acd1ad6c9b3c18407be7214 100644 (file)
@@ -2050,6 +2050,49 @@ int snd_config_load(snd_config_t *config, snd_input_t *in)
        return _snd_config_load_with_include(config, in, 0, NULL);
 }
 
+/**
+ * \brief Loads a configuration tree from a string.
+ * \param[out] The function puts the handle to the configuration
+ *            node loaded from the file(s) at the address specified
+ *             by \a config.
+ * \param[in] s String with the ASCII configuration
+ * \param[in] size String size, if zero, a C string is expected (with termination)
+ * \return Zero if successful, otherwise a negative error code.
+ *
+ * The definitions loaded from the string are put to \a config, which
+ * is created as a new top node.
+ *
+ * \par Errors:
+ * Any errors encountered when parsing the input or returned by hooks or
+ * functions.
+ */
+int snd_config_load_string(snd_config_t **config, const char *s, size_t size)
+{
+       snd_input_t *input;
+       snd_config_t *dst;
+       int err;
+
+       assert(config && s);
+       if (size == 0)
+               size = strlen(s);
+       err = snd_input_buffer_open(&input, s, size);
+       if (err < 0)
+               return err;
+       err = snd_config_top(&dst);
+       if (err < 0) {
+               snd_input_close(input);
+               return err;
+       }
+       err = snd_config_load(dst, input);
+       snd_input_close(input);
+       if (err < 0) {
+               snd_config_delete(dst);
+               return err;
+       }
+       *config = dst;
+       return 0;
+}
+
 /**
  * \brief Loads a configuration tree and overrides existing configuration nodes.
  * \param config Handle to a top level configuration node.
index b6fc7be44516199e57be54ceaa9e047d9fe1ce73..0ed400d1ed652c98174d6fd070f755d85bd2fe8c 100644 (file)
@@ -213,32 +213,6 @@ struct lookup_iterate {
        void *info;
 };
 
-static snd_config_t *parse_lookup_query(const char *query)
-{
-       snd_input_t *input;
-       snd_config_t *config;
-       int err;
-
-       err = snd_input_buffer_open(&input, query, strlen(query));
-       if (err < 0) {
-               uc_error("unable to create memory input buffer");
-               return NULL;
-       }
-       err = snd_config_top(&config);
-       if (err < 0) {
-               snd_input_close(input);
-               return NULL;
-       }
-       err = snd_config_load(config, input);
-       snd_input_close(input);
-       if (err < 0) {
-               snd_config_delete(config);
-               uc_error("wrong arguments '%s'", query);
-               return NULL;
-       }
-       return config;
-}
-
 static char *rval_lookup_main(snd_use_case_mgr_t *uc_mgr,
                              const char *query,
                              struct lookup_iterate *iter)
@@ -257,9 +231,11 @@ static char *rval_lookup_main(snd_use_case_mgr_t *uc_mgr,
                return NULL;
        }
 
-       config = parse_lookup_query(query);
-       if (config == NULL)
+       err = snd_config_load_string(&config, query, 0);
+       if (err < 0) {
+               uc_error("The lookup arguments '%s' are invalid", query);
                return NULL;
+       }
        if (iter->init && iter->init(uc_mgr, iter, config))
                goto null;
        if (snd_config_search(config, "field", &d)) {
index cc2b5d82870c52cc472b163a9039ce9c8a55da53..e8699f138c57b7be0e38129dfc01fbadc4db9057 100644 (file)
@@ -598,6 +598,22 @@ static void test_evaluate_string(void)
        }
 }
 
+static void test_load_string(void)
+{
+       const char **cfg, *configs[] = {
+               "a=1,b=2",
+               "j 3;z 15;",
+               "x 0 y -1",
+               NULL
+       };
+       snd_config_t *dst;
+
+       for (cfg = configs; *cfg; cfg++) {
+               ALSA_CHECK(snd_config_load_string(&dst, *cfg, 0));
+               ALSA_CHECK(snd_config_delete(dst));
+       }
+}
+
 int main(void)
 {
        test_top();
@@ -629,5 +645,6 @@ int main(void)
        test_iterators();
        test_for_each();
        test_evaluate_string();
+       test_load_string();
        return TEST_EXIT_CODE();
 }