2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* list.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2005 by Judd Vinet <jvinet@zeroflux.org>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
/* Check PMList sanity
|
|
|
|
*
|
|
|
|
* 1: List seems to be OK.
|
|
|
|
* 0: We're in deep ...
|
|
|
|
*/
|
|
|
|
int _alpm_list_check(PMList* list)
|
|
|
|
{
|
|
|
|
PMList* it = NULL;
|
|
|
|
|
|
|
|
if(list == NULL) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
if(list->last == NULL) {
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(it = list; it && it->next; it = it->next);
|
|
|
|
if(it != list->last) {
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
PMList* pm_list_new()
|
|
|
|
{
|
|
|
|
PMList *list = NULL;
|
|
|
|
|
|
|
|
list = (PMList *)malloc(sizeof(PMList));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pm_list_free(PMList *list)
|
|
|
|
{
|
2005-03-26 15:56:08 -05:00
|
|
|
PMList *ptr, *it = list;
|
|
|
|
|
|
|
|
while(it) {
|
|
|
|
ptr = it->next;
|
|
|
|
free(it->data);
|
|
|
|
free(it);
|
|
|
|
it = ptr;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PMList* pm_list_add(PMList *list, void *data)
|
|
|
|
{
|
|
|
|
PMList *ptr, *lp;
|
|
|
|
|
|
|
|
ptr = list;
|
|
|
|
if(ptr == NULL) {
|
|
|
|
ptr = pm_list_new();
|
2005-03-26 04:00:49 -05:00
|
|
|
if(ptr == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
lp = pm_list_last(ptr);
|
|
|
|
if(lp == ptr && lp->data == NULL) {
|
|
|
|
/* nada */
|
|
|
|
} else {
|
|
|
|
lp->next = pm_list_new();
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
PMList* pm_list_add_sorted(PMList *list, void *data, pm_fn_cmp fn)
|
|
|
|
{
|
|
|
|
PMList *add;
|
|
|
|
PMList *prev = NULL;
|
|
|
|
PMList *iter = list;
|
|
|
|
|
|
|
|
add = pm_list_new();
|
|
|
|
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-03-25 17:09:14 -05:00
|
|
|
/* list: the beginning of the list
|
|
|
|
* item: the item in the list to be removed
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* list with item removed
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
2005-03-25 17:09:14 -05:00
|
|
|
|
2005-03-26 08:29:47 -05:00
|
|
|
PMList* _alpm_list_remove(PMList* list, PMList* item)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2005-03-25 17:09:14 -05:00
|
|
|
assert(_alpm_list_check(list));
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
if(list == NULL || item == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove first item in list. */
|
|
|
|
if(item == list) {
|
|
|
|
if(list->next == NULL) { /* Only item in list. */
|
|
|
|
pm_list_free(item);
|
|
|
|
return(NULL);
|
|
|
|
} else {
|
|
|
|
list->next->prev = NULL;
|
|
|
|
list->next->last = list->last;
|
|
|
|
list = list->next;
|
|
|
|
item->prev = item->next = NULL;
|
|
|
|
pm_list_free(item);
|
|
|
|
return(list);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
/* Remove last item in list. */
|
|
|
|
if(list->last == item) {
|
|
|
|
list->last = item->prev;
|
|
|
|
item->prev->next = NULL;
|
|
|
|
item->prev = item->next = NULL;
|
|
|
|
pm_list_free(item);
|
|
|
|
return(list);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
/* Remove middle item in list. */
|
|
|
|
assert(item->prev != NULL && item->next != NULL);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
item->prev->next = item->next;
|
|
|
|
item->next->prev = item->prev;
|
|
|
|
item->prev = item->next = NULL;
|
|
|
|
pm_list_free(item);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
assert(_alpm_list_check(list));
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
return(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pm_list_count(PMList *list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PMList *lp;
|
|
|
|
|
|
|
|
for(lp = list, i = 0; lp; lp = lp->next, i++);
|
|
|
|
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pm_list_is_ptrin(PMList *haystack, void *needle)
|
|
|
|
{
|
|
|
|
PMList *lp;
|
|
|
|
|
|
|
|
for(lp = haystack; lp; lp = lp->next) {
|
|
|
|
if(lp->data == needle) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for existence of a string in a PMList
|
|
|
|
*/
|
|
|
|
PMList *pm_list_is_strin(char *needle, PMList *haystack)
|
|
|
|
{
|
|
|
|
PMList *lp;
|
|
|
|
|
|
|
|
for(lp = haystack; lp; lp = lp->next) {
|
|
|
|
if(lp->data && !strcmp(lp->data, needle)) {
|
|
|
|
return(lp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
PMList* pm_list_last(PMList *list)
|
|
|
|
{
|
2005-03-25 17:09:14 -05:00
|
|
|
if (list == NULL)
|
|
|
|
return(NULL);
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-03-25 17:09:14 -05:00
|
|
|
assert(list->last != NULL);
|
|
|
|
|
|
|
|
return(list->last);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reverse the order of a list
|
|
|
|
*
|
|
|
|
* The caller is responsible for freeing the old list
|
|
|
|
*/
|
|
|
|
PMList* _alpm_list_reverse(PMList *list)
|
|
|
|
{
|
|
|
|
/* simple but functional -- we just build a new list, starting
|
|
|
|
* with the old list's tail
|
|
|
|
*/
|
|
|
|
PMList *newlist = NULL;
|
|
|
|
PMList *lp;
|
|
|
|
|
|
|
|
for(lp = list->last; lp; lp = lp->prev) {
|
|
|
|
newlist = pm_list_add(newlist, lp->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(newlist);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|