1 /* $Id: cfg.c,v 1.4 2010/08/26 05:56:20 gmcnutt Exp $
3 * Copyright (C) 2006 Gordon McNutt
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Foundation, Inc., 59 Temple Place,
17 * Suite 330, Boston, MA 02111-1307 USA
27 #define CFG_HASH_SIZE 31
30 struct cfg_entry *next;
32 } *cfg_hash[CFG_HASH_SIZE];
34 static struct cfg_entry *cfg_entry_new(const char *key, const char *val)
36 struct cfg_entry *entry = (struct cfg_entry*)malloc(sizeof(*entry));
39 entry->key = strdup(key);
42 entry->val = strdup(val);
50 static int hashfn(const char *key)
52 unsigned int hashed = 0;
54 static const int bits_per_int = sizeof(unsigned int)*8;
56 for (c = key; *c; c++) {
57 /* letters have about 5 bits in them */
58 hashed = (hashed<<5) | (hashed>>(bits_per_int-5));
61 return hashed % CFG_HASH_SIZE;
66 memset(cfg_hash, 0, sizeof(cfg_hash));
70 void cfg_set(const char *key, const char *val)
72 int hashkey = hashfn(key);
73 struct cfg_entry *entry = cfg_hash[hashkey];
75 cfg_hash[hashkey] = cfg_entry_new(key, val);
77 int match = !strcmp(entry->key, key);
81 match = !strcmp(entry->key, key);
84 repstr(&entry->val, val);
86 entry->next = cfg_entry_new(key, val);
91 char *cfg_get(const char *key)
93 int hashkey = hashfn(key);
94 struct cfg_entry *entry = cfg_hash[hashkey];
98 int match = !strcmp(entry->key, key);
102 match = !strcmp(entry->key, key);