[dpdk-dev] [PATCH v2 6/6] cfgfile: add support for empty value string

Allain Legacy allain.legacy at windriver.com
Thu Mar 9 14:11:00 CET 2017


This commit adds support to the cfgfile library for parsing a key=value
line that has no value string specified (e.g., "key=").  This can be used
to override a configuration attribute that has a default value or default
list of values to set it back to an undefined value to disable
functionality.

Signed-off-by: Allain Legacy <allain.legacy at windriver.com>
---
 lib/librte_cfgfile/rte_cfgfile.c        | 11 ++++++-----
 test/test/test_cfgfile.c                | 17 +----------------
 test/test/test_cfgfiles/etc/sample1.ini |  2 +-
 test/test/test_cfgfiles/etc/sample2.ini |  2 +-
 4 files changed, 9 insertions(+), 23 deletions(-)

diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c
index 35c81e4..d3022b9 100644
--- a/lib/librte_cfgfile/rte_cfgfile.c
+++ b/lib/librte_cfgfile/rte_cfgfile.c
@@ -219,11 +219,12 @@ struct rte_cfgfile *
 
 			struct rte_cfgfile_section *sect =
 				cfg->sections[curr_section];
-			char *split[2];
-			if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=')
-				!= 2) {
+			int n;
+			char *split[2] = {NULL};
+			n = rte_strsplit(buffer, sizeof(buffer), split, 2, '=');
+			if ((n < 1) || (n > 2)) {
 				printf("Error at line %d - cannot split "
-					"string\n", lineno);
+				       "string, n=%d\n", lineno, n);
 				goto error1;
 			}
 
@@ -254,7 +255,7 @@ struct rte_cfgfile *
 			snprintf(entry->name, sizeof(entry->name), "%s",
 				split[0]);
 			snprintf(entry->value, sizeof(entry->value), "%s",
-				split[1]);
+				 split[1] ? split[1] : "");
 			_strip(entry->name, strnlen(entry->name,
 				sizeof(entry->name)));
 			_strip(entry->value, strnlen(entry->value,
diff --git a/test/test/test_cfgfile.c b/test/test/test_cfgfile.c
index eab8ccc..d8214d1 100644
--- a/test/test/test_cfgfile.c
+++ b/test/test/test_cfgfile.c
@@ -105,8 +105,7 @@
 		    "key2 unexpected value: %s", value);
 
 	value = rte_cfgfile_get_entry(cfgfile, "section2", "key3");
-	TEST_ASSERT(strcmp("value3", value) == 0,
-		    "key3 unexpected value: %s", value);
+	TEST_ASSERT(strlen(value) == 0, "key3 unexpected value: %s", value);
 
 	return 0;
 }
@@ -167,17 +166,6 @@
 }
 
 static int
-test_cfgfile_invalid_key_value_pair(void)
-{
-	struct rte_cfgfile *cfgfile;
-
-	cfgfile = rte_cfgfile_load(CFG_FILES_ETC "/invalid_key_value.ini", 0);
-	TEST_ASSERT_NULL(cfgfile, "Expected failured did not occur");
-
-	return 0;
-}
-
-static int
 test_cfgfile_missing_section(void)
 {
 	struct rte_cfgfile *cfgfile;
@@ -251,9 +239,6 @@
 	if (test_cfgfile_invalid_section_header())
 		return -1;
 
-	if (test_cfgfile_invalid_key_value_pair())
-		return -1;
-
 	if (test_cfgfile_missing_section())
 		return -1;
 
diff --git a/test/test/test_cfgfiles/etc/sample1.ini b/test/test/test_cfgfiles/etc/sample1.ini
index aef91c2..5b001d3 100644
--- a/test/test/test_cfgfiles/etc/sample1.ini
+++ b/test/test/test_cfgfiles/etc/sample1.ini
@@ -8,5 +8,5 @@ key1=value1
 ; this is section 2
 ;key1=value1
 key2=value2
-key3=value3 ; this is key3
+key3= ; this is key3
 ignore-missing-separator
diff --git a/test/test/test_cfgfiles/etc/sample2.ini b/test/test/test_cfgfiles/etc/sample2.ini
index 21075e9..8fee5a7 100644
--- a/test/test/test_cfgfiles/etc/sample2.ini
+++ b/test/test/test_cfgfiles/etc/sample2.ini
@@ -8,5 +8,5 @@ key1=value1
 # this is section 2
 #key1=value1
 key2=value2
-key3=value3 # this is key3
+key3= # this is key3
 ignore-missing-separator
-- 
1.8.3.1



More information about the dev mailing list