src/server/ucx/list.c

Sun, 26 Feb 2012 19:51:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 26 Feb 2012 19:51:14 +0100
changeset 26
37ff8bf54b89
parent 23
a2c8fc23c90e
child 36
450d2d5f4735
permissions
-rw-r--r--

Minimal webdav support

#include "list.h"

UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
    UcxList *ret = NULL;
    while (l != NULL) {
        if (fnc != NULL) {
            ret = ucx_list_append(ret, fnc(l->data, data));
        } else {
            ret = ucx_list_append(ret, l->data);
        }
        l = l->next;
    }
    return ret;
}

int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void* data) {
    if (l1 == l2) return 1;
    
    while (l1 != NULL && l2 != NULL) {
        if (fnc == NULL) {
            if (l1->data != l2->data) return 0;
        } else {
            if (fnc(l1->data, l2->data, data) != 0) return 0;
        }
        l1 = l1->next;
        l2 = l2->next;
    }

    return (l1 == NULL && l2 == NULL);
}

void ucx_list_free(UcxList *l) {
    UcxList *e = l, *f;
    while (e != NULL) {
        f = e;
        e = e->next;
        free(f);
    }
}

UcxList *ucx_list_append(UcxList *l, void *data)  {
    UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
    if (nl == NULL) return NULL;

    nl->data = data;
    nl->next = NULL;
    if (l == NULL) {
        return nl;
    } else {
        UcxList *t = ucx_list_last(l);
        t->next = nl;
        return l;
    }
}

UcxList *ucx_list_prepend(UcxList *l, void *data) {
    UcxList *nl = ucx_list_append(NULL, data);
    if (nl == NULL) return NULL;

    if (l != NULL) {
        nl->next = l;
    }
    return nl;
}

UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
    if (l1 == NULL) {
        return l2;
    } else {
        UcxList *last = ucx_list_last(l1);
        last->next = l2;
        return l1;
    }
}

UcxList *ucx_list_last(UcxList *l) {
    if (l == NULL) return NULL;

    UcxList *e = l;
    while (e->next != NULL) {
        e = e->next;
    }
    return e;
}

UcxList *ucx_list_get(UcxList *l, int index) {
    if (l == NULL) return NULL;

    UcxList *e = l;
    while (e->next != NULL && index > 0) {
        e = e->next;
        index--;
    }

    return index == 0 ? e : NULL;
}

size_t ucx_list_size(UcxList *l) {
    if (l == NULL) return 0;

    UcxList *e = l;
    size_t s = 1;
    while (e->next != NULL) {
        e = e->next;
        s++;
    }

    return s;
}

void ucx_list_foreach(UcxList *l, ucx_callback fnc, void* data) {
    UcxList *e = l;
    UcxList *n;
    while (e != NULL) {
        n = e->next;
        fnc(e, data);
        e = n;
    }
}

/* list specific functions */
UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
    if (e == l) {
        l = e->next;
        free(e);
    } else {
        UcxList *f = l;
        while (f->next != NULL && f->next != e) {
            f = f->next;
        }
        /* perform remove iff this element is found in this list */
        if (f->next == e) {
            f->next = e->next;
            free(e);
        }
    }
    return l;
}

mercurial