From: Michael Tremer Date: Sat, 11 Jan 2025 12:50:41 +0000 (+0000) Subject: parser: Refactor listing namespaces X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=20f4550c225071710f005bc9cf233ed0fb4e2cd6;p=people%2Fric9%2Fpakfire.git parser: Refactor listing namespaces Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/parser.c b/src/pakfire/parser.c index b051c3e65..fe6c88bec 100644 --- a/src/pakfire/parser.c +++ b/src/pakfire/parser.c @@ -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) {