genconf: typo and comments
[libcmdline.git] / src / genconf / conf_htable.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <sys/queue.h>
6
7 #include "expression.h"
8 #include "conf_parser.h"
9 #include "confnode.h"
10
11 #define HASHTABLE_ORDER 10
12 #define HASHTABLE_SIZE  (1<<HASHTABLE_ORDER)
13 #define HASHTABLE_MASK  (HASHTABLE_ORDER-1)
14
15 static struct confnode_list conf_htable[HASHTABLE_SIZE];
16
17 /* return the hash from name */
18 static inline uint32_t hash_name(const char *name)
19 {
20         uint32_t h = 0;
21         const unsigned char *c;
22
23         for (c = (const unsigned char *)name; *c; c++)
24                 h += ((((uint32_t)*c) << 4) + (((uint32_t)*c) >> 4)) * 11;
25
26         return h & HASHTABLE_MASK;
27 }
28
29 /*
30  * Return the configuration node associated to the given name. If not
31  * found, return NULL.
32  */
33 struct confnode *conf_htable_lookup(const char *name)
34 {
35         struct confnode *c;
36         uint32_t h = hash_name(name);
37
38         TAILQ_FOREACH(c, &conf_htable[h], hnext) {
39                 if (!strncmp(name, c->name, sizeof(c->name)))
40                         break;
41         }
42         return c;
43 }
44
45 /*
46  * Parse a configuration tree and add all the nodes in the hashtable.
47  */
48 int conf_htable_fill(struct confnode *root)
49 {
50         struct confnode *c;
51         uint32_t h = hash_name(root->name);
52
53         TAILQ_INSERT_TAIL(&conf_htable[h], root, hnext);
54
55         TAILQ_FOREACH(c, &root->children, next) {
56                 conf_htable_fill(c);
57         }
58         return 0;
59 }
60
61 /*
62  * Initialize the htable to be empty.
63  */
64 int conf_htable_init(void)
65 {
66         unsigned i;
67
68         for (i=0; i<HASHTABLE_SIZE; i++) {
69                 TAILQ_INIT(&conf_htable[i]);
70         }
71         return 0;
72 }
73