git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@2077 77e5149b-7576-45b1-b177-96237e5ba77b
Nigel Horne authored on 2006/07/14 16:46:02... | ... |
@@ -1,3 +1,7 @@ |
1 |
+Fri Jul 14 08:45:04 BST 2006 (njh) |
|
2 |
+---------------------------------- |
|
3 |
+ * libclamav/table: Added helper routines to delete items |
|
4 |
+ |
|
1 | 5 |
Wed Jul 12 16:37:44 BST 2006 (njh) |
2 | 6 |
---------------------------------- |
3 | 7 |
* clamav-milter: Added DONT_SCAN_BLACK_HOLES (off by default) |
... | ... |
@@ -57,7 +57,8 @@ tableDestroy(table_t *table) |
57 | 57 |
|
58 | 58 |
assert(tableItem->key != NULL); |
59 | 59 |
|
60 |
- free(tableItem->key); |
|
60 |
+ if(tableItem->key) |
|
61 |
+ free(tableItem->key); |
|
61 | 62 |
free(tableItem); |
62 | 63 |
|
63 | 64 |
tableItem = tableNext; |
... | ... |
@@ -79,6 +80,23 @@ tableInsert(table_t *table, const char *key, int value) |
79 | 79 |
|
80 | 80 |
assert(value != -1); /* that would confuse us */ |
81 | 81 |
|
82 |
+ /* |
|
83 |
+ * Re-use deleted items |
|
84 |
+ */ |
|
85 |
+ if(table->flags&TABLE_HAS_DELETED_ENTRIES) { |
|
86 |
+ tableEntry *tableItem; |
|
87 |
+ |
|
88 |
+ for(tableItem = table->tableHead; tableItem; tableItem = tableItem->next) |
|
89 |
+ if(tableItem->key == NULL) { |
|
90 |
+ /* This item has been deleted */ |
|
91 |
+ tableItem->key = strdup(key); |
|
92 |
+ tableItem->value = value; |
|
93 |
+ return value; |
|
94 |
+ } |
|
95 |
+ |
|
96 |
+ table->flags &= ~TABLE_HAS_DELETED_ENTRIES; |
|
97 |
+ } |
|
98 |
+ |
|
82 | 99 |
if(table->tableHead == NULL) |
83 | 100 |
table->tableLast = table->tableHead = (tableEntry *)cli_malloc(sizeof(tableEntry)); |
84 | 101 |
else |
... | ... |
@@ -161,3 +179,47 @@ tableUpdate(table_t *table, const char *key, int new_value) |
161 | 161 |
/* not found */ |
162 | 162 |
return tableInsert(table, key, new_value); |
163 | 163 |
} |
164 |
+ |
|
165 |
+/* |
|
166 |
+ * Remove an item from the table |
|
167 |
+ */ |
|
168 |
+void |
|
169 |
+tableRemove(table_t *table, const char *key) |
|
170 |
+{ |
|
171 |
+ tableEntry *tableItem; |
|
172 |
+ |
|
173 |
+ assert(table != NULL); |
|
174 |
+ |
|
175 |
+ if(key == NULL) |
|
176 |
+ return; /* not treated as a fatal error */ |
|
177 |
+ |
|
178 |
+ if(table->tableHead == NULL) |
|
179 |
+ /* not populated yet */ |
|
180 |
+ return; |
|
181 |
+ |
|
182 |
+ for(tableItem = table->tableHead; tableItem; tableItem = tableItem->next) |
|
183 |
+ if(strcasecmp(tableItem->key, key) == 0) { |
|
184 |
+ free(tableItem->key); |
|
185 |
+ tableItem->key = NULL; |
|
186 |
+ table->flags |= TABLE_HAS_DELETED_ENTRIES; |
|
187 |
+ /* don't break, duplicate keys are allowed */ |
|
188 |
+ } |
|
189 |
+} |
|
190 |
+ |
|
191 |
+void |
|
192 |
+tableIterate(table_t *table, void(*callback)(char *key, int value)) |
|
193 |
+{ |
|
194 |
+ tableEntry *tableItem; |
|
195 |
+ |
|
196 |
+ if(table == NULL) |
|
197 |
+ return; |
|
198 |
+ |
|
199 |
+ if(table->tableHead == NULL) |
|
200 |
+ /* not populated yet */ |
|
201 |
+ return; |
|
202 |
+ |
|
203 |
+ for(tableItem = table->tableHead; tableItem; tableItem = tableItem->next) |
|
204 |
+ if(tableItem->key) /* check leaf is not deleted */ |
|
205 |
+ (*callback)(tableItem->key, tableItem->value); |
|
206 |
+ |
|
207 |
+} |
... | ... |
@@ -29,10 +29,15 @@ typedef struct tableEntry { |
29 | 29 |
typedef struct table { |
30 | 30 |
tableEntry *tableHead; |
31 | 31 |
tableEntry *tableLast; |
32 |
+ unsigned int flags; |
|
32 | 33 |
} table_t; |
33 | 34 |
|
35 |
+#define TABLE_HAS_DELETED_ENTRIES 0x1 |
|
36 |
+ |
|
34 | 37 |
struct table *tableCreate(void); |
35 | 38 |
void tableDestroy(table_t *table); |
36 | 39 |
int tableInsert(table_t *table, const char *key, int value); |
37 | 40 |
int tableUpdate(table_t *table, const char *key, int new_value); |
38 | 41 |
int tableFind(const table_t *table, const char *key); |
42 |
+void tableRemove(table_t *table, const char *key); |
|
43 |
+void tableIterate(table_t *table, void(*callback)(char *key, int value)); |