Open SCAP Library
|
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 00037 OSCAP_HIDDEN_START; 00038 00039 // list item dump function type 00040 typedef void (*oscap_dump_func) (); 00041 // generic comparison function type 00042 typedef bool (*oscap_cmp_func) (void *, void *); 00043 00044 /* 00045 * Linear linked list. 00046 */ 00047 00048 struct oscap_list_item { 00049 void *data; 00050 struct oscap_list_item *next; 00051 }; 00052 00053 struct oscap_list { 00054 struct oscap_list_item *first; 00055 struct oscap_list_item *last; 00056 size_t itemcount; 00057 }; 00058 00059 struct oscap_list *oscap_list_new(void); 00060 void oscap_create_lists(struct oscap_list **first, ...); 00061 bool oscap_list_add(struct oscap_list *list, void *value); 00062 bool oscap_list_push(struct oscap_list *list, void *value); 00063 bool oscap_list_pop(struct oscap_list *list, oscap_destruct_func destructor); 00064 struct oscap_list *oscap_list_clone(const struct oscap_list * list, oscap_clone_func cloner); 00065 void oscap_list_free(struct oscap_list *list, oscap_destruct_func destructor); 00066 void oscap_list_free0(struct oscap_list *list); 00067 void oscap_list_dump(struct oscap_list *list, oscap_dump_func dumper, int depth); 00068 int oscap_list_get_itemcount(struct oscap_list *list); 00069 bool oscap_list_contains(struct oscap_list *list, void *what, oscap_cmp_func compare); 00070 struct oscap_list *oscap_list_destructive_join(struct oscap_list *list1, struct oscap_list *list2); 00071 00072 00073 /* Linked List iterator. */ 00074 00075 typedef bool(*oscap_filter_func) (void *, void *); 00076 00077 struct oscap_iterator { 00078 struct oscap_list_item *cur; 00079 struct oscap_list *list; 00080 oscap_filter_func filter; 00081 void *user_data; 00082 }; 00083 00084 void *oscap_iterator_new(struct oscap_list *list); 00085 void *oscap_iterator_new_filter(struct oscap_list *list, oscap_filter_func filter, void *user_data); 00086 void *oscap_iterator_next(struct oscap_iterator *it); 00087 size_t oscap_iterator_get_itemcount(const struct oscap_iterator *it); 00088 bool oscap_iterator_has_more(struct oscap_iterator *it); 00089 void oscap_iterator_reset(struct oscap_iterator *it); 00090 void *oscap_iterator_detach(struct oscap_iterator *it); 00091 void oscap_iterator_free(struct oscap_iterator *it); 00092 00093 void *oscap_list_find(struct oscap_list *list, void *what, oscap_cmp_func compare); 00094 00095 /* 00096 * Hash table 00097 */ 00098 00099 // Comparison function. 00100 typedef int (*oscap_compare_func) (const char *, const char *); 00101 // Hash table item. 00102 struct oscap_htable_item { 00103 struct oscap_htable_item *next; // Next item. 00104 char *key; // Item key. 00105 void *value; // Item value. 00106 }; 00107 00108 // Hash table. 00109 struct oscap_htable { 00110 size_t hsize; // Size of the hash table. 00111 size_t itemcount; // Number of elements in the hash table. 00112 struct oscap_htable_item **table; // The table itself. 00113 oscap_compare_func cmp; // Funcion used to compare keys (e.g. strcmp). 00114 }; 00115 00116 /* 00117 * Create a new hash table. 00118 * @param cmp Pointer to a function used as the key comparator. 00119 * @hsize Size of the hash table. 00120 * @internal 00121 * @return new hash table 00122 */ 00123 struct oscap_htable *oscap_htable_new1(oscap_compare_func cmp, size_t hsize); 00124 00125 /* 00126 * Create a new hash table. 00127 * 00128 * The table will use strcmp() as the comparison function and will have default table size. 00129 * @see oscap_htable_new1() 00130 * @return new hash table 00131 */ 00132 struct oscap_htable *oscap_htable_new(void); 00133 00134 /* 00135 * Do a Deep Copy of a hashtable and all of its items 00136 * 00137 * @return deep copy of hash table 00138 */ 00139 struct oscap_htable * oscap_htable_clone(const struct oscap_htable * table, oscap_clone_func cloner); 00140 00141 /* 00142 * Add an item to the hash table. 00143 * @return True on success, false if the key already exists. 00144 */ 00145 bool oscap_htable_add(struct oscap_htable *htable, const char *key, void *item); 00146 00147 /* 00148 * Get a hash table item. 00149 * @return An item, NULL if item with specified key is not present in the hash table. 00150 */ 00151 void *oscap_htable_get(struct oscap_htable *htable, const char *key); 00152 00153 void *oscap_htable_detach(struct oscap_htable *htable, const char *key); 00154 00155 void oscap_htable_dump(struct oscap_htable *htable, oscap_dump_func dumper, int depth); 00156 00157 /* 00158 * Delete the hash table. 00159 * @param htable Hash table to be deleted. 00160 * @param destructor Function used to delete individual items. 00161 */ 00162 void oscap_htable_free(struct oscap_htable *htable, oscap_destruct_func destructor); 00163 00164 void oscap_print_depth(int depth); 00165 00166 OSCAP_HIDDEN_END; 00167 00168 #endif