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-2003 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 * Append an item to the list
38 void alist::append(void *item) {
41 num_grow = 1; /* default if not initialized */
43 items = (void **)malloc(num_grow * sizeof(void *));
45 } else if (num_items == max_items) {
46 max_items += num_grow;
47 items = (void **)realloc(items, max_items * sizeof(void *));
49 items[num_items++] = item;
52 /* Get the index item -- we should probably allow real indexing here */
53 void * alist::get(int index)
55 if (index < 0 || index >= num_items) {
61 /* Destroy the list and its contents */
66 for (int i=0; i<num_items; i++) {
87 fileset = (FILESET *)malloc(sizeof(FILESET));
88 memset(fileset, 0, sizeof(FILESET));
89 fileset->mylist.init();
91 printf("Manual allocation/destruction of list:\n");
93 for (int i=0; i<20; i++) {
94 sprintf(buf, "This is item %d", i);
95 fileset->mylist.append(bstrdup(buf));
97 for (int i=0; i< fileset->mylist.size(); i++) {
98 printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
100 fileset->mylist.destroy();
103 printf("Allocation/destruction using new delete\n");
104 mlist = new alist(10);
106 for (int i=0; i<20; i++) {
107 sprintf(buf, "This is item %d", i);
108 mlist->append(bstrdup(buf));
110 for (int i=0; i< mlist->size(); i++) {
111 printf("Item %d = %s\n", i, (char *)mlist->get(i));