X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fstring-utils%2Fstring-utils.c;h=55399a1156b54d9a3b33113a73eabaec45e9bda0;hb=a0377dfefe40662ba7d68617bce6ff467114136c;hp=325f54b537a29cb36ac150a144460fcd298a2e52;hpb=f181fa5afa242b51c1a6cd8868f56a579a7e39a0;p=lttng-tools.git diff --git a/src/common/string-utils/string-utils.c b/src/common/string-utils/string-utils.c index 325f54b53..55399a115 100644 --- a/src/common/string-utils/string-utils.c +++ b/src/common/string-utils/string-utils.c @@ -1,25 +1,14 @@ /* - * Copyright (C) 2017 - Philippe Proulx + * Copyright (C) 2017 Philippe Proulx * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License, version 2 only, as - * published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 51 - * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _LGPL_SOURCE #include #include #include -#include #include "string-utils.h" #include "../macros.h" @@ -41,7 +30,7 @@ void strutils_normalize_star_glob_pattern(char *pattern) char *np; bool got_star = false; - assert(pattern); + LTTNG_ASSERT(pattern); for (p = pattern, np = pattern; *p != '\0'; p++) { switch (*p) { @@ -85,7 +74,7 @@ enum star_glob_pattern_type_flags strutils_test_glob_pattern(const char *pattern STAR_GLOB_PATTERN_TYPE_FLAG_NONE; const char *p; - assert(pattern); + LTTNG_ASSERT(pattern); for (p = pattern; *p != '\0'; p++) { switch (*p) { @@ -147,7 +136,7 @@ char *strutils_unescape_string(const char *input, char only_char) char *o; const char *i; - assert(input); + LTTNG_ASSERT(input); output = zmalloc(strlen(input) + 1); if (!output) { goto end; @@ -204,11 +193,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: * @@ -251,21 +238,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_ASSERT(input); + LTTNG_ASSERT(!(escape_delim && delim == '\\')); + LTTNG_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++) { @@ -295,18 +286,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); - substrings[at] = zmalloc(longest_substring_len + 1); - if (!substrings[at]) { + if (!substring) { + goto error; + } + + ret = lttng_dynamic_pointer_array_add_pointer( + out_strings, substring); + if (ret) { + free(substring); goto error; } @@ -314,7 +307,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) { /* @@ -353,13 +346,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 @@ -368,7 +361,7 @@ size_t strutils_array_of_strings_len(char * const *array) char * const *item; size_t count = 0; - assert(array); + LTTNG_ASSERT(array); for (item = array; *item; item++) { count++;