File: set.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (246 lines) | stat: -rw-r--r-- 5,930 bytes parent folder | download | duplicates (2)
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
/*
  Copyright 2024 Northern.tech AS

  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  To the extent this program is licensed as part of the Enterprise
  versions of CFEngine, the applicable Commercial Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

#include <platform.h>
#include <set.h>
#include <string.h> // strlen()

#include <alloc.h>
#include <string_lib.h>
#include <buffer.h>

TYPED_SET_DEFINE(String, char *,
                 StringHash_untyped, StringEqual_untyped, free)

Set *SetNew(MapHashFn element_hash_fn,
            MapKeyEqualFn element_equal_fn,
            MapDestroyDataFn element_destroy_fn)
{
    return MapNew(element_hash_fn, element_equal_fn, element_destroy_fn, NULL);
}

void SetDestroy(Set *set)
{
    MapDestroy(set);
}

void SetAdd(Set *set, void *element)
{
    assert(set != NULL);
    MapInsert(set, element, element);
}

bool SetContains(const Set *set, const void *element)
{
    assert(set != NULL);
    return MapHasKey(set, element);
}

bool SetRemove(Set *set, const void *element)
{
    assert(set != NULL);
    return MapRemove(set, element);
}

void SetClear(Set *set)
{
    assert(set != NULL);
    MapClear(set);
}

size_t SetSize(const Set *set)
{
    assert(set != NULL);
    return MapSize(set);
}

bool SetIsEqual(const Set *set1, const Set *set2)
{
    assert(set1 != NULL);
    assert(set2 != NULL);
    return MapContainsSameKeys(set1, set2);
}

SetIterator SetIteratorInit(Set *set)
{
    assert(set != NULL);
    return MapIteratorInit(set);
}

void *SetIteratorNext(SetIterator *i)
{
    MapKeyValue *kv = MapIteratorNext(i);
    return kv ? kv->key : NULL;
}

void SetJoin(Set *set, Set *otherset, SetElementCopyFn copy_function)
{
    assert(set != NULL);
    assert(otherset != NULL);
    if (set == otherset)
        return;

    SetIterator si = SetIteratorInit(otherset);
    void *ptr = NULL;

    for (ptr = SetIteratorNext(&si); ptr != NULL; ptr = SetIteratorNext(&si))
    {
        if (copy_function != NULL)
        {
            ptr = copy_function(ptr);
        }
        SetAdd(set, ptr);
    }
}

Buffer *StringSetToBuffer(StringSet *set, const char delimiter)
{
    assert(set != NULL);

    Buffer *buf = BufferNew();
    StringSetIterator it = StringSetIteratorInit(set);
    const char *element = NULL;
    int pos = 0;
    int size = StringSetSize(set);
    char minibuf[2];

    minibuf[0] = delimiter;
    minibuf[1] = '\0';

    while ((element = StringSetIteratorNext(&it)))
    {
        BufferAppend(buf, element, strlen(element));
        if (pos < size-1)
        {
            BufferAppend(buf, minibuf, sizeof(char));
        }

        pos++;
    }

    return buf;
}

void StringSetAddSplit(StringSet *set, const char *str, char delimiter)
{
    assert(set != NULL);
    if (str) // TODO: remove this inconsistency, add assert(str)
    {
        const char *prev = str;
        const char *cur = str;

        while (*cur != '\0')
        {
            if (*cur == delimiter)
            {
                size_t len = cur - prev;
                if (len > 0)
                {
                    StringSetAdd(set, xstrndup(prev, len));
                }
                else
                {
                    StringSetAdd(set, xstrdup(""));
                }
                prev = cur + 1;
            }

            cur++;
        }

        if (cur > prev)
        {
            StringSetAdd(set, xstrndup(prev, cur - prev));
        }
    }
}

StringSet *StringSetFromString(const char *str, char delimiter)
{
    StringSet *set = StringSetNew();

    StringSetAddSplit(set, str, delimiter);

    return set;
}

JsonElement *StringSetToJson(const StringSet *set)
{
    assert(set != NULL);

    JsonElement *arr = JsonArrayCreate(StringSetSize(set));
    StringSetIterator it = StringSetIteratorInit((StringSet *)set);
    const char *el = NULL;

    while ((el = StringSetIteratorNext(&it)))
    {
        JsonArrayAppendString(arr, el);
    }

    return arr;
}

static bool VisitJsonArrayFirst(ARG_UNUSED JsonElement *element, void *data)
{
    /* We only want one array with items, so just make sure there are no items
     * in the string set yet. This doesn't fail if there is a nested array as
     * the first item in the top-level array, but let's just live with that for
     * the sake of simplicity. */
    StringSet *set = data;
    return (StringSetSize(set) == 0);
}

static bool AddArrayItemToStringSet(JsonElement *element, void *data)
{
    char *element_str = JsonPrimitiveToString(element);
    StringSet *set = data;
    if ((element_str != NULL) && (set != NULL))
    {
        StringSetAdd(set, element_str);
        return true;
    }
    return false;
}

StringSet *JsonArrayToStringSet(const JsonElement *array)
{
    assert(array != NULL);

    if (JsonGetType(array) != JSON_TYPE_ARRAY)
    {
        return NULL;
    }

    StringSet *ret = StringSetNew();

    /* We know our visitor functions don't modify the given array so we can
     * safely type-cast the array to JsonElement* without 'const'. */
    bool success = JsonWalk((JsonElement *) array, JsonErrorVisitor, VisitJsonArrayFirst,
                            AddArrayItemToStringSet, ret);
    if (!success)
    {
        StringSetDestroy(ret);
        return NULL;
    }
    return ret;
}