pkg: make pkgconf_scan_all() API more flexible to allow it to scan until it finds a qualifying pkgconf_pkg_t

pull/100/head
William Pitcock 2016-08-27 09:48:53 -05:00
parent 5ba46130d2
commit bbe6dcc088
3 changed files with 37 additions and 17 deletions

View File

@ -126,7 +126,7 @@ struct pkgconf_pkg_ {
#define PKGCONF_PKG_ERRF_PACKAGE_CONFLICT 0x4
#define PKGCONF_PKG_ERRF_DEPGRAPH_BREAK 0x8
typedef void (*pkgconf_pkg_iteration_func_t)(const pkgconf_pkg_t *pkg);
typedef bool (*pkgconf_pkg_iteration_func_t)(const pkgconf_pkg_t *pkg, void *data);
typedef void (*pkgconf_pkg_traverse_func_t)(pkgconf_pkg_t *pkg, void *data, unsigned int flags);
typedef bool (*pkgconf_queue_apply_func_t)(pkgconf_pkg_t *world, void *data, int maxdepth, unsigned int flags);
typedef bool (*pkgconf_error_handler_func_t)(const char *msg);
@ -160,7 +160,7 @@ pkgconf_pkg_comparator_t pkgconf_pkg_comparator_lookup_by_name(const char *name)
pkgconf_pkg_t *pkgconf_builtin_pkg_get(const char *name);
int pkgconf_compare_version(const char *a, const char *b);
void pkgconf_scan_all(pkgconf_pkg_iteration_func_t func);
pkgconf_pkg_t *pkgconf_scan_all(void *ptr, pkgconf_pkg_iteration_func_t func);
/* parse.c */
pkgconf_pkg_t *pkgconf_pkg_new_from_file(const char *path, FILE *f, unsigned int flags);

View File

@ -417,15 +417,16 @@ pkgconf_pkg_try_specific_path(const char *path, const char *name, unsigned int f
return pkg;
}
static void
pkgconf_pkg_scan_dir(const char *path, pkgconf_pkg_iteration_func_t func)
static pkgconf_pkg_t *
pkgconf_pkg_scan_dir(const char *path, void *data, pkgconf_pkg_iteration_func_t func)
{
DIR *dir;
struct dirent *dirent;
pkgconf_pkg_t *outpkg = NULL;
dir = opendir(path);
if (dir == NULL)
return;
return NULL;
for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir))
{
@ -449,18 +450,26 @@ pkgconf_pkg_scan_dir(const char *path, pkgconf_pkg_iteration_func_t func)
pkg = pkgconf_pkg_new_from_file(filebuf, f, 0);
if (pkg != NULL)
{
func(pkg);
if (func(pkg, data))
{
outpkg = pkg;
goto out;
}
pkgconf_pkg_unref(pkg);
}
}
out:
closedir(dir);
return outpkg;
}
void
pkgconf_scan_all(pkgconf_pkg_iteration_func_t func)
pkgconf_pkg_t *
pkgconf_scan_all(void *data, pkgconf_pkg_iteration_func_t func)
{
pkgconf_node_t *n;
pkgconf_pkg_t *pkg;
pkgconf_pkg_dir_list_build(0);
@ -468,8 +477,11 @@ pkgconf_scan_all(pkgconf_pkg_iteration_func_t func)
{
pkg_path_t *pkg_path = n->data;
pkgconf_pkg_scan_dir(pkg_path->path, func);
if ((pkg = pkgconf_pkg_scan_dir(pkg_path->path, data, func)) != NULL)
return pkg;
}
return NULL;
}
#ifdef _WIN32

24
main.c
View File

@ -125,22 +125,30 @@ print_fragment(pkgconf_fragment_t *frag)
printf("%s ", frag->data);
}
static void
print_list_entry(const pkgconf_pkg_t *entry)
static bool
print_list_entry(const pkgconf_pkg_t *entry, void *data)
{
(void) data;
if (entry->flags & PKGCONF_PKG_PROPF_UNINSTALLED)
return;
return false;
printf("%-30s %s - %s\n", entry->id, entry->realname, entry->description);
return false;
}
static void
print_package_entry(const pkgconf_pkg_t *entry)
static bool
print_package_entry(const pkgconf_pkg_t *entry, void *data)
{
(void) data;
if (entry->flags & PKGCONF_PKG_PROPF_UNINSTALLED)
return;
return false;
printf("%s\n", entry->id);
return false;
}
static void
@ -818,13 +826,13 @@ main(int argc, char *argv[])
if ((want_flags & PKG_LIST) == PKG_LIST)
{
pkgconf_scan_all(print_list_entry);
pkgconf_scan_all(NULL, print_list_entry);
return EXIT_SUCCESS;
}
if ((want_flags & PKG_LIST_PACKAGE_NAMES) == PKG_LIST_PACKAGE_NAMES)
{
pkgconf_scan_all(print_package_entry);
pkgconf_scan_all(NULL, print_package_entry);
return EXIT_SUCCESS;
}