2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* list.c
|
|
|
|
*
|
2006-01-02 14:55:35 -05:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2005-03-14 20:51:43 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2005-03-25 17:09:14 -05:00
|
|
|
#include <assert.h>
|
2005-03-14 20:51:43 -05:00
|
|
|
/* pacman */
|
|
|
|
#include "list.h"
|
2006-10-31 01:39:59 -05:00
|
|
|
#include "util.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_new()
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *list = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
list = (pmlist_t *)malloc(sizeof(pmlist_t));
|
2005-03-14 20:51:43 -05:00
|
|
|
if(list == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
list->data = NULL;
|
|
|
|
list->prev = NULL;
|
|
|
|
list->next = NULL;
|
2005-03-25 17:09:14 -05:00
|
|
|
list->last = list;
|
2005-03-14 20:51:43 -05:00
|
|
|
return(list);
|
|
|
|
}
|
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
void _alpm_list_free(pmlist_t *list, _alpm_fn_free fn)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *ptr, *it = list;
|
2005-03-26 15:56:08 -05:00
|
|
|
|
|
|
|
while(it) {
|
|
|
|
ptr = it->next;
|
2006-10-31 01:39:59 -05:00
|
|
|
if(fn && it->data) {
|
2006-02-22 15:49:33 -05:00
|
|
|
fn(it->data);
|
|
|
|
}
|
2006-10-31 01:39:59 -05:00
|
|
|
FREE(it);
|
2005-03-26 15:56:08 -05:00
|
|
|
it = ptr;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_add(pmlist_t *list, void *data)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *ptr, *lp;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
ptr = list;
|
|
|
|
if(ptr == NULL) {
|
2006-01-05 16:49:42 -05:00
|
|
|
ptr = _alpm_list_new();
|
2005-03-26 04:00:49 -05:00
|
|
|
if(ptr == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2006-01-05 16:49:42 -05:00
|
|
|
lp = _alpm_list_last(ptr);
|
2005-03-14 20:51:43 -05:00
|
|
|
if(lp == ptr && lp->data == NULL) {
|
|
|
|
/* nada */
|
|
|
|
} else {
|
2006-01-05 16:49:42 -05:00
|
|
|
lp->next = _alpm_list_new();
|
2005-03-14 20:51:43 -05:00
|
|
|
if(lp->next == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
lp->next->prev = lp;
|
2005-03-25 17:09:14 -05:00
|
|
|
lp->last = NULL;
|
2005-03-14 20:51:43 -05:00
|
|
|
lp = lp->next;
|
|
|
|
}
|
2005-03-25 17:09:14 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
lp->data = data;
|
2005-03-25 17:09:14 -05:00
|
|
|
ptr->last = lp;
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
return(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add items to a list in sorted order. Use the given comparision func to
|
|
|
|
* determine order.
|
|
|
|
*/
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_add_sorted(pmlist_t *list, void *data, _alpm_fn_cmp fn)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *add;
|
|
|
|
pmlist_t *prev = NULL;
|
|
|
|
pmlist_t *iter = list;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2006-01-05 16:49:42 -05:00
|
|
|
add = _alpm_list_new();
|
2005-03-14 20:51:43 -05:00
|
|
|
add->data = data;
|
|
|
|
|
|
|
|
/* Find insertion point. */
|
|
|
|
while(iter) {
|
|
|
|
if(fn(add->data, iter->data) <= 0) break;
|
|
|
|
prev = iter;
|
|
|
|
iter = iter->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert node before insertion point. */
|
|
|
|
add->prev = prev;
|
|
|
|
add->next = iter;
|
2005-03-25 17:09:14 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
if(iter != NULL) {
|
2005-03-25 17:09:14 -05:00
|
|
|
iter->prev = add; /* Not at end. */
|
|
|
|
} else {
|
|
|
|
if (list != NULL) {
|
|
|
|
list->last = add; /* Added new to end, so update the link to last. */
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2005-03-25 17:09:14 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
if(prev != NULL) {
|
2005-03-25 17:09:14 -05:00
|
|
|
prev->next = add; /* In middle. */
|
2005-03-14 20:51:43 -05:00
|
|
|
} else {
|
2005-03-25 17:09:14 -05:00
|
|
|
if(list == NULL) {
|
|
|
|
add->last = add;
|
|
|
|
} else {
|
|
|
|
add->last = list->last;
|
|
|
|
list->last = NULL;
|
|
|
|
}
|
|
|
|
list = add; /* Start or empty, new list head. */
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return(list);
|
|
|
|
}
|
|
|
|
|
2005-05-03 13:24:20 -04:00
|
|
|
/* Remove an item in a list. Use the given comparaison function to find the
|
|
|
|
* item.
|
|
|
|
* If the item is found, 'data' is pointing to the removed element.
|
|
|
|
* Otherwise, it is set to NULL.
|
|
|
|
* Return the new list (without the removed element).
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_remove(pmlist_t *haystack, void *needle, _alpm_fn_cmp fn, void **data)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *i = haystack;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-05-03 13:24:20 -04:00
|
|
|
if(data) {
|
|
|
|
*data = NULL;
|
2005-03-25 17:09:14 -05:00
|
|
|
}
|
|
|
|
|
2005-05-03 13:24:20 -04:00
|
|
|
while(i) {
|
|
|
|
if(i->data == NULL) {
|
|
|
|
continue;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2005-05-03 13:24:20 -04:00
|
|
|
if(fn(needle, i->data) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = i->next;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2005-05-03 13:24:20 -04:00
|
|
|
if(i) {
|
|
|
|
/* we found a matching item */
|
|
|
|
if(i->next) {
|
|
|
|
i->next->prev = i->prev;
|
|
|
|
}
|
|
|
|
if(i->prev) {
|
|
|
|
i->prev->next = i->next;
|
|
|
|
}
|
|
|
|
if(i == haystack) {
|
|
|
|
/* The item found is the first in the chain */
|
|
|
|
if(haystack->next) {
|
|
|
|
haystack->next->last = haystack->last;
|
|
|
|
}
|
|
|
|
haystack = haystack->next;
|
|
|
|
} else if(i == haystack->last) {
|
|
|
|
/* The item found is the last in the chain */
|
|
|
|
haystack->last = i->prev;
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-05-03 13:24:20 -04:00
|
|
|
if(data) {
|
|
|
|
*data = i->data;
|
|
|
|
}
|
|
|
|
i->data = NULL;
|
2006-10-31 01:39:59 -05:00
|
|
|
FREE(i);
|
2005-05-03 13:24:20 -04:00
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-05-03 13:24:20 -04:00
|
|
|
return(haystack);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
int _alpm_list_count(pmlist_t *list)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
int i;
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *lp;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
for(lp = list, i = 0; lp; lp = lp->next, i++);
|
|
|
|
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
int _alpm_list_is_in(void *needle, pmlist_t *haystack)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *lp;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
for(lp = haystack; lp; lp = lp->next) {
|
|
|
|
if(lp->data == needle) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
/* Test for existence of a string in a pmlist_t
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
2006-10-20 02:26:55 -04:00
|
|
|
int _alpm_list_is_strin(char *needle, pmlist_t *haystack)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *lp;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
for(lp = haystack; lp; lp = lp->next) {
|
|
|
|
if(lp->data && !strcmp(lp->data, needle)) {
|
2006-02-16 16:02:39 -05:00
|
|
|
return(1);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
2006-02-16 16:02:39 -05:00
|
|
|
return(0);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_last(pmlist_t *list)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2005-05-03 13:24:20 -04:00
|
|
|
if(list == NULL) {
|
2005-03-25 17:09:14 -05:00
|
|
|
return(NULL);
|
2005-05-03 13:24:20 -04:00
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
assert(list->last != NULL);
|
|
|
|
|
|
|
|
return(list->last);
|
|
|
|
}
|
|
|
|
|
2005-10-07 19:29:49 -04:00
|
|
|
/* Filter out any duplicate strings in a list.
|
|
|
|
*
|
|
|
|
* Not the most efficient way, but simple to implement -- we assemble
|
|
|
|
* a new list, using is_in() to check for dupes at each iteration.
|
|
|
|
*
|
|
|
|
*/
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_remove_dupes(pmlist_t *list)
|
2005-10-07 19:29:49 -04:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *i, *newlist = NULL;
|
2005-10-07 19:29:49 -04:00
|
|
|
|
|
|
|
for(i = list; i; i = i->next) {
|
2006-02-17 17:35:26 -05:00
|
|
|
if(!_alpm_list_is_strin(i->data, newlist)) {
|
|
|
|
newlist = _alpm_list_add(newlist, strdup(i->data));
|
2005-10-07 19:29:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newlist;
|
|
|
|
}
|
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
/* Reverse the order of a list
|
|
|
|
*
|
|
|
|
* The caller is responsible for freeing the old list
|
|
|
|
*/
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_reverse(pmlist_t *list)
|
2005-03-25 17:09:14 -05:00
|
|
|
{
|
|
|
|
/* simple but functional -- we just build a new list, starting
|
|
|
|
* with the old list's tail
|
|
|
|
*/
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *newlist = NULL;
|
|
|
|
pmlist_t *lp;
|
2005-03-25 17:09:14 -05:00
|
|
|
|
|
|
|
for(lp = list->last; lp; lp = lp->prev) {
|
2006-02-17 17:35:26 -05:00
|
|
|
newlist = _alpm_list_add(newlist, lp->data);
|
2005-03-25 17:09:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return(newlist);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *_alpm_list_strdup(pmlist_t *list)
|
2005-04-20 15:41:22 -04:00
|
|
|
{
|
2006-10-20 02:26:55 -04:00
|
|
|
pmlist_t *newlist = NULL;
|
|
|
|
pmlist_t *lp;
|
2005-04-20 15:41:22 -04:00
|
|
|
|
|
|
|
for(lp = list; lp; lp = lp->next) {
|
2006-02-17 17:35:26 -05:00
|
|
|
newlist = _alpm_list_add(newlist, strdup(lp->data));
|
2005-04-20 15:41:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return(newlist);
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|