libpkgconf: refactor some path operations

feature/tap-sh
William Pitcock 2016-11-30 22:15:13 -06:00
parent 082fd4af24
commit f4da1082cb
4 changed files with 82 additions and 58 deletions

View File

@ -87,7 +87,8 @@ libpkgconf_la_SOURCES = \
libpkgconf/fileio.c \
libpkgconf/tuple.c \
libpkgconf/dependency.c \
libpkgconf/queue.c
libpkgconf/queue.c \
libpkgconf/path.c
libpkgconf_la_LDFLAGS = -version-info 1:0:0
# -export-symbols-regex '^pkgconf_'

View File

@ -21,6 +21,19 @@
#include <libpkgconf/iter.h>
#include <libpkgconf/bsdstubs.h>
/* pkg-config uses ';' on win32 as ':' is part of path */
#ifdef _WIN32
#define PKG_CONFIG_PATH_SEP_S ";"
#else
#define PKG_CONFIG_PATH_SEP_S ":"
#endif
#ifdef _WIN32
#define PKG_DIR_SEP_S '\\'
#else
#define PKG_DIR_SEP_S '/'
#endif
#define PKGCONF_BUFSIZE (65535)
typedef enum {
@ -39,6 +52,7 @@ typedef struct pkgconf_pkg_ pkgconf_pkg_t;
typedef struct pkgconf_dependency_ pkgconf_dependency_t;
typedef struct pkgconf_tuple_ pkgconf_tuple_t;
typedef struct pkgconf_fragment_ pkgconf_fragment_t;
typedef struct pkgconf_path_ pkgconf_path_t;
#define PKGCONF_ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
@ -74,6 +88,12 @@ struct pkgconf_tuple_ {
char *value;
};
struct pkgconf_path_ {
pkgconf_node_t lnode;
char *path;
};
#define PKGCONF_PKG_PROPF_NONE 0x0
#define PKGCONF_PKG_PROPF_VIRTUAL 0x1
#define PKGCONF_PKG_PROPF_CACHED 0x2
@ -215,4 +235,8 @@ void pkgconf_audit_open_log(FILE *auditf);
void pkgconf_audit_log(const char *format, ...) PRINTFLIKE(1, 2);
void pkgconf_audit_log_dependency(const pkgconf_pkg_t *dep, const pkgconf_dependency_t *depnode);
/* path.c */
void pkgconf_path_add(const char *text, pkgconf_list_t *dirlist);
size_t pkgconf_path_split(const char *text, pkgconf_list_t *dirlist);
#endif

49
libpkgconf/path.c Normal file
View File

@ -0,0 +1,49 @@
/*
* path.c
* filesystem path management
*
* Copyright (c) 2016 pkgconf authors (see AUTHORS).
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <libpkgconf/libpkgconf.h>
void
pkgconf_path_add(const char *text, pkgconf_list_t *dirlist)
{
pkgconf_path_t *node;
node = calloc(sizeof(pkgconf_path_t), 1);
node->path = strdup(text);
pkgconf_node_insert_tail(&node->lnode, node, dirlist);
}
size_t
pkgconf_path_split(const char *text, pkgconf_list_t *dirlist)
{
size_t count = 0;
char *workbuf, *p, *iter;
if (text == NULL)
return 0;
iter = workbuf = strdup(text);
while ((p = strtok(iter, PKG_CONFIG_PATH_SEP_S)) != NULL)
{
pkgconf_path_add(p, dirlist);
count++, iter = NULL;
}
free(workbuf);
return count;
}

View File

@ -53,56 +53,6 @@ pkgconf_set_error_handler(pkgconf_error_handler_func_t func)
#define PKG_CONFIG_EXT ".pc"
#define PKG_CONFIG_PATH_SZ (65535)
/* pkg-config uses ';' on win32 as ':' is part of path */
#ifdef _WIN32
#define PKG_CONFIG_PATH_SEP_S ";"
#else
#define PKG_CONFIG_PATH_SEP_S ":"
#endif
#ifdef _WIN32
#define PKG_DIR_SEP_S '\\'
#else
#define PKG_DIR_SEP_S '/'
#endif
typedef struct {
char *path;
pkgconf_node_t node;
} pkg_path_t;
static inline void
path_add(const char *text, pkgconf_list_t *dirlist)
{
pkg_path_t *pkg_path;
pkg_path = calloc(sizeof(pkg_path_t), 1);
pkg_path->path = strdup(text);
pkgconf_node_insert_tail(&pkg_path->node, pkg_path, dirlist);
}
static inline size_t
path_split(const char *text, pkgconf_list_t *dirlist)
{
size_t count = 0;
char *workbuf, *p, *iter;
if (text == NULL)
return 0;
iter = workbuf = strdup(text);
while ((p = strtok(iter, PKG_CONFIG_PATH_SEP_S)) != NULL)
{
path_add(p, dirlist);
count++, iter = NULL;
}
free(workbuf);
return count;
}
static inline bool
str_has_suffix(const char *str, const char *suffix)
{
@ -185,12 +135,12 @@ pkgconf_pkg_dir_list_build(unsigned int flags)
/* PKG_CONFIG_PATH has to take precedence */
env_path = getenv("PKG_CONFIG_PATH");
if (env_path)
path_split(env_path, &pkg_dir_list);
pkgconf_path_split(env_path, &pkg_dir_list);
if (!(flags & PKGCONF_PKG_PKGF_ENV_ONLY))
{
env_path = get_pkgconfig_path();
path_split(env_path, &pkg_dir_list);
pkgconf_path_split(env_path, &pkg_dir_list);
}
}
@ -476,9 +426,9 @@ pkgconf_scan_all(void *data, pkgconf_pkg_iteration_func_t func)
PKGCONF_FOREACH_LIST_ENTRY(pkg_dir_list.head, n)
{
pkg_path_t *pkg_path = n->data;
pkgconf_path_t *pnode = n->data;
if ((pkg = pkgconf_pkg_scan_dir(pkg_path->path, data, func)) != NULL)
if ((pkg = pkgconf_pkg_scan_dir(pnode->path, data, func)) != NULL)
return pkg;
}
@ -540,7 +490,7 @@ pkgconf_pkg_find(const char *name, unsigned int flags)
pkgconf_pkg_t *pkg;
pkg = pkgconf_pkg_new_from_file(name, f, flags);
path_add(pkg_get_parent_dir(pkg), &pkg_dir_list);
pkgconf_path_add(pkg_get_parent_dir(pkg), &pkg_dir_list);
return pkg;
}
@ -562,9 +512,9 @@ pkgconf_pkg_find(const char *name, unsigned int flags)
PKGCONF_FOREACH_LIST_ENTRY(pkg_dir_list.head, n)
{
pkg_path_t *pkg_path = n->data;
pkgconf_path_t *pnode = n->data;
pkg = pkgconf_pkg_try_specific_path(pkg_path->path, name, flags);
pkg = pkgconf_pkg_try_specific_path(pnode->path, name, flags);
if (pkg != NULL)
goto out;
}