]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.c
b9425c61143b3e3080ee7b5bd88c82912ca659b7
[bacula/bacula] / bacula / src / lib / alist.c
1 /*
2  *  Bacula array list routines     
3  *
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.
7  *
8  *   Kern Sibbald, June MMIII
9  *
10  *   Version $Id$
11  *
12  */
13 /*
14    Copyright (C) 2000-2003 Kern Sibbald and John Walker
15
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.
20
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.
25
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,
29    MA 02111-1307, USA.
30
31  */
32
33 #include "bacula.h"
34
35 /*
36  * Append an item to the list
37  */
38 void alist::append(void *item) {
39    if (num_items == 0) {
40       if (num_grow == 0) {
41          num_grow = 1;                /* default if not initialized */
42       }
43       items = (void **)malloc(num_grow * sizeof(void *));
44       max_items = num_grow;
45    } else if (num_items == max_items) {
46       max_items += num_grow;
47       items = (void **)realloc(items, max_items * sizeof(void *));
48    }
49    items[num_items++] = item;
50 }
51
52 /* Get the index item -- we should probably allow real indexing here */
53 void * alist::get(int index)
54 {
55    if (index < 0 || index >= num_items) {
56       return NULL;
57    }
58    return items[index];
59 }
60
61 /* Destroy the list and its contents */
62 void alist::destroy()
63 {
64    if (items) {
65       if (own_items) {
66          for (int i=0; i<num_items; i++) {
67             free(items[i]);
68          }
69       }
70       free(items);
71    }
72 }
73
74 #ifdef TEST_PROGRAM
75
76
77 struct FILESET {
78    alist mylist;
79 };
80
81 int main()
82 {
83    FILESET *fileset;
84    char buf[30];
85    alist *mlist;
86
87    fileset = (FILESET *)malloc(sizeof(FILESET));
88    memset(fileset, 0, sizeof(FILESET));
89    fileset->mylist.init();
90
91    printf("Manual allocation/destruction of list:\n");
92    
93    for (int i=0; i<20; i++) {
94       sprintf(buf, "This is item %d", i);
95       fileset->mylist.append(bstrdup(buf));
96    } 
97    for (int i=0; i< fileset->mylist.size(); i++) {
98       printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);  
99    }
100    fileset->mylist.destroy();
101    free(fileset);
102
103    printf("Allocation/destruction using new delete\n");
104    mlist = new alist(10);
105
106    for (int i=0; i<20; i++) {
107       sprintf(buf, "This is item %d", i);
108       mlist->append(bstrdup(buf));
109    } 
110    for (int i=0; i< mlist->size(); i++) {
111       printf("Item %d = %s\n", i, (char *)mlist->get(i));  
112    }
113
114    delete mlist;
115
116
117    sm_dump(False);
118
119 }
120 #endif