]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.c
State file debug + full functionality for alist
[bacula/bacula] / bacula / src / lib / alist.c
1 /*
2  *  Bacula array list routines     
3  *
4  *    alist is a simple malloc'ed array of pointers.  For the moment,
5  *      it simply malloc's a bigger array controlled by num_grow.         
6  *      Default is to realloc the pointer array for each new member.
7  *
8  *   Kern Sibbald, June MMIII
9  *
10  *   Version $Id$
11  *
12  */
13 /*
14    Copyright (C) 2000-2003 Kern Sibbald and John Walker
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License as
18    published by the Free Software Foundation; either version 2 of
19    the License, or (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29    MA 02111-1307, USA.
30
31  */
32
33 #include "bacula.h"
34
35 /*
36  * Private grow list function. Used to insure that 
37  *   at least one more "slot" is available.
38  */
39 void alist::grow_list()
40 {
41    if (num_items == 0) {
42       if (num_grow == 0) {
43          num_grow = 1;                /* default if not initialized */
44       }
45       items = (void **)malloc(num_grow * sizeof(void *));
46       max_items = num_grow;
47    } else if (num_items == max_items) {
48       max_items += num_grow;
49       items = (void **)realloc(items, max_items * sizeof(void *));
50    }
51 }
52
53 void *alist::first()
54 {
55    cur_item = 1;
56    if (num_items == 0) {
57       return NULL;
58    } else {
59       return items[0];
60    }
61 }
62
63 void *alist::last()
64 {
65    if (num_items == 0) {
66       return NULL;
67    } else {
68       cur_item = num_items;    
69       return items[num_items-1];
70    }
71 }
72
73 void *alist::next()
74 {
75    if (cur_item >= num_items) {
76       return NULL;
77    } else {
78       return items[cur_item++];
79    }
80 }
81
82 void *alist::prev()
83 {
84    if (cur_item <= 1) {
85       return NULL;
86    } else {
87       return items[--cur_item];
88    }
89 }
90
91 /*
92  * prepend an item to the list
93  */
94 void alist::prepend(void *item) {
95    grow_list();
96    if (num_items == 0) {
97       items[num_items++] = item;
98       return;
99    }
100    for (int i=num_items; i > 0; i--) {
101       items[i] = items[i-1];
102    }
103    items[0] = item;
104    num_items++;
105 }
106
107
108 /*
109  * Append an item to the list
110  */
111 void alist::append(void *item) {
112    grow_list();
113    items[num_items++] = item;
114 }
115
116 /* Remove an item from the list */
117 void * alist::remove(int index)
118 {
119    void *item;
120    if (index < 0 || index >= num_items) {
121       return NULL;
122    }
123    item = items[index];
124    num_items--;
125    for (int i=index; i < num_items; i++) {
126       items[i] = items[i+1];
127    }
128    return item;
129 }
130
131
132 /* Get the index item -- we should probably allow real indexing here */
133 void * alist::get(int index)
134 {
135    if (index < 0 || index >= num_items) {
136       return NULL;
137    }
138    return items[index];
139 }
140
141 /* Destroy the list and its contents */
142 void alist::destroy()
143 {
144    if (items) {
145       if (own_items) {
146          for (int i=0; i<num_items; i++) {
147             free(items[i]);
148          }
149       }
150       free(items);
151    }
152 }
153
154 #ifdef TEST_PROGRAM
155
156
157 struct FILESET {
158    alist mylist;
159 };
160
161 int main()
162 {
163    FILESET *fileset;
164    char buf[30];
165    alist *mlist;
166
167    fileset = (FILESET *)malloc(sizeof(FILESET));
168    memset(fileset, 0, sizeof(FILESET));
169    fileset->mylist.init();
170
171    printf("Manual allocation/destruction of list:\n");
172    
173    for (int i=0; i<20; i++) {
174       sprintf(buf, "This is item %d", i);
175       fileset->mylist.append(bstrdup(buf));
176    } 
177    for (int i=0; i< fileset->mylist.size(); i++) {
178       printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);  
179    }
180    fileset->mylist.destroy();
181    free(fileset);
182
183    printf("Allocation/destruction using new delete\n");
184    mlist = new alist(10);
185
186    for (int i=0; i<20; i++) {
187       sprintf(buf, "This is item %d", i);
188       mlist->append(bstrdup(buf));
189    } 
190    for (int i=0; i< mlist->size(); i++) {
191       printf("Item %d = %s\n", i, (char *)mlist->get(i));  
192    }
193
194    delete mlist;
195
196
197    sm_dump(False);
198
199 }
200 #endif