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