pkg: add pkg_scan() and pkg_scan_all() functions.

pull/36/head
William Pitcock 2012-07-25 23:49:07 -05:00
parent 1394a4edfe
commit 6f5fbb80d4
3 changed files with 64 additions and 0 deletions

60
pkg.c
View File

@ -274,6 +274,66 @@ pkg_try_specific_path(const char *path, const char *name, unsigned int flags)
return pkg; 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 #ifdef _WIN32
pkg_t * pkg_t *
pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags) pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags)

3
pkg.h
View File

@ -114,11 +114,14 @@ struct pkg_ {
#define PKG_ERRF_PACKAGE_VER_MISMATCH 0x2 #define PKG_ERRF_PACKAGE_VER_MISMATCH 0x2
#define PKG_ERRF_PACKAGE_CONFLICT 0x4 #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); typedef void (*pkg_traverse_func_t)(pkg_t *pkg, void *data, unsigned int flags);
/* pkg.c */ /* pkg.c */
void pkg_free(pkg_t *pkg); void pkg_free(pkg_t *pkg);
pkg_t *pkg_find(const char *name, unsigned int flags); 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_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); unsigned int pkg_verify_graph(pkg_t *root, int depth, unsigned int flags);
int pkg_compare_version(const char *a, const char *b); int pkg_compare_version(const char *a, const char *b);

View File

@ -23,6 +23,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <libgen.h> #include <libgen.h>
#include <dirent.h>
#ifdef _WIN32 #ifdef _WIN32
# define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN