X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fstring-utils%2Fstring-utils.c;fp=src%2Fcommon%2Fstring-utils%2Fstring-utils.c;h=669f7d05fd01c64513b68128a740c8940fdcaa9d;hp=e77ddfedbf51a1a88ad3c1f47d5b498ff3611032;hb=e358ddd51a5be6017f524523ac10d7c17fb78f65;hpb=651dd74ddc4e98f443888d3afea237df85ca1b22 diff --git a/src/common/string-utils/string-utils.c b/src/common/string-utils/string-utils.c index e77ddfedb..669f7d05f 100644 --- a/src/common/string-utils/string-utils.c +++ b/src/common/string-utils/string-utils.c @@ -194,11 +194,9 @@ void strutils_free_null_terminated_array_of_strings(char **array) /* * Splits the input string `input` using the given delimiter `delim`. * - * The return value is an allocated null-terminated array of the - * resulting substrings (also allocated). You can free this array and - * its content with strutils_free_null_terminated_array_of_strings(). You - * can get the number of substrings in it with - * strutils_array_of_strings_len(). + * The return value is a dynamic pointer array that is assumed to be empty. The + * array must be discarded by the caller by invoking + * lttng_dynamic_pointer_array_reset(). * * Empty substrings are part of the result. For example: * @@ -241,21 +239,25 @@ void strutils_free_null_terminated_array_of_strings(char **array) * `\` * `hi` * - * Returns NULL if there's an error. + * Returns -1 if there's an error. */ LTTNG_HIDDEN -char **strutils_split(const char *input, char delim, bool escape_delim) +int strutils_split(const char *input, + char delim, + bool escape_delim, + struct lttng_dynamic_pointer_array *out_strings) { + int ret; size_t at; size_t number_of_substrings = 1; size_t longest_substring_len = 0; const char *s; const char *last; - char **substrings = NULL; assert(input); assert(!(escape_delim && delim == '\\')); assert(delim != '\0'); + lttng_dynamic_pointer_array_init(out_strings, free); /* First pass: count the number of substrings. */ for (s = input, last = input - 1; *s != '\0'; s++) { @@ -285,18 +287,20 @@ char **strutils_split(const char *input, char delim, bool escape_delim) longest_substring_len = s - last - 1; } - substrings = calloc(number_of_substrings + 1, sizeof(*substrings)); - if (!substrings) { - goto error; - } - /* Second pass: actually split and copy substrings. */ for (at = 0, s = input; at < number_of_substrings; at++) { const char *ss; char *d; + char *substring = zmalloc(longest_substring_len + 1); + + if (!substring) { + goto error; + } - substrings[at] = zmalloc(longest_substring_len + 1); - if (!substrings[at]) { + ret = lttng_dynamic_pointer_array_add_pointer( + out_strings, substring); + if (ret) { + free(substring); goto error; } @@ -304,7 +308,7 @@ char **strutils_split(const char *input, char delim, bool escape_delim) * Copy characters to substring until we find the next * delimiter or the end of the input string. */ - for (ss = s, d = substrings[at]; *ss != '\0'; ss++) { + for (ss = s, d = substring; *ss != '\0'; ss++) { if (escape_delim && *ss == '\\') { if (ss[1] == delim) { /* @@ -343,13 +347,13 @@ char **strutils_split(const char *input, char delim, bool escape_delim) s = ss + 1; } + ret = 0; goto end; error: - strutils_free_null_terminated_array_of_strings(substrings); - substrings = NULL; + ret = -1; end: - return substrings; + return ret; } LTTNG_HIDDEN