summaryrefslogtreecommitdiffstats
path: root/src/basic/ordered-set.c
blob: fb82c17b5a81ecb4bb94caff948783fa5d8d2a18 (plain)
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
/* SPDX-License-Identifier: LGPL-2.1+ */

#include "fileio.h"
#include "ordered-set.h"
#include "strv.h"

int _ordered_set_ensure_allocated(OrderedSet **s, const struct hash_ops *ops  HASHMAP_DEBUG_PARAMS) {
        if (*s)
                return 0;

        *s = _ordered_set_new(ops  HASHMAP_DEBUG_PASS_ARGS);
        if (!*s)
                return -ENOMEM;

        return 0;
}

int _ordered_set_ensure_put(OrderedSet **s, const struct hash_ops *ops, void *p  HASHMAP_DEBUG_PARAMS) {
        int r;

        r = _ordered_set_ensure_allocated(s, ops  HASHMAP_DEBUG_PASS_ARGS);
        if (r < 0)
                return r;

        return ordered_set_put(*s, p);
}

int ordered_set_consume(OrderedSet *s, void *p) {
        int r;

        r = ordered_set_put(s, p);
        if (r <= 0)
                free(p);

        return r;
}

int ordered_set_put_strdup(OrderedSet *s, const char *p) {
        char *c;
        int r;

        assert(s);
        assert(p);

        c = strdup(p);
        if (!c)
                return -ENOMEM;

        r = ordered_set_consume(s, c);
        if (r == -EEXIST)
                return 0;

        return r;
}

int ordered_set_put_strdupv(OrderedSet *s, char **l) {
        int n = 0, r;
        char **i;

        STRV_FOREACH(i, l) {
                r = ordered_set_put_strdup(s, *i);
                if (r < 0)
                        return r;

                n += r;
        }

        return n;
}

int ordered_set_put_string_set(OrderedSet *s, OrderedSet *l) {
        int n = 0, r;
        Iterator i;
        char *p;

        /* Like ordered_set_put_strv, but for an OrderedSet of strings */

        ORDERED_SET_FOREACH(p, l, i) {
                r = ordered_set_put_strdup(s, p);
                if (r < 0)
                        return r;

                n += r;
        }

        return n;
}

void ordered_set_print(FILE *f, const char *field, OrderedSet *s) {
        bool space = false;
        Iterator i;
        char *p;

        if (ordered_set_isempty(s))
                return;

        fputs(field, f);

        ORDERED_SET_FOREACH(p, s, i)
                fputs_with_space(f, p, NULL, &space);

        fputc('\n', f);
}