[PATCH v2 4/4] lib: Convert str_to_list() to use alist
Simon Glass
sjg at chromium.org
Wed Jul 10 16:38:54 CEST 2024
Use this new data structure in the utility function.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
(no changes since v1)
lib/strto.c | 35 +++++++++++++++++++----------------
1 file changed, 19 insertions(+), 16 deletions(-)
diff --git a/lib/strto.c b/lib/strto.c
index f83ac67c666..f059408755a 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -9,6 +9,7 @@
* Wirzenius wrote this portably, Torvalds fucked it up :-)
*/
+#include <alist.h>
#include <errno.h>
#include <malloc.h>
#include <vsprintf.h>
@@ -226,37 +227,39 @@ void str_to_upper(const char *in, char *out, size_t len)
const char **str_to_list(const char *instr)
{
- const char **ptr;
- char *str, *p;
- int count, i;
+ struct alist alist;
+ char *str, *p, *start;
/* don't allocate if the string is empty */
str = *instr ? strdup(instr) : (char *)instr;
if (!str)
return NULL;
- /* count the number of space-separated strings */
- for (count = 0, p = str; *p; p++) {
+ alist_init_struct(&alist, char *);
+
+ if (*str)
+ alist_add(&alist, &str, char *);
+ for (start = str, p = str; *p; p++) {
if (*p == ' ') {
- count++;
*p = '\0';
+ start = p + 1;
+ if (*start)
+ alist_add(&alist, &start, char *);
}
}
- if (p != str && p[-1])
- count++;
- /* allocate the pointer array, allowing for a NULL terminator */
- ptr = calloc(count + 1, sizeof(char *));
- if (!ptr) {
- if (*str)
+ /* terminate list */
+ p = NULL;
+ alist_add(&alist, &p, char *);
+ if (alist_err(&alist)) {
+ alist_uninit(&alist);
+
+ if (*instr)
free(str);
return NULL;
}
- for (i = 0, p = str; i < count; p += strlen(p) + 1, i++)
- ptr[i] = p;
-
- return ptr;
+ return alist_uninit_move(&alist, NULL, const char *);
}
void str_free_list(const char **ptr)
--
2.34.1
More information about the U-Boot
mailing list