]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.c
15Jan06
[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) 2003-2006 Kern Sibbald
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
18    version 2 as amended with additional clauses defined in the
19    file LICENSE in the main source directory.
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 
24    the file LICENSE for additional details.
25
26  */
27
28 #include "bacula.h"
29
30 /*
31  * Private grow list function. Used to insure that
32  *   at least one more "slot" is available.
33  */
34 void alist::grow_list()
35 {
36    if (items == NULL) {
37       if (num_grow == 0) {
38          num_grow = 1;                /* default if not initialized */
39       }
40       items = (void **)malloc(num_grow * sizeof(void *));
41       max_items = num_grow;
42    } else if (num_items == max_items) {
43       max_items += num_grow;
44       items = (void **)realloc(items, max_items * sizeof(void *));
45    }
46 }
47
48 void *alist::first()
49 {
50    cur_item = 1;
51    if (num_items == 0) {
52       return NULL;
53    } else {
54       return items[0];
55    }
56 }
57
58 void *alist::last()
59 {
60    if (num_items == 0) {
61       return NULL;
62    } else {
63       cur_item = num_items;
64       return items[num_items-1];
65    }
66 }
67
68 void *alist::next()
69 {
70    if (cur_item >= num_items) {
71       return NULL;
72    } else {
73       return items[cur_item++];
74    }
75 }
76
77 void *alist::prev()
78 {
79    if (cur_item <= 1) {
80       return NULL;
81    } else {
82       return items[--cur_item];
83    }
84 }
85
86 /*
87  * prepend an item to the list -- i.e. add to beginning
88  */
89 void alist::prepend(void *item) {
90    grow_list();
91    if (num_items == 0) {
92       items[num_items++] = item;
93       return;
94    }
95    for (int i=num_items; i > 0; i--) {
96       items[i] = items[i-1];
97    }
98    items[0] = item;
99    num_items++;
100 }
101
102
103 /*
104  * Append an item to the list
105  */
106 void alist::append(void *item) {
107    grow_list();
108    items[num_items++] = item;
109 }
110
111 /* Remove an item from the list */
112 void * alist::remove(int index)
113 {
114    void *item;
115    if (index < 0 || index >= num_items) {
116       return NULL;
117    }
118    item = items[index];
119    num_items--;
120    for (int i=index; i < num_items; i++) {
121       items[i] = items[i+1];
122    }
123    return item;
124 }
125
126
127 /* Get the index item -- we should probably allow real indexing here */
128 void * alist::get(int index)
129 {
130    if (index < 0 || index >= num_items) {
131       return NULL;
132    }
133    return items[index];
134 }
135
136 /* Destroy the list and its contents */
137 void alist::destroy()
138 {
139    if (items) {
140       if (own_items) {
141          for (int i=0; i<num_items; i++) {
142             free(items[i]);
143             items[i] = NULL;
144          }
145       }
146       free(items);
147       items = NULL;
148    }
149 }
150
151 #ifdef TEST_PROGRAM
152
153
154 struct FILESET {
155    alist mylist;
156 };
157
158 int main()
159 {
160    FILESET *fileset;
161    char buf[30];
162    alist *mlist;
163
164    fileset = (FILESET *)malloc(sizeof(FILESET));
165    memset(fileset, 0, sizeof(FILESET));
166    fileset->mylist.init();
167
168    printf("Manual allocation/destruction of list:\n");
169
170    for (int i=0; i<20; i++) {
171       sprintf(buf, "This is item %d", i);
172       fileset->mylist.append(bstrdup(buf));
173    }
174    for (int i=0; i< fileset->mylist.size(); i++) {
175       printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
176    }
177    fileset->mylist.destroy();
178    free(fileset);
179
180    printf("Allocation/destruction using new delete\n");
181    mlist = new alist(10);
182
183    for (int i=0; i<20; i++) {
184       sprintf(buf, "This is item %d", i);
185       mlist->append(bstrdup(buf));
186    }
187    for (int i=0; i< mlist->size(); i++) {
188       printf("Item %d = %s\n", i, (char *)mlist->get(i));
189    }
190
191    delete mlist;
192
193
194    sm_dump(false);
195
196 }
197 #endif