[3/7] eal: add function finding integer in a string

Message ID 20200422072747.15960-4-talshn@mellanox.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series Windows bus/pci support |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail apply issues

Commit Message

Tal Shnaiderman April 22, 2020, 7:27 a.m. UTC
  From: Tal Shnaiderman <talshn@mellanox.com>

Addition of a function to skip leading chars which are not part of
the numeric base and return the number in the needed base.

This is needed to call strtoul correctly and will be used
by bus/PCI to get the BDF from a PCI output.

Signed-off-by: Tal Shnaiderman <talshn@mellanox.com>
---
 lib/librte_eal/common/eal_common_string_fns.c | 29 +++++++++++++++++++++++++++
 lib/librte_eal/include/rte_string_fns.h       | 17 ++++++++++++++++
 2 files changed, 46 insertions(+)
  

Patch

diff --git a/lib/librte_eal/common/eal_common_string_fns.c b/lib/librte_eal/common/eal_common_string_fns.c
index 60c5dd66f..29d1539da 100644
--- a/lib/librte_eal/common/eal_common_string_fns.c
+++ b/lib/librte_eal/common/eal_common_string_fns.c
@@ -8,6 +8,7 @@ 
 #include <errno.h>
 
 #include <rte_string_fns.h>
+#include <rte_errno.h>
 
 /* split string into tokens */
 int
@@ -64,3 +65,31 @@  rte_strscpy(char *dst, const char *src, size_t dsize)
 		dst[res - 1] = '\0';
 	return -E2BIG;
 }
+
+/* Skip leading chars to return the number in the needed base
+ *
+ * Return 0 and rte_errno if no number found,
+ * Otherwise return the number in the needed base
+ */
+unsigned long
+rte_find_numerical_value(char *str, int base)
+{
+	unsigned int num = 0;
+	uint8_t i = 0;
+
+	if (str == NULL)
+		goto einval_error;
+
+	while (str[i]) {
+		if ((base == 10 && isdigit(str[i])) ||
+			(base == 16 && isxdigit(str[i]))) {
+			num = strtoul(&str[i], NULL, base);
+			goto end;
+		}
+		i++;
+	}
+einval_error:
+	rte_errno = EINVAL;
+end:
+	return num;
+}
diff --git a/lib/librte_eal/include/rte_string_fns.h b/lib/librte_eal/include/rte_string_fns.h
index 8bac8243c..df6e07dd3 100644
--- a/lib/librte_eal/include/rte_string_fns.h
+++ b/lib/librte_eal/include/rte_string_fns.h
@@ -50,6 +50,23 @@  int
 rte_strsplit(char *string, int stringlen,
              char **tokens, int maxtokens, char delim);
 
+/**
+ * Skips leading characters to return a number in the needed base
+ *
+ * @param str
+ *   The input string to search upon
+ *
+ * @param base
+ *   The base of the needed number.
+ *   (currently supports bases 10 and 16)
+ *
+ * @return
+ *   - the number in the correct base if found
+ *   - zero and rte_errno = EINVAL if no number was found
+ */
+unsigned long
+rte_find_numerical_value(char *str, int base);
+
 /**
  * @internal
  * DPDK-specific version of strlcpy for systems without