Open SCAP Library: list.h Source File

Open SCAP Library

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

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 struct oscap_list *oscap_list_clone(const struct oscap_list * list, oscap_clone_func cloner);
69 void oscap_list_free(struct oscap_list *list, oscap_destruct_func destructor);
70 void oscap_list_free0(struct oscap_list *list);
71 void oscap_list_dump(struct oscap_list *list, oscap_dump_func dumper, int depth);
72 int oscap_list_get_itemcount(struct oscap_list *list);
73 bool oscap_list_contains(struct oscap_list *list, void *what, oscap_cmp_func compare);
74 struct oscap_list *oscap_list_destructive_join(struct oscap_list *list1, struct oscap_list *list2);
75
76 OSCAP_HIDDEN_START;
77
78
79 /* Linked List iterator. */
80
81 typedef bool(*oscap_filter_func) (void *, void *);
82
83 struct oscap_iterator {
84 struct oscap_list_item *cur;
85 struct oscap_list *list;
86 oscap_filter_func filter;
87 void *user_data;
88 };
89
90 // FIXME: SCE engine uses these
91 OSCAP_HIDDEN_END;
92
93 void *oscap_iterator_new(struct oscap_list *list);
94 void *oscap_iterator_new_filter(struct oscap_list *list, oscap_filter_func filter, void *user_data);
95 void *oscap_iterator_next(struct oscap_iterator *it);
96 size_t oscap_iterator_get_itemcount(const struct oscap_iterator *it);
97 bool oscap_iterator_has_more(struct oscap_iterator *it);
98 void oscap_iterator_reset(struct oscap_iterator *it);
99 void *oscap_iterator_detach(struct oscap_iterator *it);
100 void oscap_iterator_free(struct oscap_iterator *it);
101
102 OSCAP_HIDDEN_START;
103
104 void *oscap_list_find(struct oscap_list *list, void *what, oscap_cmp_func compare);
105
112 #define OSCAP_FOREACH_GENERIC(itype, vtype, val, init_val, code) \
113 { \
114 struct itype##_iterator *val##_iter = (init_val); \
115 vtype val; \
116 while (itype##_iterator_has_more(val##_iter)) { \
117 val = itype##_iterator_next(val##_iter); \
118 code \
119 } \
120 itype##_iterator_free(val##_iter); \
121 }
122
131 #define OSCAP_FOREACH(type, val, init_val, code) \
132 OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code)
133
145 #define OSCAP_FOR_GENERIC(itype, vtype, val, init_val) \
146 vtype val = NULL; struct itype##_iterator *val##_iter = (init_val); \
147 while (itype##_iterator_has_more(val##_iter) \
148 ? (val = itype##_iterator_next(val##_iter), true) \
149 : (itype##_iterator_free(val##_iter), val##_iter = NULL, false))
150
158 #define OSCAP_FOR(type, val, init_val) OSCAP_FOR_GENERIC(type, struct type *, val, init_val)
159
166 #define OSCAP_FOR_STR(val, init_val) OSCAP_FOR_GENERIC(oscap_string, const char *, val, init_val)
167
168 /*
169 * Hash table
170 */
171
172 // Comparison function.
173 typedef int (*oscap_compare_func) (const char *, const char *);
174 // Hash table item.
175 struct oscap_htable_item {
176 struct oscap_htable_item *next; // Next item.
177 char *key; // Item key.
178 void *value; // Item value.
179 };
180
181 // Hash table.
182 struct oscap_htable {
183 size_t hsize; // Size of the hash table.
184 size_t itemcount; // Number of elements in the hash table.
185 struct oscap_htable_item **table; // The table itself.
186 oscap_compare_func cmp; // Funcion used to compare keys (e.g. strcmp).
187 };
188
189 /*
190 * Create a new hash table.
191 * @param cmp Pointer to a function used as the key comparator.
192 * @hsize Size of the hash table.
193 * @internal
194 * @return new hash table
195 */
196 struct oscap_htable *oscap_htable_new1(oscap_compare_func cmp, size_t hsize);
197
198 /*
199 * Create a new hash table.
200 *
201 * The table will use strcmp() as the comparison function and will have default table size.
202 * @see oscap_htable_new1()
203 * @return new hash table
204 */
205 struct oscap_htable *oscap_htable_new(void);
206
207 /*
208 * Do a Deep Copy of a hashtable and all of its items
209 *
210 * @return deep copy of hash table
211 */
212 struct oscap_htable * oscap_htable_clone(const struct oscap_htable * table, oscap_clone_func cloner);
213
214 /*
215 * Add an item to the hash table.
216 * @return True on success, false if the key already exists.
217 */
218 bool oscap_htable_add(struct oscap_htable *htable, const char *key, void *item);
219
220 /*
221 * Get a hash table item.
222 * @return An item, NULL if item with specified key is not present in the hash table.
223 */
224 void *oscap_htable_get(struct oscap_htable *htable, const char *key);
225
226 void *oscap_htable_detach(struct oscap_htable *htable, const char *key);
227
228 void oscap_htable_dump(struct oscap_htable *htable, oscap_dump_func dumper, int depth);
229
230 /*
231 * Delete the hash table.
232 * @param htable Hash table to be deleted.
233 * @param destructor Function used to delete individual items.
234 */
235 void oscap_htable_free(struct oscap_htable *htable, oscap_destruct_func destructor);
236 /*
237 * Dispose the hash table -- do not dispose individial items.
238 * @param htable Hash table to be deleted.
239 */
240 void oscap_htable_free0(struct oscap_htable *htable);
241
242
246 struct oscap_htable_iterator;
247
253 struct oscap_htable_iterator *oscap_htable_iterator_new(struct oscap_htable *htable);
254
260 bool oscap_htable_iterator_has_more(struct oscap_htable_iterator *hit);
261
267 const struct oscap_htable_item *oscap_htable_iterator_next(struct oscap_htable_iterator *hit);
268
274 const char *oscap_htable_iterator_next_key(struct oscap_htable_iterator *hit);
275
281 void *oscap_htable_iterator_next_value(struct oscap_htable_iterator *hit);
282
290 void oscap_htable_iterator_next_kv(struct oscap_htable_iterator *hit, const char **key, void **value);
291
296 void oscap_htable_iterator_reset(struct oscap_htable_iterator *hit);
297
302 void oscap_htable_iterator_free(struct oscap_htable_iterator *hit);
303
304 void oscap_print_depth(int depth);
305
306 OSCAP_HIDDEN_END;
307
308 #endif