All of lore.kernel.org
 help / color / mirror / Atom feed
From: Parav Pandit <parav@nvidia.com>
To: <virtualization@lists.linux-foundation.org>,
	<netdev@vger.kernel.org>, <dsahern@gmail.com>,
	<stephen@networkplumber.org>, <mst@redhat.com>,
	<jasowang@redhat.com>
Subject: [PATCH iproute2-next v4 4/5] utils: Add helper to map string to unsigned int
Date: Fri, 5 Feb 2021 20:10:28 +0200	[thread overview]
Message-ID: <20210205181029.365461-5-parav@nvidia.com> (raw)
In-Reply-To: <20210205181029.365461-1-parav@nvidia.com>

In subsequent patch need to map a string to a unsigned int.
Hence, add an API to map a string to unsigned int.

Signed-off-by: Parav Pandit <parav@nvidia.com>
---
changelog:
v2->v3:
 - new patch to reuse string to unsigned int mapping
---
 include/utils.h |  4 +++-
 lib/utils.c     | 17 +++++++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index 9b76c92a..b29c3798 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -342,10 +342,12 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
 
 struct str_num_map {
 	const char *str;
-	int num;
+	unsigned int num;
 };
 
 int str_map_lookup_str(const struct str_num_map *map, const char *needle);
+const char *str_map_lookup_uint(const struct str_num_map *map,
+				unsigned int val);
 const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
 const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
 
diff --git a/lib/utils.c b/lib/utils.c
index cc6d0e34..633f6359 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1953,9 +1953,22 @@ int str_map_lookup_str(const struct str_num_map *map, const char *needle)
 	return -EINVAL;
 }
 
+const char *str_map_lookup_uint(const struct str_num_map *map, unsigned int val)
+{
+	unsigned int num = val;
+
+	while (map && map->str) {
+		if (num == map->num)
+			return map->str;
+
+		map++;
+	}
+	return NULL;
+}
+
 const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
 {
-	int num = val;
+	unsigned int num = val;
 
 	while (map && map->str) {
 		if (num == map->num)
@@ -1968,7 +1981,7 @@ const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
 
 const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val)
 {
-	int num = val;
+	unsigned int num = val;
 
 	while (map && map->str) {
 		if (num == map->num)
-- 
2.26.2

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

WARNING: multiple messages have this Message-ID (diff)
From: Parav Pandit <parav@nvidia.com>
To: <virtualization@lists.linux-foundation.org>,
	<netdev@vger.kernel.org>, <dsahern@gmail.com>,
	<stephen@networkplumber.org>, <mst@redhat.com>,
	<jasowang@redhat.com>
Cc: Parav Pandit <parav@nvidia.com>
Subject: [PATCH iproute2-next v4 4/5] utils: Add helper to map string to unsigned int
Date: Fri, 5 Feb 2021 20:10:28 +0200	[thread overview]
Message-ID: <20210205181029.365461-5-parav@nvidia.com> (raw)
In-Reply-To: <20210205181029.365461-1-parav@nvidia.com>

In subsequent patch need to map a string to a unsigned int.
Hence, add an API to map a string to unsigned int.

Signed-off-by: Parav Pandit <parav@nvidia.com>
---
changelog:
v2->v3:
 - new patch to reuse string to unsigned int mapping
---
 include/utils.h |  4 +++-
 lib/utils.c     | 17 +++++++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index 9b76c92a..b29c3798 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -342,10 +342,12 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
 
 struct str_num_map {
 	const char *str;
-	int num;
+	unsigned int num;
 };
 
 int str_map_lookup_str(const struct str_num_map *map, const char *needle);
+const char *str_map_lookup_uint(const struct str_num_map *map,
+				unsigned int val);
 const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
 const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
 
diff --git a/lib/utils.c b/lib/utils.c
index cc6d0e34..633f6359 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1953,9 +1953,22 @@ int str_map_lookup_str(const struct str_num_map *map, const char *needle)
 	return -EINVAL;
 }
 
+const char *str_map_lookup_uint(const struct str_num_map *map, unsigned int val)
+{
+	unsigned int num = val;
+
+	while (map && map->str) {
+		if (num == map->num)
+			return map->str;
+
+		map++;
+	}
+	return NULL;
+}
+
 const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
 {
-	int num = val;
+	unsigned int num = val;
 
 	while (map && map->str) {
 		if (num == map->num)
@@ -1968,7 +1981,7 @@ const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
 
 const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val)
 {
-	int num = val;
+	unsigned int num = val;
 
 	while (map && map->str) {
 		if (num == map->num)
-- 
2.26.2


  parent reply	other threads:[~2021-02-05 18:10 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-22 11:26 [PATCH iproute2-next 0/2] Add vdpa device management tool Parav Pandit
2021-01-22 11:26 ` Parav Pandit
2021-01-22 11:26 ` [PATCH iproute2-next 1/2] Add kernel headers Parav Pandit
2021-01-22 11:26   ` Parav Pandit
2021-01-22 11:26 ` [PATCH iproute2-next 2/2] vdpa: Add vdpa tool Parav Pandit
2021-01-22 11:26   ` Parav Pandit
2021-01-26  4:22   ` David Ahern
2021-01-26  4:22     ` David Ahern
2021-01-26 13:26     ` Parav Pandit
2021-01-26 13:26       ` Parav Pandit
2021-01-28 18:43   ` [PATCH iproute2-next v2 0/2] Add vdpa device management tool Parav Pandit
2021-01-28 18:43     ` Parav Pandit
2021-01-28 18:43     ` [PATCH iproute2-next v2 1/2] Add kernel headers Parav Pandit
2021-01-28 18:43       ` Parav Pandit
2021-01-28 18:43     ` [PATCH iproute2-next v2 2/2] vdpa: Add vdpa tool Parav Pandit
2021-01-28 18:43       ` Parav Pandit
2021-01-29  3:23       ` Parav Pandit
2021-01-29  3:23         ` Parav Pandit
2021-02-02 10:35   ` [PATCH iproute2-next v3 0/5] Add vdpa device management tool Parav Pandit
2021-02-02 10:35     ` Parav Pandit
2021-02-02 10:35     ` [PATCH iproute2-next v3 1/5] Add kernel headers Parav Pandit
2021-02-02 10:35       ` Parav Pandit
2021-02-04  1:37       ` David Ahern
2021-02-04  1:37         ` David Ahern
2021-02-05 17:54         ` Parav Pandit
2021-02-05 17:54           ` Parav Pandit
2021-02-02 10:35     ` [PATCH iproute2-next v3 2/5] utils: Add helper routines for indent handling Parav Pandit
2021-02-02 10:35       ` Parav Pandit
2021-02-02 10:35     ` [PATCH iproute2-next v3 3/5] utils: Add generic socket helpers Parav Pandit
2021-02-02 10:35       ` Parav Pandit
2021-02-02 10:35     ` [PATCH iproute2-next v3 4/5] utils: Add helper to map string to unsigned int Parav Pandit
2021-02-02 10:35       ` Parav Pandit
2021-02-02 10:35     ` [PATCH iproute2-next v3 5/5] vdpa: Add vdpa tool Parav Pandit
2021-02-02 10:35       ` Parav Pandit
2021-02-04  3:16     ` [PATCH iproute2-next v3 0/5] Add vdpa device management tool Jason Wang
2021-02-04  3:16       ` Jason Wang
2021-02-04 11:15       ` Adrian Moreno
2021-02-04 11:15         ` Adrian Moreno
2021-02-05  3:40         ` Jason Wang
2021-02-05  3:40           ` Jason Wang
2021-02-05 17:53           ` Parav Pandit
2021-02-05 17:53             ` Parav Pandit
2021-02-05 18:06             ` Adrian Moreno
2021-02-05 18:06               ` Adrian Moreno
2021-02-05 18:07           ` Adrian Moreno
2021-02-05 18:07             ` Adrian Moreno
2021-02-05 18:10   ` [PATCH iproute2-next v4 " Parav Pandit
2021-02-05 18:10     ` Parav Pandit
2021-02-05 18:10     ` [PATCH iproute2-next v4 1/5] Add kernel headers Parav Pandit
2021-02-05 18:10       ` Parav Pandit
2021-02-08 15:47       ` David Ahern
2021-02-08 15:47         ` David Ahern
2021-02-10 18:28         ` Parav Pandit
2021-02-10 18:28           ` Parav Pandit
2021-02-05 18:10     ` [PATCH iproute2-next v4 2/5] utils: Add helper routines for indent handling Parav Pandit
2021-02-05 18:10       ` Parav Pandit
2021-02-05 18:10     ` [PATCH iproute2-next v4 3/5] utils: Add generic socket helpers Parav Pandit
2021-02-05 18:10       ` Parav Pandit
2021-02-05 18:10     ` Parav Pandit [this message]
2021-02-05 18:10       ` [PATCH iproute2-next v4 4/5] utils: Add helper to map string to unsigned int Parav Pandit
2021-02-05 18:10     ` [PATCH iproute2-next v4 5/5] vdpa: Add vdpa tool Parav Pandit
2021-02-05 18:10       ` Parav Pandit

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210205181029.365461-5-parav@nvidia.com \
    --to=parav@nvidia.com \
    --cc=dsahern@gmail.com \
    --cc=jasowang@redhat.com \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.