summaryrefslogtreecommitdiff
path: root/src/idlist.c
blob: 5b4d99b67e1323d1ad29f8edfe92f6d0a4e1aa2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdlib.h>
#include "idlist.h"

struct idlist *list_new()
{
    struct idlist *list = calloc(1, sizeof(struct idlist));
    list->id = 1;
    return list;
}

struct idlist *list_append(struct idlist *list)
{
    if (!list)
        return list;
    struct idlist *last = list;
    while (last->next)
        last = last->next;
    last->next = calloc(1, sizeof(struct idlist));
    last->next->id = last->id + 1;
    last->next->prev = last;
    return last->next;
}

struct idlist *list_get(const struct idlist *list, unsigned long id)
{
    const struct idlist *sought = list;
    if (sought) {
        if (sought->id == id)
            return (struct idlist *)sought;
        while ((sought = sought->next))
            if (sought->id == id)
                return (struct idlist *)sought;
    }
    return NULL;
}

void list_rm_node(struct idlist *node)
{
    if (node) {
        if (node->prev)
            node->prev->next = node->next;
        if (node->next)
            node->next->prev = node->prev;
        free(node);
    }
}

void list_foreach(struct idlist *list, void (*job)(void *))
{
    if (list) {
        job(list->obj);
        while ((list = list->next))
            job(list->obj);
    }
}