]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
00616955 | 2 | |
53ae3f64 | 3 | #include "fileio.h" |
00616955 LP |
4 | #include "ordered-set.h" |
5 | #include "strv.h" | |
6 | ||
1f25c71d ZJS |
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 | ||
00616955 LP |
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 | ||
cf453507 | 38 | int _ordered_set_put_strdup(OrderedSet **s, const char *p HASHMAP_DEBUG_PARAMS) { |
00616955 LP |
39 | char *c; |
40 | int r; | |
41 | ||
42 | assert(s); | |
43 | assert(p); | |
44 | ||
cf453507 YW |
45 | r = _ordered_set_ensure_allocated(s, &string_hash_ops_free HASHMAP_DEBUG_PASS_ARGS); |
46 | if (r < 0) | |
47 | return r; | |
48 | ||
49 | if (ordered_set_contains(*s, p)) | |
50 | return 0; | |
51 | ||
00616955 LP |
52 | c = strdup(p); |
53 | if (!c) | |
54 | return -ENOMEM; | |
55 | ||
cf453507 | 56 | return ordered_set_consume(*s, c); |
00616955 LP |
57 | } |
58 | ||
cf453507 | 59 | int _ordered_set_put_strdupv(OrderedSet **s, char **l HASHMAP_DEBUG_PARAMS) { |
00616955 LP |
60 | int n = 0, r; |
61 | char **i; | |
62 | ||
63 | STRV_FOREACH(i, l) { | |
cf453507 | 64 | r = _ordered_set_put_strdup(s, *i HASHMAP_DEBUG_PASS_ARGS); |
00616955 LP |
65 | if (r < 0) |
66 | return r; | |
67 | ||
68 | n += r; | |
69 | } | |
70 | ||
71 | return n; | |
72 | } | |
ef79eae0 | 73 | |
cf453507 | 74 | int ordered_set_put_string_set(OrderedSet **s, OrderedSet *l) { |
ef79eae0 | 75 | int n = 0, r; |
ef79eae0 ZJS |
76 | char *p; |
77 | ||
78 | /* Like ordered_set_put_strv, but for an OrderedSet of strings */ | |
79 | ||
90e74a66 | 80 | ORDERED_SET_FOREACH(p, l) { |
ef79eae0 ZJS |
81 | r = ordered_set_put_strdup(s, p); |
82 | if (r < 0) | |
83 | return r; | |
84 | ||
85 | n += r; | |
86 | } | |
87 | ||
88 | return n; | |
89 | } | |
53ae3f64 ZJS |
90 | |
91 | void ordered_set_print(FILE *f, const char *field, OrderedSet *s) { | |
92 | bool space = false; | |
53ae3f64 ZJS |
93 | char *p; |
94 | ||
95 | if (ordered_set_isempty(s)) | |
96 | return; | |
97 | ||
98 | fputs(field, f); | |
99 | ||
90e74a66 | 100 | ORDERED_SET_FOREACH(p, s) |
53ae3f64 ZJS |
101 | fputs_with_space(f, p, NULL, &space); |
102 | ||
103 | fputc('\n', f); | |
104 | } |