]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.c
Move host.h from filed to src + add Project to home page
[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       for (int i=0; i<num_items; i++) {
66          free(items[i]);
67       }
68       free(items);
69    }
70 }
71
72 #ifdef TEST_PROGRAM
73
74
75 struct FILESET {
76    alist mylist;
77 };
78
79 int main()
80 {
81    FILESET *fileset;
82    char buf[30];
83    alist *mlist;
84
85    fileset = (FILESET *)malloc(sizeof(FILESET));
86    memset(fileset, 0, sizeof(FILESET));
87    fileset->mylist.init();
88
89    printf("Manual allocation/destruction of list:\n");
90    
91    for (int i=0; i<20; i++) {
92       sprintf(buf, "This is item %d", i);
93       fileset->mylist.append(bstrdup(buf));
94    } 
95    for (int i=0; i<fileset->mylist.size(); i++) {
96       printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);  
97    }
98    fileset->mylist.destroy();
99    free(fileset);
100
101    printf("Allocation/destruction using new delete\n");
102    mlist = new alist(10);
103
104    for (int i=0; i<20; i++) {
105       sprintf(buf, "This is item %d", i);
106       mlist->append(bstrdup(buf));
107    } 
108    for (int i=0; i<mlist->size(); i++) {
109       printf("Item %d = %s\n", i, (char *)mlist->get(i));  
110    }
111
112    delete mlist;
113
114
115    sm_dump(False);
116
117 }
118 #endif