1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/* Services list handler implementation.
*
* (C) 2003 Anope Team
* Contact us at info@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
* $Id$
*
*/
#include "services.h"
#include "slist.h"
static SListOpts slist_defopts = { 0, NULL, NULL, NULL };
/*************************************************************************/
/* Adds a pointer to the list. Returns the index of the new item.
Returns -2 if there are too many items in the list, -3 if the
item already exists when the flags of the list contain SLISTF_NODUP. */
int slist_add(SList * slist, void *item)
{
if (slist->limit != 0 && slist->count >= slist->limit)
return -2;
if (slist->opts && (slist->opts->flags & SLISTF_NODUP)
&& slist_indexof(slist, item) != -1)
return -3;
if (slist->capacity == slist->count)
slist_setcapacity(slist, slist->capacity + 1);
if (slist->opts && (slist->opts->flags & SLISTF_SORT)
&& slist->opts->compareitem) {
int i;
for (i = 0; i < slist->count; i++) {
if (slist->opts->compareitem(slist, item, slist->list[i]) <= 0) {
memmove(&slist->list[i + 1], &slist->list[i],
sizeof(void *) * (slist->count - i));
slist->list[i] = item;
break;
}
}
if (i == slist->count)
slist->list[slist->count] = item;
} else {
slist->list[slist->count] = item;
}
return slist->count++;
}
/*************************************************************************/
/* Clears the list. If free is 1, the freeitem function will be called
* for each item before clearing.
*/
void slist_clear(SList * slist, int mustfree)
{
if (mustfree && slist->opts && slist->opts->freeitem && slist->count) {
int i;
for (i = 0; i < slist->count; i++)
if (slist->list[i])
slist->opts->freeitem(slist, slist->list[i]);
}
if (slist->list) {
free(slist->list);
slist->list = NULL;
}
slist->capacity = 0;
slist->count = 0;
}
/*************************************************************************/
/* Deletes an item from the list, by index. Returns 1 if successful,
0 otherwise. */
int slist_delete(SList * slist, int index)
{
/* Range check */
if (index >= slist->count)
return 0;
if (slist->list[index] && slist->opts && slist->opts->freeitem)
slist->opts->freeitem(slist, slist->list[index]);
slist->list[index] = NULL;
slist->count--;
if (index < slist->count)
memmove(&slist->list[index], &slist->list[index + 1],
sizeof(void *) * (slist->count - index));
slist_setcapacity(slist, slist->capacity - 1);
return 1;
}
/*************************************************************************/
/* Deletes a range of entries. Return -1 if the permission was denied,
* 0 if no records were deleted, or the number of records deleted
*/
int slist_delete_range(SList * slist, char *range, slist_delcheckcb_t cb,
...)
{
int count = 0, i, n1, n2;
va_list args;
va_start(args, cb);
for (;;) {
n1 = n2 = strtol(range, (char **) &range, 10);
range += strcspn(range, "0123456789,-");
if (*range == '-') {
range++;
range += strcspn(range, "0123456789,");
if (isdigit(*range)) {
n2 = strtol(range, (char **) &range, 10);
range += strcspn(range, "0123456789,-");
}
}
for (i = n1; i <= n2 && i > 0 && i <= slist->count; i++) {
if (!slist->list[i - 1])
continue;
if (cb && !cb(slist, slist->list[i - 1], args))
return -1;
if (slist->opts && slist->opts->freeitem)
slist->opts->freeitem(slist, slist->list[i - 1]);
slist->list[i - 1] = NULL;
count++;
}
range += strcspn(range, ",");
if (*range)
range++;
else
break;
}
/* We only really delete the items from the list after having processed
* everything because it would change the position of the items in the
* list otherwise.
*/
slist_pack(slist);
va_end(args);
return count;
}
/*************************************************************************/
/* Enumerates all entries of the list. If range is not NULL, will only
* enumerate entries that are in the range. Returns the total number
* of entries enumerated.
*/
int slist_enum(SList * slist, char *range, slist_enumcb_t cb, ...)
{
int count = 0, i, res;
va_list args;
va_start(args, cb);
if (!range) {
for (i = 0; i < slist->count; i++) {
if (!slist->list[i]) {
alog("SList: warning: NULL pointer in the list (?)");
continue;
}
res = cb(slist, i + 1, slist->list[i], args);
if (res < 0)
break;
count += res;
}
} else {
int n1, n2;
for (;;) {
res = 0;
n1 = n2 = strtol(range, (char **) &range, 10);
range += strcspn(range, "0123456789,-");
if (*range == '-') {
range++;
range += strcspn(range, "0123456789,");
if (isdigit(*range)) {
n2 = strtol(range, (char **) &range, 10);
range += strcspn(range, "0123456789,-");
}
}
for (i = n1; i <= n2 && i > 0 && i <= slist->count; i++) {
if (!slist->list[i - 1]) {
alog("SList: warning: NULL pointer in the list (?)");
continue;
}
res = cb(slist, i, slist->list[i - 1], args);
if (res < 0)
break;
count += res;
}
if (res < -1)
break;
range += strcspn(range, ",");
if (*range)
range++;
else
break;
}
}
va_end(args);
return count;
}
/*************************************************************************/
/* Determines whether the list is full. */
int slist_full(SList * slist)
{
if (slist->limit != 0 && slist->count >= slist->limit)
return 1;
else
return 0;
}
/*************************************************************************/
/* Initialization of the list. */
void slist_init(SList * slist)
{
memset(slist, 0, sizeof(SList));
slist->limit = SLIST_DEFAULT_LIMIT;
slist->opts = &slist_defopts;
}
/*************************************************************************/
/* Returns the index of an item in the list, -1 if inexistant. */
int slist_indexof(SList * slist, void *item)
{
int16 i;
void *entry;
if (slist->count == 0)
return -1;
for (i = 0, entry = slist->list[0]; i < slist->count;
i++, entry = slist->list[i]) {
if ((slist->opts
&& slist->opts->isequal) ? (slist->opts->isequal(slist, item,
entry))
: (item == entry))
return i;
}
return -1;
}
/*************************************************************************/
/* Removes all NULL pointers from the list. */
void slist_pack(SList * slist)
{
int i;
for (i = slist->count - 1; i >= 0; i--)
if (!slist->list[i])
slist_delete(slist, i);
}
/*************************************************************************/
/* Removes a specific item from the list. Returns the old index of the
deleted item, or -1 if the item was not found. */
int slist_remove(SList * slist, void *item)
{
int index = slist_indexof(slist, item);
if (index == -1)
return -1;
slist_delete(slist, index);
return index;
}
/*************************************************************************/
/* Sets the maximum capacity of the list */
int slist_setcapacity(SList * slist, int16 capacity)
{
if (slist->capacity == capacity)
return 1;
slist->capacity = capacity;
if (slist->capacity)
slist->list =
srealloc(slist->list, sizeof(void *) * slist->capacity);
else {
free(slist->list);
slist->list = NULL;
}
if (slist->capacity < slist->count)
slist->count = slist->capacity;
return 1;
}
|