diff mbox series

[v2,3/5] kvargs: add get by key function

Message ID 1610983002-7630-4-git-send-email-xuemingl@nvidia.com (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers show
Series eal: enable global device syntax | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Xueming(Steven) Li Jan. 18, 2021, 3:16 p.m. UTC
Adds a new function to get value of a specific key from kvargs list.

Signed-off-by: Xueming Li <xuemingl@nvidia.com>
---
 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 mbox series

Patch

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;
 
 };