[dpdk-dev] [PATCH 3/5] cfgfile: add support for unamed global section

Allain Legacy allain.legacy at windriver.com
Thu Mar 2 20:29:29 CET 2017


The current implementation of the cfgfile library requires that all
key=value pairs be within [SECTION] definitions.  The ini file standard
allows for key=value pairs in an unnamed section.  That section is
considered the [GLOBAL] section.

This commit adds the capability of parsing key=value pairs from such an
unnamed section. The CFG_FLAG_GLOBAL_SECTION flag must be passed to the
rte_cfgfile_load() API to enable this functionality.

Signed-off-by: Allain Legacy <allain.legacy at windriver.com>
---
 lib/librte_cfgfile/rte_cfgfile.c | 16 ++++++++++++++++
 lib/librte_cfgfile/rte_cfgfile.h |  6 ++++++
 2 files changed, 22 insertions(+)

diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c
index 7a9206d..2aba169 100644
--- a/lib/librte_cfgfile/rte_cfgfile.c
+++ b/lib/librte_cfgfile/rte_cfgfile.c
@@ -108,6 +108,22 @@ struct rte_cfgfile *
 
 	memset(cfg, 0, size);
 
+	if (flags & CFG_FLAG_GLOBAL_SECTION) {
+		curr_section = 0;
+		allocated_entries = CFG_ALLOC_ENTRY_BATCH;
+		cfg->sections[curr_section] = malloc(
+			sizeof(*cfg->sections[0]) +
+			sizeof(cfg->sections[0]->entries[0]) *
+			allocated_entries);
+		if (cfg->sections[curr_section] == NULL) {
+			printf("Error - no memory for global section\n");
+			goto error1;
+		}
+
+		snprintf(cfg->sections[curr_section]->name,
+				 sizeof(cfg->sections[0]->name), "GLOBAL");
+	}
+
 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
 		char *pos = NULL;
 		size_t len = strnlen(buffer, sizeof(buffer));
diff --git a/lib/librte_cfgfile/rte_cfgfile.h b/lib/librte_cfgfile/rte_cfgfile.h
index b40e6a1..fce1efc 100644
--- a/lib/librte_cfgfile/rte_cfgfile.h
+++ b/lib/librte_cfgfile/rte_cfgfile.h
@@ -67,6 +67,12 @@ struct rte_cfgfile_entry {
 };
 
 /**
+ * Indicates that the file supports key value entries before the first defined
+ * section.  These entries can be accessed in the "GLOBAL" section.
+ */
+#define CFG_FLAG_GLOBAL_SECTION (1 << 0)
+
+/**
 * Open config file
 *
 * @param filename
-- 
1.8.3.1



More information about the dev mailing list