summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c62
1 files changed, 37 insertions, 25 deletions
diff --git a/hash.c b/hash.c
index d834222..3a3e67e 100644
--- a/hash.c
+++ b/hash.c
@@ -39,15 +39,16 @@ hash(const char *string)
{
int i;
unsigned u = 0;
- for(i = 0; string[i] != '\0'; i++)
- u = (u<<5) + (u >> (LOG2_NUMBUCKETS - 5)) + (unsigned char)string[i];
+
+ for (i = 0; string[i] != '\0'; i++)
+ u = (u << 5) + (u >> (LOG2_NUMBUCKETS - 5)) + (unsigned char) string[i];
return (u & (NUMBUCKETS - 1));
}
static void
str_tolower(char *s)
{
- while(*s != '\0') {
+ while (*s != '\0') {
*s = tolower(*s);
s++;
}
@@ -65,8 +66,8 @@ destroyHashTable(HashTablePtr table)
int i;
HashBucketPtr bp;
- for(i = 0; i < NUMBUCKETS; i++) {
- while(table[i]) {
+ for (i = 0; i < NUMBUCKETS; i++) {
+ while (table[i]) {
bp = table[i];
table[i] = table[i]->next;
free(bp->key);
@@ -82,8 +83,9 @@ getHash(HashTablePtr table, const char *key)
{
unsigned int i = hash(key);
HashBucketPtr bp;
- for(bp = table[i]; bp; bp = bp->next) {
- if(strcasecmp(bp->key, key) == 0)
+
+ for (bp = table[i]; bp; bp = bp->next) {
+ if (strcasecmp(bp->key, key) == 0)
return bp->value;
}
return NULL;
@@ -95,14 +97,17 @@ putHash(HashTablePtr table, char *key, char *value, int prio)
unsigned int i = hash(key);
char *keycopy = NULL, *valuecopy = NULL;
HashBucketPtr bp;
- for(bp = table[i]; bp; bp = bp->next) {
- if(strcasecmp(bp->key, key) == 0) {
- if(prio > bp->prio) {
+
+ for (bp = table[i]; bp; bp = bp->next) {
+ if (strcasecmp(bp->key, key) == 0) {
+ if (prio > bp->prio) {
keycopy = strdup(key);
- if(keycopy == NULL) goto fail;
+ if (keycopy == NULL)
+ goto fail;
str_tolower(keycopy);
valuecopy = strdup(value);
- if(valuecopy == NULL) goto fail;
+ if (valuecopy == NULL)
+ goto fail;
free(bp->key);
free(bp->value);
bp->key = keycopy;
@@ -112,14 +117,14 @@ putHash(HashTablePtr table, char *key, char *value, int prio)
}
}
keycopy = strdup(key);
- if(keycopy == NULL)
+ if (keycopy == NULL)
goto fail;
str_tolower(keycopy);
valuecopy = strdup(value);
- if(valuecopy == NULL)
+ if (valuecopy == NULL)
goto fail;
bp = malloc(sizeof(HashBucketRec));
- if(bp == NULL)
+ if (bp == NULL)
goto fail;
bp->key = keycopy;
bp->value = valuecopy;
@@ -129,8 +134,10 @@ putHash(HashTablePtr table, char *key, char *value, int prio)
return 1;
fail:
- if(keycopy) free(keycopy);
- if(valuecopy) free(valuecopy);
+ if (keycopy)
+ free(keycopy);
+ if (valuecopy)
+ free(valuecopy);
return -1;
}
@@ -141,8 +148,8 @@ hashElements(HashTablePtr table)
HashBucketPtr bp;
n = 0;
- for(i = 0; i < NUMBUCKETS; i++) {
- for(bp = table[i]; bp; bp = bp->next) {
+ for (i = 0; i < NUMBUCKETS; i++) {
+ for (bp = table[i]; bp; bp = bp->next) {
n++;
}
}
@@ -154,7 +161,9 @@ key_first_cmp(const void *v1, const void *v2)
{
const HashBucketPtr *b1 = v1, *b2 = v2;
int c1 = strcasecmp((*b1)->key, (*b2)->key);
- if(c1 != 0) return c1;
+
+ if (c1 != 0)
+ return c1;
return strcmp((*b1)->value, (*b2)->value);
}
@@ -163,7 +172,9 @@ value_first_cmp(const void *v1, const void *v2)
{
const HashBucketPtr *b1 = v1, *b2 = v2;
int c1 = strcmp((*b1)->value, (*b2)->value);
- if(c1 != 0) return c1;
+
+ if (c1 != 0)
+ return c1;
return strcasecmp((*b1)->key, (*b2)->key);
}
@@ -175,12 +186,12 @@ hashArray(HashTablePtr table, int value_first)
n = hashElements(table);
dst = malloc((n + 1) * sizeof(HashBucketPtr));
- if(dst == NULL)
+ if (dst == NULL)
return NULL;
j = 0;
- for(i = 0; i < NUMBUCKETS; i++) {
- while(table[i]) {
+ for (i = 0; i < NUMBUCKETS; i++) {
+ while (table[i]) {
dst[j++] = table[i];
table[i] = table[i]->next;
}
@@ -197,7 +208,8 @@ void
destroyHashArray(HashBucketPtr *array)
{
int i = 0;
- while(array[i]) {
+
+ while (array[i]) {
free(array[i]->key);
free(array[i]->value);
free(array[i]);