diff options
Diffstat (limited to 'src/string2.c')
-rw-r--r-- | src/string2.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/string2.c b/src/string2.c new file mode 100644 index 0000000..2d59897 --- /dev/null +++ b/src/string2.c @@ -0,0 +1,33 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#include <string.h> + +char ** +strsplit(char *str, char delim, int *size) +{ + char **lines = NULL; + char *s = str; + + int count = 0; + + for (int start = 0, end = 0; ; s++, end++) { + if (*s == delim || *s == '\0') { + int _size = end - start; + lines = realloc(lines, sizeof(char *) * ++count); + int idx = count - 1; + if (_size > 1) { + lines[idx] = calloc(_size + 1, sizeof(char *)); + strncpy(lines[idx], str + start, _size); + } else { + lines[idx] = strdup(""); + } + start = end + 1; /* plus 1 - skip the delim */ + } + if (*s == '\0') break; + } + + *size = count; + + return lines; +} |