api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_strvec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #define _GNU_SOURCE /* qsort_r */
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <errno.h>
10
11 #include <ecoli_malloc.h>
12 #include <ecoli_test.h>
13 #include <ecoli_log.h>
14 #include <ecoli_dict.h>
15 #include <ecoli_strvec.h>
16
17 EC_LOG_TYPE_REGISTER(strvec);
18
19 struct ec_strvec_elt {
20         unsigned int refcnt;
21         char *str;
22         struct ec_dict *attrs;
23 };
24
25 struct ec_strvec {
26         size_t len;
27         struct ec_strvec_elt **vec;
28 };
29
30 struct ec_strvec *ec_strvec(void)
31 {
32         struct ec_strvec *strvec;
33
34         strvec = ec_calloc(1, sizeof(*strvec));
35         if (strvec == NULL)
36                 return NULL;
37
38         return strvec;
39 }
40
41 static struct ec_strvec_elt *
42 __ec_strvec_elt(const char *s)
43 {
44         struct ec_strvec_elt *elt;
45
46         elt = ec_calloc(1, sizeof(*elt));
47         if (elt == NULL)
48                 return NULL;
49
50         elt->str = ec_strdup(s);
51         if (elt->str == NULL) {
52                 ec_free(elt);
53                 return NULL;
54         }
55         elt->refcnt = 1;
56
57         return elt;
58 }
59
60 static void
61 __ec_strvec_elt_free(struct ec_strvec_elt *elt)
62 {
63         elt->refcnt--;
64         if (elt->refcnt == 0) {
65                 ec_free(elt->str);
66                 ec_dict_free(elt->attrs);
67                 ec_free(elt);
68         }
69 }
70
71 int ec_strvec_set(struct ec_strvec *strvec, size_t idx, const char *s)
72 {
73         struct ec_strvec_elt *elt;
74
75         if (strvec == NULL || s == NULL || idx >= strvec->len) {
76                 errno = EINVAL;
77                 return -1;
78         }
79
80         elt = __ec_strvec_elt(s);
81         if (elt == NULL)
82                 return -1;
83
84         __ec_strvec_elt_free(strvec->vec[idx]);
85         strvec->vec[idx] = elt;
86
87         return 0;
88 }
89
90 int ec_strvec_add(struct ec_strvec *strvec, const char *s)
91 {
92         struct ec_strvec_elt *elt, **new_vec;
93
94         if (strvec == NULL || s == NULL) {
95                 errno = EINVAL;
96                 return -1;
97         }
98
99         new_vec = ec_realloc(strvec->vec,
100                 sizeof(*strvec->vec) * (strvec->len + 1));
101         if (new_vec == NULL)
102                 return -1;
103
104         strvec->vec = new_vec;
105
106         elt = __ec_strvec_elt(s);
107         if (elt == NULL)
108                 return -1;
109
110         new_vec[strvec->len] = elt;
111         strvec->len++;
112
113         return 0;
114 }
115
116 struct ec_strvec *ec_strvec_from_array(const char * const *strarr,
117         size_t n)
118 {
119         struct ec_strvec *strvec = NULL;
120         size_t i;
121
122         strvec = ec_strvec();
123         if (strvec == NULL)
124                 goto fail;
125
126         for (i = 0; i < n; i++) {
127                 if (ec_strvec_add(strvec, strarr[i]) < 0)
128                         goto fail;
129         }
130
131         return strvec;
132
133 fail:
134         ec_strvec_free(strvec);
135         return NULL;
136 }
137
138 int ec_strvec_del_last(struct ec_strvec *strvec)
139 {
140         if (strvec->len == 0) {
141                 errno = EINVAL;
142                 return -1;
143         }
144
145         __ec_strvec_elt_free(strvec->vec[strvec->len - 1]);
146         strvec->len--;
147
148         return 0;
149 }
150
151 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec, size_t off,
152         size_t len)
153 {
154         struct ec_strvec *copy = NULL;
155         size_t i, veclen;
156
157         veclen = ec_strvec_len(strvec);
158         if (off + len > veclen)
159                 return NULL;
160
161         copy = ec_strvec();
162         if (copy == NULL)
163                 goto fail;
164
165         if (len == 0)
166                 return copy;
167
168         copy->vec = ec_calloc(len, sizeof(*copy->vec));
169         if (copy->vec == NULL)
170                 goto fail;
171
172         for (i = 0; i < len; i++) {
173                 copy->vec[i] = strvec->vec[i + off];
174                 copy->vec[i]->refcnt++;
175         }
176         copy->len = len;
177
178         return copy;
179
180 fail:
181         ec_strvec_free(copy);
182         return NULL;
183 }
184
185 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec)
186 {
187         return ec_strvec_ndup(strvec, 0, ec_strvec_len(strvec));
188 }
189
190 void ec_strvec_free(struct ec_strvec *strvec)
191 {
192         struct ec_strvec_elt *elt;
193         size_t i;
194
195         if (strvec == NULL)
196                 return;
197
198         for (i = 0; i < ec_strvec_len(strvec); i++) {
199                 elt = strvec->vec[i];
200                 __ec_strvec_elt_free(elt);
201         }
202
203         ec_free(strvec->vec);
204         ec_free(strvec);
205 }
206
207 size_t ec_strvec_len(const struct ec_strvec *strvec)
208 {
209         return strvec->len;
210 }
211
212 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx)
213 {
214         if (strvec == NULL || idx >= strvec->len)
215                 return NULL;
216
217         return strvec->vec[idx]->str;
218 }
219
220 const struct ec_dict *ec_strvec_get_attrs(const struct ec_strvec *strvec,
221         size_t idx)
222 {
223         if (strvec == NULL || idx >= strvec->len) {
224                 errno = EINVAL;
225                 return NULL;
226         }
227
228         return strvec->vec[idx]->attrs;
229 }
230
231 int ec_strvec_set_attrs(struct ec_strvec *strvec, size_t idx,
232                         struct ec_dict *attrs)
233 {
234         struct ec_strvec_elt *elt;
235
236         if (strvec == NULL || idx >= strvec->len) {
237                 errno = EINVAL;
238                 goto fail;
239         }
240
241         elt = strvec->vec[idx];
242         if (elt->refcnt > 1) {
243                 if (ec_strvec_set(strvec, idx, elt->str) < 0)
244                         goto fail;
245                 elt = strvec->vec[idx];
246         }
247
248         if (elt->attrs != NULL)
249                 ec_dict_free(elt->attrs);
250
251         elt->attrs = attrs;
252
253         return 0;
254
255 fail:
256         ec_dict_free(attrs);
257         return -1;
258 }
259
260 int ec_strvec_cmp(const struct ec_strvec *strvec1,
261                 const struct ec_strvec *strvec2)
262 {
263         size_t i;
264
265         if (ec_strvec_len(strvec1) != ec_strvec_len(strvec2))
266                 return -1;
267
268         for (i = 0; i < ec_strvec_len(strvec1); i++) {
269                 if (strcmp(ec_strvec_val(strvec1, i),
270                                 ec_strvec_val(strvec2, i)))
271                         return -1;
272         }
273
274         return 0;
275 }
276
277 static int
278 cmp_vec_elt(const void *p1, const void *p2, void *arg)
279 {
280         int (*str_cmp)(const char *s1, const char *s2) = arg;
281         const struct ec_strvec_elt * const *e1 = p1, * const *e2 = p2;
282
283         return str_cmp((*e1)->str, (*e2)->str);
284 }
285
286 void ec_strvec_sort(struct ec_strvec *strvec,
287         int (*str_cmp)(const char *s1, const char *s2))
288 {
289         if (str_cmp == NULL)
290                 str_cmp = strcmp;
291         qsort_r(strvec->vec, ec_strvec_len(strvec),
292                 sizeof(*strvec->vec), cmp_vec_elt, str_cmp);
293 }
294
295 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec)
296 {
297         size_t i;
298
299         if (strvec == NULL) {
300                 fprintf(out, "none\n");
301                 return;
302         }
303
304         fprintf(out, "strvec (len=%zu) [", strvec->len);
305         for (i = 0; i < ec_strvec_len(strvec); i++) {
306                 if (i == 0)
307                         fprintf(out, "%s", strvec->vec[i]->str);
308                 else
309                         fprintf(out, ", %s", strvec->vec[i]->str);
310         }
311         fprintf(out, "]\n");
312
313 }
314
315 /* LCOV_EXCL_START */
316 static int ec_strvec_testcase(void)
317 {
318         struct ec_strvec *strvec = NULL;
319         struct ec_strvec *strvec2 = NULL;
320         const struct ec_dict *const_attrs = NULL;
321         struct ec_dict *attrs = NULL;
322         FILE *f = NULL;
323         char *buf = NULL;
324         size_t buflen = 0;
325         int testres = 0;
326
327         strvec = ec_strvec();
328         if (strvec == NULL) {
329                 EC_TEST_ERR("cannot create strvec\n");
330                 goto fail;
331         }
332         if (ec_strvec_len(strvec) != 0) {
333                 EC_TEST_ERR("bad strvec len (0)\n");
334                 goto fail;
335         }
336         if (ec_strvec_add(strvec, "0") < 0) {
337                 EC_TEST_ERR("cannot add (0) in strvec\n");
338                 goto fail;
339         }
340         if (ec_strvec_len(strvec) != 1) {
341                 EC_TEST_ERR("bad strvec len (1)\n");
342                 goto fail;
343         }
344         if (ec_strvec_add(strvec, "1") < 0) {
345                 EC_TEST_ERR("cannot add (1) in strvec\n");
346                 goto fail;
347         }
348         if (ec_strvec_len(strvec) != 2) {
349                 EC_TEST_ERR("bad strvec len (2)\n");
350                 goto fail;
351         }
352         if (strcmp(ec_strvec_val(strvec, 0), "0")) {
353                 EC_TEST_ERR("invalid element in strvec (0)\n");
354                 goto fail;
355         }
356         if (strcmp(ec_strvec_val(strvec, 1), "1")) {
357                 EC_TEST_ERR("invalid element in strvec (1)\n");
358                 goto fail;
359         }
360         if (ec_strvec_val(strvec, 2) != NULL) {
361                 EC_TEST_ERR("strvec val should be NULL\n");
362                 goto fail;
363         }
364
365         strvec2 = ec_strvec_dup(strvec);
366         if (strvec2 == NULL) {
367                 EC_TEST_ERR("cannot create strvec2\n");
368                 goto fail;
369         }
370         if (ec_strvec_len(strvec2) != 2) {
371                 EC_TEST_ERR("bad strvec2 len (2)\n");
372                 goto fail;
373         }
374         if (strcmp(ec_strvec_val(strvec2, 0), "0")) {
375                 EC_TEST_ERR("invalid element in strvec2 (0)\n");
376                 goto fail;
377         }
378         if (strcmp(ec_strvec_val(strvec2, 1), "1")) {
379                 EC_TEST_ERR("invalid element in strvec2 (1)\n");
380                 goto fail;
381         }
382         if (ec_strvec_val(strvec2, 2) != NULL) {
383                 EC_TEST_ERR("strvec2 val should be NULL\n");
384                 goto fail;
385         }
386         ec_strvec_free(strvec2);
387
388         strvec2 = ec_strvec_ndup(strvec, 0, 0);
389         if (strvec2 == NULL) {
390                 EC_TEST_ERR("cannot create strvec2\n");
391                 goto fail;
392         }
393         if (ec_strvec_len(strvec2) != 0) {
394                 EC_TEST_ERR("bad strvec2 len (0)\n");
395                 goto fail;
396         }
397         if (ec_strvec_val(strvec2, 0) != NULL) {
398                 EC_TEST_ERR("strvec2 val should be NULL\n");
399                 goto fail;
400         }
401         ec_strvec_free(strvec2);
402
403         strvec2 = ec_strvec_ndup(strvec, 1, 1);
404         if (strvec2 == NULL) {
405                 EC_TEST_ERR("cannot create strvec2\n");
406                 goto fail;
407         }
408         if (ec_strvec_len(strvec2) != 1) {
409                 EC_TEST_ERR("bad strvec2 len (1)\n");
410                 goto fail;
411         }
412         if (strcmp(ec_strvec_val(strvec2, 0), "1")) {
413                 EC_TEST_ERR("invalid element in strvec2 (1)\n");
414                 goto fail;
415         }
416         if (ec_strvec_val(strvec2, 1) != NULL) {
417                 EC_TEST_ERR("strvec2 val should be NULL\n");
418                 goto fail;
419         }
420         ec_strvec_free(strvec2);
421
422         strvec2 = ec_strvec_ndup(strvec, 3, 1);
423         if (strvec2 != NULL) {
424                 EC_TEST_ERR("strvec2 should be NULL\n");
425                 goto fail;
426         }
427         ec_strvec_free(strvec2);
428
429         strvec2 = EC_STRVEC("0", "1");
430         if (strvec2 == NULL) {
431                 EC_TEST_ERR("cannot create strvec from array\n");
432                 goto fail;
433         }
434         testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0,
435                 "strvec and strvec2 should be equal\n");
436         ec_strvec_free(strvec2);
437
438         f = open_memstream(&buf, &buflen);
439         if (f == NULL)
440                 goto fail;
441         ec_strvec_dump(f, strvec);
442         fclose(f);
443         f = NULL;
444         testres |= EC_TEST_CHECK(
445                 strstr(buf, "strvec (len=2) [0, 1]"), "bad dump\n");
446         free(buf);
447         buf = NULL;
448
449         ec_strvec_del_last(strvec);
450         strvec2 = EC_STRVEC("0");
451         if (strvec2 == NULL) {
452                 EC_TEST_ERR("cannot create strvec from array\n");
453                 goto fail;
454         }
455         testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0,
456                 "strvec and strvec2 should be equal\n");
457         ec_strvec_free(strvec2);
458         strvec2 = NULL;
459
460         f = open_memstream(&buf, &buflen);
461         if (f == NULL)
462                 goto fail;
463         ec_strvec_dump(f, NULL);
464         fclose(f);
465         f = NULL;
466         testres |= EC_TEST_CHECK(
467                 strstr(buf, "none"), "bad dump\n");
468         free(buf);
469         buf = NULL;
470
471         ec_strvec_free(strvec);
472
473         strvec = EC_STRVEC("e", "a", "f", "d", "b", "c");
474         if (strvec == NULL) {
475                 EC_TEST_ERR("cannot create strvec from array\n");
476                 goto fail;
477         }
478         attrs = ec_dict();
479         if (attrs == NULL) {
480                 EC_TEST_ERR("cannot create attrs\n");
481                 goto fail;
482         }
483         if (ec_dict_set(attrs, "key", "value", NULL) < 0) {
484                 EC_TEST_ERR("cannot set attr\n");
485                 goto fail;
486         }
487         if (ec_strvec_set_attrs(strvec, 1, attrs) < 0) {
488                 attrs = NULL;
489                 EC_TEST_ERR("cannot set attrs in strvec\n");
490                 goto fail;
491         }
492         attrs = NULL;
493
494         ec_strvec_sort(strvec, NULL);
495
496         /* attrs are now at index 0 after sorting */
497         const_attrs = ec_strvec_get_attrs(strvec, 0);
498         if (const_attrs == NULL) {
499                 EC_TEST_ERR("cannot get attrs\n");
500                 goto fail;
501         }
502         testres |= EC_TEST_CHECK(
503                 ec_dict_has_key(const_attrs, "key"), "cannot get attrs key\n");
504
505         strvec2 = EC_STRVEC("a", "b", "c", "d", "e", "f");
506         if (strvec2 == NULL) {
507                 EC_TEST_ERR("cannot create strvec from array\n");
508                 goto fail;
509         }
510         testres |= EC_TEST_CHECK(ec_strvec_cmp(strvec, strvec2) == 0,
511                 "strvec and strvec2 should be equal\n");
512         ec_strvec_free(strvec);
513         strvec = NULL;
514         ec_strvec_free(strvec2);
515         strvec2 = NULL;
516
517         return testres;
518
519 fail:
520         if (f != NULL)
521                 fclose(f);
522         ec_dict_free(attrs);
523         ec_strvec_free(strvec);
524         ec_strvec_free(strvec2);
525         free(buf);
526
527         return -1;
528 }
529 /* LCOV_EXCL_STOP */
530
531 static struct ec_test ec_node_str_test = {
532         .name = "strvec",
533         .test = ec_strvec_testcase,
534 };
535
536 EC_TEST_REGISTER(ec_node_str_test);