From 3ab385063cb9d0b6bf28564823fe4932a559d055 Mon Sep 17 00:00:00 2001 From: Xueming Li Date: Tue, 13 Apr 2021 03:14:10 +0000 Subject: [PATCH] kvargs: add get by key Adds a new function to get value of a specific key from kvargs list. Signed-off-by: Xueming Li Reviewed-by: Gaetan Rivet --- lib/librte_kvargs/rte_kvargs.c | 15 +++++++++++++++ lib/librte_kvargs/rte_kvargs.h | 20 ++++++++++++++++++++ lib/librte_kvargs/version.map | 2 ++ 3 files changed, 37 insertions(+) diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c index ffae8914cf..4cce8e953b 100644 --- a/lib/librte_kvargs/rte_kvargs.c +++ b/lib/librte_kvargs/rte_kvargs.c @@ -203,6 +203,21 @@ rte_kvargs_free(struct rte_kvargs *kvlist) free(kvlist); } +/* Lookup a value in an rte_kvargs list by its key. */ +const char * +rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key) +{ + unsigned int i; + + if (kvlist == NULL || key == NULL) + return NULL; + for (i = 0; i < kvlist->count; ++i) { + if (strcmp(kvlist->pairs[i].key, key) == 0) + return kvlist->pairs[i].value; + } + return NULL; +} + /* * Parse the arguments "key=value,key=value,..." string and return * an allocated structure that contains a key/value list. Also diff --git a/lib/librte_kvargs/rte_kvargs.h b/lib/librte_kvargs/rte_kvargs.h index eff598e08b..12a8f90435 100644 --- a/lib/librte_kvargs/rte_kvargs.h +++ b/lib/librte_kvargs/rte_kvargs.h @@ -114,6 +114,26 @@ struct rte_kvargs *rte_kvargs_parse_delim(const char *args, */ void rte_kvargs_free(struct rte_kvargs *kvlist); +/** + * Get the value associated with a given key. + * + * If multiple key matches, the value of the first one is returned. + * + * The memory returned is allocated as part of the rte_kvargs structure, + * it must never be modified. + * + * @param kvlist + * A list of rte_kvargs pair of 'key=value'. + * @param key + * The matching key. + + * @return + * NULL if no key matches the input, + * a value associated with a matching key otherwise. + */ +__rte_experimental +const char *rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key); + /** * Call a handler function for each key/value matching the key * diff --git a/lib/librte_kvargs/version.map b/lib/librte_kvargs/version.map index ed375bf4a3..ce8a9175dd 100644 --- a/lib/librte_kvargs/version.map +++ b/lib/librte_kvargs/version.map @@ -15,4 +15,6 @@ EXPERIMENTAL { rte_kvargs_parse_delim; rte_kvargs_strcmp; + # added in 21.05 + rte_kvargs_get; }; -- 2.20.1