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 
37 OSCAP_HIDDEN_START;
38 
39 // list item dump function type
40 typedef void (*oscap_dump_func) ();
41 // generic comparison function type
42 typedef bool (*oscap_cmp_func) (void *, void *);
43 
44 /*
45  * Linear linked list.
46  */
47 
49  void *data;
50  struct oscap_list_item *next;
51 };
52 
53 struct oscap_list {
54  struct oscap_list_item *first;
55  struct oscap_list_item *last;
56  size_t itemcount;
57 };
58 
59 struct oscap_list *oscap_list_new(void);
60 void oscap_create_lists(struct oscap_list **first, ...);
61 bool oscap_list_add(struct oscap_list *list, void *value);
62 bool oscap_list_push(struct oscap_list *list, void *value);
63 bool oscap_list_pop(struct oscap_list *list, oscap_destruct_func destructor);
64 struct oscap_list *oscap_list_clone(const struct oscap_list * list, oscap_clone_func cloner);
65 void oscap_list_free(struct oscap_list *list, oscap_destruct_func destructor);
66 void oscap_list_free0(struct oscap_list *list);
67 void oscap_list_dump(struct oscap_list *list, oscap_dump_func dumper, int depth);
68 int oscap_list_get_itemcount(struct oscap_list *list);
69 bool oscap_list_contains(struct oscap_list *list, void *what, oscap_cmp_func compare);
70 struct oscap_list *oscap_list_destructive_join(struct oscap_list *list1, struct oscap_list *list2);
71 
72 
73 /* Linked List iterator. */
74 
75 typedef bool(*oscap_filter_func) (void *, void *);
76 
78  struct oscap_list_item *cur;
79  struct oscap_list *list;
80  oscap_filter_func filter;
81  void *user_data;
82 };
83 
84 void *oscap_iterator_new(struct oscap_list *list);
85 void *oscap_iterator_new_filter(struct oscap_list *list, oscap_filter_func filter, void *user_data);
86 void *oscap_iterator_next(struct oscap_iterator *it);
87 size_t oscap_iterator_get_itemcount(const struct oscap_iterator *it);
88 bool oscap_iterator_has_more(struct oscap_iterator *it);
89 void oscap_iterator_reset(struct oscap_iterator *it);
90 void *oscap_iterator_detach(struct oscap_iterator *it);
91 void oscap_iterator_free(struct oscap_iterator *it);
92 
93 void *oscap_list_find(struct oscap_list *list, void *what, oscap_cmp_func compare);
94 
95 /*
96  * Hash table
97  */
98 
99 // Comparison function.
100 typedef int (*oscap_compare_func) (const char *, const char *);
101 // Hash table item.
103  struct oscap_htable_item *next; // Next item.
104  char *key; // Item key.
105  void *value; // Item value.
106 };
107 
108 // Hash table.
109 struct oscap_htable {
110  size_t hsize; // Size of the hash table.
111  size_t itemcount; // Number of elements in the hash table.
112  struct oscap_htable_item **table; // The table itself.
113  oscap_compare_func cmp; // Funcion used to compare keys (e.g. strcmp).
114 };
115 
116 /*
117  * Create a new hash table.
118  * @param cmp Pointer to a function used as the key comparator.
119  * @hsize Size of the hash table.
120  * @internal
121  * @return new hash table
122  */
123 struct oscap_htable *oscap_htable_new1(oscap_compare_func cmp, size_t hsize);
124 
125 /*
126  * Create a new hash table.
127  *
128  * The table will use strcmp() as the comparison function and will have default table size.
129  * @see oscap_htable_new1()
130  * @return new hash table
131  */
132 struct oscap_htable *oscap_htable_new(void);
133 
134 /*
135  * Do a Deep Copy of a hashtable and all of its items
136  *
137  * @return deep copy of hash table
138  */
139 struct oscap_htable * oscap_htable_clone(const struct oscap_htable * table, oscap_clone_func cloner);
140 
141 /*
142  * Add an item to the hash table.
143  * @return True on success, false if the key already exists.
144  */
145 bool oscap_htable_add(struct oscap_htable *htable, const char *key, void *item);
146 
147 /*
148  * Get a hash table item.
149  * @return An item, NULL if item with specified key is not present in the hash table.
150  */
151 void *oscap_htable_get(struct oscap_htable *htable, const char *key);
152 
153 void *oscap_htable_detach(struct oscap_htable *htable, const char *key);
154 
155 void oscap_htable_dump(struct oscap_htable *htable, oscap_dump_func dumper, int depth);
156 
157 /*
158  * Delete the hash table.
159  * @param htable Hash table to be deleted.
160  * @param destructor Function used to delete individual items.
161  */
162 void oscap_htable_free(struct oscap_htable *htable, oscap_destruct_func destructor);
163 
164 void oscap_print_depth(int depth);
165 
166 OSCAP_HIDDEN_END;
167 
168 #endif