root/trunk/freewrt/tools/nfotizer/hash.c

Revision 2100, 2.5 kB (checked in by n0-1, 5 years ago)

added license information and (C)

Line 
1 /* nfotizer - split information given in info.nfo files into several places.
2  * Copyright (C) 2007  Phil Sutter <n0-1@freewrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #include "nfotizer.h"
19 #include <stdlib.h>             /* malloc */
20 #include <string.h>             /* strcmp */
21
22 /* Create a new hash table.
23  * This initially holds size elements,
24  * but is dynamically growing. */
25 struct htab *
26 htab_create(int size)
27 {
28         struct htab *table = malloc(sizeof(struct htab));
29         table->size = size;
30         table->data = calloc(size, sizeof(struct entry));
31         table->items = 0;
32         return table;
33 }
34
35 /* destroy a hash table, freeing it's space */
36 void
37 htab_destroy(struct htab *tab)
38 {
39         int i = 0;
40         for (; i < tab->size; i++) {
41                 zap(tab->data[i].key);
42                 zap(tab->data[i].val);
43         }
44         zap(tab->data);
45         zap(tab);
46 }
47
48 /* return the index of key in the hash table tab or -1 if not found */
49 int
50 htab_indexof(char *key, struct htab tab)
51 {
52         int i = 0;
53         for (; i < tab.items; i++) {
54                 if (!strcmp(key, tab.data[i].key)) {
55                         DBG("struct htab_indexof(): found \"%s\" at %i", key, i);
56                         return i;
57                 }
58         }
59         return -1;
60 }
61
62 /* return the value of key in tab */
63 char *
64 htab_get(char *key, struct htab tab)
65 {
66         int i;
67         if ((i = htab_indexof(key, tab)) >= 0)
68                 return strdup(tab.data[i].val);
69         else
70                 return NULL;
71 }
72
73 /* insert key with value val into tab */
74 int
75 htab_put(char *key, char *val, struct htab *tab)
76 {
77         if (htab_indexof(key, *tab) >= 0)
78                 return -1;
79         if (tab->items == tab->size) {
80                 tab->size = tab->size << 2;
81                 DBG("resizing hash to %i items", tab->size);
82                 tab->data = realloc(tab->data, tab->size);
83         }
84         tab->data[tab->items].key = strdup(key);
85         tab->data[tab->items].val = strdup(val);
86         DBG("inserted item: %s: %s", tab->data[tab->items].key,
87               tab->data[tab->items].val);
88         tab->items++;
89         return 0;
90 }
91
92 /* print the whole hash table tab */
93 void
94 htab_print(struct htab tab)
95 {
96         int i=0;
97         for (; i < tab.items; i++)
98                 PINFO("%s: %s", tab.data[i].key, tab.data[i].val);
99 }
Note: See TracBrowser for help on using the browser.