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