summaryrefslogtreecommitdiff
path: root/app/mkfontscale/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/mkfontscale/hash.c')
-rw-r--r--app/mkfontscale/hash.c83
1 files changed, 43 insertions, 40 deletions
diff --git a/app/mkfontscale/hash.c b/app/mkfontscale/hash.c
index d83422278..6a6089663 100644
--- a/app/mkfontscale/hash.c
+++ b/app/mkfontscale/hash.c
@@ -37,17 +37,17 @@
static unsigned
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 (int 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++;
}
@@ -62,12 +62,9 @@ makeHashTable(void)
void
destroyHashTable(HashTablePtr table)
{
- int i;
- HashBucketPtr bp;
-
- for(i = 0; i < NUMBUCKETS; i++) {
- while(table[i]) {
- bp = table[i];
+ for (int i = 0; i < NUMBUCKETS; i++) {
+ while (table[i]) {
+ HashBucketPtr bp = table[i];
table[i] = table[i]->next;
free(bp->key);
free(bp->value);
@@ -81,9 +78,9 @@ char *
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 (HashBucketPtr bp = table[i]; bp; bp = bp->next) {
+ if (strcasecmp(bp->key, key) == 0)
return bp->value;
}
return NULL;
@@ -95,14 +92,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 +112,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,20 +129,20 @@ 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;
}
int
hashElements(HashTablePtr table)
{
- int i, n;
- HashBucketPtr bp;
+ int n = 0;
- n = 0;
- for(i = 0; i < NUMBUCKETS; i++) {
- for(bp = table[i]; bp; bp = bp->next) {
+ for (int i = 0; i < NUMBUCKETS; i++) {
+ for (HashBucketPtr bp = table[i]; bp; bp = bp->next) {
n++;
}
}
@@ -154,7 +154,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,24 +165,24 @@ 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);
}
HashBucketPtr *
hashArray(HashTablePtr table, int value_first)
{
- int i, j, n;
- HashBucketPtr *dst;
-
- n = hashElements(table);
- dst = malloc((n + 1) * sizeof(HashBucketPtr));
- if(dst == NULL)
+ unsigned int j;
+ int n = hashElements(table);
+ HashBucketPtr *dst = malloc((n + 1) * sizeof(HashBucketPtr));
+ if (dst == NULL)
return NULL;
j = 0;
- for(i = 0; i < NUMBUCKETS; i++) {
- while(table[i]) {
+ for (unsigned int i = 0; i < NUMBUCKETS; i++) {
+ while (table[i]) {
dst[j++] = table[i];
table[i] = table[i]->next;
}
@@ -196,8 +198,9 @@ hashArray(HashTablePtr table, int value_first)
void
destroyHashArray(HashBucketPtr *array)
{
- int i = 0;
- while(array[i]) {
+ unsigned int i = 0;
+
+ while (array[i]) {
free(array[i]->key);
free(array[i]->value);
free(array[i]);