summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/alpm_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/alpm_list.c')
-rw-r--r--lib/libalpm/alpm_list.c175
1 files changed, 104 insertions, 71 deletions
diff --git a/lib/libalpm/alpm_list.c b/lib/libalpm/alpm_list.c
index 42ef367a..83b9824c 100644
--- a/lib/libalpm/alpm_list.c
+++ b/lib/libalpm/alpm_list.c
@@ -20,7 +20,6 @@
#include <stdlib.h>
#include <string.h>
-#include <stdio.h>
/* libalpm */
#include "alpm_list.h"
@@ -92,7 +91,7 @@ alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
ptr = calloc(1, sizeof(alpm_list_t));
if(ptr == NULL) {
- return(list);
+ return list;
}
ptr->data = data;
@@ -101,7 +100,7 @@ alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
/* Special case: the input list is empty */
if(list == NULL) {
ptr->prev = ptr;
- return(ptr);
+ return ptr;
}
lp = alpm_list_last(list);
@@ -109,7 +108,7 @@ alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
ptr->prev = lp;
list->prev = ptr;
- return(list);
+ return list;
}
/**
@@ -124,13 +123,13 @@ alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
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));
+ 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);
+ return list;
}
add->data = data;
@@ -146,19 +145,19 @@ alpm_list_t SYMEXPORT *alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_
add->prev = list->prev; /* list != NULL */
add->next = list;
list->prev = add;
- return(add);
+ return add;
} else if(next == NULL) { /* another special case: add last element */
add->prev = prev;
add->next = NULL;
prev->next = add;
list->prev = add;
- return(list);
+ return list;
} else {
add->prev = prev;
add->next = next;
next->prev = add;
prev->next = add;
- return(list);
+ return list;
}
}
}
@@ -178,11 +177,11 @@ 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(first == NULL) {
+ return second;
}
- if (second == NULL) {
- return(first);
+ if(second == NULL) {
+ return first;
}
/* tmp is the last element of the first list */
tmp = first->prev;
@@ -193,7 +192,7 @@ alpm_list_t SYMEXPORT *alpm_list_join(alpm_list_t *first, alpm_list_t *second)
/* set the back reference to the tail */
second->prev = tmp;
- return(first);
+ return first;
}
/**
@@ -209,12 +208,12 @@ alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, a
{
alpm_list_t *newlist, *lp;
- if (left == NULL)
+ if(left == NULL)
return right;
- if (right == NULL)
+ if(right == NULL)
return left;
- if (fn(left->data, right->data) <= 0) {
+ if(fn(left->data, right->data) <= 0) {
newlist = left;
left = left->next;
}
@@ -226,8 +225,8 @@ alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, a
newlist->next = NULL;
lp = newlist;
- while ((left != NULL) && (right != NULL)) {
- if (fn(left->data, right->data) <= 0) {
+ while((left != NULL) && (right != NULL)) {
+ if(fn(left->data, right->data) <= 0) {
lp->next = left;
left->prev = lp;
left = left->next;
@@ -240,11 +239,11 @@ alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, a
lp = lp->next;
lp->next = NULL;
}
- if (left != NULL) {
+ if(left != NULL) {
lp->next = left;
left->prev = lp;
}
- else if (right != NULL) {
+ else if(right != NULL) {
lp->next = right;
right->prev = lp;
}
@@ -257,7 +256,7 @@ alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, a
}
newlist->prev = lp;
- return(newlist);
+ return newlist;
}
/**
@@ -271,7 +270,7 @@ alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, a
*/
alpm_list_t SYMEXPORT *alpm_list_msort(alpm_list_t *list, size_t n, alpm_list_fn_cmp fn)
{
- if (n > 1) {
+ if(n > 1) {
alpm_list_t *left = list;
alpm_list_t *lastleft = alpm_list_nth(list, n/2 - 1);
alpm_list_t *right = lastleft->next;
@@ -282,7 +281,7 @@ alpm_list_t SYMEXPORT *alpm_list_msort(alpm_list_t *list, size_t n, alpm_list_fn
right = alpm_list_msort(right, n - (n/2), fn);
list = alpm_list_mmerge(left, right, fn);
}
- return(list);
+ return list;
}
/**
@@ -298,7 +297,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove_item(alpm_list_t *haystack,
alpm_list_t *item)
{
if(haystack == NULL || item == NULL) {
- return(haystack);
+ return haystack;
}
if(item == haystack) {
@@ -328,7 +327,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove_item(alpm_list_t *haystack,
}
}
- return(haystack);
+ return haystack;
}
@@ -352,7 +351,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove(alpm_list_t *haystack,
}
if(needle == NULL) {
- return(haystack);
+ return haystack;
}
while(i) {
@@ -373,7 +372,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove(alpm_list_t *haystack,
}
}
- return(haystack);
+ return haystack;
}
/**
@@ -388,8 +387,8 @@ alpm_list_t SYMEXPORT *alpm_list_remove(alpm_list_t *haystack,
alpm_list_t SYMEXPORT *alpm_list_remove_str(alpm_list_t *haystack,
const char *needle, char **data)
{
- return(alpm_list_remove(haystack, (const void *)needle,
- (alpm_list_fn_cmp)strcmp, (void **)data));
+ return alpm_list_remove(haystack, (const void *)needle,
+ (alpm_list_fn_cmp)strcmp, (void **)data);
}
/**
@@ -411,7 +410,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove_dupes(const alpm_list_t *list)
}
lp = lp->next;
}
- return(newlist);
+ return newlist;
}
/**
@@ -429,7 +428,7 @@ alpm_list_t SYMEXPORT *alpm_list_strdup(const alpm_list_t *list)
newlist = alpm_list_add(newlist, strdup(lp->data));
lp = lp->next;
}
- return(newlist);
+ return newlist;
}
/**
@@ -447,7 +446,7 @@ alpm_list_t SYMEXPORT *alpm_list_copy(const alpm_list_t *list)
newlist = alpm_list_add(newlist, lp->data);
lp = lp->next;
}
- return(newlist);
+ return newlist;
}
/**
@@ -473,7 +472,7 @@ alpm_list_t SYMEXPORT *alpm_list_copy_data(const alpm_list_t *list,
lp = lp->next;
}
}
- return(newlist);
+ return newlist;
}
/**
@@ -489,7 +488,7 @@ alpm_list_t SYMEXPORT *alpm_list_reverse(alpm_list_t *list)
alpm_list_t *newlist = NULL, *backup;
if(list == NULL) {
- return(NULL);
+ return NULL;
}
lp = alpm_list_last(list);
@@ -502,28 +501,12 @@ alpm_list_t SYMEXPORT *alpm_list_reverse(alpm_list_t *list)
lp = lp->prev;
}
list->prev = backup; /* restore tail pointer */
- return(newlist);
+ return newlist;
}
/* Accessors */
/**
- * @brief Get the first element of a list.
- *
- * @param list the list
- *
- * @return the first element in the list
- */
-inline alpm_list_t SYMEXPORT *alpm_list_first(const alpm_list_t *list)
-{
- if(list) {
- return((alpm_list_t*)list);
- } else {
- return(NULL);
- }
-}
-
-/**
* @brief Return nth element from list (starting from 0).
*
* @param list the list
@@ -537,7 +520,7 @@ alpm_list_t SYMEXPORT *alpm_list_nth(const alpm_list_t *list, size_t n)
while(n--) {
i = i->next;
}
- return((alpm_list_t*)i);
+ return (alpm_list_t *)i;
}
/**
@@ -550,9 +533,26 @@ alpm_list_t SYMEXPORT *alpm_list_nth(const alpm_list_t *list, size_t n)
inline alpm_list_t SYMEXPORT *alpm_list_next(const alpm_list_t *node)
{
if(node) {
- return(node->next);
+ return node->next;
} else {
- return(NULL);
+ return NULL;
+ }
+}
+
+/**
+ * @brief Get the previous element of a list.
+ *
+ * @param list the list head
+ * @param node the list node
+ *
+ * @return the previous element, or NULL when no previous element exist
+ */
+inline alpm_list_t SYMEXPORT *alpm_list_previous(const alpm_list_t *list)
+{
+ if(list && list->prev->next) {
+ return list->prev;
+ } else {
+ return NULL;
}
}
@@ -566,9 +566,9 @@ inline alpm_list_t SYMEXPORT *alpm_list_next(const alpm_list_t *node)
alpm_list_t SYMEXPORT *alpm_list_last(const alpm_list_t *list)
{
if(list) {
- return(list->prev);
+ return list->prev;
} else {
- return(NULL);
+ return NULL;
}
}
@@ -581,8 +581,8 @@ alpm_list_t SYMEXPORT *alpm_list_last(const alpm_list_t *list)
*/
void SYMEXPORT *alpm_list_getdata(const alpm_list_t *node)
{
- if(node == NULL) return(NULL);
- return(node->data);
+ if(node == NULL) return NULL;
+ return node->data;
}
/* Misc */
@@ -602,7 +602,7 @@ size_t SYMEXPORT alpm_list_count(const alpm_list_t *list)
++i;
lp = lp->next;
}
- return(i);
+ return i;
}
/**
@@ -620,17 +620,17 @@ void SYMEXPORT *alpm_list_find(const alpm_list_t *haystack, const void *needle,
const alpm_list_t *lp = haystack;
while(lp) {
if(lp->data && fn(lp->data, needle) == 0) {
- return(lp->data);
+ return lp->data;
}
lp = lp->next;
}
- return(NULL);
+ return NULL;
}
/* trivial helper function for alpm_list_find_ptr */
static int ptr_cmp(const void *p, const void *q)
{
- return(p != q);
+ return (p != q);
}
/**
@@ -643,9 +643,10 @@ static int ptr_cmp(const void *p, const void *q)
*
* @return `needle` if found, NULL otherwise
*/
-void SYMEXPORT *alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle)
+void SYMEXPORT *alpm_list_find_ptr(const alpm_list_t *haystack,
+ const void *needle)
{
- return(alpm_list_find(haystack, needle, ptr_cmp));
+ return alpm_list_find(haystack, needle, ptr_cmp);
}
/**
@@ -659,8 +660,8 @@ void SYMEXPORT *alpm_list_find_ptr(const alpm_list_t *haystack, const void *need
char SYMEXPORT *alpm_list_find_str(const alpm_list_t *haystack,
const char *needle)
{
- return((char *)alpm_list_find(haystack, (const void*)needle,
- (alpm_list_fn_cmp)strcmp));
+ return (char *)alpm_list_find(haystack, (const void *)needle,
+ (alpm_list_fn_cmp)strcmp);
}
/**
@@ -688,7 +689,7 @@ void SYMEXPORT alpm_list_diff_sorted(const alpm_list_t *left,
return;
}
- while (l != NULL && r != NULL) {
+ while(l != NULL && r != NULL) {
int cmp = fn(l->data, r->data);
if(cmp < 0) {
if(onlyleft) {
@@ -706,13 +707,13 @@ void SYMEXPORT alpm_list_diff_sorted(const alpm_list_t *left,
r = r->next;
}
}
- while (l != NULL) {
+ while(l != NULL) {
if(onlyleft) {
*onlyleft = alpm_list_add(*onlyleft, l->data);
}
l = l->next;
}
- while (r != NULL) {
+ while(r != NULL) {
if(onlyright) {
*onlyright = alpm_list_add(*onlyright, r->data);
}
@@ -745,7 +746,39 @@ alpm_list_t SYMEXPORT *alpm_list_diff(const alpm_list_t *lhs,
alpm_list_free(left);
alpm_list_free(right);
- return(ret);
+ return ret;
+}
+
+/**
+ * @brief Copy a list and data into a standard C array of fixed length.
+ * Note that the data elements are shallow copied so any contained pointers
+ * will point to the original data.
+ *
+ * @param list the list to copy
+ * @param n the size of the list
+ * @param size the size of each data element
+ *
+ * @return an array version of the original list, data copied as well
+ */
+void SYMEXPORT *alpm_list_to_array(const alpm_list_t *list, size_t n,
+ size_t size)
+{
+ size_t i;
+ const alpm_list_t *item;
+ char *array;
+
+ if(n == 0) {
+ return NULL;
+ }
+
+ array = calloc(n, size);
+ if(array == NULL) {
+ return NULL;
+ }
+ for(i = 0, item = list; i < n && item; i++, item = item->next) {
+ memcpy(array + i * size, item->data, size);
+ }
+ return array;
}
/** @} */