diff --git a/libpkgconf/fragment.c b/libpkgconf/fragment.c index 0c2533d..6b7e6d4 100644 --- a/libpkgconf/fragment.c +++ b/libpkgconf/fragment.c @@ -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) { diff --git a/libpkgconf/libpkgconf.h b/libpkgconf/libpkgconf.h index beaa77a..e29eb12 100644 --- a/libpkgconf/libpkgconf.h +++ b/libpkgconf/libpkgconf.h @@ -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); diff --git a/main.c b/main.c index 0192c45..d4f7ff3 100644 --- a/main.c +++ b/main.c @@ -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);