Generalise concat_alpm_depends for any list
Replace concat_alpm_depends() with concat_list() which takes an additional parameter to handle the formatting of non-string data types. Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
parent
366b527757
commit
e58d799c47
1 changed files with 23 additions and 19 deletions
|
@ -405,26 +405,30 @@ char *strreplace(const char *str, const char *needle, const char *replace)
|
||||||
return newstr;
|
return newstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *concat_alpm_depends(alpm_list_t *lst)
|
typedef char *(*formatfn)(void*);
|
||||||
|
|
||||||
|
static char *concat_list(alpm_list_t *lst, formatfn fn)
|
||||||
{
|
{
|
||||||
char *depends = NULL;
|
char *output = NULL, *tmp = NULL;
|
||||||
char *tmp = NULL;
|
asprintf(&output, "%s", "");
|
||||||
|
|
||||||
for(alpm_list_t *i = lst; i; i = alpm_list_next(i)) {
|
for(alpm_list_t *i = lst; i; i = alpm_list_next(i)) {
|
||||||
alpm_depend_t *dep = i->data;
|
char *str = fn ? fn(i->data) : i->data;
|
||||||
char *depstring = alpm_dep_compute_string(dep);
|
|
||||||
if(tmp) {
|
if(str == NULL) {
|
||||||
asprintf(&depends, "%s %s", tmp, depstring);
|
continue;
|
||||||
free(tmp);
|
}
|
||||||
} else {
|
|
||||||
asprintf(&depends, "%s", depstring);
|
tmp = output;
|
||||||
|
asprintf(&output, "%s %s", tmp, str);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
|
if(fn) {
|
||||||
|
free(str);
|
||||||
}
|
}
|
||||||
tmp = depends;
|
|
||||||
free(depstring);
|
|
||||||
}
|
}
|
||||||
if(!depends) {
|
|
||||||
asprintf(&depends, "%s", "");
|
return output;
|
||||||
}
|
|
||||||
return depends;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t string_length(const char *s)
|
static size_t string_length(const char *s)
|
||||||
|
@ -1244,7 +1248,7 @@ void print_packages(const alpm_list_t *packages)
|
||||||
/* %C : checkdepends */
|
/* %C : checkdepends */
|
||||||
if(strstr(temp, "%C")) {
|
if(strstr(temp, "%C")) {
|
||||||
alpm_list_t *lst = alpm_pkg_get_checkdepends(pkg);
|
alpm_list_t *lst = alpm_pkg_get_checkdepends(pkg);
|
||||||
char *depends = concat_alpm_depends(lst);
|
char *depends = concat_list(lst, (formatfn)alpm_dep_compute_string);
|
||||||
string = strreplace(temp, "%C", lst ? depends : "");
|
string = strreplace(temp, "%C", lst ? depends : "");
|
||||||
free(depends);
|
free(depends);
|
||||||
free(temp);
|
free(temp);
|
||||||
|
@ -1253,7 +1257,7 @@ void print_packages(const alpm_list_t *packages)
|
||||||
/* %D : depends */
|
/* %D : depends */
|
||||||
if(strstr(temp, "%D")) {
|
if(strstr(temp, "%D")) {
|
||||||
alpm_list_t *lst = alpm_pkg_get_depends(pkg);
|
alpm_list_t *lst = alpm_pkg_get_depends(pkg);
|
||||||
char *depends = concat_alpm_depends(lst);
|
char *depends = concat_list(lst, (formatfn)alpm_dep_compute_string);
|
||||||
string = strreplace(temp, "%D", depends);
|
string = strreplace(temp, "%D", depends);
|
||||||
free(depends);
|
free(depends);
|
||||||
free(temp);
|
free(temp);
|
||||||
|
@ -1262,7 +1266,7 @@ void print_packages(const alpm_list_t *packages)
|
||||||
/* %M : makedepends */
|
/* %M : makedepends */
|
||||||
if(strstr(temp, "%M")) {
|
if(strstr(temp, "%M")) {
|
||||||
alpm_list_t *lst = alpm_pkg_get_makedepends(pkg);
|
alpm_list_t *lst = alpm_pkg_get_makedepends(pkg);
|
||||||
char *depends = concat_alpm_depends(lst);
|
char *depends = concat_list(lst, (formatfn)alpm_dep_compute_string);
|
||||||
string = strreplace(temp, "%M", depends);
|
string = strreplace(temp, "%M", depends);
|
||||||
free(depends);
|
free(depends);
|
||||||
free(temp);
|
free(temp);
|
||||||
|
|
Loading…
Add table
Reference in a new issue