2 Bacula® - The Network Backup Solution
4 Copyright (C) 2003-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Bacula array list routines
31 * alist is a simple malloc'ed array of pointers. For the moment,
32 * it simply malloc's a bigger array controlled by num_grow.
33 * Default is to realloc the pointer array for each new member.
35 * Kern Sibbald, June MMIII
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));