File: objlist.c

package info (click to toggle)
ion2 20040729-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,828 kB
  • ctags: 3,466
  • sloc: ansic: 28,432; makefile: 473; sh: 230; perl: 16
file content (111 lines) | stat: -rw-r--r-- 1,996 bytes parent folder | download | duplicates (3)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * ion/ioncore/objlist.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2004. 
 *
 * Ion is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 */

#include "common.h"
#include "objlist.h"


static WObjList *iter_next=NULL;


static void free_node(WObjList **objlist, WObjList *node)
{
    UNLINK_ITEM(*objlist, node, next, prev);
    free(node);
}

    
void watch_handler(WWatch *watch, WObj *obj)
{
    WObjList *node=(WObjList*)watch;
    WObjList **list=node->list;
    
    if(iter_next==node)
        iter_next=node->next;
    
    free_node(list, node);
}



bool objlist_insert(WObjList **objlist, WObj *obj)
{
    WObjList *node;
    
    if(obj==NULL)
        return FALSE;
    
    node=ALLOC(WObjList);
    
    if(node==NULL)
        return FALSE;
        
    init_watch(&(node->watch));
    setup_watch(&(node->watch), obj, watch_handler);
    node->list=objlist;
    
    LINK_ITEM_FIRST(*objlist, node, next, prev);
    
    return TRUE;
}


void objlist_remove(WObjList **objlist, WObj *obj)
{
    WObjList *node=*objlist;
    
    while(node!=NULL){
        if(node->watch.obj==obj){
            reset_watch(&(node->watch));
            free_node(objlist, node);
            return;
        }
        node=node->next;
    }
}


void objlist_clear(WObjList **objlist)
{
    while(*objlist!=NULL){
        reset_watch(&((*objlist)->watch));
        free_node(objlist, *objlist);
    }
}


/* Warning: not thread-safe */


WObj *objlist_init_iter(WObjList *objlist)
{
    if(objlist==NULL){
        iter_next=NULL;
        return NULL;
    }
    
    iter_next=objlist->next;
    return objlist->watch.obj;
}


WObj *objlist_iter()
{
    WObjList *ret;
    
    if(iter_next==NULL)
        return NULL;
    
    ret=iter_next;
    iter_next=iter_next->next;
    
    return ret->watch.obj;
}