aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c135
1 files changed, 71 insertions, 64 deletions
diff --git a/hash.c b/hash.c
index 3a43f7e..5b6b98d 100644
--- a/hash.c
+++ b/hash.c
@@ -1,4 +1,4 @@
-/* $Id: hash.c,v 1.22 2004/06/20 10:09:56 reinelt Exp $
+/* $Id: hash.c,v 1.23 2004/06/26 09:27:21 reinelt Exp $
*
* hashes (associative arrays)
*
@@ -23,6 +23,12 @@
*
*
* $Log: hash.c,v $
+ * Revision 1.23 2004/06/26 09:27:21 reinelt
+ *
+ * added '-W' to CFLAGS
+ * changed all C++ comments to C ones ('//' => '/* */')
+ * cleaned up a lot of signed/unsigned mistakes
+ *
* Revision 1.22 2004/06/20 10:09:56 reinelt
*
* 'const'ified the whole source
@@ -163,14 +169,14 @@
#include <dmalloc.h>
#endif
-// number of slots for delta processing
+/* number of slots for delta processing */
#define DELTA_SLOTS 64
-// string buffer chunk size
+/* string buffer chunk size */
#define CHUNK_SIZE 16
-// initialize a new hash table
+/* initialize a new hash table */
void hash_create (HASH *Hash)
{
Hash->sorted = 0;
@@ -188,7 +194,7 @@ void hash_create (HASH *Hash)
}
-// bsearch compare function for hash items
+/* bsearch compare function for hash items */
static int hash_lookup_item (const void *a, const void *b)
{
char *key = (char*)a;
@@ -198,7 +204,7 @@ static int hash_lookup_item (const void *a, const void *b)
}
-// qsort compare function for hash items
+/* qsort compare function for hash items */
static int hash_sort_item (const void *a, const void *b)
{
HASH_ITEM *ha = (HASH_ITEM*)a;
@@ -208,7 +214,7 @@ static int hash_sort_item (const void *a, const void *b)
}
-// bsearch compare function for hash headers
+/* bsearch compare function for hash headers */
static int hash_lookup_column (const void *a, const void *b)
{
char *key = (char*)a;
@@ -218,7 +224,7 @@ static int hash_lookup_column (const void *a, const void *b)
}
-// qsort compare function for hash headers
+/* qsort compare function for hash headers */
static int hash_sort_column (const void *a, const void *b)
{
HASH_COLUMN *ha = (HASH_COLUMN*)a;
@@ -228,13 +234,14 @@ static int hash_sort_column (const void *a, const void *b)
}
-// split a value into columns and
-// return the nth column in a string
-// WARNING: does return a pointer to a static string!!
+/* split a value into columns and */
+/* return the nth column in a string */
+/* WARNING: does return a pointer to a static string!! */
static char* split (const char *val, const int column, const char *delimiter)
{
static char buffer[256];
- int num, len;
+ int num;
+ size_t len;
const char *beg, *end;
if (column < 0) return (char*)val;
@@ -251,7 +258,7 @@ static char* split (const char *val, const int column, const char *delimiter)
beg = end ? end+1 : NULL;
}
if (beg != NULL) {
- len = end ? end - beg : strlen(beg);
+ len = end ? (size_t)(end - beg) : strlen(beg);
if (len >= sizeof(buffer)) len = sizeof(buffer)-1;
strncpy (buffer, beg, len);
}
@@ -261,29 +268,29 @@ static char* split (const char *val, const int column, const char *delimiter)
}
-// search an entry in the hash table:
-// If the table is flagged "sorted", the entry is looked
-// up using the bsearch function. If the table is
-// unsorted, it will be searched in a linear way
+/* search an entry in the hash table: */
+/* If the table is flagged "sorted", the entry is looked */
+/* up using the bsearch function. If the table is */
+/* unsorted, it will be searched in a linear way */
static HASH_ITEM* hash_lookup (HASH *Hash, const char *key, const int do_sort)
{
HASH_ITEM *Item = NULL;
- // maybe sort the array
+ /* maybe sort the array */
if (do_sort && !Hash->sorted) {
qsort(Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_sort_item);
Hash->sorted = 1;
}
- // no key was passed
+ /* no key was passed */
if (key == NULL) return NULL;
- // lookup using bsearch
+ /* lookup using bsearch */
if (Hash->sorted) {
Item = bsearch(key, Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_lookup_item);
}
- // linear search
+ /* linear search */
if (Item == NULL) {
int i;
for (i = 0; i < Hash->nItems; i++) {
@@ -299,9 +306,9 @@ static HASH_ITEM* hash_lookup (HASH *Hash, const char *key, const int do_sort)
}
-// return the age in milliseconds of an entry from the hash table
-// or from the hash table itself if key is NULL
-// returns -1 if entry does not exist
+/* return the age in milliseconds of an entry from the hash table */
+/* or from the hash table itself if key is NULL */
+/* returns -1 if entry does not exist */
int hash_age (HASH *Hash, const char *key)
{
HASH_ITEM *Item;
@@ -321,7 +328,7 @@ int hash_age (HASH *Hash, const char *key)
}
-// add an entry to the column header table
+/* add an entry to the column header table */
void hash_set_column (HASH *Hash, const int number, const char *column)
{
if (Hash == NULL) return;
@@ -336,7 +343,7 @@ void hash_set_column (HASH *Hash, const int number, const char *column)
}
-// fetch a column number by column header
+/* fetch a column number by column header */
static int hash_get_column (HASH *Hash, const char *key)
{
HASH_COLUMN *Column;
@@ -350,7 +357,7 @@ static int hash_get_column (HASH *Hash, const char *key)
}
-// set column delimiters
+/* set column delimiters */
void hash_set_delimiter (HASH *Hash, const char *delimiter)
{
if (Hash->delimiter != NULL) free (Hash->delimiter);
@@ -358,7 +365,7 @@ void hash_set_delimiter (HASH *Hash, const char *delimiter)
}
-// get a string from the hash table
+/* get a string from the hash table */
char *hash_get (HASH *Hash, const char *key, const char *column)
{
HASH_ITEM *Item;
@@ -372,7 +379,7 @@ char *hash_get (HASH *Hash, const char *key, const char *column)
}
-// get a delta value from the delta table
+/* get a delta value from the delta table */
double hash_get_delta (HASH *Hash, const char *key, const char *column, const int delay)
{
HASH_ITEM *Item;
@@ -382,20 +389,20 @@ double hash_get_delta (HASH *Hash, const char *key, const char *column, const in
double dv, dt;
struct timeval now, end;
- // lookup item
+ /* lookup item */
Item = hash_lookup(Hash, key, 1);
if (Item == NULL) return 0.0;
- // this is the "current" Slot
+ /* this is the "current" Slot */
Slot1 = &(Item->Slot[Item->index]);
- // fetch column number
+ /* fetch column number */
c = hash_get_column(Hash, column);
- // if delay is zero, return absolute value
+ /* if delay is zero, return absolute value */
if (delay == 0) return atof(split(Slot1->value, c, Hash->delimiter));
- // prepare timing values
+ /* prepare timing values */
now = Slot1->timestamp;
end.tv_sec = now.tv_sec;
end.tv_usec = now.tv_usec - 1000*delay;
@@ -404,7 +411,7 @@ double hash_get_delta (HASH *Hash, const char *key, const char *column, const in
end.tv_usec += 1000000;
}
- // search delta slot
+ /* search delta slot */
Slot2 = &(Item->Slot[Item->index]);
for (i = 1; i < Item->nSlot; i++) {
Slot2 = &(Item->Slot[(Item->index + i) % Item->nSlot]);
@@ -412,16 +419,16 @@ double hash_get_delta (HASH *Hash, const char *key, const char *column, const in
if (timercmp(&(Slot2->timestamp), &end, <)) break;
}
- // empty slot => try the one before
+ /* empty slot => try the one before */
if (Slot2->timestamp.tv_sec == 0) {
i--;
Slot2 = &(Item->Slot[(Item->index + i) % Item->nSlot]);
}
- // not enough slots available...
+ /* not enough slots available... */
if (i == 0) return 0.0;
- // delta value, delta time
+ /* delta value, delta time */
v1 = atof(split(Slot1->value, c, Hash->delimiter));
v2 = atof(split(Slot2->value, c, Hash->delimiter));
dv = v1 - v2;
@@ -433,9 +440,9 @@ double hash_get_delta (HASH *Hash, const char *key, const char *column, const in
}
-// get a delta value from the delta table
-// key may contain regular expressions, and the sum
-// of all matching entries is returned.
+/* get a delta value from the delta table */
+/* key may contain regular expressions, and the sum */
+/* of all matching entries is returned. */
double hash_get_regex (HASH *Hash, const char *key, const char *column, const int delay)
{
double sum;
@@ -451,7 +458,7 @@ double hash_get_regex (HASH *Hash, const char *key, const char *column, const in
return 0.0;
}
- // force the table to be sorted by requesting anything
+ /* force the table to be sorted by requesting anything */
hash_lookup(Hash, NULL, 1);
sum = 0.0;
@@ -465,11 +472,11 @@ double hash_get_regex (HASH *Hash, const char *key, const char *column, const in
}
-// insert a key/val pair into the hash table
-// If the entry does already exist, it will be overwritten,
-// and the table stays sorted (if it has been before).
-// Otherwise, the entry is appended at the end, and
-// the table will be flagged 'unsorted' afterwards
+/* insert a key/val pair into the hash table */
+/* If the entry does already exist, it will be overwritten, */
+/* and the table stays sorted (if it has been before). */
+/* Otherwise, the entry is appended at the end, and */
+/* the table will be flagged 'unsorted' afterwards */
static HASH_ITEM* hash_set (HASH *Hash, const char *key, const char *value, const int delta)
{
@@ -481,7 +488,7 @@ static HASH_ITEM* hash_set (HASH *Hash, const char *key, const char *value, cons
if (Item == NULL) {
- // add entry
+ /* add entry */
Hash->sorted = 0;
Hash->nItems++;
Hash->Items = realloc(Hash->Items, Hash->nItems * sizeof(HASH_ITEM));
@@ -495,7 +502,7 @@ static HASH_ITEM* hash_set (HASH *Hash, const char *key, const char *value, cons
} else {
- // maybe enlarge delta table
+ /* maybe enlarge delta table */
if (Item->nSlot < delta) {
Item->nSlot = delta;
Item->Slot = realloc (Item->Slot, Item->nSlot * sizeof(HASH_SLOT));
@@ -504,26 +511,26 @@ static HASH_ITEM* hash_set (HASH *Hash, const char *key, const char *value, cons
}
if (Item->nSlot > 1) {
- // move the pointer to the next free slot, wrap around if necessary
+ /* move the pointer to the next free slot, wrap around if necessary */
if (--Item->index < 0) Item->index = Item->nSlot-1;
}
- // create entry
+ /* create entry */
Slot = &(Item->Slot[Item->index]);
size = strlen(value) + 1;
- // maybe enlarge value buffer
+ /* maybe enlarge value buffer */
if (size > Slot->size) {
- // buffer is either empty or too small
- // allocate memory in multiples of CHUNK_SIZE
+ /* buffer is either empty or too small */
+ /* allocate memory in multiples of CHUNK_SIZE */
Slot->size = CHUNK_SIZE * (size / CHUNK_SIZE + 1);
Slot->value = realloc(Slot->value, Slot->size);
}
- // set value
+ /* set value */
strcpy(Slot->value, value);
- // set timestamps
+ /* set timestamps */
gettimeofday(&(Hash->timestamp), NULL);
Slot->timestamp = Hash->timestamp;
@@ -531,16 +538,16 @@ static HASH_ITEM* hash_set (HASH *Hash, const char *key, const char *value, cons
}
-// insert a string into the hash table
-// without delta processing
+/* insert a string into the hash table */
+/* without delta processing */
void hash_put (HASH *Hash, const char *key, const char *value)
{
hash_set (Hash, key, value, 1);
}
-// insert a string into the hash table
-// with delta processing
+/* insert a string into the hash table */
+/* with delta processing */
void hash_put_delta (HASH *Hash, const char *key, const char *value)
{
hash_set (Hash, key, value, DELTA_SLOTS);
@@ -553,21 +560,21 @@ void hash_destroy (HASH *Hash)
if (Hash->Items) {
- // free all headers
+ /* free all headers */
for (i = 0; i < Hash->nColumns; i++) {
if (Hash->Columns[i].key) free (Hash->Columns[i].key);
}
- // free header table
+ /* free header table */
free (Hash->Columns);
- // free all items
+ /* free all items */
for (i = 0; i < Hash->nItems; i++) {
if (Hash->Items[i].key) free (Hash->Items[i].key);
if (Hash->Items[i].Slot) free (Hash->Items[i].Slot);
}
- // free items table
+ /* free items table */
free (Hash->Items);
}