6 * jmap - map from indices to values (based on libJudy)
8 * This provides a convenient wrapper for using JudyL arrays; using
9 * integers or pointers as an index, Judy arrays provide an efficient
10 * map to integers or pointers.
12 * You define a struct for your particular index and value types using
13 * the JMAP_MEMBERS macro, then use the jmap routines to manipulate
16 * Note: if you use an integer type for the index or value types and
17 * your compiler doesn't support "typeof", you will get warnings about
18 * mixing pointers and integers.
21 * // Silly example of associating data with arguments by pointer and int.
24 * #include <ccan/jmap/jmap.h>
28 * size_t length; // == 1 if !is_long.
31 * // Define map type for int -> argv.
33 * JMAP_MEMBERS(int, char *);
35 * // Define map type for argv -> struct opt_detail *.
37 * JMAP_MEMBERS(char *, struct opt_detail *);
40 * int main(int argc, char *argv[])
43 * // This map is equivalent to the argv[] array. Silly example.
44 * struct arg_map *arg = jmap_new(struct arg_map);
45 * struct opt_map *opt = jmap_new(struct opt_map);
46 * struct opt_detail *d;
48 * // Note: this is not correct for real parsing!
49 * for (i = 1; i < argc; i++) {
50 * jmap_add(arg, i, argv[i]);
51 * if (argv[i][0] != '-')
53 * d = malloc(sizeof(*d));
54 * if (argv[i][1] == '-') {
57 * d->length = strlen(argv[i]+2);
63 * jmap_add(opt, argv[i], d);
66 * printf("Found %lu options:\n", jmap_count(opt));
67 * for (i = jmap_first(arg); i; i = jmap_next(arg,i)) {
68 * char *a = jmap_get(arg, i);
69 * d = jmap_get(opt, a);
70 * printf(" Arg %i ('%s') is a %s of %zu chars\n",
72 * d == NULL ? "normal arg"
73 * : d->is_long ? "long opt"
75 * d == NULL ? strlen(a) : d->length);
76 * // We no longer need it, so free it here.
83 * // Given "--help" output contains "Arg 1 ('--help') is a long opt of 4 chars"
84 * // Given "-h" output contains "Arg 1 ('-h') is a short opt of 1 chars"
85 * // Given "foo" output contains "Arg 1 ('foo') is a normal arg of 3 chars"
87 * License: LGPL (v2.1 or any later version)
88 * Author: Rusty Russell <rusty@rustcorp.com.au>
90 int main(int argc, char *argv[])
95 if (strcmp(argv[1], "depends") == 0) {
96 printf("ccan/build_assert\n");
97 printf("ccan/compiler\n");
98 printf("ccan/tcon\n");
103 if (strcmp(argv[1], "libs") == 0) {