Open SCAP Library

list.h

00001 /*
00002  * Copyright 2009 Red Hat Inc., Durham, North Carolina.
00003  * All Rights Reserved.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * Authors:
00020  *      Lukas Kuklinek <lkuklinek@redhat.com>
00021  */
00022 
00023 /*
00024  * @file
00025  * @internal
00026  * @{
00027  */
00028 #ifndef OSCAP_LIST_
00029 #define OSCAP_LIST_
00030 
00031 #include <stdlib.h>
00032 #include <stdbool.h>
00033 
00034 #include "util.h"
00035 #include "public/oscap.h"
00036 #include "public/oscap_text.h"
00037 
00038 OSCAP_HIDDEN_START;
00039 
00040 // list item dump function type
00041 typedef void (*oscap_dump_func) ();
00042 // generic comparison function type
00043 typedef bool (*oscap_cmp_func) (void *, void *);
00044 
00045 /*
00046  * Linear linked list.
00047  */
00048 
00049 struct oscap_list_item {
00050         void *data;
00051         struct oscap_list_item *next;
00052 };
00053 
00054 struct oscap_list {
00055         struct oscap_list_item *first;
00056         struct oscap_list_item *last;
00057         size_t itemcount;
00058 };
00059 
00060 // FIXME: SCE engine uses these
00061 OSCAP_HIDDEN_END;
00062 
00063 struct oscap_list *oscap_list_new(void);
00064 void oscap_create_lists(struct oscap_list **first, ...);
00065 bool oscap_list_add(struct oscap_list *list, void *value);
00066 bool oscap_list_push(struct oscap_list *list, void *value);
00067 bool oscap_list_pop(struct oscap_list *list, oscap_destruct_func destructor);
00068 bool oscap_list_remove(struct oscap_list *list, void *value, oscap_cmp_func compare, oscap_destruct_func destructor);
00069 struct oscap_list *oscap_list_clone(const struct oscap_list * list, oscap_clone_func cloner);
00070 void oscap_list_free(struct oscap_list *list, oscap_destruct_func destructor);
00071 void oscap_list_free0(struct oscap_list *list);
00072 void oscap_list_dump(struct oscap_list *list, oscap_dump_func dumper, int depth);
00073 int oscap_list_get_itemcount(struct oscap_list *list);
00074 bool oscap_list_contains(struct oscap_list *list, void *what, oscap_cmp_func compare);
00075 struct oscap_list *oscap_list_destructive_join(struct oscap_list *list1, struct oscap_list *list2);
00076 
00077 OSCAP_HIDDEN_START;
00078 
00079 
00080 /* Linked List iterator. */
00081 
00082 typedef bool(*oscap_filter_func) (void *, void *);
00083 
00084 struct oscap_iterator {
00085         struct oscap_list_item *cur;
00086         struct oscap_list *list;
00087         oscap_filter_func filter;
00088         void *user_data;
00089 };
00090 
00091 // FIXME: SCE engine uses these
00092 OSCAP_HIDDEN_END;
00093 
00094 void *oscap_iterator_new(struct oscap_list *list);
00095 void *oscap_iterator_new_filter(struct oscap_list *list, oscap_filter_func filter, void *user_data);
00096 void *oscap_iterator_next(struct oscap_iterator *it);
00097 size_t oscap_iterator_get_itemcount(const struct oscap_iterator *it);
00098 bool oscap_iterator_has_more(struct oscap_iterator *it);
00099 void oscap_iterator_reset(struct oscap_iterator *it);
00100 void *oscap_iterator_detach(struct oscap_iterator *it);
00101 void oscap_iterator_free(struct oscap_iterator *it);
00102 
00103 OSCAP_HIDDEN_START;
00104 
00105 void *oscap_list_find(struct oscap_list *list, void *what, oscap_cmp_func compare);
00106 
00113 #define OSCAP_FOREACH_GENERIC(itype, vtype, val, init_val, code) \
00114     {                                                            \
00115         struct itype##_iterator *val##_iter = (init_val);        \
00116         vtype val;                                               \
00117         while (itype##_iterator_has_more(val##_iter)) {          \
00118             val = itype##_iterator_next(val##_iter);             \
00119             code                                                 \
00120         }                                                        \
00121         itype##_iterator_free(val##_iter);                       \
00122     }
00123 
00132 #define OSCAP_FOREACH(type, val, init_val, code) \
00133         OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code)
00134 
00146 #define OSCAP_FOR_GENERIC(itype, vtype, val, init_val)                  \
00147     vtype val = NULL; struct itype##_iterator *val##_iter = (init_val); \
00148     while (itype##_iterator_has_more(val##_iter)                        \
00149             ? (val = itype##_iterator_next(val##_iter), true)           \
00150             : (itype##_iterator_free(val##_iter), val##_iter = NULL, false))
00151 
00159 #define OSCAP_FOR(type, val, init_val) OSCAP_FOR_GENERIC(type, struct type *, val, init_val)
00160 
00167 #define OSCAP_FOR_STR(val, init_val) OSCAP_FOR_GENERIC(oscap_string, const char *, val, init_val)
00168 
00169 /*
00170  * Hash table
00171  */
00172 
00173 // Comparison function.
00174 typedef int (*oscap_compare_func) (const char *, const char *);
00175 // Hash table item.
00176 struct oscap_htable_item {
00177         struct oscap_htable_item *next; // Next item.
00178         char *key;              // Item key.
00179         void *value;            // Item value.
00180 };
00181 
00182 // Hash table.
00183 struct oscap_htable {
00184         size_t hsize;           // Size of the hash table.
00185         size_t itemcount;       // Number of elements in the hash table.
00186         struct oscap_htable_item **table;       // The table itself.
00187         oscap_compare_func cmp; // Funcion used to compare keys (e.g. strcmp).
00188 };
00189 
00190 /*
00191  * Create a new hash table.
00192  * @param cmp Pointer to a function used as the key comparator.
00193  * @hsize Size of the hash table.
00194  * @internal
00195  * @return new hash table
00196  */
00197 struct oscap_htable *oscap_htable_new1(oscap_compare_func cmp, size_t hsize);
00198 
00199 /*
00200  * Create a new hash table.
00201  *
00202  * The table will use strcmp() as the comparison function and will have default table size.
00203  * @see oscap_htable_new1()
00204  * @return new hash table
00205  */
00206 struct oscap_htable *oscap_htable_new(void);
00207 
00208 /*
00209  * Do a Deep Copy of a hashtable and all of its items
00210  *
00211  * @return deep copy of hash table
00212  */
00213 struct oscap_htable * oscap_htable_clone(const struct oscap_htable * table, oscap_clone_func cloner);
00214 
00215 /*
00216  * Add an item to the hash table.
00217  * @return True on success, false if the key already exists.
00218  */
00219 bool oscap_htable_add(struct oscap_htable *htable, const char *key, void *item);
00220 
00221 /*
00222  * Get a hash table item.
00223  * @return An item, NULL if item with specified key is not present in the hash table.
00224  */
00225 void *oscap_htable_get(struct oscap_htable *htable, const char *key);
00226 
00227 void *oscap_htable_detach(struct oscap_htable *htable, const char *key);
00228 
00229 void oscap_htable_dump(struct oscap_htable *htable, oscap_dump_func dumper, int depth);
00230 
00231 /*
00232  * Delete the hash table.
00233  * @param htable Hash table to be deleted.
00234  * @param destructor Function used to delete individual items.
00235  */
00236 void oscap_htable_free(struct oscap_htable *htable, oscap_destruct_func destructor);
00237 /*
00238  * Dispose the hash table -- do not dispose individial items.
00239  * @param htable Hash table to be deleted.
00240  */
00241 void oscap_htable_free0(struct oscap_htable *htable);
00242 
00243 
00247 struct oscap_htable_iterator;
00248 
00254 struct oscap_htable_iterator *oscap_htable_iterator_new(struct oscap_htable *htable);
00255 
00261 bool oscap_htable_iterator_has_more(struct oscap_htable_iterator *hit);
00262 
00268 const struct oscap_htable_item *oscap_htable_iterator_next(struct oscap_htable_iterator *hit);
00269 
00275 const char *oscap_htable_iterator_next_key(struct oscap_htable_iterator *hit);
00276 
00282 void *oscap_htable_iterator_next_value(struct oscap_htable_iterator *hit);
00283 
00291 void oscap_htable_iterator_next_kv(struct oscap_htable_iterator *hit, const char **key, void **value);
00292 
00297 void oscap_htable_iterator_reset(struct oscap_htable_iterator *hit);
00298 
00303 void oscap_htable_iterator_free(struct oscap_htable_iterator *hit);
00304 
00305 void oscap_print_depth(int depth);
00306 
00307 OSCAP_HIDDEN_END;
00308 
00309 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines