]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
string: Move array functions into strings.c
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Jan 2025 09:50:10 +0000 (09:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 3 Jan 2025 09:50:10 +0000 (09:50 +0000)
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 <michael.tremer@ipfire.org>
src/pakfire/string.c
src/pakfire/string.h

index 10348ca4463cb9db716911c87a8057c97fea407d..e409bba3c1ec6f4d0ebad82a0da44b065fe9894a 100644 (file)
@@ -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) {
index 47c2f373ee166d8809d39d8e1b7ab18b328bfd56..f9f0c2d816cd50964cfad69f1ddb330c487a5dac 100644 (file)
@@ -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