From 6f5fbb80d4a76656749964ba44226f8ff154c625 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Wed, 25 Jul 2012 23:49:07 -0500 Subject: [PATCH] pkg: add pkg_scan() and pkg_scan_all() functions. --- pkg.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkg.h | 3 +++ stdinc.h | 1 + 3 files changed, 64 insertions(+) diff --git a/pkg.c b/pkg.c index e1c8e50..67f3c1c 100644 --- a/pkg.c +++ b/pkg.c @@ -274,6 +274,66 @@ pkg_try_specific_path(const char *path, const char *name, unsigned int flags) return pkg; } +static void +pkg_scan_dir(const char *path, pkg_iteration_func_t func) +{ + DIR *dir; + struct dirent *dirent; + + dir = opendir(path); + if (dir == NULL) + return; + + for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) + { + pkg_t *pkg; + FILE *f; + + f = fopen(dirent->d_name, "r"); + if (f == NULL) + continue; + + pkg = pkg_new_from_file(dirent->d_name, f); + if (pkg != NULL) + func(pkg); + } + + closedir(dir); +} + +void +pkg_scan(const char *search_path, pkg_iteration_func_t func) +{ + char **path = NULL; + size_t count = 0, iter = 0; + + /* PKG_CONFIG_PATH has to take precedence */ + if (search_path == NULL) + return; + + count = path_split(search_path, &path); + + for (iter = 0; iter < count; iter++) + pkg_scan_dir(path[iter], func); + + path_free(path, count); +} + +void +pkg_scan_all(pkg_iteration_func_t func) +{ + char *path; + + path = getenv("PKG_CONFIG_PATH"); + if (path) + { + pkg_scan_dir(path, func); + return; + } + + pkg_scan_dir(get_pkgconfig_path(), func); +} + #ifdef _WIN32 pkg_t * pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags) diff --git a/pkg.h b/pkg.h index 4ba4fb9..651d174 100644 --- a/pkg.h +++ b/pkg.h @@ -114,11 +114,14 @@ struct pkg_ { #define PKG_ERRF_PACKAGE_VER_MISMATCH 0x2 #define PKG_ERRF_PACKAGE_CONFLICT 0x4 +typedef void (*pkg_iteration_func_t)(const pkg_t *pkg); typedef void (*pkg_traverse_func_t)(pkg_t *pkg, void *data, unsigned int flags); /* pkg.c */ void pkg_free(pkg_t *pkg); pkg_t *pkg_find(const char *name, unsigned int flags); +void pkg_scan(const char *search_path, pkg_iteration_func_t func); +void pkg_scan_all(pkg_iteration_func_t func); unsigned int pkg_traverse(pkg_t *root, pkg_traverse_func_t func, void *data, int maxdepth, unsigned int flags); unsigned int pkg_verify_graph(pkg_t *root, int depth, unsigned int flags); int pkg_compare_version(const char *a, const char *b); diff --git a/stdinc.h b/stdinc.h index 8d561f9..cc35e57 100644 --- a/stdinc.h +++ b/stdinc.h @@ -23,6 +23,7 @@ #include #include #include +#include #ifdef _WIN32 # define WIN32_LEAN_AND_MEAN