#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;
while(it) {
if(fn && it->data) {
fn(it->data);
}
it = it->next;
}
}
alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
{
alpm_list_t *ptr, *lp;
ptr = calloc(1, sizeof(alpm_list_t));
if(ptr == NULL) {
return list;
}
ptr->data = data;
ptr->next = NULL;
if(list == NULL) {
ptr->prev = ptr;
return ptr;
}
lp = alpm_list_last(list);
lp->next = ptr;
ptr->prev = lp;
list->prev = ptr;
return list;
}
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 = calloc(1, 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;
|