c3c5b9855536829d56cb64713e3d6621b3e3781b
[ccan] / ccan / hashtable / _info
1 #include <string.h>
2
3 /**
4  * hashtable - hash table routines
5  *
6  * A hash table is an efficient structure for looking up keys.  This version
7  * grows with usage and allows efficient deletion.
8  *
9  * Caveat: pointers placed into the hash must be aligned to 64 bits.
10  * ie. from malloc or because the structure they point to is aligned.
11  * An assert will be raised if this isn't true.
12  *
13  * Example:
14  *      #include <ccan/hashtable/hashtable.h>
15  *      #include <ccan/hash/hash.h>
16  *      #include <stdio.h>
17  *      #include <err.h>
18  *      #include <string.h>
19  *      
20  *      struct name_to_digit {
21  *              const char *name;
22  *              unsigned int val;
23  *      };
24  *      
25  *      static struct name_to_digit map[] = { 
26  *              { "zero", 0},
27  *              { "one", 1 },
28  *              { "two", 2 },
29  *              { "three", 3 },
30  *              { "four", 4 },
31  *              { "five", 5 },
32  *              { "six", 6 },
33  *              { "seven", 7 },
34  *              { "eight", 8 },
35  *              { "nine", 9 }
36  *      };
37  *      
38  *      // Wrapper for rehash function pointer.
39  *      static unsigned long rehash(const void *e, void *unused)
40  *      {
41  *              return hash_string(((struct name_to_digit *)e)->name);
42  *      }
43  *      
44  *      // Comparison function.
45  *      static bool streq(const void *e, void *string)
46  *      {
47  *              return strcmp(((struct name_to_digit *)e)->name, string) == 0;
48  *      }
49  *      
50  *      // We let them add their own aliases, eg. --alias=v=5
51  *      static void add_alias(struct hashtable *ht, const char *alias)
52  *      {
53  *              char *eq;
54  *              struct name_to_digit *n;
55  *      
56  *              n = malloc(sizeof(*n));
57  *              n->name = strdup(alias);
58  *      
59  *              eq = strchr(n->name, '=');
60  *              if (!eq || ((n->val = atoi(eq+1)) == 0 && !strcmp(eq+1, "0")))
61  *                      errx(1, "Usage: --alias=<name>=<value>");
62  *              *eq = '\0';
63  *              hashtable_add(ht, hash_string(n->name), n);
64  *      }
65  *      
66  *      int main(int argc, char *argv[])
67  *      {
68  *              struct hashtable *ht;
69  *              unsigned int i;
70  *              unsigned long val;
71  *      
72  *              if (argc < 2)
73  *                      errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
74  *                           argv[0]);
75  *      
76  *              // Create and populate hash table.
77  *              ht = hashtable_new(rehash, NULL);
78  *              for (i = 0; i < sizeof(map)/sizeof(map[0]); i++)
79  *                      hashtable_add(ht, hash_string(map[i].name), &map[i]);
80  *      
81  *              // Add any aliases to the hash table.
82  *              for (i = 1; i < argc; i++) {
83  *                      if (!strncmp(argv[i], "--alias=", strlen("--alias=")))
84  *                              add_alias(ht, argv[i] + strlen("--alias="));
85  *                      else
86  *                              break;
87  *              }
88  *      
89  *              // Find the other args in the hash table.
90  *              for (val = 0; i < argc; i++) {
91  *                      struct name_to_digit *n;
92  *                      n = hashtable_find(ht, hash_string(argv[i]),
93  *                                         streq, argv[i]);
94  *                      if (!n)
95  *                              errx(1, "Invalid digit name %s", argv[i]);
96  *                      // Append it to the value we are building up.
97  *                      val *= 10;
98  *                      val += n->val;
99  *              }
100  *              printf("%lu\n", val);
101  *              return 0;
102  *      }
103  *
104  * License: GPLv2 (or later)
105  * Author: Rusty Russell <rusty@rustcorp.com.au>
106  */
107 int main(int argc, char *argv[])
108 {
109         if (argc != 2)
110                 return 1;
111
112         if (strcmp(argv[1], "depends") == 0) {
113                 return 0;
114         }
115
116         return 1;
117 }