cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

set.c
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #include <platform.h>
26 #include <set.h>
27 #include <string.h> // strlen()
28 
29 #include <alloc.h>
30 #include <string_lib.h>
31 #include <buffer.h>
32 
33 TYPED_SET_DEFINE(String, char *,
35 
36 Set *SetNew(MapHashFn element_hash_fn,
37  MapKeyEqualFn element_equal_fn,
38  MapDestroyDataFn element_destroy_fn)
39 {
40  return MapNew(element_hash_fn, element_equal_fn, element_destroy_fn, NULL);
41 }
42 
43 void SetDestroy(Set *set)
44 {
45  MapDestroy(set);
46 }
47 
48 void SetAdd(Set *set, void *element)
49 {
50  assert(set != NULL);
51  MapInsert(set, element, element);
52 }
53 
54 bool SetContains(const Set *set, const void *element)
55 {
56  assert(set != NULL);
57  return MapHasKey(set, element);
58 }
59 
60 bool SetRemove(Set *set, const void *element)
61 {
62  assert(set != NULL);
63  return MapRemove(set, element);
64 }
65 
66 void SetClear(Set *set)
67 {
68  assert(set != NULL);
69  MapClear(set);
70 }
71 
72 size_t SetSize(const Set *set)
73 {
74  assert(set != NULL);
75  return MapSize(set);
76 }
77 
78 bool SetIsEqual(const Set *set1, const Set *set2)
79 {
80  assert(set1 != NULL);
81  assert(set2 != NULL);
82  return MapContainsSameKeys(set1, set2);
83 }
84 
86 {
87  assert(set != NULL);
88  return MapIteratorInit(set);
89 }
90 
92 {
94  return kv ? kv->key : NULL;
95 }
96 
97 void SetJoin(Set *set, Set *otherset, SetElementCopyFn copy_function)
98 {
99  assert(set != NULL);
100  assert(otherset != NULL);
101  if (set == otherset)
102  return;
103 
104  SetIterator si = SetIteratorInit(otherset);
105  void *ptr = NULL;
106 
107  for (ptr = SetIteratorNext(&si); ptr != NULL; ptr = SetIteratorNext(&si))
108  {
109  if (copy_function != NULL)
110  {
111  ptr = copy_function(ptr);
112  }
113  SetAdd(set, ptr);
114  }
115 }
116 
117 Buffer *StringSetToBuffer(StringSet *set, const char delimiter)
118 {
119  assert(set != NULL);
120 
121  Buffer *buf = BufferNew();
123  const char *element = NULL;
124  int pos = 0;
125  int size = StringSetSize(set);
126  char minibuf[2];
127 
128  minibuf[0] = delimiter;
129  minibuf[1] = '\0';
130 
131  while ((element = StringSetIteratorNext(&it)))
132  {
133  BufferAppend(buf, element, strlen(element));
134  if (pos < size-1)
135  {
136  BufferAppend(buf, minibuf, sizeof(char));
137  }
138 
139  pos++;
140  }
141 
142  return buf;
143 }
144 
145 void StringSetAddSplit(StringSet *set, const char *str, char delimiter)
146 {
147  assert(set != NULL);
148  if (str) // TODO: remove this inconsistency, add assert(str)
149  {
150  const char *prev = str;
151  const char *cur = str;
152 
153  while (*cur != '\0')
154  {
155  if (*cur == delimiter)
156  {
157  size_t len = cur - prev;
158  if (len > 0)
159  {
160  StringSetAdd(set, xstrndup(prev, len));
161  }
162  else
163  {
164  StringSetAdd(set, xstrdup(""));
165  }
166  prev = cur + 1;
167  }
168 
169  cur++;
170  }
171 
172  if (cur > prev)
173  {
174  StringSetAdd(set, xstrndup(prev, cur - prev));
175  }
176  }
177 }
178 
179 StringSet *StringSetFromString(const char *str, char delimiter)
180 {
181  StringSet *set = StringSetNew();
182 
183  StringSetAddSplit(set, str, delimiter);
184 
185  return set;
186 }
187 
189 {
190  assert(set != NULL);
191 
194  const char *el = NULL;
195 
196  while ((el = StringSetIteratorNext(&it)))
197  {
198  JsonArrayAppendString(arr, el);
199  }
200 
201  return arr;
202 }
char * xstrdup(const char *str)
Definition: alloc-mini.c:56
char * xstrndup(const char *str, size_t n)
Definition: alloc.c:61
Buffer * BufferNew(void)
Buffer initialization routine.
Definition: buffer.c:48
void BufferAppend(Buffer *buffer, const char *bytes, unsigned int length)
Definition: buffer.c:269
void free(void *)
#define NULL
Definition: getopt1.c:56
JsonElement * JsonArrayCreate(const size_t initialCapacity)
Create a new JSON array.
Definition: json.c:1281
void JsonArrayAppendString(JsonElement *const array, const char *const value)
Append a string to an array.
Definition: json.c:1287
MapIterator MapIteratorInit(Map *map)
Definition: map.c:325
bool MapRemove(Map *map, const void *key)
Definition: map.c:226
bool MapContainsSameKeys(const Map *map1, const Map *map2)
Definition: map.c:286
Map * MapNew(MapHashFn hash_fn, MapKeyEqualFn equal_fn, MapDestroyDataFn destroy_key_fn, MapDestroyDataFn destroy_value_fn)
Definition: map.c:81
size_t MapSize(const Map *map)
Definition: map.c:112
bool MapInsert(Map *map, void *key, void *value)
Definition: map.c:161
MapKeyValue * MapIteratorNext(MapIterator *i)
Definition: map.c:343
void MapClear(Map *map)
Definition: map.c:240
bool MapHasKey(const Map *map, const void *key)
Definition: map.c:203
void MapDestroy(Map *map)
Definition: map.c:270
void(* MapDestroyDataFn)(void *key)
Definition: map_common.h:41
bool(* MapKeyEqualFn)(const void *key1, const void *key2)
Definition: map_common.h:40
unsigned int(* MapHashFn)(const void *p, unsigned int seed)
Definition: map_common.h:39
JsonElement * StringSetToJson(const StringSet *set)
Definition: set.c:188
SetIterator SetIteratorInit(Set *set)
Definition: set.c:85
Set * SetNew(MapHashFn element_hash_fn, MapKeyEqualFn element_equal_fn, MapDestroyDataFn element_destroy_fn)
Definition: set.c:36
void SetClear(Set *set)
Definition: set.c:66
StringSetIterator StringSetIteratorInit(StringSet *set)
Definition: set.c:34
StringSet * StringSetFromString(const char *str, char delimiter)
Definition: set.c:179
size_t StringSetSize(const StringSet *set)
Definition: set.c:34
bool SetContains(const Set *set, const void *element)
Definition: set.c:54
Buffer * StringSetToBuffer(StringSet *set, const char delimiter)
Definition: set.c:117
StringSet * StringSetNew(void)
Definition: set.c:34
void SetAdd(Set *set, void *element)
Definition: set.c:48
void StringSetAddSplit(StringSet *set, const char *str, char delimiter)
Definition: set.c:145
bool SetRemove(Set *set, const void *element)
Definition: set.c:60
size_t SetSize(const Set *set)
Definition: set.c:72
void SetJoin(Set *set, Set *otherset, SetElementCopyFn copy_function)
Definition: set.c:97
void * SetIteratorNext(SetIterator *i)
Definition: set.c:91
void StringSetAdd(const StringSet *set, char *element)
Definition: set.c:34
void SetDestroy(Set *set)
Definition: set.c:43
char * StringSetIteratorNext(StringSetIterator *iter)
Definition: set.c:34
bool SetIsEqual(const Set *set1, const Set *set2)
Definition: set.c:78
#define TYPED_SET_DEFINE(Prefix, ElementType, hash_fn, equal_fn, destroy_fn)
Definition: set.h:74
void *(* SetElementCopyFn)(const void *)
Definition: set.h:34
bool StringEqual_untyped(const void *a, const void *b)
Definition: string_lib.c:306
unsigned int StringHash_untyped(const void *str, unsigned int seed)
Definition: string_lib.c:114
Definition: buffer.h:50
void * key
Definition: map_common.h:35
Definition: map.c:46
Definition: set.h:138