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
|
/* This file is part of "reprepro"
* Copyright (C) 2003,2005,2007 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "strlist.h"
bool strlist_in(const struct strlist *strlist, const char *element) {
int c;
char **t;
assert(strlist != NULL);
c = strlist->count;
t = strlist->values;
while (c-- != 0) {
if (strcmp(*(t++), element) == 0)
return true;
}
return false;
}
int strlist_ofs(const struct strlist *strlist, const char *element) {
int c;
char **t;
assert(strlist != NULL);
c = strlist->count;
t = strlist->values;
while (c-- != 0) {
if (strcmp(*(t++), element) == 0)
return (t-strlist->values)-1;
}
return -1;
}
bool strlist_subset(const struct strlist *strlist, const struct strlist *subset, const char **missing) {
int c;
char **t;
assert(subset != NULL);
c = subset->count;
t = subset->values;
while (c-- != 0) {
if (!strlist_in(strlist, *(t++))) {
if (missing != NULL)
*missing = *(t-1);
return false;
}
}
return true;
}
retvalue strlist_init_n(int startsize, struct strlist *strlist) {
assert(strlist != NULL && startsize >= 0);
if (startsize == 0)
startsize = 1;
strlist->count = 0;
strlist->size = startsize;
if (startsize > 0) {
strlist->values = malloc(startsize*sizeof(char *));
if (FAILEDTOALLOC(strlist->values))
return RET_ERROR_OOM;
} else {
strlist->values = NULL;
}
return RET_OK;
}
retvalue strlist_init_singleton(char *value, struct strlist *strlist) {
assert(strlist != NULL);
strlist->count = 1;
strlist->size = 1;
strlist->values = NEW(char *);
if (FAILEDTOALLOC(strlist->values)) {
free(value);
return RET_ERROR_OOM;
}
strlist->values[0] = value;
return RET_OK;
}
void strlist_init(struct strlist *strlist) {
assert(strlist != NULL);
strlist->count = 0;
strlist->size = 0;
strlist->values = NULL;
}
void strlist_done(struct strlist *strlist) {
int c;
char **t;
assert(strlist != NULL);
c = strlist->count;
t = strlist->values;
while (c-- != 0) {
free(*t);
t++;
}
free(strlist->values);
strlist->values = NULL;
}
retvalue strlist_add(struct strlist *strlist, char *element) {
char **v;
assert(strlist != NULL && element != NULL);
if (strlist->count >= strlist->size) {
strlist->size += 8;
v = realloc(strlist->values, strlist->size*sizeof(char *));
if (FAILEDTOALLOC(v)) {
free(element);
return RET_ERROR_OOM;
}
strlist->values = v;
}
strlist->values[strlist->count++] = element;
return RET_OK;
}
retvalue strlist_add_dup(struct strlist *strlist, const char *todup) {
char *element = strdup(todup);
if (FAILEDTOALLOC(element))
return RET_ERROR_OOM;
return strlist_add(strlist, element);
}
retvalue strlist_include(struct strlist *strlist, char *element) {
char **v;
assert(strlist != NULL && element != NULL);
if (strlist->count >= strlist->size) {
strlist->size += 1;
v = realloc(strlist->values, strlist->size*sizeof(char *));
if (FAILEDTOALLOC(v)) {
free(element);
return RET_ERROR_OOM;
}
strlist->values = v;
}
arrayinsert(char *, strlist->values, 0, strlist->count);
strlist->count++;
strlist->values[0] = element;
return RET_OK;
}
retvalue strlist_fprint(FILE *file, const struct strlist *strlist) {
int c;
char **p;
retvalue result;
assert(strlist != NULL);
assert(file != NULL);
c = strlist->count;
p = strlist->values;
result = RET_OK;
while (c > 0) {
if (fputs(*(p++), file) == EOF)
result = RET_ERROR;
if (--c > 0 && fputc(' ', file) == EOF)
result = RET_ERROR;
}
return result;
}
/* replace the contents of dest with those from orig, which get emptied */
void strlist_move(struct strlist *dest, struct strlist *orig) {
assert(dest != NULL && orig != NULL);
if (dest == orig)
return;
dest->size = orig->size;
dest->count = orig->count;
dest->values = orig->values;
orig->size = orig->count = 0;
orig->values = NULL;
}
retvalue strlist_adduniq(struct strlist *strlist, char *element) {
// TODO: is there something better feasible?
if (strlist_in(strlist, element)) {
free(element);
return RET_OK;
} else
return strlist_add(strlist, element);
}
bool strlist_intersects(const struct strlist *a, const struct strlist *b) {
int i;
for (i = 0 ; i < a->count ; i++)
if (strlist_in(b, a->values[i]))
return true;
return false;
}
char *strlist_concat(const struct strlist *list, const char *prefix, const char *infix, const char *suffix) {
size_t l, prefix_len, infix_len, suffix_len, line_len;
char *c, *n;
int i;
prefix_len = strlen(prefix);
infix_len = strlen(infix);
suffix_len = strlen(suffix);
l = prefix_len + suffix_len;
for (i = 0 ; i < list->count ; i++)
l += strlen(list->values[i]);
if (list->count > 0)
l += (list->count-1)*infix_len;
c = malloc(l + 1);
if (FAILEDTOALLOC(c))
return c;
memcpy(c, prefix, prefix_len);
n = c + prefix_len;
for (i = 0 ; i < list->count ; i++) {
line_len = strlen(list->values[i]);
memcpy(n, list->values[i], line_len);
n += line_len;
if (i+1 < list->count) {
memcpy(n, infix, infix_len);
n += infix_len;
} else {
memcpy(n, suffix, suffix_len);
n += suffix_len;
}
}
assert ((size_t)(n-c) == l);
*n = '\0';
return c;
}
void strlist_remove(struct strlist *strlist, const char *element) {
int i, j;
assert(strlist != NULL);
assert(element != NULL);
j = 0;
for (i = 0 ; i < strlist->count ; i++) {
if (strcmp(strlist->values[i], element) != 0) {
if (i != j)
strlist->values[j] = strlist->values[i];
j++;
} else
free(strlist->values[i]);
}
strlist->count = j;
}
|