From: QI Fuli <qi.f...@fujitsu.com>

Add parse-config helper to help ndctl commands read the ndctl global
configuration file.

Signed-off-by: QI Fuli <qi.f...@fujitsu.com>
---
 Makefile.am          |  2 ++
 util/parse-configs.c | 47 ++++++++++++++++++++++++++++++++++++++++++++
 util/parse-configs.h | 26 ++++++++++++++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 util/parse-configs.c
 create mode 100644 util/parse-configs.h

diff --git a/Makefile.am b/Makefile.am
index 960b5e9..6e50741 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -74,6 +74,8 @@ noinst_LIBRARIES += libutil.a
 libutil_a_SOURCES = \
        util/parse-options.c \
        util/parse-options.h \
+       util/parse-configs.c \
+       util/parse-configs.h \
        util/usage.c \
        util/size.c \
        util/main.c \
diff --git a/util/parse-configs.c b/util/parse-configs.c
new file mode 100644
index 0000000..d404786
--- /dev/null
+++ b/util/parse-configs.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2021, FUJITSU LIMITED. ALL rights reserved.
+
+#include <errno.h>
+#include <util/parse-configs.h>
+#include <util/strbuf.h>
+#include <ccan/ciniparser/ciniparser.h>
+
+static void set_value(const char **value, char *val)
+{
+       struct strbuf buf = STRBUF_INIT;
+       size_t len = *value ? strlen(*value) : 0;
+
+       if (!val)
+               return;
+
+       if (len) {
+               strbuf_add(&buf, *value, len);
+               strbuf_addstr(&buf, " ");
+       }
+       strbuf_addstr(&buf, val);
+       *value = strbuf_detach(&buf, NULL);
+}
+
+int parse_configs(const char *config_file, const struct config *configs)
+{
+       dictionary *dic;
+
+       dic = ciniparser_load(config_file);
+       if (!dic)
+               return -errno;
+
+       for (; configs->type != CONFIG_END; configs++) {
+               switch (configs->type) {
+               case CONFIG_STRING:
+                       set_value((const char **)configs->value,
+                                       ciniparser_getstring(dic,
+                                       configs->key, configs->defval));
+                       break;
+               case CONFIG_END:
+                       break;
+               }
+       }
+
+       ciniparser_freedict(dic);
+       return 0;
+}
diff --git a/util/parse-configs.h b/util/parse-configs.h
new file mode 100644
index 0000000..31886f7
--- /dev/null
+++ b/util/parse-configs.h
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2021, FUJITSU LIMITED. ALL rights reserved.
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <util/util.h>
+
+enum parse_conf_type {
+       CONFIG_STRING,
+       CONFIG_END,
+};
+
+struct config {
+       enum parse_conf_type type;
+       const char *key;
+       void *value;
+       void *defval;
+};
+
+#define check_vtype(v, type) ( 
BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v )
+
+#define CONF_END() { .type = CONFIG_END }
+#define CONF_STR(k,v,d) \
+       { .type = CONFIG_STRING, .key = (k), .value = check_vtype(v, const char 
**), .defval = (d) }
+
+int parse_configs(const char *config_file, const struct config *configs);
-- 
2.30.2
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org

Reply via email to