kvargs: support list value
authorThomas Monjalon <thomas@monjalon.net>
Mon, 22 Oct 2018 13:15:27 +0000 (15:15 +0200)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 26 Oct 2018 20:14:06 +0000 (22:14 +0200)
If a value contains a comma, rte_kvargs_tokenize() will split here.
In order to support list syntax [a,b] as value, an extra parsing of
the square brackets is added.

Signed-off-by: Thomas Monjalon <thomas@monjalon.net>
lib/librte_kvargs/rte_kvargs.c
test/test/test_kvargs.c

index 7ec1ea5..f7030c6 100644 (file)
@@ -44,6 +44,20 @@ rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
                    kvlist->pairs[i].value == NULL)
                        return -1;
 
+               /* Detect list [a,b] to skip comma delimiter in list. */
+               str = kvlist->pairs[i].value;
+               if (str[0] == '[') {
+                       /* Find the end of the list. */
+                       while (str[strlen(str) - 1] != ']') {
+                               /* Restore the comma erased by strtok_r(). */
+                               str[strlen(str)] = ',';
+                               /* Parse until next comma. */
+                               str = strtok_r(NULL, RTE_KVARGS_PAIRS_DELIM, &ctx1);
+                               if (str == NULL)
+                                       return -1; /* no closing bracket */
+                       }
+               }
+
                kvlist->count++;
                str = NULL;
        }
index e673862..a42056f 100644 (file)
@@ -137,6 +137,26 @@ static int test_valid_kvargs(void)
        }
        rte_kvargs_free(kvlist);
 
+       /* third test using list as value */
+       args = "foo=[0,1],check=value2";
+       valid_keys = valid_keys_list;
+       kvlist = rte_kvargs_parse(args, valid_keys);
+       if (kvlist == NULL) {
+               printf("rte_kvargs_parse() error");
+               goto fail;
+       }
+       if (strcmp(kvlist->pairs[0].value, "[0,1]") != 0) {
+               printf("wrong value %s", kvlist->pairs[0].value);
+               goto fail;
+       }
+       count = kvlist->count;
+       if (count != 2) {
+               printf("invalid count value %d\n", count);
+               rte_kvargs_free(kvlist);
+               goto fail;
+       }
+       rte_kvargs_free(kvlist);
+
        return 0;
 
  fail:
@@ -162,6 +182,7 @@ static int test_invalid_kvargs(void)
                "foo=1,,foo=2",    /* empty key/value */
                "foo=1,foo",       /* no value */
                "foo=1,=2",        /* no key */
+               "foo=[1,2",        /* no closing bracket in value */
                ",=",              /* also test with a smiley */
                NULL };
        const char **args;