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) 2003-2006 Kern Sibbald
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 version 2 as amended with additional clauses defined in the
19 file LICENSE in the main source directory.
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
24 the file LICENSE for additional details.
31 * Private grow list function. Used to insure that
32 * at least one more "slot" is available.
34 void alist::grow_list()
38 num_grow = 1; /* default if not initialized */
40 items = (void **)malloc(num_grow * sizeof(void *));
42 } else if (num_items == max_items) {
43 max_items += num_grow;
44 items = (void **)realloc(items, max_items * sizeof(void *));
64 return items[num_items-1];
70 if (cur_item >= num_items) {
73 return items[cur_item++];
82 return items[--cur_item];
87 * prepend an item to the list -- i.e. add to beginning
89 void alist::prepend(void *item) {
92 items[num_items++] = item;
95 for (int i=num_items; i > 0; i--) {
96 items[i] = items[i-1];
104 * Append an item to the list
106 void alist::append(void *item) {
108 items[num_items++] = item;
111 /* Remove an item from the list */
112 void * alist::remove(int index)
115 if (index < 0 || index >= num_items) {
120 for (int i=index; i < num_items; i++) {
121 items[i] = items[i+1];
127 /* Get the index item -- we should probably allow real indexing here */
128 void * alist::get(int index)
130 if (index < 0 || index >= num_items) {
136 /* Destroy the list and its contents */
137 void alist::destroy()
141 for (int i=0; i<num_items; i++) {
164 fileset = (FILESET *)malloc(sizeof(FILESET));
165 memset(fileset, 0, sizeof(FILESET));
166 fileset->mylist.init();
168 printf("Manual allocation/destruction of list:\n");
170 for (int i=0; i<20; i++) {
171 sprintf(buf, "This is item %d", i);
172 fileset->mylist.append(bstrdup(buf));
174 for (int i=0; i< fileset->mylist.size(); i++) {
175 printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
177 fileset->mylist.destroy();
180 printf("Allocation/destruction using new delete\n");
181 mlist = new alist(10);
183 for (int i=0; i<20; i++) {
184 sprintf(buf, "This is item %d", i);
185 mlist->append(bstrdup(buf));
187 for (int i=0; i< mlist->size(); i++) {
188 printf("Item %d = %s\n", i, (char *)mlist->get(i));