#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#ifndef CL_DEBUG
#define NDEBUG
#endif
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include "table.h"
#include "others.h"
struct table *
tableCreate(void)
{
return (struct table *)cli_calloc(1, sizeof(struct table));
}
void
tableDestroy(table_t *table)
{
tableEntry *tableItem;
assert(table != NULL);
tableItem = table->tableHead;
while(tableItem) {
tableEntry *tableNext = tableItem->next;
assert(tableItem->key != NULL);
free(tableItem->key);
free(tableItem);
tableItem = tableNext;
}
free(table);
}
int
tableInsert(table_t *table, const char *key, int value)
{
const int v = tableFind(table, key);
if(v > 0)
return (v == value) ? value : -1;
assert(value != -1);
if(table->tableHead == NULL)
table->tableLast = table->tableHead = (tableEntry *)cli_calloc(1, sizeof(tableEntry));
else
table->tableLast = table->tableLast->next =
(tableEntry *)cli_calloc(1, sizeof(tableEntry));
if(table->tableLast == NULL)
return -1;
table->tableLast->next = NULL;
table->tableLast->key = strdup(key);
table->tableLast->value = value;
return value;
}
int
tableFind(const table_t *table, const char *key)
{
const tableEntry *tableItem;
#ifdef CL_DEBUG
int cost;
#endif
assert(table != NULL);
if(key == NULL)
return -1;
if(table->tableHead == NULL)
return -1;
#ifdef CL_DEBUG
cost = 0;
#endif
for(tableItem = table->tableHead; tableItem; tableItem = tableItem->next) {
#ifdef CL_DEBUG
cost++;
#endif
if(strcasecmp(tableItem->key, key) == 0) {
#ifdef CL_DEBUG
cli_dbgmsg("tableFind: Cost of '%s' = %d\n", key, cost);
#endif
return(tableItem->value);
}
}
return -1;
}