]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
parser: Use string functions to manage filelists
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:43:00 +0000 (12:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:43:00 +0000 (12:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/parser.c

index 70f934de508b4fa175aedda9032b15154d2215d7..b051c3e65fadefa5dc1d1fe07244297866e6086b 100644 (file)
@@ -765,34 +765,9 @@ char* pakfire_parser_get(struct pakfire_parser* parser, const char* namespace, c
        return pakfire_parser_expand(parser, namespace, value);
 }
 
-static int append_to_array(char*** array, const char* s) {
-       unsigned int length = 0;
-
-       // Determine the length of the existing array
-       if (*array) {
-               for (char** element = *array; *element; element++)
-                       length++;
-       }
-
-       // Allocate space
-       *array = reallocarray(*array, length + 2, sizeof(**array));
-       if (!*array)
-               return 1;
-
-       // Copy the string to the heap
-       char* p = strdup(s);
-       if (!p)
-               return 1;
-
-       // Append p and terminate the array
-       (*array)[length] = p;
-       (*array)[length + 1] = NULL;
-
-       return 0;
-}
-
 int pakfire_parser_get_filelist(struct pakfire_parser* parser, const char* namespace,
                const char* name, char*** includes, char*** excludes) {
+       const char* file = NULL;
        char* p = NULL;
        int r = 0;
 
@@ -801,19 +776,23 @@ int pakfire_parser_get_filelist(struct pakfire_parser* parser, const char* names
 
        // Nothing to do for empty lists
        if (!list)
-               return 0;
+               goto ERROR;
 
-       const char* file = strtok_r(list, " \n", &p);
+       // Split the string
+       file = strtok_r(list, " \n", &p);
 
        // Split into includes and excludes
        while (file) {
+               // Excludes
                if (excludes && *file == '!') {
-                       r = append_to_array(excludes, file + 1);
-                       if (r)
+                       r = pakfire_strings_append(excludes, file + 1);
+                       if (r < 0)
                                goto ERROR;
-               } else {
-                       r = append_to_array(includes, file);
-                       if (r)
+
+               // Includes
+               } else if (includes) {
+                       r = pakfire_strings_append(includes, file);
+                       if (r < 0)
                                goto ERROR;
                }