#include <stdlib.h>
#include <string.h>
#include "alpm_list.h"
#define SYMEXPORT __attribute__((visibility("default")))
#define SYMHIDDEN __attribute__((visibility("internal")))
void SYMEXPORT alpm_list_free(alpm_list_t *list)
{
alpm_list_t *it = list;
while(it) {
alpm_list_t *tmp = it->next;
free(it);
it = tmp;
}
}
void SYMEXPORT alpm_list_free_inner(alpm_list_t *list, alpm_list_fn_free fn)
{
alpm_list_t *it = list;
if(fn) {
while(it) {
if(it->data) {
fn(it->data);
}
it = it->next;
}
}
}
alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
{
alpm_list_append(&list, data);
return list;
}
alpm_list_t SYMEXPORT *alpm_list_append(alpm_list_t **list, void *data)
{
alpm_list_t *ptr;
ptr = malloc(sizeof(alpm_list_t));
if(ptr == NULL) {
return NULL;
}
ptr->data = data;
ptr->next = NULL;
if(*list == NULL) {
*list = ptr;
ptr->prev = ptr;
} else {
alpm_list_t *lp = alpm_list_last(*list);
lp->next = ptr;
ptr->prev = lp;
(*list)->prev = ptr;
}
return ptr;
}
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;
}
}
alpm_list_t SYMEXPORT *alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_list_fn_cmp fn)
{
if(!fn || !list) {
return alpm_list_add(list, data);
} else {
alpm_list_t *add = NULL, *prev = NULL, *next = list;
add = malloc(sizeof(alpm_list_t));
if(add == NULL) {
return list;
}
add->data = data;
while(next) {
if(fn(add->data, next->data) <= 0) break;
prev = next;
next = next->next;
}
if(prev == NULL) {
add->prev = list->prev;
add->next = list;
list->prev = add;
return add;
} else if(next == NULL) {
add->prev = prev;
add->next = NULL;
prev->next = add;
list->prev = add;
return list;
} else {
add->prev = prev;
add->next = next;
next->prev = add;
prev->next = add;
return list;
}
}
}
alpm_list_t SYMEXPORT *alpm_list_join(alpm_list_t *first, alpm_list_t *second)
{
alpm_list_t *tmp;
if(first == NULL) {
return second;
}
if(second == NULL) {
return first;
}
tmp = first->prev;
tmp->next = second;
first->prev = second->prev;
second->prev = tmp;
return first;
}
alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right,
alpm_list_fn_cmp fn)
{
alpm_list_t *newlist, *lp, *tail_ptr, *left_tail_ptr, *right_tail_ptr;
if(left == NULL) {
return right;
}
if(right == NULL) {
return left;
}
left_tail_ptr = left->prev;
right_tail_ptr = right->prev;
if(fn(left->data, right->data) <= 0) {
newlist = left;
left = left->next;
|