1 /*
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2003-2011 Free Software Foundation Europe e.V.
5 Copyright (C) 2016-2016 Bareos GmbH & Co. KG
6
7 This program is Free Software; you can redistribute it and/or
8 modify it under the terms of version three of the GNU Affero General Public
9 License as published by the Free Software Foundation and included
10 in the file LICENSE.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Affero General Public License for more details.
16
17 You should have received a copy of the GNU Affero General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
21 */
22 /*
23 * Kern Sibbald, June MMIII
24 */
25 /**
26 * @file
27 * BAREOS array list routines
28 *
29 * alist is a simple malloc'ed array of pointers. For the moment,
30 * it simply malloc's a bigger array controlled by num_grow.
31 * Default is to realloc the pointer array for each new member.
32 */
33
34 #include "include/bareos.h"
35
36 /**
37 * Private grow list function. Used to insure that
38 * at least one more "slot" is available.
39 */
GrowList()40 void alist::GrowList()
41 {
42 if (items == NULL) {
43 if (num_grow == 0) {
44 num_grow = 1; /* default if not initialized */
45 }
46 items = (void **)malloc(num_grow * sizeof(void *));
47 max_items = num_grow;
48 } else if (num_items == max_items) {
49 max_items += num_grow;
50 items = (void **)realloc(items, max_items * sizeof(void *));
51 }
52 }
53
first()54 void *alist::first()
55 {
56 cur_item = 1;
57 if (num_items == 0) {
58 return NULL;
59 } else {
60 return items[0];
61 }
62 }
63
last()64 void *alist::last()
65 {
66 if (num_items == 0) {
67 return NULL;
68 } else {
69 cur_item = num_items;
70 return items[num_items-1];
71 }
72 }
73
next()74 void *alist::next()
75 {
76 if (cur_item >= num_items) {
77 return NULL;
78 } else {
79 return items[cur_item++];
80 }
81 }
82
prev()83 void *alist::prev()
84 {
85 if (cur_item <= 1) {
86 return NULL;
87 } else {
88 return items[--cur_item];
89 }
90 }
91
92 /**
93 * prepend an item to the list -- i.e. add to beginning
94 */
prepend(void * item)95 void alist::prepend(void *item) {
96 GrowList();
97 if (num_items == 0) {
98 items[num_items++] = item;
99 return;
100 }
101 for (int i=num_items; i > 0; i--) {
102 items[i] = items[i-1];
103 }
104 items[0] = item;
105 num_items++;
106 }
107
108
109 /**
110 * Append an item to the list
111 */
append(void * item)112 void alist::append(void *item) {
113 GrowList();
114 items[num_items++] = item;
115 }
116
117 /* Remove an item from the list */
remove(int index)118 void * alist::remove(int index)
119 {
120 void *item;
121 if (index < 0 || index >= num_items) {
122 return NULL;
123 }
124 item = items[index];
125 num_items--;
126 for (int i=index; i < num_items; i++) {
127 items[i] = items[i+1];
128 }
129 return item;
130 }
131
132
133 /* Get the index item -- we should probably allow real indexing here */
get(int index)134 void * alist::get(int index)
135 {
136 if (index < 0 || index >= num_items) {
137 return NULL;
138 }
139 return items[index];
140 }
141
142 /* Destroy the list and its contents */
destroy()143 void alist::destroy()
144 {
145 if (items) {
146 if (own_items) {
147 for (int i=0; i<num_items; i++) {
148 free(items[i]);
149 items[i] = NULL;
150 }
151 }
152 free(items);
153 items = NULL;
154 }
155 }
156