diff options
Diffstat (limited to 'bio/vaje/1/programi/explode.c')
-rw-r--r-- | bio/vaje/1/programi/explode.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/bio/vaje/1/programi/explode.c b/bio/vaje/1/programi/explode.c new file mode 100644 index 0000000..094dd41 --- /dev/null +++ b/bio/vaje/1/programi/explode.c @@ -0,0 +1,56 @@ +#pragma once +#include <string.h> +char *strdup(const char *src) +{ + char *tmp = malloc(strlen(src) + 1); + if(tmp) + strcpy(tmp, src); + return tmp; +} + +void explode(const char *src, const char *tokens, char ***list, size_t *len) +{ + if(src == NULL || list == NULL || len == NULL) + return; + + char *str, *copy, **_list = NULL, **tmp; + *list = NULL; + *len = 0; + + copy = strdup(src); + if(copy == NULL) + return; + + str = strtok(copy, tokens); + if(str == NULL) + goto free_and_exit; + + _list = realloc(NULL, sizeof *_list); + if(_list == NULL) + goto free_and_exit; + + _list[*len] = strdup(str); + if(_list[*len] == NULL) + goto free_and_exit; + (*len)++; + + + while((str = strtok(NULL, tokens))) + { + tmp = realloc(_list, (sizeof *_list) * (*len + 1)); + if(tmp == NULL) + goto free_and_exit; + + _list = tmp; + + _list[*len] = strdup(str); + if(_list[*len] == NULL) + goto free_and_exit; + (*len)++; + } + + +free_and_exit: + *list = _list; + free(copy); +} |