]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/ordered-set.c
network: also introduce UseDomains= for [DHCPv6] section
[thirdparty/systemd.git] / src / basic / ordered-set.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "fileio.h"
4 #include "ordered-set.h"
5 #include "strv.h"
6
7 int _ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops HASHMAP_DEBUG_PARAMS) {
8 if (*s)
9 return 0;
10
11 *s = _ordered_set_new(ops HASHMAP_DEBUG_PASS_ARGS);
12 if (!*s)
13 return -ENOMEM;
14
15 return 0;
16 }
17
18 int _ordered_set_ensure_put(OrderedSet **s, const struct hash_ops *ops, void *p HASHMAP_DEBUG_PARAMS) {
19 int r;
20
21 r = _ordered_set_ensure_allocated(s, ops HASHMAP_DEBUG_PASS_ARGS);
22 if (r < 0)
23 return r;
24
25 return ordered_set_put(*s, p);
26 }
27
28 int ordered_set_consume(OrderedSet *s, void *p) {
29 int r;
30
31 r = ordered_set_put(s, p);
32 if (r <= 0)
33 free(p);
34
35 return r;
36 }
37
38 int ordered_set_put_strdup(OrderedSet *s, const char *p) {
39 char *c;
40 int r;
41
42 assert(s);
43 assert(p);
44
45 c = strdup(p);
46 if (!c)
47 return -ENOMEM;
48
49 r = ordered_set_consume(s, c);
50 if (r == -EEXIST)
51 return 0;
52
53 return r;
54 }
55
56 int ordered_set_put_strdupv(OrderedSet *s, char **l) {
57 int n = 0, r;
58 char **i;
59
60 STRV_FOREACH(i, l) {
61 r = ordered_set_put_strdup(s, *i);
62 if (r < 0)
63 return r;
64
65 n += r;
66 }
67
68 return n;
69 }
70
71 int ordered_set_put_string_set(OrderedSet *s, OrderedSet *l) {
72 int n = 0, r;
73 char *p;
74
75 /* Like ordered_set_put_strv, but for an OrderedSet of strings */
76
77 ORDERED_SET_FOREACH(p, l) {
78 r = ordered_set_put_strdup(s, p);
79 if (r < 0)
80 return r;
81
82 n += r;
83 }
84
85 return n;
86 }
87
88 void ordered_set_print(FILE *f, const char *field, OrderedSet *s) {
89 bool space = false;
90 char *p;
91
92 if (ordered_set_isempty(s))
93 return;
94
95 fputs(field, f);
96
97 ORDERED_SET_FOREACH(p, s)
98 fputs_with_space(f, p, NULL, &space);
99
100 fputc('\n', f);
101 }