]> git.ozlabs.org Git - ccan/blob - ccan/jmap/_info
tdb2: suppress failtest more than once on mmap.
[ccan] / ccan / jmap / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * jmap - map from indices to values (based on libJudy)
7  *
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.
11  *
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
14  * the mapping.
15  *
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.
19  *
20  * Example:
21  * // Silly example of associating data with arguments by pointer and int.
22  * #include <string.h>
23  * #include <stdio.h>
24  * #include <ccan/jmap/jmap.h>
25  * 
26  * struct opt_detail {
27  *      bool is_long;
28  *      size_t length; // == 1 if !is_long.
29  * };
30  * 
31  * // Define map type for int -> argv.
32  * struct arg_map {
33  *      JMAP_MEMBERS(int, char *);
34  * };
35  * // Define map type for argv -> struct opt_detail *.
36  * struct opt_map {
37  *      JMAP_MEMBERS(char *, struct opt_detail *);
38  * };
39  * 
40  * int main(int argc, char *argv[])
41  * {
42  *      int i;
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;
47  * 
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] != '-')
52  *                      continue;
53  *              d = malloc(sizeof(*d));
54  *              if (argv[i][1] == '-') {
55  *                      // --<stuff>
56  *                      d->is_long = true;
57  *                      d->length = strlen(argv[i]+2);
58  *              } else {
59  *                      // -<opt1>
60  *                      d->is_long = false;
61  *                      d->length = 1;
62  *              }
63  *              jmap_add(opt, argv[i], d);
64  *      }
65  * 
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",
71  *                     i, a,
72  *                     d == NULL ? "normal arg"
73  *                     : d->is_long ? "long opt"
74  *                     : "short opt",
75  *                     d == NULL ? strlen(a) : d->length);
76  *              // We no longer need it, so free it here.
77  *              free(d);
78  *      }
79  *      jmap_free(opt);
80  *      jmap_free(arg);
81  *      return 0;
82  * }
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"
86  *
87  * License: LGPL (v2.1 or any later version)
88  * Author: Rusty Russell <rusty@rustcorp.com.au>
89  */
90 int main(int argc, char *argv[])
91 {
92         if (argc != 2)
93                 return 1;
94
95         if (strcmp(argv[1], "depends") == 0) {
96                 printf("ccan/build_assert\n");
97                 printf("ccan/compiler\n");
98                 printf("ccan/tcon\n");
99                 printf("Judy\n");
100                 return 0;
101         }
102
103         if (strcmp(argv[1], "libs") == 0) {
104                 printf("Judy\n");
105                 return 0;
106         }
107
108         return 1;
109 }