[dpdk-dev] [PATCH v1 5/7] kvargs: add get by key function
From: Xueming Li <hidden>
Date: 2021-01-08 14:56:04
Subsystem:
library code, the rest · Maintainers:
Andrew Morton, Linus Torvalds
Adds a new function to get value of a specific key from kvargs list. Signed-off-by: Xueming Li <redacted> --- lib/librte_kvargs/rte_kvargs.c | 20 ++++++++++++++++++++ lib/librte_kvargs/rte_kvargs.h | 14 ++++++++++++++ lib/librte_kvargs/version.map | 1 + 3 files changed, 35 insertions(+)
diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c
index 285081c86c..bc734915f9 100644
--- a/lib/librte_kvargs/rte_kvargs.c
+++ b/lib/librte_kvargs/rte_kvargs.c@@ -160,6 +160,26 @@ rte_kvargs_free(struct rte_kvargs *kvlist) free(kvlist); } +/* lookup the rte_kvargs structure by key */ +const char * +rte_kvargs_get(struct rte_kvargs *kvlist, const char *key) +{ + unsigned int i; + + if (!kvlist) + return NULL; + for (i = 0; i < kvlist->count; ++i) { + /* Allows key to be NULL. */ + if (!key && !kvlist->pairs[i].key) + return kvlist->pairs[i].value; + if (!key || !kvlist->pairs[i].key) + continue; + if (!strcmp(kvlist->pairs[i].key, key)) + 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..6d426241ea 100644
--- a/lib/librte_kvargs/rte_kvargs.h
+++ b/lib/librte_kvargs/rte_kvargs.h@@ -114,6 +114,20 @@ struct rte_kvargs *rte_kvargs_parse_delim(const char *args, */ void rte_kvargs_free(struct rte_kvargs *kvlist); +/** + * Get the value matching the given key + * + * @param kvlist + * The rte_kvargs structure + * @param key + * The key that should match + + * @return + * The value that match, NULL if not found. + */ +__rte_experimental +const char *rte_kvargs_get(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..d6cde16f30 100644
--- a/lib/librte_kvargs/version.map
+++ b/lib/librte_kvargs/version.map@@ -14,5 +14,6 @@ EXPERIMENTAL { rte_kvargs_parse_delim; rte_kvargs_strcmp; + rte_kvargs_get; };
--
2.25.1