2 * Bacula doubly linked list routines.
4 * dlist is a doubly linked list with the links being in the
7 * Kern Sibbald, July MMIII
13 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
34 /* ===================================================================
38 * Append an item to the list
40 void dlist::append(void *item)
42 ((dlink *)((char *)item+loffset))->next = NULL;
43 ((dlink *)((char *)item+loffset))->prev = tail;
45 ((dlink *)((char *)tail+loffset))->next = item;
48 if (head == NULL) { /* if empty list, */
49 head = item; /* item is head as well */
54 * Append an item to the list
56 void dlist::prepend(void *item)
58 ((dlink *)((char *)item+loffset))->next = head;
59 ((dlink *)((char *)item+loffset))->prev = NULL;
61 ((dlink *)((char *)head+loffset))->prev = item;
64 if (tail == NULL) { /* if empty list, */
65 tail = item; /* item is tail too */
69 void dlist::insert_before(void *item, void *where)
71 dlink *where_link = (dlink *)((char *)where+loffset);
73 ((dlink *)((char *)item+loffset))->next = where;
74 ((dlink *)((char *)item+loffset))->prev = where_link->prev;
76 if (where_link->prev) {
77 ((dlink *)((char *)(where_link->prev)+loffset))->next = item;
78 where_link->prev = item;
85 void dlist::insert_after(void *item, void *where)
87 dlink *where_link = (dlink *)((char *)where+loffset);
89 ((dlink *)((char *)item+loffset))->next = where_link->next;
90 ((dlink *)((char *)item+loffset))->prev = where;
92 if (where_link->next) {
93 ((dlink *)((char *)(where_link->next)+loffset))->prev = item;
94 where_link->next = item;
102 void dlist::remove(void *item)
105 dlink *ilink = (dlink *)((char *)item+loffset); /* item's link */
109 ((dlink *)((char *)head+loffset))->prev = NULL;
114 } else if (item == tail) {
117 ((dlink *)((char *)tail+loffset))->next = NULL;
121 ((dlink *)((char *)xitem+loffset))->prev = ilink->prev;
123 ((dlink *)((char *)xitem+loffset))->next = ilink->next;
127 void * dlist::next(void *item)
132 return ((dlink *)((char *)item+loffset))->next;
135 void * dlist::prev(void *item)
140 return ((dlink *)((char *)item+loffset))->prev;
144 /* Destroy the list and its contents */
145 void dlist::destroy()
147 for (void *n=head; n; ) {
148 void *ni = ((dlink *)((char *)n+loffset))->next;
168 MYJCR *save_jcr = NULL;
171 jcr_chain = (dlist *)malloc(sizeof(dlist));
172 jcr_chain->init(jcr, &jcr->link);
174 printf("Prepend 20 items 0-19\n");
175 for (int i=0; i<20; i++) {
176 sprintf(buf, "This is dlist item %d", i);
177 jcr = (MYJCR *)malloc(sizeof(MYJCR));
178 jcr->buf = bstrdup(buf);
179 jcr_chain->prepend(jcr);
185 next_jcr = (MYJCR *)jcr_chain->next(save_jcr);
186 printf("11th item=%s\n", next_jcr->buf);
187 jcr = (MYJCR *)malloc(sizeof(MYJCR));
188 jcr->buf = save_jcr->buf;
189 printf("Remove 10th item\n");
190 jcr_chain->remove(save_jcr);
191 printf("Re-insert 10th item\n");
192 jcr_chain->insert_before(jcr, next_jcr);
194 printf("Print remaining list.\n");
195 for (MYJCR *jcr=NULL; (jcr=(MYJCR *)jcr_chain->next(jcr)); ) {
196 printf("Dlist item = %s\n", jcr->buf);
199 jcr_chain->destroy();
202 jcr_chain = new dlist(jcr, &jcr->link);
203 printf("append 20 items 0-19\n");
204 for (int i=0; i<20; i++) {
205 sprintf(buf, "This is dlist item %d", i);
206 jcr = (MYJCR *)malloc(sizeof(MYJCR));
207 jcr->buf = bstrdup(buf);
208 jcr_chain->append(jcr);
214 next_jcr = (MYJCR *)jcr_chain->next(save_jcr);
215 printf("11th item=%s\n", next_jcr->buf);
216 jcr = (MYJCR *)malloc(sizeof(MYJCR));
217 jcr->buf = save_jcr->buf;
218 printf("Remove 10th item\n");
219 jcr_chain->remove(save_jcr);
220 printf("Re-insert 10th item\n");
221 jcr_chain->insert_before(jcr, next_jcr);
223 printf("Print remaining list.\n");
224 for (MYJCR *jcr=NULL; (jcr=(MYJCR *)jcr_chain->next(jcr)); ) {
225 printf("Dlist item = %s\n", jcr->buf);