From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id A10C9A0546;
	Sat, 10 Apr 2021 16:24:29 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 730DD1412BD;
	Sat, 10 Apr 2021 16:24:25 +0200 (CEST)
Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129])
 by mails.dpdk.org (Postfix) with ESMTP id 43BCE1412B8
 for <dev@dpdk.org>; Sat, 10 Apr 2021 16:24:24 +0200 (CEST)
Received: from Internal Mail-Server by MTLPINE1 (envelope-from
 xuemingl@nvidia.com) with SMTP; 10 Apr 2021 17:24:21 +0300
Received: from nvidia.com (pegasus05.mtr.labs.mlnx [10.210.16.100])
 by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 13AEO6E1018265;
 Sat, 10 Apr 2021 17:24:20 +0300
From: Xueming Li <xuemingl@nvidia.com>
To: Thomas Monjalon <thomas@monjalon.net>, Gaetan Rivet <gaetanr@nvidia.com>
Cc: dev@dpdk.org, xuemingl@nvidia.com, Asaf Penso <asafp@nvidia.com>,
 Olivier Matz <olivier.matz@6wind.com>, Ray Kinsella <mdr@ashroe.eu>,
 Neil Horman <nhorman@tuxdriver.com>
Date: Sat, 10 Apr 2021 14:23:55 +0000
Message-Id: <1618064637-16413-4-git-send-email-xuemingl@nvidia.com>
X-Mailer: git-send-email 1.8.3.1
In-Reply-To: <1618064637-16413-1-git-send-email-xuemingl@nvidia.com>
References: <1618064637-16413-1-git-send-email-xuemingl@nvidia.com>
In-Reply-To: <1608304614-13908-2-git-send-email-xuemingl@nvidia.com>
References: <1608304614-13908-2-git-send-email-xuemingl@nvidia.com>
Subject: [dpdk-dev] [PATCH v4 3/5] kvargs: add get by key function
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

Adds a new function to get value of a specific key from kvargs list.

Signed-off-by: Xueming Li <xuemingl@nvidia.com>
Reviewed-by: Gaetan Rivet <grive@u256.net>
---
 lib/librte_kvargs/rte_kvargs.c | 20 ++++++++++++++++++++
 lib/librte_kvargs/rte_kvargs.h | 21 +++++++++++++++++++++
 lib/librte_kvargs/version.map  |  3 +++
 3 files changed, 44 insertions(+)

diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c
index ffae8914cf..40e7670ab3 100644
--- a/lib/librte_kvargs/rte_kvargs.c
+++ b/lib/librte_kvargs/rte_kvargs.c
@@ -203,6 +203,26 @@ 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)
+		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..cb3ea99850 100644
--- a/lib/librte_kvargs/rte_kvargs.h
+++ b/lib/librte_kvargs/rte_kvargs.h
@@ -114,6 +114,27 @@ 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 the key is NULL, the first value from the list is returned.
+ * 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..fb9bed4f93 100644
--- a/lib/librte_kvargs/version.map
+++ b/lib/librte_kvargs/version.map
@@ -15,4 +15,7 @@ EXPERIMENTAL {
 	rte_kvargs_parse_delim;
 	rte_kvargs_strcmp;
 
+	# added in 21.05
+	rte_kvargs_get;
+
 };
-- 
2.25.1