Make general list copy function

Package dup needs to copy all members. Nathan had his implementation, but
I generalized it to this new alpm_list function (and will use it in the
next commit).

CC: Nathan Jones <nathanj@insightbb.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2007-10-29 21:03:41 -05:00
parent 014306eb99
commit 74aa54a1f6
2 changed files with 25 additions and 2 deletions

View File

@ -361,8 +361,6 @@ alpm_list_t SYMEXPORT *alpm_list_remove_dupes(const alpm_list_t *list)
/**
* @brief Copy a string list, including data.
*
* This is gross, assumes string data members.
*
* @param list the list to copy
*
* @return a copy of the original list
@ -396,6 +394,30 @@ alpm_list_t SYMEXPORT *alpm_list_copy(const alpm_list_t *list)
return(newlist);
}
/**
* @brief Copy a list and copy the data.
*
* The data must be constant size!
*
* @param list the list to copy
*
* @return a copy of the original list, data copied as well
*/
alpm_list_t SYMEXPORT *alpm_list_copy_data(const alpm_list_t *list)
{
const alpm_list_t *lp = list;
alpm_list_t *newlist = NULL;
while(lp) {
void *newdata = calloc(1, sizeof(lp->data));
if(newdata) {
memcpy(newdata, lp->data, sizeof(lp->data));
newlist = alpm_list_add(newlist, newdata);
lp = lp->next;
}
}
return(newlist);
}
/**
* @brief Create a new list in reverse order.
*

View File

@ -61,6 +61,7 @@ alpm_list_t *alpm_list_remove_node(alpm_list_t *node);
alpm_list_t *alpm_list_remove_dupes(const alpm_list_t *list);
alpm_list_t *alpm_list_strdup(const alpm_list_t *list);
alpm_list_t *alpm_list_copy(const alpm_list_t *list);
alpm_list_t *alpm_list_copy_data(const alpm_list_t *list);
alpm_list_t *alpm_list_reverse(alpm_list_t *list);
/* item accessors */