From: Michael Tremer Date: Fri, 3 Jan 2025 09:50:10 +0000 (+0000) Subject: string: Move array functions into strings.c X-Git-Tag: 0.9.30~564 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56010887fc062f568678edc56fc4b918186af045;p=pakfire.git string: Move array functions into strings.c The inline option has some limitations and it makes things messy. The compiler will hopefully inline these functions no matter what. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/string.c b/src/pakfire/string.c index 10348ca44..e409bba3c 100644 --- a/src/pakfire/string.c +++ b/src/pakfire/string.c @@ -309,6 +309,87 @@ char* pakfire_string_join(const char** list, const char* delim) { return string; } +size_t pakfire_strings_length(char** array) { + size_t length = 0; + + // If we have received NULL, we will return zero + if (!array) + return 0; + + // Count all elements + for (char** s = array; *s; s++) + length++; + + return length; +} + +void pakfire_strings_free(char** array) { + for (char** s = array; *s; s++) + free(*s); + + free(array); +} + +static int __pakfire_strings_append(char*** array, char* s) { + if (!array) + return -EINVAL; + + // Fetch the length + size_t length = pakfire_strings_length(*array); + + // Resize the array + *array = reallocarray(*array, length + 2, sizeof(**array)); + if (!*array) + return -errno; + + // Store the string + (*array)[length++] = s; + + // Terminate the array + (*array)[length] = NULL; + + // Return the total length of the array + return length; +} + +int pakfire_strings_append(char*** array, const char* s) { + int r; + + // Copy the string + char* p = strdup(s); + if (!p) + return -errno; + + // Append it to the array + r = __pakfire_strings_append(array, p); + if (r < 0) + free(p); + + return r; +} + +int pakfire_strings_appendf(char*** array, const char* format, ...) { + char* p = NULL; + va_list args; + int r; + + // Format the string + va_start(args, format); + r = vasprintf(&p, format, args); + va_end(args); + + // Break on any errors + if (r < 0) + return -errno; + + // Append it to the array + r = __pakfire_strings_append(array, p); + if (r < 0) + free(p); + + return r; +} + #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" int __pakfire_format_size(char* dst, size_t length, double value) { diff --git a/src/pakfire/string.h b/src/pakfire/string.h index 47c2f373e..f9f0c2d81 100644 --- a/src/pakfire/string.h +++ b/src/pakfire/string.h @@ -83,53 +83,16 @@ static inline int pakfire_string_contains_whitespace(const char* s) { } /* - Cleanup Stuff + String Arrays */ -inline size_t pakfire_strings_length(char** array) { - size_t length = 0; +size_t pakfire_strings_length(char** array); - // If we have received NULL, we will return zero - if (!array) - return 0; +void pakfire_strings_free(char** array); - // Count all elements - for (char** s = array; *s; s++) - length++; - - return length; -} - -inline void pakfire_strings_free(char** array) { - for (char** s = array; *s; s++) - free(*s); - - free(array); -} - -inline int pakfire_strings_append(char*** array, const char* s) { - if (!array) - return -EINVAL; - - // Fetch the length - size_t length = pakfire_strings_length(*array); - - // Resize the array - *array = reallocarray(*array, length + 2, sizeof(**array)); - if (!*array) - return -errno; - - // Copy the string - char* p = (*array)[length++] = strdup(s); - if (!p) - return -errno; - - // Terminate the array - (*array)[length] = NULL; - - // Return the total length of the array - return length; -} +int pakfire_strings_append(char*** array, const char* s); +int pakfire_strings_appendf(char*** array, const char* format, ...) + __attribute__((format(printf, 2, 3))); #define TIME_STRING_MAX 32