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) {