diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/core/include/strlist.h | 6 | ||||
| -rw-r--r-- | src/lib/core/strlist.c | 59 |
2 files changed, 42 insertions, 23 deletions
diff --git a/src/lib/core/include/strlist.h b/src/lib/core/include/strlist.h index b2d7da7..f44025c 100644 --- a/src/lib/core/include/strlist.h +++ b/src/lib/core/include/strlist.h @@ -45,8 +45,10 @@ int strlist_append_file(struct StrList *pStrList, char *path, ReaderFn *readerFn void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2); void strlist_append(struct StrList **pStrList, char *str); void strlist_append_array(struct StrList *pStrList, char **arr); -void strlist_append_tokenize(struct StrList *pStrList, char *str, char *delim); -void strlist_append_tokenize_raw(struct StrList *pStrList, char *str, char *delim); + +int strlist_append_tokenize(struct StrList *pStrList, char *str, char *delim); + +int strlist_append_tokenize_raw(struct StrList *pStrList, char *str, char *delim); int strlist_appendf(struct StrList **pStrList, const char *fmt, ...); struct StrList *strlist_copy(struct StrList *pStrList); int strlist_cmp(struct StrList *a, struct StrList *b); diff --git a/src/lib/core/strlist.c b/src/lib/core/strlist.c index 42d5b85..0d25a66 100644 --- a/src/lib/core/strlist.c +++ b/src/lib/core/strlist.c @@ -218,22 +218,30 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2 * @param str * @param delim */ - void strlist_append_tokenize(struct StrList *pStrList, char *str, char *delim) { - if (!str || !delim) { - return; - } +int strlist_append_tokenize(struct StrList *pStrList, char *str, char *delim) { + if (!str || !delim) { + return -1; + } - char *tmp = strdup(str); - char **token = split(tmp, delim, 0); - if (token) { - for (size_t i = 0; token[i] != NULL; i++) { - lstrip(token[i]); - strlist_append(&pStrList, token[i]); - } - guard_array_free(token); - } + char *tmp = strdup(str); + if (!tmp) { + return -1; + } + + char **token = split(tmp, delim, 0); + if (!token) { + guard_free(tmp); + return -1; + } + for (size_t i = 0; token[i] != NULL; i++) { + lstrip(token[i]); + strlist_append(&pStrList, token[i]); + } + guard_array_free(token); guard_free(tmp); - } + + return 0; +} /** * Append the contents of a newline delimited string without @@ -242,20 +250,29 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2 * @param str * @param delim */ -void strlist_append_tokenize_raw(struct StrList *pStrList, char *str, char *delim) { +int strlist_append_tokenize_raw(struct StrList *pStrList, char *str, char *delim) { if (!str || !delim) { - return; + return -1; } char *tmp = strdup(str); + if (!tmp) { + return -1; + } + char **token = split(tmp, delim, 0); - if (token) { - for (size_t i = 0; token[i] != NULL; i++) { - strlist_append(&pStrList, token[i]); - } - guard_array_free(token); + if (!token) { + guard_free(tmp); + return -1; } + + for (size_t i = 0; token[i] != NULL; i++) { + strlist_append(&pStrList, token[i]); + } + + guard_array_free(token); guard_free(tmp); + return 0; } /** |
