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 Bacula® - The Network Backup Solution
16 Copyright (C) 2003-2006 Free Software Foundation Europe e.V.
18 The main author of Bacula is Kern Sibbald, with contributions from
19 many others, a complete list can be found in the file AUTHORS.
20 This program is Free Software; you can redistribute it and/or
21 modify it under the terms of version two of the GNU General Public
22 License as published by the Free Software Foundation plus additions
23 that are listed in the file LICENSE.
25 This program is distributed in the hope that it will be useful, but
26 WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 Bacula® is a registered trademark of John Walker.
36 The licensor of Bacula is the Free Software Foundation Europe
37 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
38 Switzerland, email:ftf@fsfeurope.org.
44 * Private grow list function. Used to insure that
45 * at least one more "slot" is available.
47 void alist::grow_list()
51 num_grow = 1; /* default if not initialized */
53 items = (void **)malloc(num_grow * sizeof(void *));
55 } else if (num_items == max_items) {
56 max_items += num_grow;
57 items = (void **)realloc(items, max_items * sizeof(void *));
77 return items[num_items-1];
83 if (cur_item >= num_items) {
86 return items[cur_item++];
95 return items[--cur_item];
100 * prepend an item to the list -- i.e. add to beginning
102 void alist::prepend(void *item) {
104 if (num_items == 0) {
105 items[num_items++] = item;
108 for (int i=num_items; i > 0; i--) {
109 items[i] = items[i-1];
117 * Append an item to the list
119 void alist::append(void *item) {
121 items[num_items++] = item;
124 /* Remove an item from the list */
125 void * alist::remove(int index)
128 if (index < 0 || index >= num_items) {
133 for (int i=index; i < num_items; i++) {
134 items[i] = items[i+1];
140 /* Get the index item -- we should probably allow real indexing here */
141 void * alist::get(int index)
143 if (index < 0 || index >= num_items) {
149 /* Destroy the list and its contents */
150 void alist::destroy()
154 for (int i=0; i<num_items; i++) {
177 fileset = (FILESET *)malloc(sizeof(FILESET));
178 memset(fileset, 0, sizeof(FILESET));
179 fileset->mylist.init();
181 printf("Manual allocation/destruction of list:\n");
183 for (int i=0; i<20; i++) {
184 sprintf(buf, "This is item %d", i);
185 fileset->mylist.append(bstrdup(buf));
187 for (int i=0; i< fileset->mylist.size(); i++) {
188 printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
190 fileset->mylist.destroy();
193 printf("Allocation/destruction using new delete\n");
194 mlist = new alist(10);
196 for (int i=0; i<20; i++) {
197 sprintf(buf, "This is item %d", i);
198 mlist->append(bstrdup(buf));
200 for (int i=0; i< mlist->size(); i++) {
201 printf("Item %d = %s\n", i, (char *)mlist->get(i));