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