2 * Bacula array list routines
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.
8 * Kern Sibbald, June MMIII
14 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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.
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.
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,
36 * Private grow list function. Used to insure that
37 * at least one more "slot" is available.
39 void alist::grow_list()
43 num_grow = 1; /* default if not initialized */
45 items = (void **)malloc(num_grow * sizeof(void *));
47 } else if (num_items == max_items) {
48 max_items += num_grow;
49 items = (void **)realloc(items, max_items * sizeof(void *));
69 return items[num_items-1];
75 if (cur_item >= num_items) {
78 return items[cur_item++];
87 return items[--cur_item];
92 * prepend an item to the list
94 void alist::prepend(void *item) {
97 items[num_items++] = item;
100 for (int i=num_items; i > 0; i--) {
101 items[i] = items[i-1];
109 * Append an item to the list
111 void alist::append(void *item) {
113 items[num_items++] = item;
116 /* Remove an item from the list */
117 void * alist::remove(int index)
120 if (index < 0 || index >= num_items) {
125 for (int i=index; i < num_items; i++) {
126 items[i] = items[i+1];
132 /* Get the index item -- we should probably allow real indexing here */
133 void * alist::get(int index)
135 if (index < 0 || index >= num_items) {
141 /* Destroy the list and its contents */
142 void alist::destroy()
146 for (int i=0; i<num_items; i++) {
169 fileset = (FILESET *)malloc(sizeof(FILESET));
170 memset(fileset, 0, sizeof(FILESET));
171 fileset->mylist.init();
173 printf("Manual allocation/destruction of list:\n");
175 for (int i=0; i<20; i++) {
176 sprintf(buf, "This is item %d", i);
177 fileset->mylist.append(bstrdup(buf));
179 for (int i=0; i< fileset->mylist.size(); i++) {
180 printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
182 fileset->mylist.destroy();
185 printf("Allocation/destruction using new delete\n");
186 mlist = new alist(10);
188 for (int i=0; i<20; i++) {
189 sprintf(buf, "This is item %d", i);
190 mlist->append(bstrdup(buf));
192 for (int i=0; i< mlist->size(); i++) {
193 printf("Item %d = %s\n", i, (char *)mlist->get(i));