2 Bacula® - The Network Backup Solution
4 Copyright (C) 2003-2010 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 three of the GNU Affero 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 Affero 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
42 * Private grow list function. Used to insure that
43 * at least one more "slot" is available.
45 void alist::grow_list()
49 num_grow = 1; /* default if not initialized */
51 items = (void **)malloc(num_grow * sizeof(void *));
53 } else if (num_items == max_items) {
54 max_items += num_grow;
55 items = (void **)realloc(items, max_items * sizeof(void *));
75 return items[num_items-1];
81 if (cur_item >= num_items) {
84 return items[cur_item++];
93 return items[--cur_item];
98 * prepend an item to the list -- i.e. add to beginning
100 void alist::prepend(void *item) {
102 if (num_items == 0) {
103 items[num_items++] = item;
106 for (int i=num_items; i > 0; i--) {
107 items[i] = items[i-1];
115 * Append an item to the list
117 void alist::append(void *item) {
119 items[num_items++] = item;
122 /* Remove an item from the list */
123 void * alist::remove(int index)
126 if (index < 0 || index >= num_items) {
131 for (int i=index; i < num_items; i++) {
132 items[i] = items[i+1];
138 /* Get the index item -- we should probably allow real indexing here */
139 void * alist::get(int index)
141 if (index < 0 || index >= num_items) {
147 /* Destroy the list and its contents */
148 void alist::destroy()
152 for (int i=0; i<num_items; i++) {
175 fileset = (FILESET *)malloc(sizeof(FILESET));
176 memset(fileset, 0, sizeof(FILESET));
177 fileset->mylist.init();
179 printf("Manual allocation/destruction of list:\n");
181 for (int i=0; i<20; i++) {
182 sprintf(buf, "This is item %d", i);
183 fileset->mylist.append(bstrdup(buf));
185 for (int i=0; i< fileset->mylist.size(); i++) {
186 printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
188 fileset->mylist.destroy();
191 printf("Allocation/destruction using new delete\n");
192 mlist = new alist(10);
194 for (int i=0; i<20; i++) {
195 sprintf(buf, "This is item %d", i);
196 mlist->append(bstrdup(buf));
198 for (int i=0; i< mlist->size(); i++) {
199 printf("Item %d = %s\n", i, (char *)mlist->get(i));
205 sm_dump(false); /* test program */