]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/dlist.c
Implement Qmsg() + conio linking
[bacula/bacula] / bacula / src / lib / dlist.c
1 /*
2  *  Bacula doubly linked list routines.          
3  *
4  *    dlist is a doubly linked list with the links being in the
5  *       list data item.
6  *                
7  *   Kern Sibbald, July MMIII
8  *
9  *   Version $Id$
10  *
11  */
12 /*
13    Copyright (C) 2000-2004 Kern Sibbald and John Walker
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33
34 /* ===================================================================
35  *    dlist
36  */
37 /*
38  * Append an item to the list
39  */
40 void dlist::append(void *item) 
41 {
42    ((dlink *)(((char *)item)+loffset))->next = NULL;
43    ((dlink *)(((char *)item)+loffset))->prev = tail;
44    if (tail) {
45       ((dlink *)(((char *)tail)+loffset))->next = item;
46    }
47    tail = item;
48    if (head == NULL) {                /* if empty list, */
49       head = item;                    /* item is head as well */
50    }
51    num_items++;
52 }
53
54 /*
55  * Append an item to the list
56  */
57 void dlist::prepend(void *item) 
58 {
59    ((dlink *)(((char *)item)+loffset))->next = head;
60    ((dlink *)(((char *)item)+loffset))->prev = NULL;
61    if (head) {
62       ((dlink *)(((char *)head)+loffset))->prev = item;
63    }
64    head = item;
65    if (tail == NULL) {                /* if empty list, */                    
66       tail = item;                    /* item is tail too */
67    }
68    num_items++;
69 }
70
71 void dlist::insert_before(void *item, void *where)       
72 {
73    dlink *where_link = (dlink *)((char *)where+loffset);     
74
75    ((dlink *)(((char *)item)+loffset))->next = where;
76    ((dlink *)(((char *)item)+loffset))->prev = where_link->prev;
77
78    if (where_link->prev) {
79       ((dlink *)(((char *)(where_link->prev))+loffset))->next = item;
80    }
81    where_link->prev = item;
82    if (head == where) {
83       head = item;
84    }
85    num_items++;
86 }
87
88 void dlist::insert_after(void *item, void *where)       
89 {
90    dlink *where_link = (dlink *)((char *)where+loffset);     
91
92    ((dlink *)(((char *)item)+loffset))->next = where_link->next;
93    ((dlink *)(((char *)item)+loffset))->prev = where;
94
95    if (where_link->next) {
96       ((dlink *)(((char *)(where_link->next))+loffset))->prev = item;
97    }
98    where_link->next = item;
99    if (tail == where) {
100       tail = item;
101    }
102    num_items++;
103 }
104
105
106 void dlist::remove(void *item)
107 {
108    void *xitem;
109    dlink *ilink = (dlink *)(((char *)item)+loffset);   /* item's link */
110    if (item == head) {
111       head = ilink->next;
112       if (head) {
113          ((dlink *)(((char *)head)+loffset))->prev = NULL;
114       }
115       if (item == tail) {
116          tail = ilink->prev;
117       }
118    } else if (item == tail) {
119       tail = ilink->prev;
120       if (tail) {
121          ((dlink *)(((char *)tail)+loffset))->next = NULL;
122       }
123    } else {
124       xitem = ilink->next;
125       ((dlink *)(((char *)xitem)+loffset))->prev = ilink->prev;
126       xitem = ilink->prev;
127       ((dlink *)(((char *)xitem)+loffset))->next = ilink->next;
128    }
129    num_items--;
130 }
131
132 void * dlist::next(void *item)
133 {
134    if (item == NULL) {
135       return head;
136    }
137    return ((dlink *)(((char *)item)+loffset))->next;
138 }
139
140 void * dlist::prev(void *item)
141 {
142    if (item == NULL) {
143       return tail;
144    }
145    return ((dlink *)(((char *)item)+loffset))->prev;
146 }
147
148
149 /* Destroy the list contents */
150 void dlist::destroy()
151 {
152    for (void *n=head; n; ) {
153       void *ni = ((dlink *)(((char *)n)+loffset))->next;
154       free(n);
155       n = ni;
156    }
157    num_items = 0;
158    head = tail = NULL;
159 }
160
161
162
163 #ifdef TEST_PROGRAM
164
165 struct MYJCR {
166    char *buf;
167    dlink link;
168 };
169
170 int main()
171 {
172    char buf[30];
173    dlist *jcr_chain;
174    MYJCR *jcr = NULL;
175    MYJCR *save_jcr = NULL;
176    MYJCR *next_jcr;
177
178    jcr_chain = (dlist *)malloc(sizeof(dlist));
179    jcr_chain->init(jcr, &jcr->link);
180     
181    printf("Prepend 20 items 0-19\n");
182    for (int i=0; i<20; i++) {
183       sprintf(buf, "This is dlist item %d", i);
184       jcr = (MYJCR *)malloc(sizeof(MYJCR));
185       jcr->buf = bstrdup(buf);
186       jcr_chain->prepend(jcr);
187       if (i == 10) {
188          save_jcr = jcr;
189       }
190    }
191
192    next_jcr = (MYJCR *)jcr_chain->next(save_jcr);
193    printf("11th item=%s\n", next_jcr->buf);
194    jcr = (MYJCR *)malloc(sizeof(MYJCR));
195    jcr->buf = save_jcr->buf;
196    printf("Remove 10th item\n");
197    jcr_chain->remove(save_jcr);
198    printf("Re-insert 10th item\n");
199    jcr_chain->insert_before(jcr, next_jcr);
200    
201    printf("Print remaining list.\n");
202    foreach_dlist (jcr, jcr_chain) {
203       printf("Dlist item = %s\n", jcr->buf);
204       free(jcr->buf);
205    }
206    jcr_chain->destroy();
207    free(jcr_chain);
208
209    jcr_chain = new dlist(jcr, &jcr->link);
210    printf("append 20 items 0-19\n");
211    for (int i=0; i<20; i++) {
212       sprintf(buf, "This is dlist item %d", i);
213       jcr = (MYJCR *)malloc(sizeof(MYJCR));
214       jcr->buf = bstrdup(buf);
215       jcr_chain->append(jcr);
216       if (i == 10) {
217          save_jcr = jcr;
218       }
219    }
220
221    next_jcr = (MYJCR *)jcr_chain->next(save_jcr);
222    printf("11th item=%s\n", next_jcr->buf);
223    jcr = (MYJCR *)malloc(sizeof(MYJCR));
224    jcr->buf = save_jcr->buf;
225    printf("Remove 10th item\n");
226    jcr_chain->remove(save_jcr);
227    printf("Re-insert 10th item\n");
228    jcr_chain->insert_before(jcr, next_jcr);
229    
230    printf("Print remaining list.\n");
231    foreach_dlist (jcr, jcr_chain) {
232       printf("Dlist item = %s\n", jcr->buf);
233       free(jcr->buf);
234    }
235
236    delete jcr_chain;
237
238    sm_dump(False);
239
240 }
241 #endif