diff mbox series

[RFC,v1,225/256] cl8k: add utils/string.c

Message ID 20210617160223.160998-226-viktor.barna@celeno.com
State New
Headers show
Series wireless: cl8k driver for Celeno IEEE 802.11ax devices | expand

Commit Message

Viktor Barna June 17, 2021, 4:01 p.m. UTC
From: Viktor Barna <viktor.barna@celeno.com>

(Part of the split. Please, take a look at the cover letter for more
details).

Signed-off-by: Viktor Barna <viktor.barna@celeno.com>
---
 .../net/wireless/celeno/cl8k/utils/string.c   | 235 ++++++++++++++++++
 1 file changed, 235 insertions(+)
 create mode 100644 drivers/net/wireless/celeno/cl8k/utils/string.c

--
2.30.0
diff mbox series

Patch

diff --git a/drivers/net/wireless/celeno/cl8k/utils/string.c b/drivers/net/wireless/celeno/cl8k/utils/string.c
new file mode 100644
index 000000000000..1300563e86a6
--- /dev/null
+++ b/drivers/net/wireless/celeno/cl8k/utils/string.c
@@ -0,0 +1,235 @@ 
+// SPDX-License-Identifier: MIT
+/* Copyright(c) 2019-2021, Celeno Communications Ltd. */
+
+#include "utils/string.h"
+#include "utils/utils.h"
+
+int cl_strtobool_vector(char *src, bool *dest, u8 elem_cnt, char *delim)
+{
+       u8 i = 0;
+       char *buf = NULL;
+       char vector[STR_LEN_256B] = {0};
+
+       if (strlen(src) >= sizeof(vector))
+               return -E2BIG;
+
+       strcpy(vector, src);
+       buf = cl_strtok(vector, delim);
+
+       if (!buf)
+               return -EIO;
+       if (kstrtobool(buf, &dest[0]) != 0)
+               return -EINVAL;
+
+       for (i = 1; i < elem_cnt; i++) {
+               buf = cl_strtok(NULL, delim);
+               if (!buf)
+                       break;
+               if (kstrtobool(buf, &dest[i]) != 0)
+                       return -EINVAL;
+       }
+
+       if (i < elem_cnt) {
+               pr_err("src %s doesn't have %u elements\n", src, elem_cnt);
+               return  -1;
+       }
+
+       return 0;
+}
+
+int cl_strtou8_vector(char *src, u8 *dest, u8 elem_cnt, char *delim)
+{
+       u8 i = 0;
+       char *buf = NULL;
+       char vector[STR_LEN_256B] = {0};
+
+       if (strlen(src) >= sizeof(vector))
+               return -E2BIG;
+
+       strcpy(vector, src);
+       buf = cl_strtok(vector, delim);
+
+       if (!buf)
+               return -EIO;
+       if (kstrtou8(buf, 0, &dest[0]) != 0)
+               return -EINVAL;
+
+       for (i = 1; i < elem_cnt; i++) {
+               buf = cl_strtok(NULL, delim);
+               if (!buf)
+                       break;
+               if (kstrtou8(buf, 0, &dest[i]) != 0)
+                       return -EINVAL;
+       }
+
+       if (i < elem_cnt) {
+               pr_err("src %s doesn't have %u elements\n", src, elem_cnt);
+               return -1;
+       }
+
+       return 0;
+}
+
+int cl_strtou8_hex_vector(char *src, u8 *dest, u8 elem_cnt, char *delim)
+{
+       u8 i = 0;
+       char *buf = NULL;
+       char vector[STR_LEN_32B] = {0};
+
+       if (strlen(src) >= sizeof(vector))
+               return -E2BIG;
+
+       strcpy(vector, src);
+       buf = cl_strtok(vector, delim);
+
+       if (!buf)
+               return -EIO;
+       if (kstrtou8(buf, 16, &dest[0]) != 0)
+               return -EINVAL;
+
+       for (i = 1; i < elem_cnt; i++) {
+               buf = cl_strtok(NULL, delim);
+               if (!buf)
+                       break;
+               if (kstrtou8(buf, 16, &dest[i]) != 0)
+                       return -1;
+       }
+
+       if (i < elem_cnt) {
+               pr_err("src %s doesn't have %u elements\n", src, elem_cnt);
+               return -1;
+       }
+
+       return 0;
+}
+
+int cl_strtou16_vector(char *src, u16 *dest, u8 elem_cnt, char *delim)
+{
+       u8 i = 0;
+       char *buf = NULL;
+       char vector[STR_LEN_256B] = {0};
+
+       if (strlen(src) >= sizeof(vector))
+               return -E2BIG;
+
+       strcpy(vector, src);
+       buf = cl_strtok(vector, delim);
+
+       if (!buf)
+               return -EIO;
+       if (kstrtou16(buf, 0, &dest[0]) != 0)
+               return -EINVAL;
+
+       for (i = 1; i < elem_cnt; i++) {
+               buf = cl_strtok(NULL, delim);
+               if (!buf)
+                       break;
+               if (kstrtou16(buf, 0, &dest[i]) != 0)
+                       return -EINVAL;
+       }
+
+       if (i < elem_cnt) {
+               pr_err("src %s doesn't have %u elements\n", src, elem_cnt);
+               return -1;
+       }
+
+       return 0;
+}
+
+int cl_strtou32_vector(char *src, u32 *dest, u8 elem_cnt, char *delim)
+{
+       u8 i = 0;
+       char *buf = NULL;
+       char vector[STR_LEN_256B] = {0};
+
+       if (strlen(src) >= sizeof(vector))
+               return -E2BIG;
+
+       strcpy(vector, src);
+       buf = cl_strtok(vector, delim);
+
+       if (!buf)
+               return -EIO;
+       if (kstrtou32(buf, 0, &dest[0]) != 0)
+               return -EINVAL;
+
+       for (i = 1; i < elem_cnt; i++) {
+               buf = cl_strtok(NULL, delim);
+               if (!buf)
+                       break;
+               if (kstrtou32(buf, 0, &dest[i]) != 0)
+                       return -EINVAL;
+       }
+
+       if (i < elem_cnt) {
+               pr_err("src %s doesn't have %u elements\n", src, elem_cnt);
+               return -1;
+       }
+
+       return 0;
+}
+
+int cl_strtos8_vector(char *src, s8 *dest, u8 elem_cnt, char *delim)
+{
+       u8 i = 0;
+       char *buf = NULL;
+       char vector[STR_LEN_256B] = {0};
+
+       if (strlen(src) >= sizeof(vector))
+               return -E2BIG;
+
+       strcpy(vector, src);
+       buf = cl_strtok(vector, delim);
+
+       if (!buf)
+               return -EIO;
+       if (kstrtos8(buf, 0, &dest[0]) != 0)
+               return -EINVAL;
+
+       for (i = 1; i < elem_cnt; i++) {
+               buf = cl_strtok(NULL, delim);
+               if (!buf)
+                       break;
+               if (kstrtos8(buf, 0, &dest[i]) != 0)
+                       return -EINVAL;
+       }
+
+       if (i < elem_cnt) {
+               pr_err("src %s doesn't have %u elements\n", src, elem_cnt);
+               return -1;
+       }
+
+       return 0;
+}
+
+static s8 *_strtok;
+
+s8 *cl_strtok(s8 *s, const s8 *ct)
+{
+       return cl_strtok_r(s, ct, &_strtok);
+}
+
+/* cl_strtok_r() is a reentrant version of cl_strtok() */
+s8 *cl_strtok_r(s8 *s, const s8 *ct, s8 **saveptr)
+{
+       s8 *sbegin, *send;
+
+       sbegin  = s ? s : *saveptr;
+       if (!sbegin)
+               return NULL;
+
+       sbegin += strspn(sbegin, ct);
+       if (*sbegin == '\0') {
+               *saveptr = NULL;
+               return NULL;
+       }
+
+       send = strpbrk(sbegin, ct);
+       if (send && *send != '\0')
+               *send++ = '\0';
+
+       *saveptr = send;
+
+       return sbegin;
+}
+