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
|
/*
Copyright (C) CFEngine AS
This file is part of CFEngine 3 - written and maintained by CFEngine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
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 <set.h>
#include <alloc.h>
#include <string_lib.h>
#include <buffer.h>
TYPED_SET_DEFINE(String, char *, (MapHashFn)&StringHash, (MapKeyEqualFn)&StringSafeEqual, &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)
{
MapInsert(set, element, element);
}
bool SetContains(const Set *set, const void *element)
{
return MapHasKey(set, element);
}
bool SetRemove(Set *set, const void *element)
{
return MapRemove(set, element);
}
void SetClear(Set *set)
{
MapClear(set);
}
size_t SetSize(const Set *set)
{
return MapSize(set);
}
bool SetIsEqual(const Set *set1, const Set *set2)
{
return MapContainsSameKeys(set1, set2);
}
SetIterator SetIteratorInit(Set *set)
{
return MapIteratorInit(set);
}
void *SetIteratorNext(SetIterator *i)
{
MapKeyValue *kv = MapIteratorNext(i);
return kv ? kv->key : NULL;
}
Buffer *StringSetToBuffer(StringSet *set, const char delimiter)
{
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)
{
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)
{
JsonElement *arr = JsonArrayCreate(StringSetSize(set));
StringSetIterator it = StringSetIteratorInit((StringSet *)set);
const char *el = NULL;
while ((el = StringSetIteratorNext(&it)))
{
JsonArrayAppendString(arr, el);
}
return arr;
}
|