]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
parser: Refactor listing namespaces
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:50:41 +0000 (12:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:50:41 +0000 (12:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/parser.c

index b051c3e65fadefa5dc1d1fe07244297866e6086b..fe6c88bec2706a8a00f667711aa38bc3da180e1c 100644 (file)
@@ -807,57 +807,45 @@ ERROR:
        return r;
 }
 
-char** pakfire_parser_list_namespaces(struct pakfire_parser* parser,
-               const char* filter) {
+char** pakfire_parser_list_namespaces(struct pakfire_parser* parser, const char* filter) {
+       struct pakfire_parser_declaration* d = NULL;
        char** namespaces = NULL;
-       unsigned int counter = 0;
+       int r;
 
        for (unsigned int i = 0; i < parser->num_declarations; i++) {
-               struct pakfire_parser_declaration* d = parser->declarations[i];
+               d = parser->declarations[i];
 
+               // Filter out anything
                if (filter) {
-                       int r = fnmatch(filter, d->namespace, 0);
-
-                       // Skip declaration if it does not match
+                       r = fnmatch(filter, d->namespace, 0);
                        if (r == FNM_NOMATCH)
                                continue;
 
                        // Check for any errors
-                       else if (r > 0) {
+                       else if (r) {
                                ERROR(parser->ctx, "fnmatch failed: %m\n");
-                               if (namespaces)
-                                       free(namespaces);
-
-                               return NULL;
-                       }
-               }
-
-               // Does this already exist on the list?
-               if (namespaces) {
-                       int found = 0;
-
-                       for (char** namespace = namespaces; *namespace; namespace++) {
-                               if (strcmp(d->namespace, *namespace) == 0) {
-                                       found = 1;
-                                       break;
-                               }
+                               r = -errno;
+                               goto ERROR;
                        }
-
-                       // Skip adding if it already exists
-                       if (found)
-                               continue;
                }
 
-               namespaces = reallocarray(namespaces, counter + 2, sizeof(*namespaces));
-               if (!namespaces)
-                       return NULL;
+               // Skip if already on the list
+               if (namespaces && pakfire_strings_contain(namespaces, d->namespace))
+                       continue;
 
-               // Add namespace and terminate the array
-               namespaces[counter++] = d->namespace;
-               namespaces[counter] = NULL;
+               // Append to the list
+               r = pakfire_strings_append(&namespaces, d->namespace);
+               if (r < 0)
+                       goto ERROR;
        }
 
        return namespaces;
+
+ERROR:
+       if (namespaces)
+               pakfire_strings_free(namespaces);
+
+       return NULL;
 }
 
 int pakfire_parser_merge(struct pakfire_parser* parser1, struct pakfire_parser* parser2) {