main: use pkgconf_fragment_render() to render fragment lists instead of custom stuff

pull/100/head
William Pitcock 2016-12-09 23:07:15 -06:00
parent af63b799f1
commit d6d7ff775a
3 changed files with 69 additions and 20 deletions

View File

@ -247,6 +247,64 @@ pkgconf_fragment_filter(const pkgconf_client_t *client, pkgconf_list_t *dest, pk
}
}
size_t
pkgconf_fragment_render_len(const pkgconf_list_t *list)
{
size_t out = 0;
pkgconf_node_t *node;
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
{
const pkgconf_fragment_t *frag = node->data;
out += 2;
if (frag->type)
out += 1;
if (frag->data != NULL)
out += strlen(frag->data);
}
return out;
}
void
pkgconf_fragment_render_buf(const pkgconf_list_t *list, char *buf, size_t buflen)
{
pkgconf_node_t *node;
char *bptr = buf;
memset(buf, 0, buflen);
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
{
const pkgconf_fragment_t *frag = node->data;
if (frag->type)
{
*bptr++ = '-';
*bptr++ = frag->type;
}
if (frag->data)
bptr += strlcpy(bptr, frag->data, buflen - (bptr - buf));
*bptr++ = ' ';
}
*bptr = '\0';
}
char *
pkgconf_fragment_render(const pkgconf_list_t *list)
{
size_t buflen = pkgconf_fragment_render_len(list);
char *buf = calloc(1, buflen);
pkgconf_fragment_render_buf(list, buf, buflen);
return buf;
}
void
pkgconf_fragment_delete(pkgconf_list_t *list, pkgconf_fragment_t *node)
{

View File

@ -234,6 +234,9 @@ void pkgconf_fragment_copy(pkgconf_list_t *list, const pkgconf_fragment_t *base,
void pkgconf_fragment_delete(pkgconf_list_t *list, pkgconf_fragment_t *node);
void pkgconf_fragment_free(pkgconf_list_t *list);
void pkgconf_fragment_filter(const pkgconf_client_t *client, pkgconf_list_t *dest, pkgconf_list_t *src, pkgconf_fragment_filter_func_t filter_func, unsigned int flags);
size_t pkgconf_fragment_render_len(const pkgconf_list_t *list);
void pkgconf_fragment_render_buf(const pkgconf_list_t *list, char *buf, size_t len);
char *pkgconf_fragment_render(const pkgconf_list_t *list);
/* fileio.c */
char *pkgconf_fgetline(char *line, size_t size, FILE *stream);

28
main.c
View File

@ -98,24 +98,6 @@ fragment_has_system_dir(const pkgconf_fragment_t *frag)
return pkgconf_path_match_list(frag->data, check_paths);
}
static void
print_fragment(pkgconf_fragment_t *frag)
{
if (frag->type)
printf("-%c%s ", frag->type, frag->data);
else
printf("%s ", frag->data);
}
static void
print_fragment_list(pkgconf_list_t *list)
{
pkgconf_node_t *node;
PKGCONF_FOREACH_LIST_ENTRY(list->head, node)
print_fragment(node->data);
}
static bool
print_list_entry(const pkgconf_pkg_t *entry, void *data)
{
@ -395,6 +377,7 @@ apply_cflags(pkgconf_client_t *client, pkgconf_pkg_t *world, void *unused, int m
pkgconf_list_t unfiltered_list = PKGCONF_LIST_INITIALIZER;
pkgconf_list_t filtered_list = PKGCONF_LIST_INITIALIZER;
int eflag;
char *render_buf;
(void) unused;
eflag = pkgconf_pkg_cflags(client, world, &unfiltered_list, maxdepth, flags | PKGCONF_PKG_PKGF_SEARCH_PRIVATE);
@ -406,7 +389,9 @@ apply_cflags(pkgconf_client_t *client, pkgconf_pkg_t *world, void *unused, int m
if (filtered_list.head == NULL)
return true;
print_fragment_list(&filtered_list);
render_buf = pkgconf_fragment_render(&filtered_list);
printf("%s", render_buf);
free(render_buf);
pkgconf_fragment_free(&unfiltered_list);
pkgconf_fragment_free(&filtered_list);
@ -420,6 +405,7 @@ apply_libs(pkgconf_client_t *client, pkgconf_pkg_t *world, void *unused, int max
pkgconf_list_t unfiltered_list = PKGCONF_LIST_INITIALIZER;
pkgconf_list_t filtered_list = PKGCONF_LIST_INITIALIZER;
int eflag;
char *render_buf;
(void) unused;
eflag = pkgconf_pkg_libs(client, world, &unfiltered_list, maxdepth, flags);
@ -431,7 +417,9 @@ apply_libs(pkgconf_client_t *client, pkgconf_pkg_t *world, void *unused, int max
if (filtered_list.head == NULL)
return true;
print_fragment_list(&filtered_list);
render_buf = pkgconf_fragment_render(&filtered_list);
printf("%s", render_buf);
free(render_buf);
pkgconf_fragment_free(&unfiltered_list);
pkgconf_fragment_free(&filtered_list);