add alpm_list_append_strdup

Makes error detection and handling easier for a common operation.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Andrew Gregory 2017-02-18 17:09:49 -05:00 committed by Allan McRae
parent a202959a19
commit a138db3c07
2 changed files with 21 additions and 0 deletions

View file

@ -131,6 +131,26 @@ alpm_list_t SYMEXPORT *alpm_list_append(alpm_list_t **list, void *data)
return ptr;
}
/**
* @brief Duplicate and append a string to a list.
*
* @param list the list to append to
* @param data the string to duplicate and append
*
* @return the newly added item
*/
alpm_list_t SYMEXPORT *alpm_list_append_strdup(alpm_list_t **list, const char *data)
{
alpm_list_t *ret;
char *dup;
if((dup = strdup(data)) && (ret = alpm_list_append(list, dup))) {
return ret;
} else {
free(dup);
return NULL;
}
}
/**
* @brief Add items to a list in sorted order.
*

View file

@ -58,6 +58,7 @@ void alpm_list_free_inner(alpm_list_t *list, alpm_list_fn_free fn);
/* item mutators */
alpm_list_t *alpm_list_add(alpm_list_t *list, void *data);
alpm_list_t *alpm_list_append(alpm_list_t **list, void *data);
alpm_list_t *alpm_list_append_strdup(alpm_list_t **list, const char *data);
alpm_list_t *alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_list_fn_cmp fn);
alpm_list_t *alpm_list_join(alpm_list_t *first, alpm_list_t *second);
alpm_list_t *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, alpm_list_fn_cmp fn);