Skip to content

Commit

Permalink
pkg: make pkgconf_scan_all() API more flexible to allow it to scan un…
Browse files Browse the repository at this point in the history
…til it finds a qualifying pkgconf_pkg_t
  • Loading branch information
kaniini committed Aug 27, 2016
1 parent 5ba4613 commit bbe6dcc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
4 changes: 2 additions & 2 deletions libpkgconf/libpkgconf.h
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 19 additions & 7 deletions libpkgconf/pkg.c
Expand Up @@ -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))
{
Expand All @@ -449,27 +450,38 @@ 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);

PKGCONF_FOREACH_LIST_ENTRY(pkg_dir_list.head, n)
{
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
Expand Down
24 changes: 16 additions & 8 deletions main.c
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit bbe6dcc

Please sign in to comment.