Open SCAP Library

list.h
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <lkuklinek@redhat.com>
21 */
22
23 /*
24 * @file
25 * @internal
26 * @{
27 */
28 #ifndef OSCAP_LIST_
29 #define OSCAP_LIST_
30
31 #include <stdlib.h>
32 #include <stdbool.h>
33
34 #include "util.h"
35 #include "public/oscap.h"
36 #include "public/oscap_text.h"
37
38 OSCAP_HIDDEN_START;
39
40 // list item dump function type
41 typedef void (*oscap_dump_func) ();
42 // generic comparison function type
43 typedef bool (*oscap_cmp_func) (void *, void *);
44
45 /*
46 * Linear linked list.
47 */
48
49 struct oscap_list_item {
50 void *data;
51 struct oscap_list_item *next;
52 };
53
54 struct oscap_list {
55 struct oscap_list_item *first;
56 struct oscap_list_item *last;
57 size_t itemcount;
58 };
59
60 // FIXME: SCE engine uses these
61 OSCAP_HIDDEN_END;
62
63 struct oscap_list *oscap_list_new(void);
64 void oscap_create_lists(struct oscap_list **first, ...);
65 bool oscap_list_add(struct oscap_list *list, void *value);
66 bool oscap_list_push(struct oscap_list *list, void *value);
67 bool oscap_list_pop(struct oscap_list *list, oscap_destruct_func destructor);
68 bool oscap_list_remove(struct oscap_list *list, void *value, oscap_cmp_func compare, oscap_destruct_func destructor);
69 struct oscap_list *oscap_list_clone(const struct oscap_list * list, oscap_clone_func cloner);
70 void oscap_list_free(struct oscap_list *list, oscap_destruct_func destructor);
71 void oscap_list_free0(struct oscap_list *list);
72 void oscap_list_dump(struct oscap_list *list, oscap_dump_func dumper, int depth);
73 int oscap_list_get_itemcount(struct oscap_list *list);
74 bool oscap_list_contains(struct oscap_list *list, void *what, oscap_cmp_func compare);
75 struct oscap_list *oscap_list_destructive_join(struct oscap_list *list1, struct oscap_list *list2);
76
77 OSCAP_HIDDEN_START;
78
79
80 /* Linked List iterator. */
81
82 typedef bool(*oscap_filter_func) (void *, void *);
83
84 struct oscap_iterator {
85 struct oscap_list_item *cur;
86 struct oscap_list *list;
87 oscap_filter_func filter;
88 void *user_data;
89 };
90
91 // FIXME: SCE engine uses these
92 OSCAP_HIDDEN_END;
93
94 void *oscap_iterator_new(struct oscap_list *list);
95 void *oscap_iterator_new_filter(struct oscap_list *list, oscap_filter_func filter, void *user_data);
96 void *oscap_iterator_next(struct oscap_iterator *it);
97 size_t oscap_iterator_get_itemcount(const struct oscap_iterator *it);
98 bool oscap_iterator_has_more(struct oscap_iterator *it);
99 void oscap_iterator_reset(struct oscap_iterator *it);
100 void *oscap_iterator_detach(struct oscap_iterator *it);
101 void oscap_iterator_free(struct oscap_iterator *it);
102
103 OSCAP_HIDDEN_START;
104
105 void *oscap_list_find(struct oscap_list *list, void *what, oscap_cmp_func compare);
106
113 #define OSCAP_FOREACH_GENERIC(itype, vtype, val, init_val, code) \
114 { \
115 struct itype##_iterator *val##_iter = (init_val); \
116 vtype val; \
117 while (itype##_iterator_has_more(val##_iter)) { \
118 val = itype##_iterator_next(val##_iter); \
119 code \
120 } \
121 itype##_iterator_free(val##_iter); \
122 }
123
132 #define OSCAP_FOREACH(type, val, init_val, code) \
133 OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code)
134
146 #define OSCAP_FOR_GENERIC(itype, vtype, val, init_val) \
147 vtype val = NULL; struct itype##_iterator *val##_iter = (init_val); \
148 while (itype##_iterator_has_more(val##_iter) \
149 ? (val = itype##_iterator_next(val##_iter), true) \
150 : (itype##_iterator_free(val##_iter), val##_iter = NULL, false))
151
159 #define OSCAP_FOR(type, val, init_val) OSCAP_FOR_GENERIC(type, struct type *, val, init_val)
160
167 #define OSCAP_FOR_STR(val, init_val) OSCAP_FOR_GENERIC(oscap_string, const char *, val, init_val)
168
169 /*
170 * Hash table
171 */
172
173 // Comparison function.
174 typedef int (*oscap_compare_func) (const char *, const char *);
175 // Hash table item.
176 struct oscap_htable_item {
177 struct oscap_htable_item *next; // Next item.
178 char *key; // Item key.
179 void *value; // Item value.
180 };
181
182 // Hash table.
183 struct oscap_htable {
184 size_t hsize; // Size of the hash table.
185 size_t itemcount; // Number of elements in the hash table.
186 struct oscap_htable_item **table; // The table itself.
187 oscap_compare_func cmp; // Funcion used to compare keys (e.g. strcmp).
188 };
189
190 /*
191 * Create a new hash table.
192 * @param cmp Pointer to a function used as the key comparator.
193 * @hsize Size of the hash table.
194 * @internal
195 * @return new hash table
196 */
197 struct oscap_htable *oscap_htable_new1(oscap_compare_func cmp, size_t hsize);
198
199 /*
200 * Create a new hash table.
201 *
202 * The table will use strcmp() as the comparison function and will have default table size.
203 * @see oscap_htable_new1()
204 * @return new hash table
205 */
206 struct oscap_htable *oscap_htable_new(void);
207
208 /*
209 * Do a Deep Copy of a hashtable and all of its items
210 *
211 * @return deep copy of hash table
212 */
213 struct oscap_htable * oscap_htable_clone(const struct oscap_htable * table, oscap_clone_func cloner);
214
215 /*
216 * Add an item to the hash table.
217 * @return True on success, false if the key already exists.
218 */
219 bool oscap_htable_add(struct oscap_htable *htable, const char *key, void *item);
220
221 /*
222 * Get a hash table item.
223 * @return An item, NULL if item with specified key is not present in the hash table.
224 */
225 void *oscap_htable_get(struct oscap_htable *htable, const char *key);
226
227 void *oscap_htable_detach(struct oscap_htable *htable, const char *key);
228
229 void oscap_htable_dump(struct oscap_htable *htable, oscap_dump_func dumper, int depth);
230
231 /*
232 * Delete the hash table.
233 * @param htable Hash table to be deleted.
234 * @param destructor Function used to delete individual items.
235 */
236 void oscap_htable_free(struct oscap_htable *htable, oscap_destruct_func destructor);
237 /*
238 * Dispose the hash table -- do not dispose individial items.
239 * @param htable Hash table to be deleted.
240 */
241 void oscap_htable_free0(struct oscap_htable *htable);
242
243
247 struct oscap_htable_iterator;
248
254 struct oscap_htable_iterator *oscap_htable_iterator_new(struct oscap_htable *htable);
255
261 bool oscap_htable_iterator_has_more(struct oscap_htable_iterator *hit);
262
268 const struct oscap_htable_item *oscap_htable_iterator_next(struct oscap_htable_iterator *hit);
269
275 const char *oscap_htable_iterator_next_key(struct oscap_htable_iterator *hit);
276
282 void *oscap_htable_iterator_next_value(struct oscap_htable_iterator *hit);
283
291 void oscap_htable_iterator_next_kv(struct oscap_htable_iterator *hit, const char **key, void **value);
292
297 void oscap_htable_iterator_reset(struct oscap_htable_iterator *hit);
298
303 void oscap_htable_iterator_free(struct oscap_htable_iterator *hit);
304
305 void oscap_print_depth(int depth);
306
307 OSCAP_HIDDEN_END;
308
309 #endif
oscap.h
General OpenScap functions and types.
oscap_htable_iterator
Definition: list.c:548
oscap_htable_item
Definition: list.h:176
oscap_iterator
Definition: list.h:84
oscap_list
Definition: list.h:54
oscap_text.h
Multilingual text processing interface.
oscap_htable
Definition: list.h:183
oscap_list_item
Definition: list.h:49